2019-01-27 22:22:42 +00:00
|
|
|
from rest_framework.decorators import api_view
|
2019-01-20 06:39:07 +00:00
|
|
|
from django.http import JsonResponse
|
2019-01-23 21:40:37 +00:00
|
|
|
from .models import *
|
2019-02-07 23:32:21 +00:00
|
|
|
from .policy import pol
|
2019-02-08 01:13:43 +00:00
|
|
|
|
2019-01-23 21:40:37 +00:00
|
|
|
|
2019-02-04 17:51:36 +00:00
|
|
|
# function that prints all the reports
|
|
|
|
def get_reports(report_pk):
|
|
|
|
# queryset = Report.objects.all()
|
|
|
|
queryset = Report.objects.filter(id=report_pk)
|
2019-02-03 08:47:04 +00:00
|
|
|
for i in queryset:
|
|
|
|
data = {
|
2019-02-04 17:51:36 +00:00
|
|
|
"report_pk": report_pk,
|
|
|
|
"title": i.title,
|
|
|
|
"date_created": i.date_created,
|
|
|
|
"submitted": i.submitted,
|
|
|
|
"date_submitted": i.date_submitted,
|
2019-02-03 08:47:04 +00:00
|
|
|
}
|
2019-02-04 17:51:36 +00:00
|
|
|
# append the sections for each report
|
|
|
|
data.update(get_sections(i.id))
|
2019-02-03 08:47:04 +00:00
|
|
|
|
2019-02-04 17:51:36 +00:00
|
|
|
# return JsonResponse(data)
|
|
|
|
return data
|
2019-02-03 08:47:04 +00:00
|
|
|
|
2019-02-04 17:51:36 +00:00
|
|
|
# function that gets all the sections
|
|
|
|
# takes report_id param
|
2019-02-03 08:47:04 +00:00
|
|
|
def get_sections(r_id):
|
2019-02-04 17:51:36 +00:00
|
|
|
# create a dict of arrays for section
|
|
|
|
section_set = {"sections": []}
|
2019-02-03 08:47:04 +00:00
|
|
|
queryset = Section.objects.filter(report_id=r_id)
|
|
|
|
# queryset = Section.objects.all()
|
|
|
|
for i in queryset:
|
2019-02-04 17:51:36 +00:00
|
|
|
data = {
|
|
|
|
"id": i.id,
|
|
|
|
"completed": i.completed,
|
|
|
|
"title": i.title,
|
|
|
|
"html_description": i.html_description,
|
2019-02-03 08:47:04 +00:00
|
|
|
}
|
2019-02-04 17:51:36 +00:00
|
|
|
# append the fields for corresponding section
|
|
|
|
data.update(get_fields(i.id))
|
|
|
|
# append section to the array
|
|
|
|
section_set["sections"].append(data.copy())
|
2019-02-03 08:47:04 +00:00
|
|
|
|
|
|
|
return section_set
|
|
|
|
|
2019-02-04 17:51:36 +00:00
|
|
|
# function that gets all the fields
|
|
|
|
# takes section_id param
|
2019-02-03 08:47:04 +00:00
|
|
|
def get_fields(s_id):
|
2019-02-04 17:51:36 +00:00
|
|
|
# create dict of arrays for fields
|
2019-02-03 08:47:04 +00:00
|
|
|
field_set = {"fields": []}
|
2019-02-07 23:32:21 +00:00
|
|
|
queryset = Field.objects.filter(section_id=s_id).order_by('number')
|
2019-02-08 02:13:32 +00:00
|
|
|
|
2019-02-03 08:47:04 +00:00
|
|
|
for i in queryset:
|
2019-02-08 02:13:32 +00:00
|
|
|
# function to print corresponding datatype
|
|
|
|
key, value = get_datatype(i)
|
2019-02-05 02:02:32 +00:00
|
|
|
data = {
|
2019-02-07 23:32:21 +00:00
|
|
|
"field_name": i.field_name,
|
2019-02-04 17:51:36 +00:00
|
|
|
"label": i.label,
|
|
|
|
"type": i.type,
|
2019-02-05 02:02:32 +00:00
|
|
|
"number": i.number,
|
2019-02-08 02:13:32 +00:00
|
|
|
key: value
|
2019-02-05 02:02:32 +00:00
|
|
|
}
|
2019-02-04 17:51:36 +00:00
|
|
|
# append the fields to array
|
|
|
|
# use copy() to avoid overwriting
|
|
|
|
field_set["fields"].append(data.copy())
|
2019-02-03 08:47:04 +00:00
|
|
|
|
|
|
|
return field_set
|
|
|
|
|
2019-02-08 02:13:32 +00:00
|
|
|
# function to convert value into JSON
|
|
|
|
def to_json(convert):
|
|
|
|
return {"value": convert}
|
|
|
|
|
|
|
|
# function that gets corresponding
|
|
|
|
# data type
|
|
|
|
def get_datatype(self):
|
|
|
|
if self.type == "boolean":
|
|
|
|
if self.data_bool:
|
|
|
|
return "data_bool", True
|
|
|
|
else:
|
|
|
|
return "data_bool", False
|
|
|
|
elif self.type == "decimal":
|
|
|
|
return "decimal", self.data_decimal
|
|
|
|
elif self.type == "date":
|
|
|
|
return "date", "{}".format(self.data_date)
|
|
|
|
elif self.type == "file":
|
|
|
|
return "file", "{}".format(self.data_file)
|
|
|
|
elif self.type == "string":
|
|
|
|
return "string", "{}".format(self.data_string)
|
|
|
|
elif self.type == "integer":
|
|
|
|
return "integer", self.data_integer
|
|
|
|
|
2019-01-23 21:40:37 +00:00
|
|
|
|
2019-01-27 22:22:42 +00:00
|
|
|
# API Endpoints
|
|
|
|
@api_view(['POST'])
|
|
|
|
def report(request):
|
|
|
|
'''
|
|
|
|
Generate a new empty report and return it
|
|
|
|
'''
|
2019-02-07 23:32:21 +00:00
|
|
|
|
|
|
|
# Create the report
|
|
|
|
report = Report.objects.create(user_id=request.user, title=request.data['title'], date_created=datetime.date.today())
|
|
|
|
report.save()
|
|
|
|
|
|
|
|
# Create the sections
|
|
|
|
for i in range(len(pol.sections)):
|
|
|
|
section = pol.sections[i]
|
|
|
|
s = Section.objects.create(report_id=report, auto_submit=section.auto_submit, required=section.required, completed=False, title=section.title, html_description=section.html_description, number=i)
|
|
|
|
s.save()
|
|
|
|
|
|
|
|
# Create the fields
|
|
|
|
j = 0
|
|
|
|
for key in section.fields:
|
|
|
|
field = section.fields[key]
|
|
|
|
f = Field.objects.create(section_id=s, field_name=key, label=field['label'], number=j, type=field['type'], completed=False)
|
|
|
|
f.save()
|
|
|
|
j = j+1
|
|
|
|
|
|
|
|
# Return the newly created report
|
|
|
|
data = get_reports(report.id)
|
2019-01-20 08:24:39 +00:00
|
|
|
return JsonResponse(data)
|
2019-01-20 06:39:07 +00:00
|
|
|
|
2019-02-03 08:47:04 +00:00
|
|
|
# List of reports
|
2019-01-27 22:22:42 +00:00
|
|
|
@api_view(['GET'])
|
|
|
|
def reports(request):
|
2019-02-04 17:51:36 +00:00
|
|
|
report_set = {"reports": []}
|
2019-02-08 01:13:43 +00:00
|
|
|
queryset = Report.objects.all().filter(user_id=request.user.id).order_by('date_created')
|
2019-02-04 17:51:36 +00:00
|
|
|
for i in queryset:
|
|
|
|
data = {
|
2019-02-08 01:13:43 +00:00
|
|
|
"user_id": request.user.id,
|
2019-02-05 20:48:49 +00:00
|
|
|
"report_pk": i.id,
|
2019-02-04 17:51:36 +00:00
|
|
|
"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)
|
|
|
|
|
2019-01-20 08:24:39 +00:00
|
|
|
|
2019-01-27 22:22:42 +00:00
|
|
|
@api_view(['GET', 'PUT', 'DELETE'])
|
|
|
|
def report_detail(request, report_pk):
|
|
|
|
if request.method == 'GET':
|
2019-02-04 17:51:36 +00:00
|
|
|
data = get_reports(report_pk)
|
2019-01-27 22:22:42 +00:00
|
|
|
return JsonResponse(data)
|
|
|
|
elif request.method == 'PUT':
|
|
|
|
return JsonResponse({"message": "Report submitted."})
|
|
|
|
elif request.method == 'DELETE':
|
|
|
|
return JsonResponse({"message": "Deleted report {0}.".format(report_pk)})
|
|
|
|
|
|
|
|
@api_view(['PUT'])
|
2019-02-04 20:10:12 +00:00
|
|
|
def section(request, report_pk, section_pk):
|
2019-01-27 22:22:42 +00:00
|
|
|
'''
|
|
|
|
Update a section with new data.
|
|
|
|
'''
|
2019-01-20 08:24:39 +00:00
|
|
|
data = {
|
2019-01-27 22:22:42 +00:00
|
|
|
"message": "Updated report {0}, section {1}.".format(report_pk, section_pk),
|
2019-01-20 19:58:30 +00:00
|
|
|
"fields": {
|
|
|
|
"international": True,
|
|
|
|
"travel_date": "2012-04-23T18:25:43.511Z",
|
|
|
|
"fare": "1024.99",
|
|
|
|
"lowest_fare_screenshot": "image",
|
|
|
|
}
|
2019-01-20 08:24:39 +00:00
|
|
|
}
|
2019-02-04 20:10:12 +00:00
|
|
|
|
2019-01-20 08:24:39 +00:00
|
|
|
return JsonResponse(data)
|