|
| 1 | +from django import forms |
| 2 | +from django.conf import settings |
| 3 | +from django.conf.urls import url |
| 4 | +from django.contrib import admin |
| 5 | +from django.contrib.sites.models import Site |
| 6 | +from django.contrib import messages |
| 7 | +from django.utils.translation import ungettext, ugettext_lazy as _ |
| 8 | +from django.shortcuts import render |
| 9 | +from django.contrib.admin import helpers |
| 10 | +from django.http import HttpResponseRedirect |
| 11 | + |
| 12 | +from .models import Gallery, Photo, PhotoEffect, PhotoSize, \ |
| 13 | + Watermark |
| 14 | +from .forms import UploadZipForm |
| 15 | + |
| 16 | +MULTISITE = getattr(settings, 'PHOTOLOGUE_MULTISITE', False) |
| 17 | + |
| 18 | + |
| 19 | +class GalleryAdminForm(forms.ModelForm): |
| 20 | + |
| 21 | + class Meta: |
| 22 | + model = Gallery |
| 23 | + if MULTISITE: |
| 24 | + exclude = [] |
| 25 | + else: |
| 26 | + exclude = ['sites'] |
| 27 | + |
| 28 | + |
| 29 | +class GalleryAdmin(admin.ModelAdmin): |
| 30 | + list_display = ('title', 'date_added', 'photo_count', 'is_public') |
| 31 | + list_filter = ['date_added', 'is_public'] |
| 32 | + if MULTISITE: |
| 33 | + list_filter.append('sites') |
| 34 | + date_hierarchy = 'date_added' |
| 35 | + prepopulated_fields = {'slug': ('title',)} |
| 36 | + form = GalleryAdminForm |
| 37 | + if MULTISITE: |
| 38 | + filter_horizontal = ['sites'] |
| 39 | + if MULTISITE: |
| 40 | + actions = [ |
| 41 | + 'add_to_current_site', |
| 42 | + 'add_photos_to_current_site', |
| 43 | + 'remove_from_current_site', |
| 44 | + 'remove_photos_from_current_site' |
| 45 | + ] |
| 46 | + |
| 47 | + def formfield_for_manytomany(self, db_field, request, **kwargs): |
| 48 | + """ Set the current site as initial value. """ |
| 49 | + if db_field.name == "sites": |
| 50 | + kwargs["initial"] = [Site.objects.get_current()] |
| 51 | + return super(GalleryAdmin, self).formfield_for_manytomany(db_field, request, **kwargs) |
| 52 | + |
| 53 | + def save_related(self, request, form, *args, **kwargs): |
| 54 | + """ |
| 55 | + If the user has saved a gallery with a photo that belongs only to |
| 56 | + different Sites - it might cause much confusion. So let them know. |
| 57 | + """ |
| 58 | + super(GalleryAdmin, self).save_related(request, form, *args, **kwargs) |
| 59 | + orphaned_photos = form.instance.orphaned_photos() |
| 60 | + if orphaned_photos: |
| 61 | + msg = ungettext( |
| 62 | + 'The following photo does not belong to the same site(s)' |
| 63 | + ' as the gallery, so will never be displayed: %(photo_list)s.', |
| 64 | + 'The following photos do not belong to the same site(s)' |
| 65 | + ' as the gallery, so will never be displayed: %(photo_list)s.', |
| 66 | + len(orphaned_photos) |
| 67 | + ) % {'photo_list': ", ".join([photo.title for photo in orphaned_photos])} |
| 68 | + messages.warning(request, msg) |
| 69 | + |
| 70 | + def add_to_current_site(modeladmin, request, queryset): |
| 71 | + current_site = Site.objects.get_current() |
| 72 | + current_site.gallery_set.add(*queryset) |
| 73 | + msg = ungettext( |
| 74 | + "The gallery has been successfully added to %(site)s", |
| 75 | + "The galleries have been successfully added to %(site)s", |
| 76 | + len(queryset) |
| 77 | + ) % {'site': current_site.name} |
| 78 | + messages.success(request, msg) |
| 79 | + |
| 80 | + add_to_current_site.short_description = \ |
| 81 | + _("Add selected galleries to the current site") |
| 82 | + |
| 83 | + def remove_from_current_site(modeladmin, request, queryset): |
| 84 | + current_site = Site.objects.get_current() |
| 85 | + current_site.gallery_set.remove(*queryset) |
| 86 | + msg = ungettext( |
| 87 | + "The gallery has been successfully removed from %(site)s", |
| 88 | + "The selected galleries have been successfully removed from %(site)s", |
| 89 | + len(queryset) |
| 90 | + ) % {'site': current_site.name} |
| 91 | + messages.success(request, msg) |
| 92 | + |
| 93 | + remove_from_current_site.short_description = \ |
| 94 | + _("Remove selected galleries from the current site") |
| 95 | + |
| 96 | + def add_photos_to_current_site(modeladmin, request, queryset): |
| 97 | + photos = Photo.objects.filter(galleries__in=queryset) |
| 98 | + current_site = Site.objects.get_current() |
| 99 | + current_site.photo_set.add(*photos) |
| 100 | + msg = ungettext( |
| 101 | + 'All photos in gallery %(galleries)s have been successfully added to %(site)s', |
| 102 | + 'All photos in galleries %(galleries)s have been successfully added to %(site)s', |
| 103 | + len(queryset) |
| 104 | + ) % { |
| 105 | + 'site': current_site.name, |
| 106 | + 'galleries': ", ".join(["'{0}'".format(gallery.title) |
| 107 | + for gallery in queryset]) |
| 108 | + } |
| 109 | + messages.success(request, msg) |
| 110 | + |
| 111 | + add_photos_to_current_site.short_description = \ |
| 112 | + _("Add all photos of selected galleries to the current site") |
| 113 | + |
| 114 | + def remove_photos_from_current_site(modeladmin, request, queryset): |
| 115 | + photos = Photo.objects.filter(galleries__in=queryset) |
| 116 | + current_site = Site.objects.get_current() |
| 117 | + current_site.photo_set.remove(*photos) |
| 118 | + msg = ungettext( |
| 119 | + 'All photos in gallery %(galleries)s have been successfully removed from %(site)s', |
| 120 | + 'All photos in galleries %(galleries)s have been successfully removed from %(site)s', |
| 121 | + len(queryset) |
| 122 | + ) % { |
| 123 | + 'site': current_site.name, |
| 124 | + 'galleries': ", ".join(["'{0}'".format(gallery.title) |
| 125 | + for gallery in queryset]) |
| 126 | + } |
| 127 | + messages.success(request, msg) |
| 128 | + |
| 129 | + remove_photos_from_current_site.short_description = \ |
| 130 | + _("Remove all photos in selected galleries from the current site") |
| 131 | + |
| 132 | +admin.site.register(Gallery, GalleryAdmin) |
| 133 | + |
| 134 | + |
| 135 | +class PhotoAdminForm(forms.ModelForm): |
| 136 | + |
| 137 | + class Meta: |
| 138 | + model = Photo |
| 139 | + if MULTISITE: |
| 140 | + exclude = [] |
| 141 | + else: |
| 142 | + exclude = ['sites'] |
| 143 | + |
| 144 | + |
| 145 | +class PhotoAdmin(admin.ModelAdmin): |
| 146 | + list_display = ('title', 'date_taken', 'date_added', |
| 147 | + 'is_public', 'view_count', 'admin_thumbnail') |
| 148 | + list_filter = ['date_added', 'is_public'] |
| 149 | + if MULTISITE: |
| 150 | + list_filter.append('sites') |
| 151 | + search_fields = ['title', 'slug', 'caption'] |
| 152 | + list_per_page = 10 |
| 153 | + prepopulated_fields = {'slug': ('title',)} |
| 154 | + readonly_fields = ('date_taken',) |
| 155 | + form = PhotoAdminForm |
| 156 | + if MULTISITE: |
| 157 | + filter_horizontal = ['sites'] |
| 158 | + if MULTISITE: |
| 159 | + actions = ['add_photos_to_current_site', 'remove_photos_from_current_site'] |
| 160 | + |
| 161 | + def formfield_for_manytomany(self, db_field, request, **kwargs): |
| 162 | + """ Set the current site as initial value. """ |
| 163 | + if db_field.name == "sites": |
| 164 | + kwargs["initial"] = [Site.objects.get_current()] |
| 165 | + return super(PhotoAdmin, self).formfield_for_manytomany(db_field, request, **kwargs) |
| 166 | + |
| 167 | + def add_photos_to_current_site(modeladmin, request, queryset): |
| 168 | + current_site = Site.objects.get_current() |
| 169 | + current_site.photo_set.add(*queryset) |
| 170 | + msg = ungettext( |
| 171 | + 'The photo has been successfully added to %(site)s', |
| 172 | + 'The selected photos have been successfully added to %(site)s', |
| 173 | + len(queryset) |
| 174 | + ) % {'site': current_site.name} |
| 175 | + messages.success(request, msg) |
| 176 | + |
| 177 | + add_photos_to_current_site.short_description = \ |
| 178 | + _("Add selected photos to the current site") |
| 179 | + |
| 180 | + def remove_photos_from_current_site(modeladmin, request, queryset): |
| 181 | + current_site = Site.objects.get_current() |
| 182 | + current_site.photo_set.remove(*queryset) |
| 183 | + msg = ungettext( |
| 184 | + 'The photo has been successfully removed from %(site)s', |
| 185 | + 'The selected photos have been successfully removed from %(site)s', |
| 186 | + len(queryset) |
| 187 | + ) % {'site': current_site.name} |
| 188 | + messages.success(request, msg) |
| 189 | + |
| 190 | + remove_photos_from_current_site.short_description = \ |
| 191 | + _("Remove selected photos from the current site") |
| 192 | + |
| 193 | + def get_urls(self): |
| 194 | + urls = super(PhotoAdmin, self).get_urls() |
| 195 | + custom_urls = [ |
| 196 | + url(r'^upload_zip/$', |
| 197 | + self.admin_site.admin_view(self.upload_zip), |
| 198 | + name='photologue_upload_zip') |
| 199 | + ] |
| 200 | + return custom_urls + urls |
| 201 | + |
| 202 | + def upload_zip(self, request): |
| 203 | + |
| 204 | + context = { |
| 205 | + 'title': _('Upload a zip archive of photos'), |
| 206 | + 'app_label': self.model._meta.app_label, |
| 207 | + 'opts': self.model._meta, |
| 208 | + 'has_change_permission': self.has_change_permission(request) |
| 209 | + } |
| 210 | + |
| 211 | + # Handle form request |
| 212 | + if request.method == 'POST': |
| 213 | + form = UploadZipForm(request.POST, request.FILES) |
| 214 | + if form.is_valid(): |
| 215 | + form.save(request=request) |
| 216 | + return HttpResponseRedirect('..') |
| 217 | + else: |
| 218 | + form = UploadZipForm() |
| 219 | + context['form'] = form |
| 220 | + context['adminform'] = helpers.AdminForm(form, |
| 221 | + list([(None, {'fields': form.base_fields})]), |
| 222 | + {}) |
| 223 | + return render(request, 'admin/photologue/photo/upload_zip.html', context) |
| 224 | + |
| 225 | + |
| 226 | +admin.site.register(Photo, PhotoAdmin) |
| 227 | + |
| 228 | + |
| 229 | +class PhotoEffectAdmin(admin.ModelAdmin): |
| 230 | + list_display = ('name', 'description', 'color', 'brightness', |
| 231 | + 'contrast', 'sharpness', 'filters', 'admin_sample') |
| 232 | + fieldsets = ( |
| 233 | + (None, { |
| 234 | + 'fields': ('name', 'description') |
| 235 | + }), |
| 236 | + ('Adjustments', { |
| 237 | + 'fields': ('color', 'brightness', 'contrast', 'sharpness') |
| 238 | + }), |
| 239 | + ('Filters', { |
| 240 | + 'fields': ('filters',) |
| 241 | + }), |
| 242 | + ('Reflection', { |
| 243 | + 'fields': ('reflection_size', 'reflection_strength', 'background_color') |
| 244 | + }), |
| 245 | + ('Transpose', { |
| 246 | + 'fields': ('transpose_method',) |
| 247 | + }), |
| 248 | + ) |
| 249 | + |
| 250 | +admin.site.register(PhotoEffect, PhotoEffectAdmin) |
| 251 | + |
| 252 | + |
| 253 | +class PhotoSizeAdmin(admin.ModelAdmin): |
| 254 | + list_display = ('name', 'width', 'height', 'crop', 'pre_cache', 'effect', 'increment_count') |
| 255 | + fieldsets = ( |
| 256 | + (None, { |
| 257 | + 'fields': ('name', 'width', 'height', 'quality') |
| 258 | + }), |
| 259 | + ('Options', { |
| 260 | + 'fields': ('upscale', 'crop', 'pre_cache', 'increment_count') |
| 261 | + }), |
| 262 | + ('Enhancements', { |
| 263 | + 'fields': ('effect', 'watermark',) |
| 264 | + }), |
| 265 | + ) |
| 266 | + |
| 267 | +admin.site.register(PhotoSize, PhotoSizeAdmin) |
| 268 | + |
| 269 | + |
| 270 | +class WatermarkAdmin(admin.ModelAdmin): |
| 271 | + list_display = ('name', 'opacity', 'style') |
| 272 | + |
| 273 | + |
| 274 | +admin.site.register(Watermark, WatermarkAdmin) |
0 commit comments