From 716b8c747d234589ed9eeac5ee64dacd062b24dc Mon Sep 17 00:00:00 2001 From: Lukasz Anwajler Date: Mon, 22 Nov 2010 14:44:39 -0600 Subject: [PATCH] Autonaming medias. --- .../migrations/0005_many2many_files_for_books.py | 2 +- apps/catalogue/models.py | 8 ++++++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/apps/catalogue/migrations/0005_many2many_files_for_books.py b/apps/catalogue/migrations/0005_many2many_files_for_books.py index 664045bc0..44411f018 100644 --- a/apps/catalogue/migrations/0005_many2many_files_for_books.py +++ b/apps/catalogue/migrations/0005_many2many_files_for_books.py @@ -32,7 +32,7 @@ class Migration(SchemaMigration): db.create_table('catalogue_bookmedia', ( ('id', self.gf('django.db.models.fields.AutoField')(primary_key=True)), ('type', self.gf('django.db.models.fields.CharField')(max_length='100')), - ('name', self.gf('django.db.models.fields.CharField')(max_length='100')), + ('name', self.gf('django.db.models.fields.CharField')(max_length='100', blank=True)), ('file', self.gf('django.db.models.fields.files.FileField')(max_length=100, blank=True)), ('uploaded_at', self.gf('django.db.models.fields.DateTimeField')(auto_now_add=True, blank=True)), )) diff --git a/apps/catalogue/models.py b/apps/catalogue/models.py index 98e37c1af..fa6c13225 100644 --- a/apps/catalogue/models.py +++ b/apps/catalogue/models.py @@ -173,13 +173,17 @@ def book_upload_path(ext=None): # how to put related book's slug here? if not ext: ext = media.type - return 'lektura/%s.%s' % (slugify(media.name), ext) + if not media.name: + name = slugify(filename.split(".")[0]) + else: + name = slugify(media.name) + return 'lektura/%s.%s' % (name, ext) return get_dynamic_path class BookMedia(models.Model): type = models.CharField(_('type'), choices=MEDIA_FORMATS, max_length="100") - name = models.CharField(_('name'), max_length="100") + name = models.CharField(_('name'), max_length="100", blank=True) file = models.FileField(_('file'), upload_to=book_upload_path(), blank=True) uploaded_at = models.DateTimeField(_('creation date'), auto_now_add=True, editable=False) -- 2.20.1