-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathspectrogram_image.py
More file actions
56 lines (43 loc) · 2.02 KB
/
spectrogram_image.py
File metadata and controls
56 lines (43 loc) · 2.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
from __future__ import annotations
from django.contrib.contenttypes.fields import GenericForeignKey
from django.contrib.contenttypes.models import ContentType
from django.db import models
from django.dispatch import receiver
def spectrogram_image_upload_to(instance, filename):
related = instance.content_object
recording = getattr(related, "recording", None) or getattr(related, "nabat_recording", None)
recording_id = getattr(recording, "id", None)
if not recording_id:
raise ValueError("Related content must have a recording or nabat_recording.")
return f"recording_{recording_id}/{instance.type}/image_{instance.index}_{filename}"
class SpectrogramImage(models.Model):
SPECTROGRAM_TYPE_CHOICES = [
("spectrogram", "Spectrogram"),
("compressed", "Compressed"),
("masks", "Masks"),
("waveform_compressed", "Waveform Compressed"),
("waveform_uncompressed", "Waveform Uncompressed"),
]
content_object = GenericForeignKey("content_type", "object_id")
content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE)
object_id = models.PositiveIntegerField()
type = models.CharField(
max_length=128,
choices=SPECTROGRAM_TYPE_CHOICES,
default="spectrogram",
)
index = models.PositiveIntegerField()
image_file = models.FileField(upload_to=spectrogram_image_upload_to) # temporary placeholder
class Meta:
ordering = ["index"]
def __str__(self):
return f"SpectrogramImage {self.pk} ({self.type}, index={self.index})"
@receiver(models.signals.pre_delete, sender=SpectrogramImage)
def delete_content(sender, instance, **kwargs):
if not instance.image_file:
return
# Only delete the file if no other SpectrogramImage references the same path
# (allows shared image references e.g. from copy_recordings management command)
same_path_count = sender.objects.filter(image_file=instance.image_file.name).count()
if same_path_count <= 1:
instance.image_file.delete(save=False)