You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
131 lines
4.1 KiB
131 lines
4.1 KiB
import json
|
|
from django import forms
|
|
from django.conf import settings
|
|
from django.shortcuts import HttpResponse
|
|
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)
|
|
|
|
|
|
#------------------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 = 'c_admin/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
|
|
data['sort'] = photo.sort
|
|
|
|
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 = 'c_admin/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 = 'c_admin/photogallery/admin_photo_list.html'
|
|
|
|
|
|
class GalleryListView(ListView):
|
|
paginate_by = settings.ADMIN_PAGINATION
|
|
model = Gallery
|
|
template_name = 'c_admin/photogallery/admin_gallery_list.html'
|
|
|
|
|
|
def delete_photo(request, photo_id):
|
|
photo = get_object_or_404(Photo, pk=photo_id)
|
|
photo.delete()
|
|
response = {'success': True}
|
|
return HttpResponse(json.dumps(response), content_type='application/json')
|
|
|