Skip to content

Commit

Permalink
feat: add certificate field
Browse files Browse the repository at this point in the history
  • Loading branch information
Adibov committed Jan 11, 2024
1 parent d21b327 commit 36ed7dc
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 4 deletions.
7 changes: 3 additions & 4 deletions backend/backend_api/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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',)
Expand Down
Original file line number Diff line number Diff line change
@@ -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)),
]
5 changes: 5 additions & 0 deletions backend/backend_api/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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',)
Expand All @@ -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',)
Expand Down
12 changes: 12 additions & 0 deletions backend/utils/image_uploader.py
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit 36ed7dc

Please sign in to comment.