Merge pull request #60 from danieldupriest/field_to_json

Field to json
This commit is contained in:
joe-arriaga 2019-02-07 18:33:45 -08:00 committed by GitHub
commit 4c560062c7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 35 additions and 5 deletions

View file

@ -12,7 +12,8 @@ class Policy():
class Section():
def __init__(self, title="Section", html_description="", required=False, auto_submit=False, fields={}):
def __init__(self, title="Section", html_description="", required=False,
auto_submit=False, fields={}):
self.title = title
self.html_description = html_description
self.required = required
@ -60,6 +61,7 @@ flight_section = Section(
"departure_date": {"label": "Departure date", "type": "date"},
"return_date": {"label": "Return date", "type": "date"},
"fare": {"label": "Fare", "type": "decimal"},
"layovers": {"label": "Transit wait", "type": "integer"},
}
)
@ -75,7 +77,8 @@ pol.add_section(flight_section)
#### Section 2
lodging_section = Section(
title="Hotel Info",
html_description="<p>Enter hotel info here.\nPer diem rates can be found at <a href='https://www.gsa.gov/travel/plan-book/per-diem-rates'></a></p>",
html_description="<p>Enter hotel info here.\nPer diem rates can be found at "
"<a href='https://www.gsa.gov/travel/plan-book/per-diem-rates'></a></p>",
fields={
"check-in_date": {"label": "Check-in date", "type": "date"},
"check-out_date": {"label": "Check-out date", "type": "date"},

View file

@ -2,7 +2,7 @@ from rest_framework.decorators import api_view
from django.http import JsonResponse
from .models import *
from .policy import pol
import json
# function that prints all the reports
def get_reports(report_pk):
@ -49,13 +49,16 @@ def get_fields(s_id):
# create dict of arrays for fields
field_set = {"fields": []}
queryset = Field.objects.filter(section_id=s_id).order_by('number')
for i in queryset:
# function to print corresponding datatype
value = get_datatype(i)
data = {
"field_name": i.field_name,
"label": i.label,
"type": i.type,
"number": i.number,
"value": "i.to_json()",
"value": value
}
# append the fields to array
# use copy() to avoid overwriting
@ -63,6 +66,29 @@ def get_fields(s_id):
return field_set
# 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 True
else:
return False
elif self.type == "decimal":
return self.data_decimal
elif self.type == "date":
return "{}".format(self.data_date)
elif self.type == "file":
return "{}".format(self.data_file)
elif self.type == "string":
return "{}".format(self.data_string)
elif self.type == "integer":
return self.data_integer
# API Endpoints
@api_view(['POST'])
@ -97,9 +123,10 @@ def report(request):
@api_view(['GET'])
def reports(request):
report_set = {"reports": []}
queryset = Report.objects.all().order_by('date_created')
queryset = Report.objects.all().filter(user_id=request.user.id).order_by('date_created')
for i in queryset:
data = {
"user_id": request.user.id,
"report_pk": i.id,
"title": i.title,
"date_created": i.date_created,

Binary file not shown.