Merge branch 'master' of https://github.com/danieldupriest/reimbursinator into user-registration

This commit is contained in:
kououken 2019-02-04 18:29:19 -08:00
commit 68e2e29c73
4 changed files with 76 additions and 170 deletions

View file

@ -1,66 +0,0 @@
# 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)

View file

@ -5,9 +5,6 @@ from rest_framework.urlpatterns import format_suffix_patterns
from . import views
urlpatterns = [
#path('', views.List.as_view()),
#path('<int:pk>/', views.Detail.as_view()),
path('report', views.report),
path('reports', views.reports),
path('report/<int:report_pk>', views.report_detail),

View file

@ -1,23 +1,70 @@
from rest_framework import generics
from rest_framework import status
from rest_framework.decorators import api_view
from django.shortcuts import render
from django.http import JsonResponse
from .models import *
from .serializers import *
# Sample view using generics
class List(generics.ListCreateAPIView):
queryset = Report.objects.all()
serializer_class = ReportSerializer
# function that prints all the reports
def get_reports(report_pk):
# queryset = Report.objects.all()
queryset = Report.objects.filter(id=report_pk)
for i in queryset:
data = {
"report_pk": report_pk,
"title": i.title,
"date_created": i.date_created,
"submitted": i.submitted,
"date_submitted": i.date_submitted,
}
# append the sections for each report
data.update(get_sections(i.id))
# return JsonResponse(data)
return data
# function that gets all the sections
# takes report_id param
def get_sections(r_id):
# create a dict of arrays for section
section_set = {"sections": []}
queryset = Section.objects.filter(report_id=r_id)
# queryset = Section.objects.all()
for i in queryset:
data = {
"id": i.id,
"completed": i.completed,
"title": i.title,
"html_description": i.html_description,
}
# append the fields for corresponding section
data.update(get_fields(i.id))
# append section to the array
section_set["sections"].append(data.copy())
return section_set
# function that gets all the fields
# takes section_id param
def get_fields(s_id):
# create dict of arrays for fields
field_set = {"fields": []}
queryset = Field.objects.filter(section_id=s_id)
# queryset = Field.objects.all()
for i in queryset:
data = {
"field_name": "TODO",
"label": i.label,
"type": i.type,
"number": i.number,
"value": "get_value",
}
# append the fields to array
# use copy() to avoid overwriting
field_set["fields"].append(data.copy())
return field_set
class Detail(generics.RetrieveUpdateDestroyAPIView):
queryset = Report.objects.all()
serializer_class = ReportSerializer
# API Endpoints
@api_view(['POST'])
def report(request):
'''
@ -85,101 +132,28 @@ def report(request):
}
return JsonResponse(data)
# List of reports
@api_view(['GET'])
def reports(request):
print("User: ", request.user)
print("User id: ", request.user.id)
data = {
"reports": [
{
"report_pk": 1,
"title": "2018 Portland trip",
"date_created": "2018-05-22T14:56:28.000Z",
"state": "created",
"date_submitted": "0000-00-00T00:00:00.000Z"
},
{
"report_pk": 2,
"title": "2017 Los Angeles trip",
"date_created": "2017-05-22T14:56:28.000Z",
"state": "submitted",
"date_submitted": "2017-07-22T14:56:28.000Z"
},
{
"report_pk": 3,
"title": "2017 Denver trip",
"date_created": "2015-04-22T14:56:28.000Z",
"state": "accepted",
"date_submitted": "2015-06-22T14:56:28.000Z"
}
]
}
return JsonResponse(data)
report_set = {"reports": []}
queryset = Report.objects.all()
for i in queryset:
data = {
"title": i.title,
"date_created": i.date_created,
"submitted": i.submitted,
"date_submitted": i.date_submitted,
}
# append the sections for each report
report_set["reports"].append(data.copy())
return JsonResponse(report_set)
@api_view(['GET', 'PUT', 'DELETE'])
def report_detail(request, report_pk):
if request.method == 'GET':
data = {
"report_pk": report_pk,
"title": "2018 Portland trip",
"date_created": "2018-05-22T14:56:28.000Z",
"submitted": False,
"date_submitted": "0000-00-00T00:00:00.000Z",
"sections": [
{
"id": 1,
"completed": True,
"title": "Flight Info",
"html_description": "<p>Enter flight details here.</p>",
"fields": {
"international": {
"label": "International flight",
"type": "boolean",
"value": True
},
"travel_date": {
"label": "Travel start date",
"type": "date",
"value": "2016-05-22T14:56:28.000Z"
},
"fare": {
"label": "Fare",
"type": "decimal",
"value": "1024.99"
},
"lowest_fare_screenshot": {
"label": "Lowest fare screenshot",
"type": "file",
"value": "e92h842jiu49f8..."
},
"plane_ticket_invoice": {
"label": "Plane ticket invoice PDF",
"type": "file",
"value": ""
}
},
"rule_violations": [
{
"error_text": "Plane ticket invoice must be submitted."
}
]
},
{
"id": 2,
"completed": False,
"title": "Hotel info",
"html_description": "<p>If you used a hotel, please enter the details.</p>",
"fields": {
"total": {
"label": "Total cost",
"type": "decimal"
}
},
"rule_violations": [
]
}
]
}
data = get_reports(report_pk)
return JsonResponse(data)
elif request.method == 'PUT':
return JsonResponse({"message": "Report submitted."})
@ -200,4 +174,5 @@ def section(request, report_pk, section_pk):
"lowest_fare_screenshot": "image",
}
}
return JsonResponse(data)

Binary file not shown.