Merge pull request #64 from danieldupriest/print_file_name

Print file name
This commit is contained in:
ppdom 2019-02-09 15:35:41 -08:00 committed by GitHub
commit edb7339543
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 51 additions and 34 deletions

View file

@ -1,6 +1,7 @@
from django.db import models from django.db import models
from django.conf import settings from django.conf import settings
import datetime import datetime
import ntpath
class Report(models.Model): class Report(models.Model):
user_id = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE) user_id = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
@ -38,6 +39,8 @@ class Field(models.Model):
data_string = models.TextField(default='', blank=True) data_string = models.TextField(default='', blank=True)
data_integer = models.IntegerField(default=0, blank=True) data_integer = models.IntegerField(default=0, blank=True)
# function that prints the string representation
# on the api?
def __str__(self): def __str__(self):
if self.type == "boolean": if self.type == "boolean":
if self.data_bool: if self.data_bool:
@ -54,3 +57,31 @@ class Field(models.Model):
return "{}".format(self.data_string) return "{}".format(self.data_string)
elif self.type == "integer": elif self.type == "integer":
return "{}".format(self.data_integer) return "{}".format(self.data_integer)
# 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":
file_name = self.path_leaf(str(self.data_file))
return "{}".format(file_name)
elif self.type == "string":
return "{}".format(self.data_string)
elif self.type == "integer":
return self.data_integer
# function that accommodates if
# path has slash at end
def path_leaf(self, path):
head, tail = ntpath.split(path)
return tail or ntpath.basename(head)

View file

@ -2,11 +2,11 @@ from rest_framework.decorators import api_view
from django.http import JsonResponse from django.http import JsonResponse
from .models import * from .models import *
from .policy import pol from .policy import pol
import ntpath
# function that prints all the reports # function that prints all the reports
def get_reports(report_pk): def get_reports(report_pk):
# queryset = Report.objects.all()
queryset = Report.objects.filter(id=report_pk) queryset = Report.objects.filter(id=report_pk)
for i in queryset: for i in queryset:
data = { data = {
@ -28,7 +28,6 @@ def get_sections(r_id):
# create a dict of arrays for section # create a dict of arrays for section
section_set = {"sections": []} section_set = {"sections": []}
queryset = Section.objects.filter(report_id=r_id) queryset = Section.objects.filter(report_id=r_id)
# queryset = Section.objects.all()
for i in queryset: for i in queryset:
data = { data = {
"id": i.id, "id": i.id,
@ -51,8 +50,8 @@ def get_fields(s_id):
queryset = Field.objects.filter(section_id=s_id).order_by('number') queryset = Field.objects.filter(section_id=s_id).order_by('number')
for i in queryset: for i in queryset:
# function to print corresponding datatype # function that gets the corresponding datatype
value = get_datatype(i) value = Field.get_datatype(i)
data = { data = {
"field_name": i.field_name, "field_name": i.field_name,
"label": i.label, "label": i.label,
@ -66,29 +65,6 @@ def get_fields(s_id):
return field_set 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 Endpoints
@api_view(['POST']) @api_view(['POST'])
@ -98,20 +74,25 @@ def report(request):
''' '''
# Create the report # Create the report
report = Report.objects.create(user_id=request.user, title=request.data['title'], date_created=datetime.date.today()) report = Report.objects.create(user_id=request.user, title=request.data['title'],
date_created=datetime.date.today())
report.save() report.save()
# Create the sections # Create the sections
for i in range(len(pol.sections)): for i in range(len(pol.sections)):
section = pol.sections[i] 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 = 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() s.save()
# Create the fields # Create the fields
j = 0 j = 0
for key in section.fields: for key in section.fields:
field = section.fields[key] 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 = Field.objects.create(section_id=s, field_name=key, label=field['label'],
number=j, type=field['type'], completed=False)
f.save() f.save()
j = j+1 j = j+1
@ -119,7 +100,7 @@ def report(request):
data = get_reports(report.id) data = get_reports(report.id)
return JsonResponse(data) return JsonResponse(data)
# List of reports # View the list of reports
@api_view(['GET']) @api_view(['GET'])
def reports(request): def reports(request):
report_set = {"reports": []} report_set = {"reports": []}
@ -139,21 +120,26 @@ def reports(request):
return JsonResponse(report_set) return JsonResponse(report_set)
# actions for an individual report
@api_view(['GET', 'PUT', 'DELETE']) @api_view(['GET', 'PUT', 'DELETE'])
def report_detail(request, report_pk): def report_detail(request, report_pk):
# view the report
if request.method == 'GET': if request.method == 'GET':
data = get_reports(report_pk) data = get_reports(report_pk)
return JsonResponse(data) return JsonResponse(data)
# submit the report
elif request.method == 'PUT': elif request.method == 'PUT':
return JsonResponse({"message": "Report submitted."}) return JsonResponse({"message": "Report submitted."})
# Delete the report
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)})
# update a section with new data
@api_view(['PUT']) @api_view(['PUT'])
def section(request, report_pk, section_pk): def section(request, report_pk, section_pk):
'''
Update a section with new data.
'''
data = { data = {
"message": "Updated report {0}, section {1}.".format(report_pk, section_pk), "message": "Updated report {0}, section {1}.".format(report_pk, section_pk),
"fields": { "fields": {

Binary file not shown.

Before

Width:  |  Height:  |  Size: 68 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 75 KiB