Committing my work from api-remove-endpoint to new branch

This commit is contained in:
Rupika 2019-02-04 09:51:36 -08:00
parent e0611793f6
commit ebed65f723
2 changed files with 145 additions and 146 deletions

View file

@ -5,14 +5,13 @@ from rest_framework.urlpatterns import format_suffix_patterns
from . import views from . import views
urlpatterns = [ urlpatterns = [
# path('', views.List.as_view()), # path('print', views.get_reports),
# path('<int:pk>/', views.Detail.as_view()), # path('print', views.get_sections),
path('print', views.print_all_reports),
#
# path('report', views.report), # path('report', views.report),
# path('reports', views.reports), path('reports', views.reports),
# path('report/<int:report_pk>', views.report_detail), path('report/<int:report_pk>', views.report_detail),
# path('report/<int:report_pk>/section/<int:section_pk>', views.section), # path('report/<int:report_pk>/section/<int:section_pk>', views.section),
# path('account', views.account), # path('account', views.account),
# path('account/login', views.account_login), # path('account/login', views.account_login),

View file

@ -6,83 +6,67 @@ from django.http import JsonResponse
from .models import * from .models import *
from .serializers import * from .serializers import *
# Sample view using generics
# class List(generics.ListCreateAPIView): # function that prints all the reports
# queryset = Report.objects.all() def get_reports(report_pk):
# serializer_class = ReportSerializer # queryset = Report.objects.all()
# queryset = Report.objects.filter(id=report_pk)
# class Detail(generics.RetrieveUpdateDestroyAPIView):
# queryset = Report.objects.all()
# serializer_class = ReportSerializer
def print_all_reports(self):
data = {}
queryset = Report.objects.all()
for i in queryset: for i in queryset:
data = { data = {
"report_pk": report_pk,
"title": i.title, "title": i.title,
"date_created": i.date_created, "date_created": i.date_created,
"submitted": i.submitted, "submitted": i.submitted,
"date_submitted": i.date_submitted, "date_submitted": i.date_submitted,
"sections": get_sections(i.id),
} }
# append the sections for each report
data.update(get_sections(i.id))
return JsonResponse(data) # return JsonResponse(data)
return data
# function that gets all the sections
# takes report_id param
def get_sections(r_id): def get_sections(r_id):
section_set = {"section_set": []} # create a dict of arrays for section
section_set = {"sections": []}
queryset = Section.objects.filter(report_id=r_id) queryset = Section.objects.filter(report_id=r_id)
# queryset = Section.objects.all() # queryset = Section.objects.all()
for i in queryset: for i in queryset:
inner_section = { data = {
"id": i.id, "id": i.id,
"completed": i.completed, "completed": i.completed,
"title": i.title, "title": i.title,
"html_description": i.html_description, "html_description": i.html_description,
"fields": get_fields(i.id),
} }
# section_set.update(inner_section) # append the fields for corresponding section
section_set["section_set"].append(inner_section.copy()) data.update(get_fields(i.id))
# append section to the array
section_set["sections"].append(data.copy())
return section_set return section_set
# return JsonResponse(full)
# function that gets all the fields
# takes section_id param
def get_fields(s_id): def get_fields(s_id):
# create dict of arrays for fields
field_set = {"fields": []} field_set = {"fields": []}
queryset = Field.objects.filter(section_id=s_id) queryset = Field.objects.filter(section_id=s_id)
# queryset = Field.objects.all() # queryset = Field.objects.all()
count = 0
for i in queryset: for i in queryset:
temp = "field" + str(count) data = {i.label: {
inner_field = {i.label: {
"label": i.label, "label": i.label,
"type": i.type, "type": i.type,
"value": i.number, "value": i.number,
}} }}
print("PRINT FIELD") # append the fields to array
print(i.label) # use copy() to avoid overwriting
print(i.type) field_set["fields"].append(data.copy())
print(i.number)
# field_set.append(inner_field)
field_set["fields"].append(inner_field.copy())
# field_set.update(inner_field)
count += 1
print("COUNT = {}".format(count))
return field_set return field_set
# return JsonResponse(field_set)
# API Endpoints # API Endpoints
@api_view(['POST']) @api_view(['POST'])
def report(request): def report(request):
''' '''
@ -153,105 +137,121 @@ def report(request):
# List of reports # List of reports
@api_view(['GET']) @api_view(['GET'])
def reports(request): def reports(request):
report_set = {"reports": []}
queryset = Report.objects.all()
for i in queryset:
data = { data = {
"reports": [ "title": i.title,
{ "date_created": i.date_created,
"report_pk": 1, "submitted": i.submitted,
"title": "2018 Portland trip", "date_submitted": i.date_submitted,
"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"
} }
] # append the sections for each report
} report_set["reports"].append(data.copy())
return JsonResponse(data)
return JsonResponse(report_set)
# 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)
@api_view(['GET', 'PUT', 'DELETE']) @api_view(['GET', 'PUT', 'DELETE'])
def report_detail(request, report_pk): def report_detail(request, report_pk):
if request.method == 'GET': if request.method == 'GET':
data = { data = get_reports(report_pk)
"report_pk": report_pk, # data = {
"title": "2018 Portland trip", # "report_pk": report_pk,
"date_created": "2018-05-22T14:56:28.000Z", # "title": "2018 Portland trip",
"submitted": False, # "date_created": "2018-05-22T14:56:28.000Z",
"date_submitted": "0000-00-00T00:00:00.000Z", # "submitted": False,
"sections": [ # "date_submitted": "0000-00-00T00:00:00.000Z",
{ # "sections": [
"id": 1, # {
"completed": True, # "id": 1,
"title": "Flight Info", # "completed": True,
"html_description": "<p>Enter flight details here.</p>", # "title": "Flight Info",
"fields": { # "html_description": "<p>Enter flight details here.</p>",
"international": { # "fields": {
"label": "International flight", # "international": {
"type": "boolean", # "label": "International flight",
"value": True # "type": "boolean",
}, # "value": True
"travel_date": { # },
"label": "Travel start date", # "travel_date": {
"type": "date", # "label": "Travel start date",
"value": "2016-05-22T14:56:28.000Z" # "type": "date",
}, # "value": "2016-05-22T14:56:28.000Z"
"fare": { # },
"label": "Fare", # "fare": {
"type": "decimal", # "label": "Fare",
"value": "1024.99" # "type": "decimal",
}, # "value": "1024.99"
"lowest_fare_screenshot": { # },
"label": "Lowest fare screenshot", # "lowest_fare_screenshot": {
"type": "file", # "label": "Lowest fare screenshot",
"value": "e92h842jiu49f8..." # "type": "file",
}, # "value": "e92h842jiu49f8..."
"plane_ticket_invoice": { # },
"label": "Plane ticket invoice PDF", # "plane_ticket_invoice": {
"type": "file", # "label": "Plane ticket invoice PDF",
"value": "" # "type": "file",
} # "value": ""
}, # }
"rule_violations": [ # },
{ # "rule_violations": [
"error_text": "Plane ticket invoice must be submitted." # {
} # "error_text": "Plane ticket invoice must be submitted."
] # }
}, # ]
{ # },
"id": 2, # {
"completed": False, # "id": 2,
"title": "Hotel info", # "completed": False,
"html_description": "<p>If you used a hotel, please enter the details.</p>", # "title": "Hotel info",
"fields": { # "html_description": "<p>If you used a hotel, please enter the details.</p>",
"total": { # "fields": {
"label": "Total cost", # "total": {
"type": "decimal" # "label": "Total cost",
} # "type": "decimal"
}, # }
"rule_violations": [ # },
] # "rule_violations": [
} # ]
] # }
} # ]
# }
return JsonResponse(data) return JsonResponse(data)
elif request.method == 'PUT': elif request.method == 'PUT':
return JsonResponse({"message": "Report submitted."}) return JsonResponse({"message": "Report submitted."})
elif request.method == 'DELETE': elif request.method == 'DELETE':
return JsonResponse({"message": "Deleted report {0}.".format(report_pk)}) return JsonResponse({"message": "Deleted report {0}.".format(report_pk)})
# change this api view again!!
@api_view(['PUT']) @api_view(['PUT'])
def section(request, report_pk, section_pk): def section(report_pk, section_pk):
''' '''
Update a section with new data. Update a section with new data.
''' '''