IDDRS_API/data_api/models.py
2023-11-30 15:06:43 +01:00

61 lines
2.1 KiB
Python

from django.db import models
import os
def dynamic_upload_to(instance, filename):
# Generate a dynamic folder name based on some criteria (e.g., user, date, etc.)
return os.path.join(
"data/Standards", str(instance.levelNumber), filename
)
# Create your models here.
class Levels(models.Model):
# The Levels model defines the schema for levels in the database.
# It has fields for the level's title, color, and number.
levelTitle = models.CharField(max_length=200)
levelColor = models.CharField(max_length=200)
levelNumber = models.IntegerField()
def __str__(self):
return str(self.levelNumber)
class StandardsList(models.Model):
# The StandardsList model defines the schema for standards in the database.
# It has a foreign key to Levels, fields for the standard's file, title and path,
# and a __str__ method to represent the standard by its title.
levelID = models.ForeignKey(Levels, on_delete=models.CASCADE, blank=True, null=True)
levelNumber = models.IntegerField(blank=True, null=True)
standardFilePDF = models.FileField(upload_to=dynamic_upload_to) # , upload_to=""
standardFileWord = models.FileField(upload_to=dynamic_upload_to) # , upload_to=""
standardTitle = models.CharField(max_length=200)
standardColor = models.CharField(max_length=200, blank=True)
uploaded_at = models.DateTimeField(auto_now_add=True)
def save(self, *args, **kwargs):
# Automatically set StdColor based on the associated Levels model's color
if self.levelID:
self.standardColor = self.levelID.levelColor
super().save(*args, **kwargs)
def __str__(self):
return self.standardTitle
class FileEvent(models.Model):
EVENT_CHOICES = (
("UPLOAD", "File Uploaded"),
("DELETE", "File Deleted"),
)
event_type = models.CharField(max_length=10, choices=EVENT_CHOICES)
file_name = models.CharField(max_length=255)
timestamp = models.DateTimeField(auto_now_add=True)
fileStatus = models.CharField(default='Pending', max_length=255)
def __str__(self):
return f"{self.get_event_type_display()}: {self.file_name}"