<s> from __future__ import absolute_import , unicode_literals <EOL> import json <EOL> from django . contrib . contenttypes . models import ContentType <EOL> from django . core . urlresolvers import reverse <EOL> from django . http import HttpResponse , HttpResponseRedirect , JsonResponse <EOL> from django . utils . safestring import mark_safe <EOL> from django . utils . translation import ugettext_lazy as _ <EOL> from django . utils . translation import ugettext <EOL> from leonardo import messages <EOL> from leonardo . views import ( ContextMixin , CreateView , ModalFormView , <EOL> ModelFormMixin , UpdateView ) <EOL> from . . models import Page <EOL> from . forms import ( WidgetDeleteForm , WidgetSelectForm , WidgetUpdateForm , <EOL> form_repository ) <EOL> from . tables import WidgetDimensionTable <EOL> from . utils import get_widget_from_id <EOL> class WidgetViewMixin ( object ) : <EOL> def handle_dimensions ( self , obj ) : <EOL> """<STR_LIT>""" <EOL> from . tables import WidgetDimensionFormset <EOL> from . . models import WidgetDimension <EOL> formset = WidgetDimensionFormset ( <EOL> self . request . POST , prefix = '<STR_LIT>' ) <EOL> for form in formset . forms : <EOL> if form . is_valid ( ) : <EOL> if '<STR_LIT:id>' in form . cleaned_data : <EOL> form . save ( ) <EOL> else : <EOL> data = form . cleaned_data <EOL> data [ '<STR_LIT>' ] = ContentType . objects . get_for_model ( obj ) <EOL> data [ '<STR_LIT>' ] = obj . id <EOL> data . pop ( '<STR_LIT>' , None ) <EOL> wd = WidgetDimension ( ** data ) <EOL> wd . save ( ) <EOL> if formset . is_valid ( ) : <EOL> formset . save ( commit = False ) <EOL> for obj in formset . deleted_objects : <EOL> obj . delete ( ) <EOL> return True <EOL> def get_page ( self ) : <EOL> if not hasattr ( self , '<STR_LIT>' ) : <EOL> self . _page = Page . objects . get ( id = self . kwargs [ '<STR_LIT>' ] ) <EOL> return self . _page <EOL> def get_form_kwargs ( self ) : <EOL> kwargs = super ( WidgetViewMixin , self ) . get_form_kwargs ( ) <EOL> kwargs . update ( { <EOL> '<STR_LIT>' : self . request , <EOL> } ) <EOL> return kwargs <EOL> def get_classes ( self , ** kwargs ) : <EOL> return '<STR_LIT:U+0020>' . join ( getattr ( self , '<STR_LIT>' , [ '<STR_LIT>' ] ) ) <EOL> class WidgetUpdateView ( WidgetViewMixin , UpdateView ) : <EOL> template_name = '<STR_LIT>' <EOL> form_class = WidgetUpdateForm <EOL> def get_context_data ( self , ** kwargs ) : <EOL> context = super ( WidgetUpdateView , self ) . get_context_data ( ** kwargs ) <EOL> context [ '<STR_LIT>' ] = self . get_classes ( ) <EOL> return context <EOL> def get_form_class ( self ) : <EOL> if not hasattr ( self , '<STR_LIT>' ) : <EOL> self . _form_class = form_repository . get_form ( ** self . kwargs ) <EOL> return self . _form_class <EOL> def get_form ( self , form_class ) : <EOL> """<STR_LIT>""" <EOL> if not hasattr ( self , '<STR_LIT>' ) : <EOL> kwargs = self . get_form_kwargs ( ) <EOL> self . _form = form_class ( ** kwargs ) <EOL> return self . _form <EOL> def form_valid ( self , form ) : <EOL> response = super ( WidgetUpdateView , self ) . form_valid ( form ) <EOL> obj = self . object <EOL> self . handle_dimensions ( obj ) <EOL> if not self . request . is_ajax ( ) : <EOL> return response <EOL> request = self . request <EOL> request . method = '<STR_LIT:GET>' <EOL> return JsonResponse ( data = { <EOL> '<STR_LIT:id>' : obj . fe_identifier , <EOL> '<STR_LIT:content>' : self . model . objects . get ( <EOL> id = self . kwargs [ "<STR_LIT:id>" ] ) . render_content ( { '<STR_LIT>' : request } ) <EOL> } ) <EOL> class WidgetCreateView ( WidgetViewMixin , CreateView ) : <EOL> template_name = '<STR_LIT>' <EOL> def get_form_class ( self ) : <EOL> if not hasattr ( self , '<STR_LIT>' ) : <EOL> self . _form_class = form_repository . get_form ( ** self . kwargs ) <EOL> return self . _form_class <EOL> def get_context_data ( self , ** kwargs ) : <EOL> context = super ( WidgetCreateView , self ) . get_context_data ( ** kwargs ) <EOL> context [ '<STR_LIT>' ] = WidgetDimensionTable ( self . request , data = [ ] ) <EOL> context [ '<STR_LIT:url>' ] = reverse ( "<STR_LIT>" , kwargs = self . kwargs ) <EOL> context [ '<STR_LIT>' ] = self . get_classes ( ) <EOL> return context <EOL> def form_valid ( self , form ) : <EOL> try : <EOL> obj = form . save ( commit = False ) <EOL> obj . save ( created = False ) <EOL> self . handle_dimensions ( obj ) <EOL> obj . parent . save ( ) <EOL> success_url = self . get_success_url ( ) <EOL> response = HttpResponseRedirect ( success_url ) <EOL> response [ '<STR_LIT>' ] = success_url <EOL> except Exception as e : <EOL> raise e <EOL> if not self . request . is_ajax ( ) : <EOL> return response <EOL> return JsonResponse ( data = { <EOL> '<STR_LIT:id>' : obj . fe_identifier , <EOL> '<STR_LIT:content>' : obj . render_content ( { '<STR_LIT>' : self . request } ) , <EOL> '<STR_LIT>' : obj . region , <EOL> '<STR_LIT>' : obj . ordering <EOL> } ) <EOL> def get_initial ( self ) : <EOL> return self . kwargs <EOL> class WidgetPreCreateView ( CreateView , WidgetViewMixin ) : <EOL> form_class = WidgetSelectForm <EOL> template_name = '<STR_LIT>' <EOL> def get_label ( self ) : <EOL> return ugettext ( "<STR_LIT>" . format ( self . get_page ( ) ) ) <EOL> def get_context_data ( self , ** kwargs ) : <EOL> context = super ( WidgetPreCreateView , self ) . get_context_data ( ** kwargs ) <EOL> context [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> context [ '<STR_LIT>' ] = _ ( '<STR_LIT>' ) <EOL> context [ '<STR_LIT>' ] = self . get_classes ( ) <EOL> return context <EOL> def get_form ( self , form_class ) : <EOL> """<STR_LIT>""" <EOL> kwargs = self . kwargs <EOL> kwargs . update ( self . get_form_kwargs ( ) ) <EOL> kwargs . update ( { <EOL> '<STR_LIT>' : self . request , <EOL> '<STR_LIT>' : WidgetCreateView <EOL> } ) <EOL> return form_class ( ** kwargs ) <EOL> class WidgetInfoView ( UpdateView , WidgetViewMixin ) : <EOL> template_name = '<STR_LIT>' <EOL> form_class = WidgetUpdateForm <EOL> def get ( self , request , cls_name , id ) : <EOL> widget = self . object <EOL> widget_info = """<STR_LIT>""" . format ( ** { <EOL> '<STR_LIT:name>' : widget . __class__ . __name__ , <EOL> '<STR_LIT:id>' : widget . id , <EOL> '<STR_LIT>' : widget . region , <EOL> '<STR_LIT>' : widget . parent , <EOL> '<STR_LIT>' : widget . parent . pk , <EOL> '<STR_LIT>' : widget . ordering , <EOL> } ) <EOL> messages . info ( request , mark_safe ( widget_info ) ) <EOL> return HttpResponse ( mark_safe ( widget_info ) ) <EOL> class SuccessUrlMixin ( object ) : <EOL> def get_success_url ( self ) : <EOL> if self . request . META . get ( "<STR_LIT>" ) != self . request . build_absolute_uri ( ) : <EOL> return self . request . META . get ( '<STR_LIT>' ) <EOL> try : <EOL> success_url = self . object . parent . get_absolute_url ( ) <EOL> except : <EOL> pass <EOL> else : <EOL> return success_url <EOL> return super ( WidgetActionMixin , self ) . get_success_url ( ) <EOL> class WidgetDeleteView ( SuccessUrlMixin , ModalFormView , <EOL> ContextMixin , ModelFormMixin , WidgetViewMixin ) : <EOL> form_class = WidgetDeleteForm <EOL> template_name = '<STR_LIT>' <EOL> def get_label ( self ) : <EOL> return ugettext ( "<STR_LIT>" . format ( self . object . _meta . verbose_name ) ) <EOL> def get_context_data ( self , ** kwargs ) : <EOL> context = super ( WidgetDeleteView , self ) . get_context_data ( ** kwargs ) <EOL> context [ '<STR_LIT:url>' ] = self . request . build_absolute_uri ( ) <EOL> context [ '<STR_LIT>' ] = self . get_header ( ) <EOL> context [ '<STR_LIT:title>' ] = self . get_header ( ) <EOL> context [ '<STR_LIT>' ] = self . get_label ( ) <EOL> context [ '<STR_LIT>' ] = self . get_header ( ) <EOL> context [ '<STR_LIT>' ] = self . get_help_text ( ) <EOL> context [ '<STR_LIT>' ] = self . get_classes ( ) <EOL> return context <EOL> def form_valid ( self , form ) : <EOL> obj = self . object <EOL> fe_identifier = obj . fe_identifier <EOL> try : <EOL> parent = obj . parent <EOL> obj . delete ( ) <EOL> parent . invalidate_cache ( ) <EOL> success_url = self . get_success_url ( ) <EOL> response = HttpResponseRedirect ( success_url ) <EOL> response [ '<STR_LIT>' ] = success_url <EOL> except Exception as e : <EOL> raise e <EOL> if not self . request . is_ajax ( ) : <EOL> return response <EOL> return JsonResponse ( data = { <EOL> '<STR_LIT:id>' : fe_identifier , <EOL> } ) <EOL> def get_initial ( self ) : <EOL> return self . kwargs <EOL> class WidgetActionMixin ( SuccessUrlMixin ) : <EOL> template_name = '<STR_LIT>' <EOL> form_class = WidgetUpdateForm <EOL> success_url = "<STR_LIT:/>" <EOL> def get ( self , * args , ** kwargs ) : <EOL> return self . post ( * args , ** kwargs ) <EOL> class WidgetSortView ( WidgetActionMixin , ModalFormView ) : <EOL> '''<STR_LIT>''' <EOL> def post ( self , * args , ** kwargs ) : <EOL> widgets = self . request . POST . getlist ( '<STR_LIT>' , [ ] ) <EOL> widget_list = [ ] <EOL> try : <EOL> for widget_id in widgets : <EOL> widget = get_widget_from_id ( widget_id ) <EOL> if widget : <EOL> widget_list . append ( widget ) <EOL> except : <EOL> messages . error ( <EOL> self . request , _ ( '<STR_LIT>' ) ) <EOL> i = <NUM_LIT:0> <EOL> for widget in widget_list : <EOL> widget . ordering = i <EOL> widget . save ( ) <EOL> i += <NUM_LIT:1> <EOL> messages . success ( self . request , _ ( '<STR_LIT>' ) ) <EOL> return HttpResponse ( '<STR_LIT>' ) <EOL> class WidgetReorderView ( WidgetActionMixin , ModalFormView , ModelFormMixin ) : <EOL> '''<STR_LIT>''' <EOL> def post ( self , * args , ** kwargs ) : <EOL> widget = self . object <EOL> ordering = self . kwargs . get ( '<STR_LIT>' ) <EOL> if int ( ordering ) == <NUM_LIT:0> : <EOL> widget . ordering = <NUM_LIT:0> <EOL> widget . save ( ) <EOL> widgets = getattr ( widget . parent . content , widget . region ) <EOL> widgets = [ w for w in widgets if w . id != widget . id ] <EOL> for i , _widget in enumerate ( widgets ) : <EOL> _widget . ordering = i + <NUM_LIT:1> <EOL> _widget . save ( ) <EOL> elif int ( ordering ) == - <NUM_LIT:1> : <EOL> next_ordering = widget . ordering - <NUM_LIT:1> <EOL> widgets = getattr ( widget . parent . content , widget . region ) <EOL> for w in widgets : <EOL> if w . ordering == next_ordering : <EOL> w . ordering = widget . ordering <EOL> w . save ( ) <EOL> widget . ordering = next_ordering <EOL> widget . save ( ) <EOL> elif int ( ordering ) == <NUM_LIT:1> : <EOL> next_ordering = widget . ordering + <NUM_LIT:1> <EOL> widgets = getattr ( widget . parent . content , widget . region ) <EOL> for w in widgets : <EOL> if w . ordering == next_ordering : <EOL> w . ordering = widget . ordering <EOL> w . save ( ) <EOL> widget . ordering = next_ordering <EOL> widget . save ( ) <EOL> else : <EOL> widget . ordering = widget . next_ordering <EOL> widget . save ( ) <EOL> widgets = getattr ( widget . parent . content , widget . region ) <EOL> widgets = [ w for w in widgets if w . id != widget . id ] <EOL> widgets . sort ( key = lambda w : w . ordering ) <EOL> for i , _widget in enumerate ( widgets ) : <EOL> _widget . ordering = i <EOL> _widget . save ( ) <EOL> messages . success ( self . request , _ ( '<STR_LIT>' ) ) <EOL> success_url = self . get_success_url ( ) <EOL> response = HttpResponseRedirect ( success_url ) <EOL> response [ '<STR_LIT>' ] = success_url <EOL> return response <EOL> class WidgetCopyView ( WidgetReorderView ) : <EOL> '''<STR_LIT>''' <EOL> def post ( self , * args , ** kwargs ) : <EOL> widget = self . object <EOL> widget . pk = None <EOL> widget . save ( created = False ) <EOL> for dimension in self . model . objects . get ( <EOL> id = self . kwargs [ "<STR_LIT:id>" ] ) . dimensions : <EOL> dimension . pk = None <EOL> dimension . widget_id = widget . id <EOL> dimension . save ( ) <EOL> messages . success ( self . request , _ ( '<STR_LIT>' ) ) <EOL> success_url = self . get_success_url ( ) <EOL> response = HttpResponseRedirect ( success_url ) <EOL> response [ '<STR_LIT>' ] = success_url <EOL> return response <EOL> class JSReverseView ( WidgetReorderView ) : <EOL> '''<STR_LIT>''' <EOL> def clean_kwargs ( self , kwargs ) : <EOL> _kwargs = { } <EOL> for key , value in kwargs . items ( ) : <EOL> if value != '<STR_LIT>' : <EOL> _kwargs [ key ] = value <EOL> return _kwargs <EOL> def post ( self , * args , ** kwargs ) : <EOL> view_name = self . request . POST . get ( '<STR_LIT>' ) <EOL> args = json . loads ( self . request . POST . get ( '<STR_LIT:args>' , "<STR_LIT:{}>" ) ) . values ( ) <EOL> kwargs = json . loads ( self . request . POST . get ( '<STR_LIT>' , "<STR_LIT:{}>" ) ) <EOL> return JsonResponse ( { '<STR_LIT:url>' : reverse ( <EOL> view_name , args = args , kwargs = self . clean_kwargs ( kwargs ) ) } ) </s>
