41 lines
1.2 KiB
Python
41 lines
1.2 KiB
Python
import logging
|
|
|
|
from django.db.models.signals import pre_save, post_save, pre_delete
|
|
from django.dispatch import receiver
|
|
|
|
import files.models
|
|
import files.tasks
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
@receiver(pre_save, sender=files.models.File)
|
|
def _record_changes(
|
|
sender: object, instance: files.models.File, update_fields: object, **kwargs: object
|
|
) -> None:
|
|
was_changed, old_state = instance.pre_save_record(update_fields=update_fields)
|
|
|
|
instance.record_status_change(was_changed, old_state, **kwargs)
|
|
|
|
|
|
def schedule_scan(file: files.models.File) -> None:
|
|
"""Schedule a scan of a given file."""
|
|
logger.info('Scheduling a scan for file pk=%s', file.pk)
|
|
verbose_name = f'clamdscan of "{file.source.name}"'
|
|
files.tasks.clamdscan(file_id=file.pk, creator=file, verbose_name=verbose_name)
|
|
|
|
|
|
@receiver(post_save, sender=files.models.File)
|
|
def _scan_new_file(
|
|
sender: object, instance: files.models.File, created: bool, **kwargs: object
|
|
) -> None:
|
|
if not created:
|
|
return
|
|
|
|
schedule_scan(instance)
|
|
|
|
|
|
@receiver(pre_delete, sender=files.models.File)
|
|
def _log_file_delete(sender: object, instance: files.models.File, **kwargs: object) -> None:
|
|
logger.info('Deleting file pk=%s source=%s', instance.pk, instance.source.name)
|