2023-02-21 16:43:46 +13:00
|
|
|
from shutil import rmtree
|
|
|
|
|
2023-03-06 18:10:25 +13:00
|
|
|
from django.db.models.signals import post_save, post_delete
|
2023-02-21 16:43:46 +13:00
|
|
|
from django.dispatch import receiver
|
|
|
|
|
2023-03-06 18:10:25 +13:00
|
|
|
from .models import Export, ActivationKey, ActivationKeySet, Questionnaire
|
2023-02-21 16:43:46 +13:00
|
|
|
from .resource_generation_utils import storage
|
|
|
|
|
|
|
|
|
|
|
|
@receiver(post_delete, sender=Export)
|
2023-03-06 18:10:25 +13:00
|
|
|
def delete_export(sender, instance, created, *args, **kwargs):
|
|
|
|
"""Clean up created files on the filesystem when an export is deleted"""
|
2023-02-21 16:43:46 +13:00
|
|
|
rmtree(storage.path(f"export_{instance.pk}"))
|
2023-03-06 18:10:25 +13:00
|
|
|
|
|
|
|
|
|
|
|
@receiver(post_save, sender=ActivationKeySet)
|
|
|
|
def create_keys(sender, instance, created, *args, **kwargs):
|
|
|
|
"""Create n ActivationKey objects where n is the ActivationKeySet.size value"""
|
|
|
|
if created:
|
|
|
|
ActivationKey.objects.bulk_create([
|
|
|
|
ActivationKey(key_set=instance, remaining_activations=instance.initial_activations)
|
|
|
|
for _ in range(instance.size)
|
|
|
|
])
|
|
|
|
|
|
|
|
|
|
|
|
@receiver(post_save, sender=Questionnaire)
|
|
|
|
def activate_key(sender, instance, created, *args, **kwargs):
|
|
|
|
"""Consume one activation on the key associated with the created Questionnaire"""
|
|
|
|
if created and (key := instance.key):
|
|
|
|
key.remaining_activations -= 1
|
|
|
|
key.save()
|