From 7c472bb917c896fb527f27e3100ebd8ee3a8744d Mon Sep 17 00:00:00 2001 From: Rupika Date: Sat, 9 Feb 2019 13:41:39 -0800 Subject: [PATCH] moved the get_data_type and get_path_leaf to models.py from views.py to clean up --- back/backend/models.py | 31 +++++++++++++++++++++++++++++++ back/backend/views.py | 30 ++---------------------------- 2 files changed, 33 insertions(+), 28 deletions(-) diff --git a/back/backend/models.py b/back/backend/models.py index b59c1ec..032ed0c 100644 --- a/back/backend/models.py +++ b/back/backend/models.py @@ -1,6 +1,7 @@ from django.db import models from django.conf import settings import datetime +import ntpath class Report(models.Model): 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_integer = models.IntegerField(default=0, blank=True) + # function that prints the string representation + # on the api? def __str__(self): if self.type == "boolean": if self.data_bool: @@ -54,3 +57,31 @@ class Field(models.Model): return "{}".format(self.data_string) elif self.type == "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) + diff --git a/back/backend/views.py b/back/backend/views.py index 1ddfa00..98cb155 100644 --- a/back/backend/views.py +++ b/back/backend/views.py @@ -50,8 +50,8 @@ def get_fields(s_id): queryset = Field.objects.filter(section_id=s_id).order_by('number') for i in queryset: - # function to print corresponding datatype - value = get_datatype(i) + # function that gets the corresponding datatype + value = Field.get_datatype(i) data = { "field_name": i.field_name, "label": i.label, @@ -65,32 +65,6 @@ def get_fields(s_id): return field_set -# 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 = 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(path): - head, tail = ntpath.split(path) - return tail or ntpath.basename(head) - # API Endpoints @api_view(['POST'])