from __future__ import annotations import os import uuid from django.contrib.auth.models import User from django.db import models from django.urls import reverse from jsonfield import JSONField from centralisedFileSystem.filetypes import ALLOWED_FILETYPES from MNF import settings # from ScriptPad2.models import BeatNotes # from ScriptPad2.models import ScreenPlay as ScreenPlay2 class ScreenPlay(models.Model): id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False) user = models.ForeignKey(User, on_delete=models.CASCADE, related_name="screenplays", editable=False) author = models.CharField(max_length=100) name = models.CharField(max_length=200, blank=False, null=False) created_on = models.DateTimeField(auto_now_add=True) version_count = models.IntegerField(default=0, editable=False) class Meta: get_latest_by = "created_on" # unique_together = ("user", "name") def __str__(self) -> str: return str(self.name) def get_absolute_url(self) -> str: return reverse("screenplay_detail", kwargs={"pk": self.pk}) def next_version(self) -> int: self.version_count = self.version_count + 1 self.save() return self.version_count class Script(models.Model): LANGUAGES = tuple((k, v) for k, v in settings.languages.items()) id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False) screenplay = models.ForeignKey(ScreenPlay, on_delete=models.CASCADE, related_name="scripts") created_on = models.DateTimeField(auto_now_add=True) modified_on = models.DateTimeField(auto_now=True) version = models.IntegerField(editable=False, null=True) language = models.CharField(max_length=100, default="", choices=LANGUAGES) interval_at = models.IntegerField(default=0, null=True,blank=True) lock_status = models.BooleanField(default=False, null=True,blank=True) no_of_pages = models.IntegerField(default=0, null=True,blank=True) # user = models.ForeignKey(User, on_delete=models.CASCADE, related_name="screenplays", editable=False) class Meta: get_latest_by = "created_on" def __str__(self) -> str: return f"{self.screenplay.name} -V{self.version}" def get_absolute_url(self) -> str: return reverse("script_detail", kwargs={"pk": self.pk}) def generate_upload_folder(instance : File, filename : str) -> str: script_folder = os.path.join(settings.MEDIA_ROOT,"scripts_folder", str(instance.script.id)) os.makedirs(script_folder, exist_ok=True) filename = f"{instance.id}.{filename.rsplit('.', 1)[1]}" return "scripts_folder/" + str(instance.script.id) + "/" + filename def generate_gpt_text_folder(instance , filename : str) -> str: script_folder = os.path.join(settings.MEDIA_ROOT,"GPT_text_folder", str(instance.script.id)) os.makedirs(script_folder, exist_ok=True) filename = f"{instance.id}.{filename.rsplit('.', 1)[1]}" return os.path.join(script_folder, filename) class File(models.Model): FILE_CHOICES = tuple(((k, k) for k in ALLOWED_FILETYPES)) id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False) script = models.ForeignKey(Script, on_delete=models.CASCADE, related_name="scripts_files") content_type = models.CharField(max_length=200, editable=False) file = models.FileField(upload_to=generate_upload_folder, blank=True) type = models.CharField(max_length=50, choices=FILE_CHOICES) created_on = models.DateTimeField(auto_now_add=True) modified_on = models.DateTimeField(auto_now=True) modification_id = models.IntegerField(default=0, editable=False) skip_post_save = models.BooleanField(default = False,null=True,blank=True) class Meta: unique_together = ("script", "type") get_latest_by = "created_on" def __str__(self) -> str: return f"{self.script}_{self.type}_{self.modification_id}" # class BeatSheet(models.Model): # id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False) # script = models.ForeignKey("ScreenPlay2", on_delete=models.CASCADE, related_name="beat_script",null=True,blank=True) # scene_number = models.IntegerField(default=0) # beat = models.CharField(max_length=250,null=True,blank=True) # # beat_note = models.ForeignKey(BeatNotes,null=True,blank=True, on_delete=models.CASCADE) # beat_note = models.ManyToManyField("BeatNotes", blank=True) # created_on = models.DateTimeField(auto_now_add=True) # modified_on = models.DateTimeField(auto_now=True) # had_used_gpt = models.BooleanField(default=False) # gpt_response = models.TextField(null=True,blank=True) # text_file_path = models.FileField(upload_to = generate_gpt_text_folder, blank = True, null=True) # has_notes = models.BooleanField(default = False,null=True,blank=True) # archeive = models.BooleanField(default = False,null=True,blank=True) # is_paid = models.BooleanField(default=False) # is_subsequent = models.BooleanField(default=False) # beat_note_id = models.UUIDField(default=False,null=True,blank = True) # gptresp = models.TextField(null=True,blank=True) # def __str__(self) -> str : # return f"{self.script} {self.scene_number}"