generate_filename: ošetřuj mezery a diakritiku
This commit is contained in:
parent
a8a4364f60
commit
481999a7f9
2 changed files with 31 additions and 25 deletions
|
@ -6,6 +6,7 @@ from django.conf import settings
|
||||||
from django.utils.encoding import python_2_unicode_compatible
|
from django.utils.encoding import python_2_unicode_compatible
|
||||||
from django.utils.encoding import force_unicode
|
from django.utils.encoding import force_unicode
|
||||||
from django.core.exceptions import ObjectDoesNotExist
|
from django.core.exceptions import ObjectDoesNotExist
|
||||||
|
from django.utils.text import get_valid_filename
|
||||||
|
|
||||||
import subprocess
|
import subprocess
|
||||||
from reversion import revisions as reversion
|
from reversion import revisions as reversion
|
||||||
|
@ -13,10 +14,11 @@ from reversion import revisions as reversion
|
||||||
from unidecode import unidecode
|
from unidecode import unidecode
|
||||||
|
|
||||||
|
|
||||||
# PrilohaReseni method
|
|
||||||
def generate_filename(self, filename):
|
def generate_filename(self, filename):
|
||||||
clean = unidecode(
|
clean = get_valid_filename(
|
||||||
filename.replace('/', '-').replace('\0', '').replace(":", "_")
|
unidecode(
|
||||||
|
filename.replace('/', '-').replace('\0', '').replace(":", "_")
|
||||||
|
)
|
||||||
)
|
)
|
||||||
fname = "%s_%s" % (
|
fname = "%s_%s" % (
|
||||||
timezone.now().strftime('%Y-%m-%d-%H_%M'),
|
timezone.now().strftime('%Y-%m-%d-%H_%M'),
|
||||||
|
|
|
@ -12,15 +12,10 @@ from django.utils.text import slugify
|
||||||
from django.core.urlresolvers import reverse
|
from django.core.urlresolvers import reverse
|
||||||
from django.core.cache import cache
|
from django.core.cache import cache
|
||||||
from django.core.exceptions import ObjectDoesNotExist
|
from django.core.exceptions import ObjectDoesNotExist
|
||||||
|
from django.utils.text import get_valid_filename
|
||||||
from imagekit.models import ImageSpecField, ProcessedImageField
|
from imagekit.models import ImageSpecField, ProcessedImageField
|
||||||
from imagekit.processors import ResizeToFit, Transpose
|
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 django_countries.fields import CountryField
|
||||||
from solo.models import SingletonModel
|
from solo.models import SingletonModel
|
||||||
from taggit.managers import TaggableManager
|
from taggit.managers import TaggableManager
|
||||||
|
@ -29,6 +24,8 @@ from reversion import revisions as reversion
|
||||||
|
|
||||||
from seminar.utils import roman
|
from seminar.utils import roman
|
||||||
|
|
||||||
|
from unidecode import unidecode
|
||||||
|
|
||||||
|
|
||||||
class SeminarModelBase(models.Model):
|
class SeminarModelBase(models.Model):
|
||||||
|
|
||||||
|
@ -557,28 +554,35 @@ class Reseni(SeminarModelBase):
|
||||||
super(Reseni, self).save(*args, **kwargs)
|
super(Reseni, self).save(*args, **kwargs)
|
||||||
|
|
||||||
|
|
||||||
# PrilohaReseni method
|
def aux_generate_filename(self, filename):
|
||||||
# TODO vyresit partial, tak aby slo migrovat
|
"""Pomocná funkce generující ošetřený název souboru v adresáři s datem"""
|
||||||
#def generate_filename(self, filename, directory):
|
clean = get_valid_filename(
|
||||||
# Django 1.9 podporuje partial
|
unidecode(filename.replace('/', '-').replace('\0', ''))
|
||||||
|
)
|
||||||
def generate_filename(self, filename):
|
|
||||||
clean = filename.replace('/','-').replace('\0', '')
|
|
||||||
datedir = timezone.now().strftime('%Y-%m')
|
datedir = timezone.now().strftime('%Y-%m')
|
||||||
fname = "%s_%s" % (
|
fname = "%s_%s" % (
|
||||||
timezone.now().strftime('%Y-%m-%d-%H:%M'),
|
timezone.now().strftime('%Y-%m-%d-%H:%M'),
|
||||||
clean)
|
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):
|
def generate_filename_konfera(self, filename):
|
||||||
clean = filename.replace('/','-').replace('\0', '')
|
return os.path.join(
|
||||||
datedir = timezone.now().strftime('%Y-%m')
|
settings.SEMINAR_KONFERY_DIR,
|
||||||
fname = "%s_%s" % (
|
aux_generate_filename(self, filename)
|
||||||
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
|
def generate_filename(self, filename):
|
||||||
# return os.path.join(directory, datedir, fname)
|
return os.path.join(
|
||||||
|
settings.SEMINAR_RESENI_DIR,
|
||||||
|
aux_generate_filename(self, filename)
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
@reversion.register(ignore_duplicate_revisions=True)
|
@reversion.register(ignore_duplicate_revisions=True)
|
||||||
|
|
Loading…
Reference in a new issue