from django.contrib.postgres.fields import ArrayField from django.db import models from django.dispatch import receiver from datetime import date import os import uuid class PhotoGroup(models.Model): name = models.CharField(unique=True, null=False, max_length=200) date = models.DateField(null=True) def __str__(self): return self.name def get_default_photogroup(): return PhotoGroup.objects.get_or_create( name="Unsortiert", date=None, ) def get_original_photo_path(instance, filename): _, ext = os.path.splitext(filename) return 'static/original_images/%s%s' % (str(uuid.uuid4()), ext) def get_cropped_photo_path(instance, filename): _, ext = os.path.splitext(filename) return 'static/cropped_images/%s%s' % (str(uuid.uuid4()), ext) class Photo(models.Model): legacy_id = models.IntegerField(unique=True, blank=True, null=True) original_image = models.ImageField(upload_to=get_original_photo_path, null=False, blank=False) cropped_image = models.ImageField(upload_to=get_cropped_photo_path, null=True, blank=True) bbox_coords = ArrayField( ArrayField( models.IntegerField(unique=False, blank=False, null=True, default=None), size=2 ), blank=True, null=True, default=None, size=4, ) rotate = models.FloatField(blank=True, null=True) intersections = ArrayField( ArrayField( models.IntegerField(unique=False, blank=False, null=True, default=None), size=2 ), blank=True, null=True, default=None ) group = models.ForeignKey( PhotoGroup, null=False, blank=True, on_delete=models.SET_DEFAULT, default=get_default_photogroup ) ocr_text = models.CharField(max_length=200, null=True, blank=True) def __str__(self): return "Photo #" + str(self.id) @receiver(models.signals.post_delete, sender=Photo) def auto_delete_file_on_delete(sender, instance, **kwargs): """ Deletes file from filesystem when corresponding `MediaFile` object is deleted. """ if instance.original_image: if os.path.isfile(instance.original_image.path): os.remove(instance.original_image.path) if instance.cropped_image: if os.path.isfile(instance.cropped_image.path): os.remove(instance.cropped_image.path) @receiver(models.signals.pre_save, sender=Photo) def auto_delete_file_on_change(sender, instance, **kwargs): """ Deletes old file from filesystem when corresponding `MediaFile` object is updated with new file. """ if not instance.pk: return False try: old_file = Photo.objects.get(pk=instance.pk).original_image except Photo.DoesNotExist: return False new_file = instance.original_image if not old_file == new_file: if os.path.isfile(old_file.path): os.remove(old_file.path) try: old_file = Photo.objects.get(pk=instance.pk).cropped_image except Photo.DoesNotExist: return False if old_file.name: new_file = instance.cropped_image if not old_file == new_file: if os.path.isfile(old_file.path): os.remove(old_file.path) def get_empty_photolog_default(): return [] def get_photolog_pdf_path(instance, filename): return "static/photolog_pdf/%s.pdf" % str(uuid.uuid4()) class PhotoLog(models.Model): title = models.CharField(null=False, blank=False, max_length=5000) date = models.DateField(auto_now=False, default=date.today) render_date = models.BooleanField(null=False, blank=True, default=True) start_slide_image = models.IntegerField(null=True, blank=True, default=3) slides = ArrayField( ArrayField( models.IntegerField(blank=False, null=True), null=True, blank=True, size=3 ), null=False, blank=True, default=get_empty_photolog_default ) pdf = models.FileField(upload_to=get_photolog_pdf_path, null=True, blank=True) def __str__(self): return self.title @receiver(models.signals.post_delete, sender=PhotoLog) def auto_delete_file_on_delete(sender, instance, **kwargs): """ Deletes file from filesystem when corresponding `MediaFile` object is deleted. """ if instance.pdf: if os.path.isfile(instance.pdf.path): os.remove(instance.pdf.path) @receiver(models.signals.pre_save, sender=PhotoLog) def auto_delete_file_on_change(sender, instance, **kwargs): """ Deletes old file from filesystem when corresponding `MediaFile` object is updated with new file. """ if not instance.pk: return False try: old_file = PhotoLog.objects.get(pk=instance.pk).pdf except PhotoLog.DoesNotExist: return False if not old_file.name: return False new_file = instance.pdf if not old_file == new_file: if os.path.isfile(old_file.path): os.remove(old_file.path)