/
newman_admin.py
121 lines (94 loc) · 3.92 KB
/
newman_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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
from django.utils.translation import ugettext_lazy as _
from django.conf.urls.defaults import patterns, url
from ella import newman
from ella.photos.models import FormatedPhoto, Format, Photo
from ella.newman.utils import JsonResponse, JsonResponseError
from ella.newman.config import STATUS_OBJECT_NOT_FOUND
from ella.newman.filterspecs import CustomFilterSpec
from ella.newman.licenses.models import License
class PhotoSizeFilter(CustomFilterSpec):
lookup_w = 'width'
lookup_h = 'height'
def title(self):
return _('Size')
def get_lookup_kwarg(self):
return [
'%s__gt' % self.lookup_w,
'%s__gt' % self.lookup_h
]
def filter_func(self):
for size in (100, 150, 200, 300, 500, 600, 700, 800):
lookup_dict = {
'%s__gt' % self.lookup_w : size,
'%s__gt' % self.lookup_h : size
}
link_txt = "> %s" % size
link = (link_txt, lookup_dict)
self.links.append(link)
return True
def generate_choice(self, **lookup_kwargs):
keys = self.get_lookup_kwarg()
for key in keys:
if key in lookup_kwargs:
return u'>%spx' % lookup_kwargs[key]
class FormatAdmin(newman.NewmanModelAdmin):
list_display = ('name', 'max_width', 'max_height', 'stretch', 'resample_quality',)
list_filter = ('sites', 'stretch', 'nocrop', 'flexible_height',)
search_fields = ('name',)
class FormatedPhotoInlineAdmin(newman.NewmanTabularInline):
model = FormatedPhoto
def photo_get_list_display():
if License._meta.installed:
return ('title', 'size', 'thumb', 'license_info', 'pk',)
return ('title', 'size', 'thumb', 'pk',)
class PhotoAdmin(newman.NewmanModelAdmin):
list_display = photo_get_list_display()
list_filter = ('created',)
unbound_list_filter = (PhotoSizeFilter,)
prepopulated_fields = {'slug': ('title',)}
search_fields = ('title', 'slug', 'id',)
suggest_fields = {'authors': ('name', 'slug',), 'source': ('name', 'url',)}
rich_text_fields = {'small': ('description',)}
fieldsets = (
(_("Heading"), {'fields': ('title', 'slug',)}),
(_("Description"), {'fields': ('description',)}),
(_("Metadata"), {'fields': ('authors', 'source', 'image',)}),
(_("Important area"), {'fields': (('important_top', 'important_right'), ('important_bottom', 'important_left'),), 'classes': ('collapsed',)})
)
def size(self, obj):
return "%dx%d px" % (obj.width, obj.height)
size.short_description = _('Size')
def license_info(self, obj):
if not License._meta.installed:
return "---"
try:
license = License.objects.get(ct=self.model_content_type, obj_id=obj.id)
except License.DoesNotExist:
return '---'
return u"%sx (%s)" % (license.max_applications, license.note)
license_info.short_description = _('Max applications')
def get_urls(self):
urlpatterns = patterns('',
url(r'^(\d+)/thumb/$',
self.json_photo_info,
name='photo-json-info'),
)
urlpatterns += super(PhotoAdmin, self).get_urls()
return urlpatterns
def json_photo_info(self, request, object_id, extra_context=None):
obj = self.get_change_view_object(object_id)
if obj is None:
return JsonResponseError(_('Photo id %s does not exists.') % object_id, status=STATUS_OBJECT_NOT_FOUND)
out = {
'title': obj.title,
'thumb_url': obj.thumb_url()
}
return JsonResponse('', out)
class FormatedPhotoAdmin(newman.NewmanModelAdmin):
list_display = ('image', 'format', 'width', 'height')
list_filter = ('format',)
search_fields = ('image',)
raw_id_fields = ('photo',)
newman.site.register(Format, FormatAdmin)
newman.site.register(Photo, PhotoAdmin)
newman.site.register(FormatedPhoto, FormatedPhotoAdmin)