From 036c68bc2a70409aad9280127095a67239fecf9e Mon Sep 17 00:00:00 2001 From: ticvac Date: Tue, 12 Nov 2024 21:07:13 +0100 Subject: [PATCH] another lambad function --- soustredeni/models.py | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/soustredeni/models.py b/soustredeni/models.py index d18d8da6..9f7269cb 100644 --- a/soustredeni/models.py +++ b/soustredeni/models.py @@ -15,14 +15,10 @@ import secrets import string from django.utils import timezone -def generate_filename_kontaktnicek(self, filename): +def generate_filename_kontaktnicek(self, filename, file_type): # generate random string length = 32 - if len(filename.split('.')) == 1: - file_type = 'lol_neumíš_tam_dát_příponu' - else: - file_type = filename.split('.')[-1] - fname = timezone.now().strftime('%Y-%m-%d-%H_%M') + "" + fname = timezone.now().strftime('%Y-%m-%d-%H_%M') + "-" fname += ''.join(secrets.choice(string.ascii_uppercase + string.digits) for _ in range(length)) fname += '.' + file_type return os.path.join(settings.SOUSTREDENI_KONTAKTNICKY_DIR, fname) @@ -80,10 +76,9 @@ class Soustredeni(SeminarModelBase): exportovat = models.BooleanField('export do AESOPa', db_column='exportovat', default=False, help_text='Exportuje se jen podle tohoto flagu (ne veřejnosti)') - - kontaktnicek_vcf = models.FileField('kontaktníček vcf', upload_to=generate_filename_kontaktnicek, blank=True, null=True) - kontaktnicek_pdf = models.FileField('kontaktníček pdf', upload_to=generate_filename_kontaktnicek, blank=True, null=True) - + #using lambda to avoid circular import + kontaktnicek_vcf = models.FileField('kontaktníček vcf', upload_to=lambda instance, filename: generate_filename_kontaktnicek(instance, filename, 'vcf'), blank=True, null=True) + kontaktnicek_pdf = models.FileField('kontaktníček pdf', upload_to=lambda instance, filename: generate_filename_kontaktnicek(instance, filename, 'pdf'), blank=True, null=True) def __str__(self): return '{} ({})'.format(self.misto, self.datum_zacatku)