-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit c45782c
Showing
7,669 changed files
with
857,468 additions
and
0 deletions.
The diff you're trying to view is too large. We only load the first 3000 changed files.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
./venv |
Empty file.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from django.contrib import admin | ||
|
||
# Register your models here. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
from django.apps import AppConfig | ||
|
||
|
||
class ApiConfig(AppConfig): | ||
default_auto_field = 'django.db.models.BigAutoField' | ||
name = 'api' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# Generated by Django 4.2.2 on 2023-06-16 16:53 | ||
|
||
from django.db import migrations, models | ||
import django.db.models.deletion | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
initial = True | ||
|
||
dependencies = [ | ||
] | ||
|
||
operations = [ | ||
migrations.CreateModel( | ||
name='Employee', | ||
fields=[ | ||
('id', models.AutoField(primary_key=True, serialize=False)), | ||
('name', models.CharField(max_length=255)), | ||
], | ||
), | ||
migrations.CreateModel( | ||
name='Query', | ||
fields=[ | ||
('id', models.AutoField(primary_key=True, serialize=False)), | ||
('date', models.DateField()), | ||
('employee', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='api.employee')), | ||
], | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# Generated by Django 4.2.2 on 2023-06-16 18:18 | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('api', '0001_initial'), | ||
] | ||
|
||
operations = [ | ||
migrations.AddField( | ||
model_name='query', | ||
name='employee_name', | ||
field=models.CharField(blank=True, max_length=255, null=True), | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# Generated by Django 4.2.2 on 2023-06-16 19:40 | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('api', '0002_query_employee_name'), | ||
] | ||
|
||
operations = [ | ||
migrations.AlterField( | ||
model_name='query', | ||
name='date', | ||
field=models.DateField(auto_now_add=True), | ||
), | ||
] |
Empty file.
Binary file not shown.
Binary file added
BIN
+827 Bytes
api/migrations/__pycache__/0002_query_employee_name.cpython-311.pyc
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
from django.db import models | ||
|
||
class Employee(models.Model): | ||
id = models.AutoField(primary_key=True) | ||
name = models.CharField(max_length=255) | ||
|
||
def __str__(self): | ||
return self.name | ||
|
||
|
||
class Query(models.Model): | ||
id = models.AutoField(primary_key=True) | ||
date = models.DateField(auto_now_add=True) | ||
employee = models.ForeignKey(Employee, on_delete=models.CASCADE) | ||
employee_name = models.CharField(max_length=255, null=True, blank=True) | ||
|
||
def __str__(self): | ||
return f"Query {self.id}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
|
||
from django.db.models import fields | ||
from rest_framework import serializers | ||
from .models import Employee, Query | ||
|
||
|
||
class EmployeeSerializer(serializers.ModelSerializer): | ||
class Meta: | ||
model = Employee | ||
fields = '__all__' | ||
|
||
class QuerySerializer(serializers.ModelSerializer): | ||
class Meta: | ||
model = Query | ||
fields = ['id', 'date', 'employee', 'employee_name'] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from django.test import TestCase | ||
|
||
# Create your tests here. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
from django.urls import path | ||
from . import views | ||
|
||
urlpatterns = [ | ||
path('', views.ApiOverview, name='home'), | ||
path('create/', views.add_employee, name='add-employee'), | ||
path('all/', views.view_employee, name='view_employee'), | ||
path('employee/<int:pk>/', views.view_employee_byId, name='view_employee_byId'), | ||
path('update/<int:pk>/', views.update_employee, name='update-employee'), | ||
path('employee/<int:pk>/delete/', views.delete_employee, name='delete-employee'), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
from django.urls import path | ||
from . import views | ||
|
||
urlpatterns = [ | ||
path('create/', views.add_query, name='add-query'), | ||
path('all/', views.view_query, name='view-query'), | ||
path('consult/', views.search_query_by_employee_name, name='query-by-employee') | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
from django.shortcuts import get_object_or_404 | ||
from rest_framework.decorators import api_view | ||
from rest_framework.response import Response | ||
from .models import Employee, Query | ||
from .serializers import EmployeeSerializer, QuerySerializer | ||
|
||
from rest_framework import serializers | ||
from rest_framework import status | ||
|
||
@api_view(['GET']) | ||
def ApiOverview(request): | ||
api_urls = { | ||
'all_items': '/', | ||
'Search by Name': '/?category=category_name', | ||
'Add': '/create', | ||
'Update': '/update/pk', | ||
'Delete': '/item/pk/delete' | ||
} | ||
|
||
return Response(api_urls) | ||
|
||
|
||
@api_view(['POST']) | ||
def add_employee(request): | ||
employee = EmployeeSerializer(data=request.data) | ||
|
||
if Employee.objects.filter(**request.data).exists(): | ||
raise serializers.ValidationError('This user already exists!') | ||
|
||
if employee.is_valid(): | ||
employee.save() | ||
return Response(employee.data) | ||
else: | ||
return Response(status=status.HTTP_404_NOT_FOUND) | ||
|
||
@api_view(['GET']) | ||
def view_employee(request): | ||
if request.query_params: | ||
employee = Employee.objects.filter(**request.query_param.dict()) | ||
else: | ||
employee = Employee.objects.all() | ||
|
||
if employee: | ||
serializer = EmployeeSerializer(employee, many=True) | ||
return Response(serializer.data) | ||
else: | ||
return Response(status=status.HTTP_404_NOT_FOUND) | ||
|
||
@api_view(['GET']) | ||
def view_employee_byId(request, pk): | ||
try: | ||
employee = Employee.objects.get(pk=pk) | ||
serializer = EmployeeSerializer(employee) | ||
return Response(serializer.data) | ||
except Employee.DoesNotExist: | ||
return Response(status=status.HTTP_404_NOT_FOUND) | ||
|
||
|
||
|
||
@api_view(['POST']) | ||
def update_employee(request, pk): | ||
employee = employee = Employee.objects.get(pk=pk) | ||
data = EmployeeSerializer(instance=employee, data=request.data) | ||
|
||
if data.is_valid(): | ||
data.save() | ||
return Response(data.data) | ||
else: | ||
return Response(status=status.HTTP_404_NOT_FOUND) | ||
|
||
|
||
@api_view(['DELETE']) | ||
def delete_employee(request, pk): | ||
employee = get_object_or_404(Employee, pk=pk) | ||
employee.delete() | ||
return Response(status=status.HTTP_202_ACCEPTED) | ||
|
||
|
||
@api_view(['POST']) | ||
def add_query(request): | ||
employee_name = request.data.get('employee_name') | ||
|
||
try: | ||
employee = Employee.objects.get(name=employee_name) | ||
except Employee.DoesNotExist: | ||
return Response({'error': 'Employee not found'}, status=status.HTTP_404_NOT_FOUND) | ||
|
||
query_data = { | ||
'date': request.data.get('date'), | ||
'employee': employee.id, | ||
'employee_name': employee_name | ||
} | ||
|
||
query_serializer = QuerySerializer(data=query_data) | ||
|
||
if query_serializer.is_valid(): | ||
query_serializer.save() | ||
return Response(query_serializer.data, status=status.HTTP_201_CREATED) | ||
else: | ||
return Response(query_serializer.errors, status=status.HTTP_400_BAD_REQUEST) | ||
|
||
@api_view(['GET']) | ||
def view_query(request): | ||
if request.query_params: | ||
query = Query.objects.filter(**request.query_param.dict()) | ||
else: | ||
query = Query.objects.all() | ||
|
||
|
||
if query: | ||
serializer = QuerySerializer(query, many=True) | ||
return Response(serializer.data) | ||
else: | ||
return Response(data='Query not founded!', status=status.HTTP_404_NOT_FOUND) | ||
|
||
@api_view(['POST']) | ||
def search_query_by_employee_name(request): | ||
employee_name = request.data.get('employee_name') | ||
|
||
if not employee_name: | ||
return Response({'error': 'employee_name parameter is required'}, status=status.HTTP_400_BAD_REQUEST) | ||
|
||
queries = Query.objects.filter(employee_name__icontains=employee_name) | ||
serializer = QuerySerializer(queries, many=True) | ||
return Response(serializer.data) |
Empty file.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
""" | ||
ASGI config for crud project. | ||
It exposes the ASGI callable as a module-level variable named ``application``. | ||
For more information on this file, see | ||
https://docs.djangoproject.com/en/4.2/howto/deployment/asgi/ | ||
""" | ||
|
||
import os | ||
|
||
from django.core.asgi import get_asgi_application | ||
|
||
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'crud.settings') | ||
|
||
application = get_asgi_application() |
Oops, something went wrong.