Conversion_Kitchen_Code/kitchen_counter/centralisedFileSystem/signals.py

103 lines
3.7 KiB
Python
Raw Permalink Normal View History

2024-04-27 09:33:09 +00:00
"""
Signals/ Triggers that will execute a code when specific model-events occure.
"""
import mimetypes
import os
import shutil
from django.db.models.signals import post_delete, post_save, pre_save
from django.core import exceptions as django_exception
from django.dispatch import receiver
from centralisedFileSystem.models import File, Script
from centralisedFileSystem.filetypes import get_filetype_obj
from MNF import settings
# commented now importing without celery - mohit 24dec
from scriptAudit.utils import audit_in_background
# from scriptAudit.models import AuditStatus
@receiver(post_save, sender=Script)
def update_version_count(sender, instance: Script, created, **kwargs) -> None:
'''
Updates the `version_count` of `Screenplay` and adds `version` number in `Script`.
'''
if created:
instance.version = instance.screenplay.next_version()
instance.save()
@receiver(post_save, sender=File)
def post_save_file_updates(sender, instance: File, created, **kwargs) -> None:
'''
Saves the mimetype of the file when the file is created.
And incriment the modification_id on every save.
'''
if created:
mimetype, _ = mimetypes.guess_type(instance.file.path)
if (instance.file.path.split('.')[-1] == 'fdx'): ## mimetype does not guess fdx
File.objects.filter(id=instance.id).update(content_type="application/vnd.openxmlformats-officedocument.wordprocessingml.document")
elif (instance.file.path.split('.')[-1] == 'docx'): ## mimetype does not guess fdx
File.objects.filter(id=instance.id).update(content_type="application/vnd.openxmlformats-officedocument.wordprocessingml.document")
else:
File.objects.filter(id=instance.id).update(content_type=mimetype)
#File.objects.filter(id=instance.id).update(content_type=mimetype)
if instance.type == "script-original" and instance.skip_post_save==False:
"""
audit is only run automaticaly on creation is if thw file type is script-original.
"""
audit_in_background(instance.script.id)
else:
new_mod_id = instance.modification_id+1
File.objects.filter(id=instance.id).update(modification_id=new_mod_id)
@receiver(pre_save, sender=File)
def pre_save_file_extention_check(sender, instance: File, **kwargs) -> None:
"""
To ensure the proper file type is provided as specified
Raises:
django_exception.ValidationError: when the extention of given file doesn't matches with the given filetype.
"""
if not kwargs.get("raw", False):
print(f"apple 103 {instance}")
instance.full_clean()
FileType_obj = get_filetype_obj(instance.type)
given_file_extention = instance.file.path.rsplit('.', 1)[1]
if given_file_extention not in FileType_obj.allowed_extentions:
print("In error condition, file type not allowed")
raise django_exception.ValidationError(
f"{given_file_extention} is not allowed in {instance.type} FileType")
if not FileType_obj.validate():
raise django_exception.ValidationError()
@receiver(post_delete, sender=Script)
def auto_delete_folder(sender, instance: Script, **kwargs) -> None:
"""
Deletes script folder `Script` object is deleted.
"""
folder_path = os.path.join(
settings.MEDIA_ROOT, "scripts_folder", str(instance.id))
if os.path.exists(folder_path):
shutil.rmtree(folder_path)
@receiver(post_delete, sender=File)
def auto_delete_file(sender, instance: File, **kwargs) -> None:
"""
Deletes file from filesystem when `File` object is deleted.a
"""
if instance.file:
if os.path.isfile(instance.file.path):
os.remove(instance.file.path)