Make it possible to fully delete unlisted/unrated extensions #81
@ -109,4 +109,31 @@ def _set_is_listed(
|
||||
extension.save()
|
||||
|
||||
|
||||
@receiver(post_save, sender=extensions.models.Preview)
|
||||
@receiver(post_save, sender=extensions.models.Version)
|
||||
def _auto_approve_subsequent_uploads(
|
||||
sender: object,
|
||||
instance: Union[extensions.models.Preview, extensions.models.Version],
|
||||
created: bool,
|
||||
raw: bool,
|
||||
**kwargs: object,
|
||||
):
|
||||
if raw:
|
||||
return
|
||||
if not created:
|
||||
return
|
||||
if not instance.file_id:
|
||||
return
|
||||
|
||||
# N.B.: currently, subsequent version and preview uploads get approved automatically,
|
||||
# if extension is currently listed (meaning, it was approved by a human already).
|
||||
extension = instance.extension
|
||||
file = instance.file
|
||||
if extension.is_listed:
|
||||
file.status = files.models.File.STATUSES.APPROVED
|
||||
args = {'f_id': file.pk, 'pk': instance.pk, 'sender': sender, 's': file.source.name}
|
||||
logger.info('Auto-approving file pk=%(f_id)s of %(sender)s pk=%(pk)s source=%(s)s', args)
|
||||
file.save(update_fields={'status', 'date_modified'})
|
||||
|
||||
|
||||
version_uploaded.connect(send_notifications, dispatch_uid='send_notifications')
|
||||
|
@ -1,7 +1,35 @@
|
||||
# Generated by Django 4.2.11 on 2024-04-18 10:10
|
||||
import logging
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def change_first_uploads_to_awaiting_review(apps, schema_editor):
|
||||
from constants.base import FILE_STATUS_CHOICES
|
||||
|
||||
File = apps.get_model('files', 'File')
|
||||
to_update = []
|
||||
for obj in File.objects.filter(status=FILE_STATUS_CHOICES.APPROVED):
|
||||
# Unless this is a subsequent upload of a version/preview of an already approved extension,
|
||||
# it should have a default status (Awaiting Review).
|
||||
extension = (
|
||||
obj.extension_preview.first() and obj.extension_preview.first().extension
|
||||
or (hasattr(obj, 'version') and obj.version.extension)
|
||||
)
|
||||
if extension.is_listed:
|
||||
continue
|
||||
logger.info(
|
||||
'Will change file pk=%s status from %s to %s', obj.pk, obj.get_status_display(),
|
||||
'Awaiting Review',
|
||||
)
|
||||
obj.status = FILE_STATUS_CHOICES.AWAITING_REVIEW
|
||||
to_update.append(obj)
|
||||
|
||||
logger.info('Updating %s files', len(to_update))
|
||||
File.objects.bulk_update(to_update, batch_size=500, fields={'status'})
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
@ -15,4 +43,5 @@ class Migration(migrations.Migration):
|
||||
name='status',
|
||||
field=models.PositiveSmallIntegerField(choices=[(2, 'Awaiting Review'), (3, 'Approved'), (4, 'Disabled by staff'), (5, 'Disabled by author')], default=2),
|
||||
),
|
||||
migrations.RunPython(change_first_uploads_to_awaiting_review),
|
||||
]
|
||||
|
Loading…
Reference in New Issue
Block a user