diff --git a/backend/backend_api/admin.py b/backend/backend_api/admin.py index 83ef67a..17ee18d 100644 --- a/backend/backend_api/admin.py +++ b/backend/backend_api/admin.py @@ -44,10 +44,10 @@ class UserAdmin(admin.ModelAdmin): def send_record_links(self, request, obj): for user in obj: presentation_participation = PresentationParticipation.objects.filter(user=user, - status=PresentationParticipation. - StatusChoices.PURCHASED) + status=PresentationParticipation. + StatusChoices.PURCHASED) workshop_participation = WorkshopRegistration.objects.filter(user=user, - status=WorkshopRegistration.StatusChoices.PURCHASED) + status=WorkshopRegistration.StatusChoices.PURCHASED) if len(presentation_participation) == 0 and len(workshop_participation) == 0: continue @@ -70,7 +70,6 @@ def send_record_links(self, request, obj): })).start() - class DiscountAdmin(admin.ModelAdmin): list_display = ('__str__', 'is_active', 'discount_percent', 'capacity', 'remaining_capacity', 'expiration_date') readonly_fields = ('participants',) diff --git a/backend/backend_api/migrations/0060_presentationparticipation_certificate_and_more.py b/backend/backend_api/migrations/0060_presentationparticipation_certificate_and_more.py new file mode 100644 index 0000000..2db9f6f --- /dev/null +++ b/backend/backend_api/migrations/0060_presentationparticipation_certificate_and_more.py @@ -0,0 +1,50 @@ +# Generated by Django 4.2.4 on 2024-01-11 13:39 + +from django.db import migrations, models +import utils.image_uploader +import uuid + + +def set_default_uuid(apps, schema_editor): + workshop_participation = apps.get_model('backend_api', 'workshopregistration') + presentation_participation = apps.get_model('backend_api', 'presentationparticipation') + for instance in presentation_participation.objects.filter(certificate_uuid__isnull=True): + instance.certificate_uuid = uuid.uuid4() + instance.save() + for instance in workshop_participation.objects.filter(certificate_uuid__isnull=True): + instance.certificate_uuid = uuid.uuid4() + instance.save() + + +class Migration(migrations.Migration): + dependencies = [ + ('backend_api', '0059_alter_discount_code_and_more'), + ] + + operations = [ + migrations.AddField( + model_name='presentationparticipation', + name='certificate', + field=models.FileField(default=None, null=True, upload_to=utils.image_uploader.update_certificate_filename), + ), + migrations.AddField( + model_name='presentationparticipation', + name='certificate_uuid', + field=models.UUIDField(default=None, editable=False, unique=False, null=True), + ), + migrations.AddField( + model_name='workshopregistration', + name='certificate', + field=models.FileField(default=None, null=True, upload_to=utils.image_uploader.update_certificate_filename), + ), + migrations.AddField( + model_name='workshopregistration', + name='certificate_uuid', + field=models.UUIDField(default=None, editable=False, unique=False, null=True), + ), + migrations.RunPython(set_default_uuid), + migrations.AlterField('presentationparticipation', 'certificate_uuid', + models.UUIDField(default=uuid.uuid4, editable=False, unique=True, null=False)), + migrations.AlterField('workshopregistration', 'certificate_uuid', + models.UUIDField(default=uuid.uuid4, editable=False, unique=True, null=False)), + ] diff --git a/backend/backend_api/models.py b/backend/backend_api/models.py index 6439c6c..a8412fd 100644 --- a/backend/backend_api/models.py +++ b/backend/backend_api/models.py @@ -20,6 +20,7 @@ from aaiss_backend.settings import BASE_URL from backend_api import validators from backend_api.email import MailerThread +from utils.image_uploader import update_certificate_filename from utils.random import create_random_string, random_password, random_discount_code from utils.renderers import new_detailed_response @@ -292,6 +293,8 @@ class StatusChoices(models.IntegerChoices): user = models.ForeignKey(User, on_delete=models.CASCADE) status = models.IntegerField(choices=StatusChoices.choices, default=StatusChoices.AWAITING_PAYMENT) password = models.CharField(max_length=SMALL_MAX_LENGTH, default=random_password) + certificate_uuid = models.UUIDField(default=uuid.uuid4, editable=False, unique=True) + certificate = models.FileField(upload_to=update_certificate_filename, null=True, default=None) class Meta: unique_together = ('workshop', 'user',) @@ -310,6 +313,8 @@ class StatusChoices(models.IntegerChoices): user = models.ForeignKey(User, on_delete=models.CASCADE) status = models.IntegerField(choices=StatusChoices.choices, default=StatusChoices.AWAITING_PAYMENT) password = models.CharField(max_length=SMALL_MAX_LENGTH, default=random_password) + certificate_uuid = models.UUIDField(default=uuid.uuid4, editable=False, unique=True) + certificate = models.FileField(upload_to=update_certificate_filename, null=True, default=None) class Meta: unique_together = ('presentation', 'user',) diff --git a/backend/utils/image_uploader.py b/backend/utils/image_uploader.py new file mode 100644 index 0000000..1bdf9b5 --- /dev/null +++ b/backend/utils/image_uploader.py @@ -0,0 +1,12 @@ +import os + + +def update_certificate_filename(instance, filename): + path = "certificates/" + if hasattr(instance, "presentation"): + path += "presentation/" + else: + path += "workshop/" + format = instance.certificate_uuid + _, file_extension = os.path.splitext(filename) + return os.path.join(path, str(format) + file_extension)