from django import forms from django.conf import settings from django.contrib import admin from django.contrib.sites.models import Site from django.contrib import messages from django.utils.translation import ungettext, ugettext_lazy as _ from .models import Gallery, Photo, GalleryUpload, PhotoEffect, PhotoSize, \ Watermark MULTISITE = getattr(settings, 'PHOTOLOGUE_MULTISITE', False) ENABLE_TAGS = getattr(settings, 'PHOTOLOGUE_ENABLE_TAGS', False) """ class GalleryAdminForm(forms.ModelForm): class Meta: model = Gallery if MULTISITE: exclude = [] else: exclude = ['sites'] if not ENABLE_TAGS: exclude.append('tags') class GalleryAdmin(admin.ModelAdmin): list_display = ('title', 'date_added', 'photo_count', 'is_public') list_filter = ['date_added', 'is_public'] if MULTISITE: list_filter.append('sites') date_hierarchy = 'date_added' prepopulated_fields = {'slug': ('title',)} form = GalleryAdminForm if MULTISITE: filter_horizontal = ['sites'] if MULTISITE: actions = [ 'add_to_current_site', 'add_photos_to_current_site', 'remove_from_current_site', 'remove_photos_from_current_site' ] def formfield_for_manytomany(self, db_field, request, **kwargs): ''' Set the current site as initial value. ''' if db_field.name == "sites": kwargs["initial"] = [Site.objects.get_current()] return super(GalleryAdmin, self).formfield_for_manytomany(db_field, request, **kwargs) def save_related(self, request, form, *args, **kwargs): ''' If the user has saved a gallery with a photo that belongs only to different Sites - it might cause much confusion. So let them know. ''' super(GalleryAdmin, self).save_related(request, form, *args, **kwargs) orphaned_photos = form.instance.orphaned_photos() if orphaned_photos: msg = ungettext( 'The following photo does not belong to the same site(s)' ' as the gallery, so will never be displayed: %(photo_list)s.', 'The following photos do not belong to the same site(s)' ' as the gallery, so will never be displayed: %(photo_list)s.', len(orphaned_photos) ) % {'photo_list': ", ".join([photo.title for photo in orphaned_photos])} messages.warning(request, msg) def add_to_current_site(modeladmin, request, queryset): current_site = Site.objects.get_current() current_site.gallery_set.add(*queryset) msg = ungettext( "The gallery has been successfully added to %(site)s", "The galleries have been successfully added to %(site)s", len(queryset) ) % {'site': current_site.name} messages.success(request, msg) add_to_current_site.short_description = \ _("Add selected galleries from the current site") def remove_from_current_site(modeladmin, request, queryset): current_site = Site.objects.get_current() current_site.gallery_set.remove(*queryset) msg = ungettext( "The gallery has been successfully removed from %(site)s", "The selected galleries have been successfully removed from %(site)s", len(queryset) ) % {'site': current_site.name} messages.success(request, msg) remove_from_current_site.short_description = \ _("Remove selected galleries from the current site") def add_photos_to_current_site(modeladmin, request, queryset): photos = Photo.objects.filter(galleries__in=queryset) current_site = Site.objects.get_current() current_site.photo_set.add(*photos) msg = ungettext( 'All photos in gallery %(galleries)s have been successfully added to %(site)s', 'All photos in galleries %(galleries)s have been successfully added to %(site)s', len(queryset) ) % { 'site': current_site.name, 'galleries': ", ".join(["'{0}'".format(gallery.title) for gallery in queryset]) } messages.success(request, msg) add_photos_to_current_site.short_description = \ _("Add all photos of selected galleries to the current site") def remove_photos_from_current_site(modeladmin, request, queryset): photos = Photo.objects.filter(galleries__in=queryset) current_site = Site.objects.get_current() current_site.photo_set.remove(*photos) msg = ungettext( 'All photos in gallery %(galleries)s have been successfully removed from %(site)s', 'All photos in galleries %(galleries)s have been successfully removed from %(site)s', len(queryset) ) % { 'site': current_site.name, 'galleries': ", ".join(["'{0}'".format(gallery.title) for gallery in queryset]) } messages.success(request, msg) remove_photos_from_current_site.short_description = \ _("Remove all photos in selected galleries from the current site") admin.site.register(Gallery, GalleryAdmin) class GalleryUploadAdmin(admin.ModelAdmin): def has_change_permission(self, request, obj=None): return False # To remove the 'Save and continue editing' button def save_model(self, request, obj, form, change): # Warning the user when things go wrong in a zip upload. obj.request = request obj.save() admin.site.register(GalleryUpload, GalleryUploadAdmin) class PhotoAdminForm(forms.ModelForm): class Meta: model = Photo if MULTISITE: exclude = [] else: exclude = ['sites'] if not ENABLE_TAGS: exclude.append('tags') class PhotoAdmin(admin.ModelAdmin): list_display = ('title', 'date_taken', 'date_added', 'is_public', 'tags', 'view_count', 'admin_thumbnail') list_filter = ['date_added', 'is_public'] if MULTISITE: list_filter.append('sites') search_fields = ['title', 'slug', 'caption'] list_per_page = 10 prepopulated_fields = {'slug': ('title',)} form = PhotoAdminForm if MULTISITE: filter_horizontal = ['sites'] if MULTISITE: actions = ['add_photos_to_current_site', 'remove_photos_from_current_site'] def formfield_for_manytomany(self, db_field, request, **kwargs): ''' Set the current site as initial value. ''' if db_field.name == "sites": kwargs["initial"] = [Site.objects.get_current()] return super(PhotoAdmin, self).formfield_for_manytomany(db_field, request, **kwargs) def add_photos_to_current_site(modeladmin, request, queryset): current_site = Site.objects.get_current() current_site.photo_set.add(*queryset) msg = ungettext( 'The photo has been successfully added to %(site)s', 'The selected photos have been successfully added to %(site)s', len(queryset) ) % {'site': current_site.name} messages.success(request, msg) add_photos_to_current_site.short_description = \ _("Add selected photos to the current site") def remove_photos_from_current_site(modeladmin, request, queryset): current_site = Site.objects.get_current() current_site.photo_set.remove(*queryset) msg = ungettext( 'The photo has been successfully removed from %(site)s', 'The selected photos have been successfully removed from %(site)s', len(queryset) ) % {'site': current_site.name} messages.success(request, msg) remove_photos_from_current_site.short_description = \ _("Remove selected photos from the current site") admin.site.register(Photo, PhotoAdmin) class PhotoEffectAdmin(admin.ModelAdmin): list_display = ('name', 'description', 'color', 'brightness', 'contrast', 'sharpness', 'filters', 'admin_sample') fieldsets = ( (None, { 'fields': ('name', 'description') }), ('Adjustments', { 'fields': ('color', 'brightness', 'contrast', 'sharpness') }), ('Filters', { 'fields': ('filters',) }), ('Reflection', { 'fields': ('reflection_size', 'reflection_strength', 'background_color') }), ('Transpose', { 'fields': ('transpose_method',) }), ) admin.site.register(PhotoEffect, PhotoEffectAdmin) class PhotoSizeAdmin(admin.ModelAdmin): list_display = ('name', 'width', 'height', 'crop', 'pre_cache', 'effect', 'increment_count') fieldsets = ( (None, { 'fields': ('name', 'width', 'height', 'quality') }), ('Options', { 'fields': ('upscale', 'crop', 'pre_cache', 'increment_count') }), ('Enhancements', { 'fields': ('effect', 'watermark',) }), ) admin.site.register(PhotoSize, PhotoSizeAdmin) class WatermarkAdmin(admin.ModelAdmin): list_display = ('name', 'opacity', 'style') admin.site.register(Watermark, WatermarkAdmin) """ #------------------EXPOMAP VIEWS---------------------------------------------- from django.views.generic import ListView, FormView from forms import PhotoForm, GalleryForm from django.shortcuts import render_to_response, get_object_or_404 from django.http import HttpResponseRedirect class AdminViewObject(FormView): """ need overwrite get_form method for every class """ form_class = None model = None template_name = None success_url = None obj = None def set_obj(self): """ this method must be called in get_form method to determine if we changing or creating """ slug = self.kwargs.get('slug') if slug: obj = get_object_or_404(self.model, slug=slug) self.obj =obj else: self.obj = None def form_valid(self, form): self.set_obj() form.save(obj=self.obj) return HttpResponseRedirect(self.success_url) def get_context_data(self, **kwargs): context = super(AdminViewObject, self).get_context_data(**kwargs) context['object'] = self.obj context['languages'] = settings.LANGUAGES return context class PhotoView(AdminViewObject): model = Photo form_class = PhotoForm template_name = 'photogallery/admin_photo.html' success_url = '/admin/photogallery/photo/all/' def get_form(self, form_class): if self.request.POST: return super(PhotoView, self).get_form(form_class) self.set_obj() if self.obj: photo = self.obj data = {} data['image'] = photo.image.url for code, name in settings.LANGUAGES: obj = Photo._meta.translations_model.objects.get(language_code = code,master__id=getattr(photo, 'id')) #access to translated fields data['title_%s' % code] = obj.title data['caption_%s' % code] = obj.caption #form.fields['tag'].widget.attrs['data-init-text'] = [item.name for item in article.tag.all()] return form_class(data) else: return form_class() class GalleryView(AdminViewObject): model = Gallery form_class = GalleryForm template_name = 'photogallery/admin_gallery.html' success_url = '/admin/photogallery/gallery/all/' def get_form(self, form_class): if self.request.POST: return super(GalleryView, self).get_form(form_class) self.set_obj() if self.obj: gallery = self.obj data = {} for code, name in settings.LANGUAGES: obj = Gallery._meta.translations_model.objects.get(language_code = code,master__id=getattr(gallery, 'id')) #access to translated fields data['title_%s' % code] = obj.title data['description_%s' % code] = obj.description #form.fields['tag'].widget.attrs['data-init-text'] = [item.name for item in article.tag.all()] return form_class(data) else: return form_class() class PhotoListView(ListView): paginate_by = settings.ADMIN_PAGINATION model = Photo template_name = 'photogallery/admin_photo_list.html' class GalleryListView(ListView): paginate_by = settings.ADMIN_PAGINATION model = Gallery template_name = 'photogallery/admin_gallery_list.html'