179 lines
6.5 KiB
Python
179 lines
6.5 KiB
Python
|
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 MNF import settings
|
||
|
from centralisedFileSystem.filetypes import ALLOWED_FILETYPES
|
||
|
|
||
|
|
||
|
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:
|
||
|
unique_together = ("user", "name")
|
||
|
get_latest_by = "created_on"
|
||
|
|
||
|
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)
|
||
|
# 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 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}"
|
||
|
|
||
|
# from django.core.files.base import ContentFile
|
||
|
# from .serializers import TitlePageSerializer
|
||
|
# from docx import Document
|
||
|
# from docx.enum.text import WD_PARAGRAPH_ALIGNMENT
|
||
|
# from django.http import HttpResponse
|
||
|
|
||
|
# from django.template.loader import render_to_string
|
||
|
# from django.contrib.staticfiles.storage import staticfiles_storage
|
||
|
# import io
|
||
|
|
||
|
# class TitlePage(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)
|
||
|
# file = models.FileField(upload_to=generate_upload_folder)
|
||
|
# type = models.CharField(max_length=50, choices=FILE_CHOICES)
|
||
|
# created_on = models.DateTimeField(auto_now_add=True,null=True,blank=True)
|
||
|
# # modified_on = models.DateTimeField(auto_now=True,null=True,blank=True)
|
||
|
# # modification_id = models.IntegerField(default=0, editable=False)
|
||
|
# class Meta:
|
||
|
# unique_together = ("script", "type")
|
||
|
# get_latest_by = "created_on"
|
||
|
|
||
|
# class GetTitlePageAPIView(APIView):
|
||
|
|
||
|
# from django.http import HttpResponse
|
||
|
# from docx import Document
|
||
|
|
||
|
# def get(self, request, script_id):
|
||
|
# try:
|
||
|
# title = TitlePage.objects.get(script=script_id)
|
||
|
# file_path = title.file.path
|
||
|
|
||
|
# # Read the contents of the file
|
||
|
# doc = Document(file_path)
|
||
|
# paragraphs = [p.text for p in doc.paragraphs]
|
||
|
|
||
|
# response = {
|
||
|
# "Name" : paragraphs[0],
|
||
|
# "Author" : paragraphs[1],
|
||
|
# "Dialouges" : paragraphs[2]
|
||
|
# }
|
||
|
|
||
|
# return Response(response)
|
||
|
|
||
|
# except TitlePage.DoesNotExist:
|
||
|
# return HttpResponse('Title page not found', status=404)
|
||
|
|
||
|
# from utils.utilities import merge_docx_files, docx_to_pdf
|
||
|
# # from django.conf import settings
|
||
|
|
||
|
# class DownloadScriptWithTitlepage(APIView):
|
||
|
# def get(self,request,script_id,type):
|
||
|
# """This API will merge the Titlepage and Script of the given TYPE format
|
||
|
|
||
|
# Args:
|
||
|
# request:
|
||
|
# script_id (str): script id
|
||
|
# type (str): file types [pdf,docx,txt]
|
||
|
|
||
|
# Returns:
|
||
|
# file : file will be downloaded
|
||
|
# """
|
||
|
# file1_path = TitlePage.objects.get(script=script_id,type="title-docx").file.path
|
||
|
# file2_path = File.objects.get(
|
||
|
# script = script_id,
|
||
|
# type = "script-"+"original"
|
||
|
# ).file.path
|
||
|
# doc= merge_docx_files(file1_path,file2_path,'media/audit_folder');
|
||
|
|
||
|
# # Convert the merged document to PDF
|
||
|
# if type=="pdf":
|
||
|
# pdf_data = docx_to_pdf(doc)
|
||
|
|
||
|
# # Create an HTTP response with the PDF file content
|
||
|
# response = HttpResponse(pdf_data, content_type='application/pdf')
|
||
|
# response['Content-Disposition'] = 'attachment; filename="merged_script.pdf"'
|
||
|
|
||
|
# return response
|
||
|
# # Save the merged document to a BytesIO buffer
|
||
|
# buffer = io.BytesIO()
|
||
|
# doc.save(buffer)
|
||
|
# buffer.seek(0)
|
||
|
|
||
|
# response = HttpResponse(buffer, content_type='application/vnd.openxmlformats-officedocument.wordprocessingml.document')
|
||
|
# response['Content-Disposition'] = 'attachment; filename="sample_docx_file.docx"'
|
||
|
# return response
|