switch media.can_download flag to a db value

This commit is contained in:
meeb 2020-12-09 19:49:48 +11:00
parent c31ed0af28
commit 35bd26727b
4 changed files with 39 additions and 9 deletions

View File

@ -15,6 +15,6 @@ class SourceAdmin(admin.ModelAdmin):
class MediaAdmin(admin.ModelAdmin):
ordering = ('-created',)
list_display = ('key', 'source')
list_display = ('key', 'source', 'can_download', 'downloaded')
readonly_fields = ('uuid', 'created')
search_fields = ('uuid', 'source__key', 'key')

View File

@ -0,0 +1,18 @@
# Generated by Django 3.1.4 on 2020-12-09 08:40
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('sync', '0017_auto_20201208_0521'),
]
operations = [
migrations.AddField(
model_name='media',
name='can_download',
field=models.BooleanField(db_index=True, default=False, help_text='Media has a matching format and can be downloaded', verbose_name='can download'),
),
]

View File

@ -439,6 +439,12 @@ class Media(models.Model):
null=True,
help_text=_('JSON encoded metadata for the media')
)
can_download = models.BooleanField(
_('can download'),
db_index=True,
default=False,
help_text=_('Media has a matching format and can be downloaded')
)
downloaded = models.BooleanField(
_('downloaded'),
db_index=True,
@ -538,14 +544,6 @@ class Media(models.Model):
return False
return False
@property
def can_download(self):
'''
Returns boolean True if the media can be downloaded, that is, the media
has stored formats which are compatible with the source requirements.
'''
return self.get_format_str() is not False
@property
def loaded_metadata(self):
try:

View File

@ -46,6 +46,10 @@ def source_post_save(sender, instance, created, **kwargs):
queue=str(instance.pk),
verbose_name=verbose_name.format(instance.name)
)
# Trigger the post_save signal for each media item linked to this source as various
# flags may need to be recalculated
for media in Media.objects.filter(source=instance):
media.save()
@receiver(pre_delete, sender=Source)
@ -91,6 +95,16 @@ def media_post_save(sender, instance, created, **kwargs):
queue=str(instance.source.pk),
verbose_name=verbose_name.format(instance.name)
)
# Recalculate the "can_download" flag, this may need to change if the source
# specifications have been changed
if instance.get_format_str():
if not instance.can_download:
instance.can_download = True
instance.save()
else:
if instance.can_download:
instance.can_download = True
instance.save()
@receiver(pre_delete, sender=Media)