2019-02-15 22:45:49 +00:00
|
|
|
from rest_framework.decorators import api_view, parser_classes
|
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-11 03:23:12 +00:00
|
|
|
import os
|
2019-02-15 22:45:49 +00:00
|
|
|
from rest_framework.exceptions import ParseError
|
|
|
|
from rest_framework.parsers import FileUploadParser, MultiPartParser
|
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.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)
|
2019-02-14 22:19:15 +00:00
|
|
|
for index in range(len(queryset)):
|
|
|
|
i = queryset[index]
|
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-14 22:19:15 +00:00
|
|
|
"rule_violations": [],
|
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))
|
2019-02-14 22:19:15 +00:00
|
|
|
# process rules from the policy file if the section is completed
|
|
|
|
if i.completed:
|
|
|
|
rules = pol.sections[index].rules
|
|
|
|
for rule in rules:
|
|
|
|
try:
|
|
|
|
named_fields = generate_named_fields_for_section(data['fields'])
|
|
|
|
if not rule['rule'](data, named_fields):
|
|
|
|
info = {
|
|
|
|
"label": rule['title'],
|
|
|
|
"rule_break_text": rule['rule_break_text'],
|
|
|
|
}
|
|
|
|
data['rule_violations'].append(info)
|
|
|
|
except Exception as e:
|
|
|
|
print('Rule "{}" encountered an error. {}'.format(rule['title'], e))
|
2019-02-04 17:51:36 +00:00
|
|
|
# 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-09 21:41:39 +00:00
|
|
|
# function that gets the corresponding datatype
|
|
|
|
value = Field.get_datatype(i)
|
2019-02-05 02:02:32 +00:00
|
|
|
data = {
|
2019-02-15 23:28:39 +00:00
|
|
|
"completed": i.completed,
|
2019-02-07 23:32:21 +00:00
|
|
|
"field_name": i.field_name,
|
2019-02-04 17:51:36 +00:00
|
|
|
"label": i.label,
|
2019-02-14 01:38:27 +00:00
|
|
|
"field_type": i.field_type,
|
2019-02-05 02:02:32 +00:00
|
|
|
"number": i.number,
|
2019-02-14 00:24:17 +00:00
|
|
|
"value": value,
|
|
|
|
"id": i.id,
|
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-01-23 21:40:37 +00:00
|
|
|
|
2019-02-14 22:19:15 +00:00
|
|
|
def generate_named_fields_for_section(fields):
|
|
|
|
result = {}
|
|
|
|
for field in fields:
|
|
|
|
key = field['field_name']
|
|
|
|
value = field['value']
|
|
|
|
result[key] = value
|
|
|
|
return result
|
|
|
|
|
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
|
2019-02-09 21:23:01 +00:00
|
|
|
report = Report.objects.create(user_id=request.user, title=request.data['title'],
|
|
|
|
date_created=datetime.date.today())
|
2019-02-07 23:32:21 +00:00
|
|
|
report.save()
|
|
|
|
|
|
|
|
# Create the sections
|
|
|
|
for i in range(len(pol.sections)):
|
|
|
|
section = pol.sections[i]
|
2019-02-09 21:23:01 +00:00
|
|
|
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)
|
2019-02-07 23:32:21 +00:00
|
|
|
s.save()
|
|
|
|
|
|
|
|
# Create the fields
|
|
|
|
for key in section.fields:
|
|
|
|
field = section.fields[key]
|
2019-02-09 21:23:01 +00:00
|
|
|
f = Field.objects.create(section_id=s, field_name=key, label=field['label'],
|
2019-02-15 22:59:42 +00:00
|
|
|
number=field['number'], field_type=field['field_type'], completed=False)
|
2019-02-07 23:32:21 +00:00
|
|
|
f.save()
|
|
|
|
|
|
|
|
# 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-09 21:23:01 +00:00
|
|
|
# View the 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-02-09 21:23:01 +00:00
|
|
|
# actions for an individual report
|
2019-01-27 22:22:42 +00:00
|
|
|
@api_view(['GET', 'PUT', 'DELETE'])
|
|
|
|
def report_detail(request, report_pk):
|
2019-02-09 21:23:01 +00:00
|
|
|
# view the report
|
2019-01-27 22:22:42 +00:00
|
|
|
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)
|
2019-02-09 21:23:01 +00:00
|
|
|
|
|
|
|
# submit the report
|
2019-01-27 22:22:42 +00:00
|
|
|
elif request.method == 'PUT':
|
|
|
|
return JsonResponse({"message": "Report submitted."})
|
2019-02-09 21:23:01 +00:00
|
|
|
|
|
|
|
# Delete the report
|
2019-01-27 22:22:42 +00:00
|
|
|
elif request.method == 'DELETE':
|
2019-02-11 03:23:12 +00:00
|
|
|
# get corresponding sections
|
|
|
|
section_set = Section.objects.filter(report_id=report_pk)
|
|
|
|
for i in section_set:
|
2019-02-11 03:59:28 +00:00
|
|
|
# gets the fields that only have a data file in them
|
2019-02-11 03:23:12 +00:00
|
|
|
field_set = Field.objects.filter(section_id=i.id).exclude(data_file__exact='')
|
|
|
|
if field_set.exists():
|
|
|
|
for j in field_set:
|
|
|
|
# delete the file if exists
|
|
|
|
path_name = str(j.data_file)
|
|
|
|
os.remove(path_name)
|
2019-02-11 03:59:28 +00:00
|
|
|
# delete the full report and catch the title
|
|
|
|
r = Report.objects.get(id=report_pk)
|
|
|
|
title = r.title
|
|
|
|
r.delete()
|
|
|
|
return JsonResponse({"message": "Deleted report: {0}.".format(title)})
|
2019-01-27 22:22:42 +00:00
|
|
|
|
2019-02-09 21:23:01 +00:00
|
|
|
|
|
|
|
# update a section with new data
|
2019-01-27 22:22:42 +00:00
|
|
|
@api_view(['PUT'])
|
2019-02-04 20:10:12 +00:00
|
|
|
def section(request, report_pk, section_pk):
|
2019-02-14 05:07:53 +00:00
|
|
|
|
|
|
|
for key in request.data:
|
2019-02-14 07:36:29 +00:00
|
|
|
# get the matching field object
|
|
|
|
update = Field.objects.get(section_id=section_pk, field_name=key)
|
2019-02-14 05:07:53 +00:00
|
|
|
|
2019-02-14 07:36:29 +00:00
|
|
|
if update.field_type == "boolean":
|
2019-02-14 05:07:53 +00:00
|
|
|
# flight check
|
2019-02-14 22:17:25 +00:00
|
|
|
if request.data[key] == "true":
|
2019-02-14 07:36:29 +00:00
|
|
|
update.data_bool = True
|
2019-02-14 22:17:25 +00:00
|
|
|
elif request.data[key] == "false":
|
2019-02-14 07:36:29 +00:00
|
|
|
update.data_bool = False
|
2019-02-15 23:28:39 +00:00
|
|
|
update.completed = True
|
2019-02-14 05:07:53 +00:00
|
|
|
|
2019-02-14 07:36:29 +00:00
|
|
|
if update.field_type == "decimal":
|
2019-02-14 05:07:53 +00:00
|
|
|
# initialize to 0
|
|
|
|
if (
|
|
|
|
request.data[key] == "" or
|
|
|
|
request.data[key] is None
|
|
|
|
):
|
2019-02-14 07:36:29 +00:00
|
|
|
update.data_decimal = 0.0
|
2019-02-14 05:07:53 +00:00
|
|
|
else:
|
2019-02-15 23:28:39 +00:00
|
|
|
update.completed = True
|
2019-02-14 07:36:29 +00:00
|
|
|
update.data_decimal = request.data[key]
|
2019-02-14 05:07:53 +00:00
|
|
|
|
2019-02-14 07:36:29 +00:00
|
|
|
if update.field_type == "date":
|
2019-02-14 05:07:53 +00:00
|
|
|
# initialize to today's date
|
|
|
|
if (
|
|
|
|
request.data[key] == "" or
|
|
|
|
request.data[key] is None
|
|
|
|
):
|
2019-02-14 07:36:29 +00:00
|
|
|
update.data_date = None
|
2019-02-14 05:07:53 +00:00
|
|
|
else:
|
2019-02-15 23:28:39 +00:00
|
|
|
update.completed = True
|
2019-02-14 07:36:29 +00:00
|
|
|
update.data_date = request.data[key]
|
2019-02-14 05:07:53 +00:00
|
|
|
|
2019-02-14 07:36:29 +00:00
|
|
|
if update.field_type == "file":
|
2019-02-15 23:28:39 +00:00
|
|
|
if not(
|
|
|
|
request.data[key] == "" or
|
|
|
|
request.data[key] is None
|
|
|
|
):
|
|
|
|
update.completed = True
|
|
|
|
update.data_file = request.data[key]
|
2019-02-14 05:07:53 +00:00
|
|
|
|
2019-02-14 07:36:29 +00:00
|
|
|
if update.field_type == "string":
|
2019-02-15 23:28:39 +00:00
|
|
|
if not(
|
|
|
|
request.data[key] == "" or
|
|
|
|
request.data[key] is None
|
|
|
|
):
|
|
|
|
update.completed = True
|
|
|
|
update.data_string = request.data[key]
|
|
|
|
|
2019-02-14 05:07:53 +00:00
|
|
|
|
2019-02-14 07:36:29 +00:00
|
|
|
if update.field_type == "integer":
|
2019-02-14 05:07:53 +00:00
|
|
|
# initialize to 0
|
|
|
|
if (
|
|
|
|
request.data[key] == "" or
|
|
|
|
request.data[key] is None
|
|
|
|
):
|
2019-02-14 07:36:29 +00:00
|
|
|
update.data_integer = 0
|
2019-02-14 05:07:53 +00:00
|
|
|
else:
|
2019-02-15 23:28:39 +00:00
|
|
|
update.completed = True
|
2019-02-14 07:36:29 +00:00
|
|
|
update.data_integer = request.data[key]
|
2019-02-14 05:07:53 +00:00
|
|
|
|
2019-02-14 07:36:29 +00:00
|
|
|
update.save()
|
2019-02-14 05:07:53 +00:00
|
|
|
|
2019-02-14 21:26:26 +00:00
|
|
|
# update section boolean to complete
|
|
|
|
complete = section_complete(section_pk)
|
2019-02-15 01:36:33 +00:00
|
|
|
s = Section.objects.get(id=section_pk)
|
2019-02-14 21:26:26 +00:00
|
|
|
if complete:
|
|
|
|
s.completed = True
|
2019-02-15 22:45:49 +00:00
|
|
|
else:
|
|
|
|
s.completed = False
|
2019-02-16 06:41:54 +00:00
|
|
|
s.save()
|
2019-02-14 21:26:26 +00:00
|
|
|
|
2019-02-16 06:41:54 +00:00
|
|
|
# get section and field details
|
2019-02-13 08:48:35 +00:00
|
|
|
data = {
|
2019-02-16 06:41:54 +00:00
|
|
|
"id": s.id,
|
|
|
|
"completed": s.completed,
|
|
|
|
"title": s.title,
|
|
|
|
"html_description": s.html_description,
|
2019-02-13 08:48:35 +00:00
|
|
|
}
|
2019-02-16 06:41:54 +00:00
|
|
|
data.update(get_fields(s.id))
|
2019-01-20 08:24:39 +00:00
|
|
|
return JsonResponse(data)
|
2019-02-14 01:41:28 +00:00
|
|
|
|
2019-02-15 23:28:39 +00:00
|
|
|
# function checks if a field is complete
|
2019-02-14 21:26:26 +00:00
|
|
|
def section_complete(section_pk):
|
|
|
|
# grab field set
|
|
|
|
check_fields = Field.objects.filter(section_id=section_pk)
|
|
|
|
|
2019-02-15 23:28:39 +00:00
|
|
|
# return true if any field is complete
|
2019-02-14 21:26:26 +00:00
|
|
|
for i in check_fields:
|
2019-02-15 23:28:39 +00:00
|
|
|
if i.completed:
|
|
|
|
return True
|
2019-02-15 22:59:42 +00:00
|
|
|
return False
|