Browse Source

generate_filename: ošetřuj mezery a diakritiku

remotes/origin/korekturovatko_emaily
Matěj Kocián 8 years ago
parent
commit
481999a7f9
  1. 6
      korektury/models.py
  2. 48
      seminar/models.py

6
korektury/models.py

@ -6,6 +6,7 @@ from django.conf import settings
from django.utils.encoding import python_2_unicode_compatible
from django.utils.encoding import force_unicode
from django.core.exceptions import ObjectDoesNotExist
from django.utils.text import get_valid_filename
import subprocess
from reversion import revisions as reversion
@ -13,11 +14,12 @@ from reversion import revisions as reversion
from unidecode import unidecode
# PrilohaReseni method
def generate_filename(self, filename):
clean = unidecode(
clean = get_valid_filename(
unidecode(
filename.replace('/', '-').replace('\0', '').replace(":", "_")
)
)
fname = "%s_%s" % (
timezone.now().strftime('%Y-%m-%d-%H_%M'),
clean)

48
seminar/models.py

@ -12,15 +12,10 @@ from django.utils.text import slugify
from django.core.urlresolvers import reverse
from django.core.cache import cache
from django.core.exceptions import ObjectDoesNotExist
from django.utils.text import get_valid_filename
from imagekit.models import ImageSpecField, ProcessedImageField
from imagekit.processors import ResizeToFit, Transpose
from PIL import Image
import os
#from functools import partial
from cStringIO import StringIO
from django.core.files.base import ContentFile
from django_countries.fields import CountryField
from solo.models import SingletonModel
from taggit.managers import TaggableManager
@ -29,6 +24,8 @@ from reversion import revisions as reversion
from seminar.utils import roman
from unidecode import unidecode
class SeminarModelBase(models.Model):
@ -557,28 +554,35 @@ class Reseni(SeminarModelBase):
super(Reseni, self).save(*args, **kwargs)
# PrilohaReseni method
# TODO vyresit partial, tak aby slo migrovat
#def generate_filename(self, filename, directory):
# Django 1.9 podporuje partial
def generate_filename(self, filename):
clean = filename.replace('/','-').replace('\0', '')
def aux_generate_filename(self, filename):
"""Pomocná funkce generující ošetřený název souboru v adresáři s datem"""
clean = get_valid_filename(
unidecode(filename.replace('/', '-').replace('\0', ''))
)
datedir = timezone.now().strftime('%Y-%m')
fname = "%s_%s" % (
timezone.now().strftime('%Y-%m-%d-%H:%M'),
clean)
return os.path.join(settings.SEMINAR_RESENI_DIR, datedir, fname)
return os.path.join(datedir, fname)
# Django neumí jednoduše serializovat partial nebo třídu s __call__
# (https://docs.djangoproject.com/en/1.8/topics/migrations/),
# neprojdou pak migrace. Takže rozlišení funkcí generujících názvy souboru
# podle adresáře řešíme takto.
def generate_filename_konfera(self, filename):
clean = filename.replace('/','-').replace('\0', '')
datedir = timezone.now().strftime('%Y-%m')
fname = "%s_%s" % (
timezone.now().strftime('%Y-%m-%d-%H:%M'),
clean)
return os.path.join(settings.SEMINAR_KONFERY_DIR, datedir, fname)
# TODO vyresit partial tak, aby slo migrovat
# return os.path.join(directory, datedir, fname)
return os.path.join(
settings.SEMINAR_KONFERY_DIR,
aux_generate_filename(self, filename)
)
def generate_filename(self, filename):
return os.path.join(
settings.SEMINAR_RESENI_DIR,
aux_generate_filename(self, filename)
)
@reversion.register(ignore_duplicate_revisions=True)

Loading…
Cancel
Save