Commit 5fc4c8d2 authored by Tom van Bussel's avatar Tom van Bussel
Browse files

Generate unique filenames for photos

Foto's krijgen nu een unieke filename (binnen het album). Deze filename
heeft de vorm "0013.jpg".
parent bbb15e13
......@@ -56,11 +56,16 @@ def save_photo(request, archive_file, photo, album):
if not os.path.basename(photo_filename):
return
# Generate unique filename
num = album.photo_set.count()
_, extension = os.path.splitext(photo_filename)
new_filename = str(num).zfill(4) + extension
photo_obj = Photo()
photo_obj.album = album
try:
with extract_file(photo) as f:
photo_obj.file.save(photo_filename, ContentFile(f.read()))
photo_obj.file.save(new_filename, ContentFile(f.read()))
except (OSError, AttributeError):
messages.add_message(request, messages.WARNING,
_("Ignoring {}").format(photo_filename))
......@@ -115,6 +120,7 @@ class PhotoAdmin(admin.ModelAdmin):
list_display = ('__str__', 'album', 'hidden')
search_fields = ('file',)
list_filter = ('album', 'hidden')
exclude = ('_digest',)
def save_model(self, request, obj, form, change):
obj.save()
......
......@@ -13,7 +13,10 @@ COVER_FILENAME = 'cover.jpg'
def photo_uploadto(instance, filename):
return os.path.join(Album.photosdir, instance.album.dirname, filename)
num = instance.album.photo_set.count()
_, extension = os.path.splitext(filename)
new_filename = str(num).zfill(4) + extension
return os.path.join(Album.photosdir, instance.album.dirname, new_filename)
class Photo(models.Model):
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment