Merge pull request #36 from danieldupriest/create_api_serializer
Create api serializer
This commit is contained in:
commit
71c1208bfa
12 changed files with 103 additions and 2 deletions
|
@ -8,6 +8,7 @@ verify_ssl = true
|
|||
[packages]
|
||||
django = "==2.1.5"
|
||||
django-cors-headers = "==2.4.0"
|
||||
djangorestframework = "==3.8.2"
|
||||
|
||||
gunicorn = "==19.6.0"
|
||||
|
||||
|
|
10
back/Pipfile.lock
generated
10
back/Pipfile.lock
generated
|
@ -1,7 +1,7 @@
|
|||
{
|
||||
"_meta": {
|
||||
"hash": {
|
||||
"sha256": "5f6bc22a581526fd5e7cba376397b8d499b15a0c071bdcc5cc799c32859f88ed"
|
||||
"sha256": "b5222b4256c8f09a9b1b1d380285fa65c443f84d28dc03450684fca84b38a26b"
|
||||
},
|
||||
"pipfile-spec": 6,
|
||||
"requires": {
|
||||
|
@ -32,6 +32,14 @@
|
|||
"index": "pypi",
|
||||
"version": "==2.4.0"
|
||||
},
|
||||
"djangorestframework": {
|
||||
"hashes": [
|
||||
"sha256:b6714c3e4b0f8d524f193c91ecf5f5450092c2145439ac2769711f7eba89a9d9",
|
||||
"sha256:c375e4f95a3a64fccac412e36fb42ba36881e52313ec021ef410b40f67cddca4"
|
||||
],
|
||||
"index": "pypi",
|
||||
"version": "==3.8.2"
|
||||
},
|
||||
"gunicorn": {
|
||||
"hashes": [
|
||||
"sha256:723234ea1fa8dff370ab69830ba8bc37469a7cba13fd66055faeef24085e6530",
|
||||
|
|
66
back/backend/serializers.py
Normal file
66
back/backend/serializers.py
Normal file
|
@ -0,0 +1,66 @@
|
|||
# Rupika Dikkala
|
||||
# January 23, 2019
|
||||
# File contains serializers needed
|
||||
# to set up API end points
|
||||
|
||||
from rest_framework import serializers
|
||||
from . import models
|
||||
|
||||
# serializer for reports
|
||||
class ReportSerializer(serializers.ModelSerializer):
|
||||
# user id is foreign key
|
||||
user_id = serializers.PrimaryKeyRelatedField(many=False, read_only=True)
|
||||
|
||||
class Meta:
|
||||
fields = (
|
||||
'user_id',
|
||||
'title',
|
||||
'date_created',
|
||||
# 'data_submitted',
|
||||
'submitted',
|
||||
)
|
||||
model = models.Report
|
||||
|
||||
|
||||
# section serializer
|
||||
class SectionSerializer(serializers.ModelSerializer):
|
||||
# report id foriegn key
|
||||
report_id = serializers.PrimaryKeyRelatedField(many=True, read_only=True)
|
||||
|
||||
|
||||
class Meta:
|
||||
fields = (
|
||||
'report_id',
|
||||
'completed',
|
||||
'title',
|
||||
'html_description',
|
||||
'number',
|
||||
)
|
||||
model = models.Section
|
||||
|
||||
|
||||
class FieldSerializer(serializers.ModelSerializer):
|
||||
# section_id is foriegn key
|
||||
section_id = serializers.PrimaryKeyRelatedField(many=True, read_only=True)
|
||||
|
||||
class Meta:
|
||||
fields = (
|
||||
'section_id',
|
||||
'label',
|
||||
'number',
|
||||
'type',
|
||||
'completed',
|
||||
)
|
||||
model = models.Field
|
||||
|
||||
|
||||
class DataSerializer(serializers.ModelSerializer):
|
||||
field_id = serializers.PrimaryKeyRelatedField(many=False, read_only=True)
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
|
@ -7,6 +7,9 @@ from django.urls import path
|
|||
from . import views
|
||||
|
||||
urlpatterns = [
|
||||
path('', views.List.as_view()),
|
||||
path('<int:pk>/', views.Detail.as_view()),
|
||||
|
||||
path('create_report/', views.create_report),
|
||||
path('delete_report/', views.delete_report),
|
||||
path('get_report/', views.get_report),
|
||||
|
|
|
@ -5,7 +5,23 @@
|
|||
|
||||
from django.shortcuts import render
|
||||
from django.http import JsonResponse
|
||||
from .models import *
|
||||
from .serializers import *
|
||||
|
||||
from rest_framework import generics
|
||||
|
||||
|
||||
# create sample view
|
||||
class List(generics.ListCreateAPIView):
|
||||
queryset = Report.objects.all()
|
||||
serializer_class = ReportSerializer
|
||||
|
||||
class Detail(generics.RetrieveUpdateDestroyAPIView):
|
||||
queryset = Report.objects.all()
|
||||
serializer_class = ReportSerializer
|
||||
|
||||
|
||||
#######################################
|
||||
|
||||
# Create Report
|
||||
def create_report(request):
|
||||
|
|
BIN
back/db.sqlite3
BIN
back/db.sqlite3
Binary file not shown.
|
@ -39,10 +39,17 @@ INSTALLED_APPS = [
|
|||
'django.contrib.messages',
|
||||
'django.contrib.staticfiles',
|
||||
'backend',
|
||||
'rest_framework',
|
||||
'users',
|
||||
'corsheaders',
|
||||
]
|
||||
|
||||
REST_FRAMEWORK = {
|
||||
'DEFAULT_PERMISSION_CLASSES': [
|
||||
'rest_framework.permissions.AllowAny',
|
||||
]
|
||||
}
|
||||
|
||||
MIDDLEWARE = [
|
||||
'corsheaders.middleware.CorsMiddleware',
|
||||
'django.middleware.security.SecurityMiddleware',
|
||||
|
|
|
@ -23,5 +23,5 @@ from django.urls import path, include
|
|||
# add urls to this array
|
||||
urlpatterns = [
|
||||
path('admin/', admin.site.urls),
|
||||
path('', include("backend.urls")),
|
||||
path('backend/', include("backend.urls")),
|
||||
]
|
||||
|
|
Loading…
Reference in a new issue