-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathadmin.py
executable file
·55 lines (45 loc) · 1.83 KB
/
admin.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import uuid
import zipfile
import pyghana.settings
from datetime import datetime
from zipfile import ZipFile
from django.contrib import admin
from django.core.files.base import ContentFile
from PIL import Image
from app.models import Album, AlbumImage
from app.forms import AlbumForm
@admin.register(Album)
class AlbumModelAdmin(admin.ModelAdmin):
form = AlbumForm
prepopulated_fields = {'slug': ('title',)}
list_display = ('title', 'thumb')
list_filter = ('created',)
def save_model(self, request, obj, form, change):
if form.is_valid():
album = form.save(commit=False)
album.modified = datetime.now()
album.save()
if form.cleaned_data['zip'] != None:
zip = zipfile.ZipFile(form.cleaned_data['zip'])
for filename in sorted(zip.namelist()):
data = zip.read(filename)
contentfile = ContentFile(data)
img = AlbumImage()
img.album = album
img.alt = filename
filename = '{0}{1}.jpg'.format(album.slug, str(uuid.uuid4())[-13:])
img.image.save(filename, contentfile)
filepath = '{0}/albums/{1}'.format(pyghana.settings.MEDIA_ROOT, filename)
with Image.open(filepath) as i:
img.width, img.height = i.size
img.thumb.save('thumb-{0}'.format(filename), contentfile)
img.save()
zip.close()
super(AlbumModelAdmin, self).save_model(request, obj, form, change)
# In case image should be removed from album.
@admin.register(AlbumImage)
class AlbumImageModelAdmin(admin.ModelAdmin):
list_display = ('alt', 'album')
list_filter = ('album', 'created')