Browse Source

another lambad function

pull/71/head
ticvac 2 days ago
parent
commit
036c68bc2a
  1. 15
      soustredeni/models.py

15
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)

Loading…
Cancel
Save