added finalize report api function & url

This commit is contained in:
Rupika 2019-03-03 12:59:26 -08:00
parent 05f29b3584
commit dac67f8fc6
2 changed files with 29 additions and 11 deletions

View file

@ -8,6 +8,7 @@ urlpatterns = [
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>/final', views.finalize_report),
path('report/<int:report_pk>/section/<int:section_pk>', views.section), path('report/<int:report_pk>/section/<int:section_pk>', views.section),
] ]

View file

@ -3,8 +3,6 @@ from django.http import JsonResponse
from .models import * from .models import *
from .policy import pol from .policy import pol
import os import os
from rest_framework.exceptions import ParseError
from rest_framework.parsers import FileUploadParser, MultiPartParser
from django.core.mail import EmailMultiAlternatives from django.core.mail import EmailMultiAlternatives
from django.template.loader import render_to_string from django.template.loader import render_to_string
from decouple import config from decouple import config
@ -99,7 +97,6 @@ def get_fields(s_id):
return field_set return field_set
def generate_named_fields_for_section(fields): def generate_named_fields_for_section(fields):
""" """
Prepares a dictionary of key-value pairs based on the raw fields Prepares a dictionary of key-value pairs based on the raw fields
@ -205,17 +202,17 @@ def report_detail(request, report_pk):
return JsonResponse(data) return JsonResponse(data)
# PUT: Submits a report to the administrator for review, # PUT: Submits a report to the administrator for review,
# and marks it as "submitted", after which changes may # but is still allowed to make further changes
# not be made.
elif request.method == 'PUT': elif request.method == 'PUT':
rep = Report.objects.get(id=report_pk) # rep = Report.objects.get(id=report_pk)
if rep.submitted == True: # if rep.submitted:
return JsonResponse({"message": "Cannot submit a report that has already been submitted."}, status=409) # return JsonResponse({"message": "Cannot submit a report that has already been submitted."}, status=409)
rep.submitted = True; # rep.submitted = True
rep.save() # rep.save()
# Send email # Send email
send_report_to_admin(request, report_pk) send_report_to_admin(request, report_pk)
return JsonResponse({"message": "Report submitted."}) return JsonResponse({"message": "Report submitted for review."})
# DELETE: Deletes a report from the user's account. # DELETE: Deletes a report from the user's account.
elif request.method == 'DELETE': elif request.method == 'DELETE':
@ -237,6 +234,26 @@ def report_detail(request, report_pk):
r.delete() r.delete()
return JsonResponse({"message": "Deleted report: {0}.".format(title)}) return JsonResponse({"message": "Deleted report: {0}.".format(title)})
@api_view(['PUT'])
def finalize_report(request, report_pk):
"""
This function serves as an API endpoint for submitting
the final report.
:param request: incoming request packet
:param report_pk: report ID
:return: JSON response containing user message
"""
r = Report.objects.get(id=report_pk)
if r.submitted:
return JsonResponse({"message": "Cannot submit a report that has already been submitted."}, status=409)
r.submitted = True
r.save()
# Send email
send_report_to_admin(request, report_pk)
return JsonResponse({"message": "Final report submitted."})
def user_owns_section(user, section): def user_owns_section(user, section):
""" """
Returns true if the specified user is owner of the section. Returns true if the specified user is owner of the section.