moved the get_data_type and get_path_leaf to models.py from views.py to clean up

This commit is contained in:
Rupika 2019-02-09 13:41:39 -08:00
parent f3d76adf3c
commit 7c472bb917
2 changed files with 33 additions and 28 deletions

View file

@ -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)

View file

@ -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'])