2019-01-20 06:39:07 +00:00
|
|
|
from django.db import models
|
2019-01-23 00:38:09 +00:00
|
|
|
from django.conf import settings
|
2019-02-01 00:55:11 +00:00
|
|
|
import datetime
|
2019-02-09 21:41:39 +00:00
|
|
|
import ntpath
|
2019-01-20 06:39:07 +00:00
|
|
|
|
2019-01-20 22:39:11 +00:00
|
|
|
class Report(models.Model):
|
2019-02-16 20:11:37 +00:00
|
|
|
"""
|
|
|
|
This model represents an expense report that can be
|
|
|
|
created, updated and submitted by a user.
|
|
|
|
"""
|
2019-01-23 00:38:09 +00:00
|
|
|
user_id = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
|
2019-01-20 22:39:11 +00:00
|
|
|
title = models.CharField(max_length=128)
|
2019-02-01 00:55:11 +00:00
|
|
|
date_created = models.DateTimeField('date created', default=datetime.date.today)
|
|
|
|
date_submitted = models.DateTimeField('date submitted', default=datetime.date.today)
|
2019-01-23 00:38:09 +00:00
|
|
|
submitted = models.BooleanField(default=False)
|
2019-03-01 20:26:43 +00:00
|
|
|
reference_number = models.CharField(max_length=32, default='')
|
2019-01-20 06:39:07 +00:00
|
|
|
|
2019-01-25 22:37:03 +00:00
|
|
|
def __str__(self):
|
2019-02-16 20:11:37 +00:00
|
|
|
"""
|
|
|
|
For debugging and display in admin view.
|
|
|
|
"""
|
2019-01-25 22:37:03 +00:00
|
|
|
return self.title
|
|
|
|
|
2019-01-20 22:39:11 +00:00
|
|
|
class Section(models.Model):
|
2019-02-16 20:11:37 +00:00
|
|
|
"""
|
|
|
|
This model represents a logical division of a report,
|
|
|
|
containing its own fields and rules that apply to those
|
|
|
|
fields.
|
|
|
|
"""
|
2019-01-20 22:39:11 +00:00
|
|
|
report_id = models.ForeignKey(Report, on_delete=models.CASCADE)
|
2019-02-14 22:23:05 +00:00
|
|
|
auto_submit = models.BooleanField(default=False)
|
|
|
|
required = models.BooleanField(default=False)
|
|
|
|
completed = models.BooleanField(default=False)
|
2019-01-20 22:39:11 +00:00
|
|
|
title = models.CharField(max_length=256)
|
|
|
|
html_description = models.TextField()
|
|
|
|
number = models.IntegerField()
|
2019-03-01 19:38:12 +00:00
|
|
|
approved = models.BooleanField(default=False)
|
2019-01-20 22:39:11 +00:00
|
|
|
|
2019-01-25 22:37:03 +00:00
|
|
|
def __str__(self):
|
2019-02-16 20:11:37 +00:00
|
|
|
"""
|
|
|
|
For debugging and display in admin view.
|
|
|
|
"""
|
2019-01-25 22:37:03 +00:00
|
|
|
return "{0}(#{1})".format(self.title, self.number)
|
|
|
|
|
2019-01-20 22:39:11 +00:00
|
|
|
class Field(models.Model):
|
2019-02-16 20:11:37 +00:00
|
|
|
"""
|
|
|
|
This model contains a piece of data entered by the user.
|
|
|
|
Depending on the type of the data ( boolean, decimal,
|
|
|
|
date, file, string or integer), different table columns
|
|
|
|
will be used to store the data.
|
|
|
|
"""
|
2019-01-20 22:39:11 +00:00
|
|
|
section_id = models.ForeignKey(Section, on_delete=models.CASCADE)
|
2019-02-07 23:32:21 +00:00
|
|
|
field_name = models.CharField(max_length=512, default="field")
|
2019-02-01 00:55:11 +00:00
|
|
|
label = models.CharField(max_length=512)
|
2019-02-14 22:23:05 +00:00
|
|
|
number = models.IntegerField(default=0)
|
2019-02-14 01:38:27 +00:00
|
|
|
field_type = models.CharField(max_length=128)
|
2019-01-23 00:38:09 +00:00
|
|
|
completed = models.BooleanField(default=False)
|
2019-02-01 00:55:11 +00:00
|
|
|
data_bool = models.BooleanField(default=False)
|
2019-02-14 22:23:05 +00:00
|
|
|
data_decimal = models.DecimalField(max_digits=9, decimal_places=2, default=0)
|
2019-02-14 05:17:31 +00:00
|
|
|
data_date = models.DateField(null=True, blank=True)
|
2019-02-01 00:55:11 +00:00
|
|
|
data_file = models.FileField(upload_to='uploads/%Y/%m/%d/', max_length=512, null=True, blank=True)
|
|
|
|
data_string = models.TextField(default='', blank=True)
|
|
|
|
data_integer = models.IntegerField(default=0, blank=True)
|
|
|
|
|
|
|
|
def __str__(self):
|
2019-02-16 20:11:37 +00:00
|
|
|
"""
|
|
|
|
For debugging and display in the admin view.
|
|
|
|
"""
|
2019-02-14 01:38:27 +00:00
|
|
|
if self.field_type == "boolean":
|
2019-02-01 00:55:11 +00:00
|
|
|
if self.data_bool:
|
|
|
|
return "True"
|
|
|
|
else:
|
|
|
|
return "False"
|
2019-02-14 01:38:27 +00:00
|
|
|
elif self.field_type == "decimal":
|
2019-02-01 00:55:11 +00:00
|
|
|
return "{}".format(self.data_decimal)
|
2019-02-14 01:38:27 +00:00
|
|
|
elif self.field_type == "date":
|
2019-02-01 00:55:11 +00:00
|
|
|
return "{}".format(self.data_date)
|
2019-02-14 01:38:27 +00:00
|
|
|
elif self.field_type == "file":
|
2019-02-01 00:55:11 +00:00
|
|
|
return "{}".format(self.data_file)
|
2019-02-14 01:38:27 +00:00
|
|
|
elif self.field_type == "string":
|
2019-02-01 00:55:11 +00:00
|
|
|
return "{}".format(self.data_string)
|
2019-02-14 01:38:27 +00:00
|
|
|
elif self.field_type == "integer":
|
2019-02-01 00:55:11 +00:00
|
|
|
return "{}".format(self.data_integer)
|
2019-02-21 01:05:36 +00:00
|
|
|
return "Invalid type"
|
2019-02-09 21:41:39 +00:00
|
|
|
|
|
|
|
def get_datatype(self):
|
2019-02-16 20:11:37 +00:00
|
|
|
"""
|
|
|
|
Returns the data corresponding to the type of the
|
|
|
|
field.
|
|
|
|
"""
|
2019-02-14 01:38:27 +00:00
|
|
|
if self.field_type == "boolean":
|
2019-02-09 21:41:39 +00:00
|
|
|
if self.data_bool:
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
2019-02-14 01:38:27 +00:00
|
|
|
elif self.field_type == "decimal":
|
2019-02-09 21:41:39 +00:00
|
|
|
return self.data_decimal
|
2019-02-14 01:38:27 +00:00
|
|
|
elif self.field_type == "date":
|
2019-02-09 21:41:39 +00:00
|
|
|
return "{}".format(self.data_date)
|
2019-02-14 01:38:27 +00:00
|
|
|
elif self.field_type == "file":
|
2019-02-09 21:41:39 +00:00
|
|
|
file_name = self.path_leaf(str(self.data_file))
|
|
|
|
return "{}".format(file_name)
|
2019-02-14 01:38:27 +00:00
|
|
|
elif self.field_type == "string":
|
2019-02-09 21:41:39 +00:00
|
|
|
return "{}".format(self.data_string)
|
2019-02-14 01:38:27 +00:00
|
|
|
elif self.field_type == "integer":
|
2019-02-09 21:41:39 +00:00
|
|
|
return self.data_integer
|
|
|
|
|
2019-02-11 20:36:24 +00:00
|
|
|
def path_leaf(self, path):
|
2019-02-16 20:11:37 +00:00
|
|
|
"""
|
|
|
|
Function accommodating path with slash at end.
|
|
|
|
"""
|
2019-02-11 03:23:12 +00:00
|
|
|
dir_path, name = ntpath.split(path)
|
2019-02-11 20:36:24 +00:00
|
|
|
return name or ntpath.basename(dir_path)
|