<s> from django import forms <EOL> from django . apps import apps <EOL> from django . core . exceptions import PermissionDenied <EOL> from django . core . urlresolvers import reverse , NoReverseMatch <EOL> from django . db import models <EOL> from django . db . models . base import ModelBase <EOL> from django . forms . forms import DeclarativeFieldsMetaclass <EOL> from django . forms . utils import flatatt <EOL> from django . template import loader <EOL> from django . http import Http404 <EOL> from django . template . context import RequestContext <EOL> from django . test . client import RequestFactory <EOL> from django . utils . encoding import force_unicode , smart_unicode <EOL> from django . utils . html import escape <EOL> from django . utils . safestring import mark_safe <EOL> from django . utils . translation import ugettext as _ <EOL> from django . utils . http import urlencode , urlquote <EOL> from django . views . decorators . cache import never_cache <EOL> from xadmin import widgets as exwidgets <EOL> from xadmin . layout import FormHelper <EOL> from xadmin . models import UserSettings , UserWidget <EOL> from xadmin . sites import site <EOL> from xadmin . views . base import CommAdminView , ModelAdminView , filter_hook , csrf_protect_m <EOL> from xadmin . views . edit import CreateAdminView <EOL> from xadmin . views . list import ListAdminView <EOL> from xadmin . util import unquote <EOL> import copy <EOL> class WidgetTypeSelect ( forms . Widget ) : <EOL> def __init__ ( self , widgets , attrs = None ) : <EOL> super ( WidgetTypeSelect , self ) . __init__ ( attrs ) <EOL> self . _widgets = widgets <EOL> def render ( self , name , value , attrs = None ) : <EOL> if value is None : <EOL> value = '<STR_LIT>' <EOL> final_attrs = self . build_attrs ( attrs , name = name ) <EOL> final_attrs [ '<STR_LIT:class>' ] = '<STR_LIT>' <EOL> output = [ u'<STR_LIT>' % flatatt ( final_attrs ) ] <EOL> options = self . render_options ( force_unicode ( value ) , final_attrs [ '<STR_LIT:id>' ] ) <EOL> if options : <EOL> output . append ( options ) <EOL> output . append ( u'<STR_LIT>' ) <EOL> output . append ( '<STR_LIT>' % <EOL> ( final_attrs [ '<STR_LIT:id>' ] , name , force_unicode ( value ) ) ) <EOL> return mark_safe ( u'<STR_LIT:\n>' . join ( output ) ) <EOL> def render_option ( self , selected_choice , widget , id ) : <EOL> if widget . widget_type == selected_choice : <EOL> selected_html = u'<STR_LIT>' <EOL> else : <EOL> selected_html = '<STR_LIT>' <EOL> return ( u'<STR_LIT>' + <EOL> '<STR_LIT>' + <EOL> '<STR_LIT>' % ( id , widget . widget_type ) + <EOL> '<STR_LIT>' ) % ( <EOL> selected_html , <EOL> widget . widget_icon , <EOL> widget . widget_title or widget . widget_type , <EOL> widget . description ) <EOL> def render_options ( self , selected_choice , id ) : <EOL> output = [ ] <EOL> for widget in self . _widgets : <EOL> output . append ( self . render_option ( selected_choice , widget , id ) ) <EOL> return u'<STR_LIT:\n>' . join ( output ) <EOL> class UserWidgetAdmin ( object ) : <EOL> model_icon = '<STR_LIT>' <EOL> list_display = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:user>' ) <EOL> list_filter = [ '<STR_LIT:user>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> list_display_links = ( '<STR_LIT>' , ) <EOL> user_fields = [ '<STR_LIT:user>' ] <EOL> hidden_menu = True <EOL> wizard_form_list = ( <EOL> ( _ ( u"<STR_LIT>" ) , ( '<STR_LIT>' , '<STR_LIT>' ) ) , <EOL> ( _ ( u"<STR_LIT>" ) , { '<STR_LIT>' : <EOL> "<STR_LIT>" , '<STR_LIT>' : "<STR_LIT>" } ) <EOL> ) <EOL> def formfield_for_dbfield ( self , db_field , ** kwargs ) : <EOL> if db_field . name == '<STR_LIT>' : <EOL> widgets = widget_manager . get_widgets ( self . request . GET . get ( '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> form_widget = WidgetTypeSelect ( widgets ) <EOL> return forms . ChoiceField ( choices = [ ( w . widget_type , w . description ) for w in widgets ] , <EOL> widget = form_widget , label = _ ( '<STR_LIT>' ) ) <EOL> if '<STR_LIT>' in self . request . GET and db_field . name == '<STR_LIT>' : <EOL> kwargs [ '<STR_LIT>' ] = forms . HiddenInput <EOL> field = super ( <EOL> UserWidgetAdmin , self ) . formfield_for_dbfield ( db_field , ** kwargs ) <EOL> return field <EOL> def get_widget_params_form ( self , wizard ) : <EOL> data = wizard . get_cleaned_data_for_step ( wizard . steps . first ) <EOL> widget_type = data [ '<STR_LIT>' ] <EOL> widget = widget_manager . get ( widget_type ) <EOL> fields = copy . deepcopy ( widget . base_fields ) <EOL> if '<STR_LIT:id>' in fields : <EOL> del fields [ '<STR_LIT:id>' ] <EOL> return DeclarativeFieldsMetaclass ( "<STR_LIT>" , ( forms . Form , ) , fields ) <EOL> def convert_widget_params ( self , wizard , cleaned_data , form ) : <EOL> widget = UserWidget ( ) <EOL> value = dict ( [ ( f . name , f . value ( ) ) for f in form ] ) <EOL> widget . set_value ( value ) <EOL> cleaned_data [ '<STR_LIT:value>' ] = widget . value <EOL> cleaned_data [ '<STR_LIT:user>' ] = self . user <EOL> def get_list_display ( self ) : <EOL> list_display = super ( UserWidgetAdmin , self ) . get_list_display ( ) <EOL> if not self . user . is_superuser : <EOL> list_display . remove ( '<STR_LIT:user>' ) <EOL> return list_display <EOL> def queryset ( self ) : <EOL> if self . user . is_superuser : <EOL> return super ( UserWidgetAdmin , self ) . queryset ( ) <EOL> return UserWidget . objects . filter ( user = self . user ) <EOL> def update_dashboard ( self , obj ) : <EOL> try : <EOL> portal_pos = UserSettings . objects . get ( <EOL> user = obj . user , key = "<STR_LIT>" % obj . page_id ) <EOL> except UserSettings . DoesNotExist : <EOL> return <EOL> pos = [ [ w for w in col . split ( '<STR_LIT:U+002C>' ) if w != str ( <EOL> obj . id ) ] for col in portal_pos . value . split ( '<STR_LIT:|>' ) ] <EOL> portal_pos . value = '<STR_LIT:|>' . join ( [ '<STR_LIT:U+002C>' . join ( col ) for col in pos ] ) <EOL> portal_pos . save ( ) <EOL> def delete_model ( self ) : <EOL> self . update_dashboard ( self . obj ) <EOL> super ( UserWidgetAdmin , self ) . delete_model ( ) <EOL> def delete_models ( self , queryset ) : <EOL> for obj in queryset : <EOL> self . update_dashboard ( obj ) <EOL> super ( UserWidgetAdmin , self ) . delete_models ( queryset ) <EOL> site . register ( UserWidget , UserWidgetAdmin ) <EOL> class WidgetManager ( object ) : <EOL> _widgets = None <EOL> def __init__ ( self ) : <EOL> self . _widgets = { } <EOL> def register ( self , widget_class ) : <EOL> self . _widgets [ widget_class . widget_type ] = widget_class <EOL> return widget_class <EOL> def get ( self , name ) : <EOL> return self . _widgets [ name ] <EOL> def get_widgets ( self , page_id ) : <EOL> return self . _widgets . values ( ) <EOL> widget_manager = WidgetManager ( ) <EOL> class WidgetDataError ( Exception ) : <EOL> def __init__ ( self , widget , errors ) : <EOL> super ( WidgetDataError , self ) . __init__ ( str ( errors ) ) <EOL> self . widget = widget <EOL> self . errors = errors <EOL> class BaseWidget ( forms . Form ) : <EOL> template = '<STR_LIT>' <EOL> description = '<STR_LIT>' <EOL> widget_title = None <EOL> widget_icon = '<STR_LIT>' <EOL> widget_type = '<STR_LIT>' <EOL> base_title = None <EOL> id = forms . IntegerField ( label = _ ( '<STR_LIT>' ) , widget = forms . HiddenInput ) <EOL> title = forms . CharField ( label = _ ( '<STR_LIT>' ) , required = False , widget = exwidgets . AdminTextInputWidget ) <EOL> def __init__ ( self , dashboard , data ) : <EOL> self . dashboard = dashboard <EOL> self . admin_site = dashboard . admin_site <EOL> self . request = dashboard . request <EOL> self . user = dashboard . request . user <EOL> self . convert ( data ) <EOL> super ( BaseWidget , self ) . __init__ ( data ) <EOL> if not self . is_valid ( ) : <EOL> raise WidgetDataError ( self , self . errors . as_text ( ) ) <EOL> self . setup ( ) <EOL> def setup ( self ) : <EOL> helper = FormHelper ( ) <EOL> helper . form_tag = False <EOL> self . helper = helper <EOL> self . id = self . cleaned_data [ '<STR_LIT:id>' ] <EOL> self . title = self . cleaned_data [ '<STR_LIT:title>' ] or self . base_title <EOL> if not ( self . user . is_superuser or self . has_perm ( ) ) : <EOL> raise PermissionDenied <EOL> @ property <EOL> def widget ( self ) : <EOL> context = { '<STR_LIT>' : self . id , '<STR_LIT>' : self . title , '<STR_LIT>' : self . widget_icon , <EOL> '<STR_LIT>' : self . widget_type , '<STR_LIT>' : self , '<STR_LIT>' : self } <EOL> self . context ( context ) <EOL> return loader . render_to_string ( self . template , context , context_instance = RequestContext ( self . request ) ) <EOL> def context ( self , context ) : <EOL> pass <EOL> def convert ( self , data ) : <EOL> pass <EOL> def has_perm ( self ) : <EOL> return False <EOL> def save ( self ) : <EOL> value = dict ( [ ( f . name , f . value ( ) ) for f in self ] ) <EOL> user_widget = UserWidget . objects . get ( id = self . id ) <EOL> user_widget . set_value ( value ) <EOL> user_widget . save ( ) <EOL> def static ( self , path ) : <EOL> return self . dashboard . static ( path ) <EOL> def vendor ( self , * tags ) : <EOL> return self . dashboard . vendor ( * tags ) <EOL> def media ( self ) : <EOL> return forms . Media ( ) <EOL> @ widget_manager . register <EOL> class HtmlWidget ( BaseWidget ) : <EOL> widget_type = '<STR_LIT:html>' <EOL> widget_icon = '<STR_LIT>' <EOL> description = _ ( <EOL> u'<STR_LIT>' ) <EOL> content = forms . CharField ( label = _ ( <EOL> '<STR_LIT>' ) , widget = exwidgets . AdminTextareaWidget , required = False ) <EOL> def has_perm ( self ) : <EOL> return True <EOL> def context ( self , context ) : <EOL> context [ '<STR_LIT:content>' ] = self . cleaned_data [ '<STR_LIT:content>' ] <EOL> class ModelChoiceIterator ( object ) : <EOL> def __init__ ( self , field ) : <EOL> self . field = field <EOL> def __iter__ ( self ) : <EOL> from xadmin import site as g_admin_site <EOL> for m , ma in g_admin_site . _registry . items ( ) : <EOL> yield ( '<STR_LIT>' % ( m . _meta . app_label , m . _meta . model_name ) , <EOL> m . _meta . verbose_name ) <EOL> class ModelChoiceField ( forms . ChoiceField ) : <EOL> def __init__ ( self , required = True , widget = None , label = None , initial = None , <EOL> help_text = None , * args , ** kwargs ) : <EOL> forms . Field . __init__ ( self , required , widget , label , initial , help_text , <EOL> * args , ** kwargs ) <EOL> self . widget . choices = self . choices <EOL> def __deepcopy__ ( self , memo ) : <EOL> result = forms . Field . __deepcopy__ ( self , memo ) <EOL> return result <EOL> def _get_choices ( self ) : <EOL> return ModelChoiceIterator ( self ) <EOL> choices = property ( _get_choices , forms . ChoiceField . _set_choices ) <EOL> def to_python ( self , value ) : <EOL> if isinstance ( value , ModelBase ) : <EOL> return value <EOL> app_label , model_name = value . lower ( ) . split ( '<STR_LIT:.>' ) <EOL> return apps . get_model ( app_label , model_name ) <EOL> def prepare_value ( self , value ) : <EOL> if isinstance ( value , ModelBase ) : <EOL> value = '<STR_LIT>' % ( value . _meta . app_label , value . _meta . model_name ) <EOL> return value <EOL> def valid_value ( self , value ) : <EOL> value = self . prepare_value ( value ) <EOL> for k , v in self . choices : <EOL> if value == smart_unicode ( k ) : <EOL> return True <EOL> return False <EOL> class ModelBaseWidget ( BaseWidget ) : <EOL> app_label = None <EOL> model_name = None <EOL> model_perm = '<STR_LIT>' <EOL> model = ModelChoiceField ( label = _ ( u'<STR_LIT>' ) , widget = exwidgets . AdminSelectWidget ) <EOL> def __init__ ( self , dashboard , data ) : <EOL> self . dashboard = dashboard <EOL> super ( ModelBaseWidget , self ) . __init__ ( dashboard , data ) <EOL> def setup ( self ) : <EOL> self . model = self . cleaned_data [ '<STR_LIT>' ] <EOL> self . app_label = self . model . _meta . app_label <EOL> self . model_name = self . model . _meta . model_name <EOL> super ( ModelBaseWidget , self ) . setup ( ) <EOL> def has_perm ( self ) : <EOL> return self . dashboard . has_model_perm ( self . model , self . model_perm ) <EOL> def filte_choices_model ( self , model , modeladmin ) : <EOL> return self . dashboard . has_model_perm ( model , self . model_perm ) <EOL> def model_admin_url ( self , name , * args , ** kwargs ) : <EOL> return reverse ( <EOL> "<STR_LIT>" % ( self . admin_site . app_name , self . app_label , <EOL> self . model_name , name ) , args = args , kwargs = kwargs ) <EOL> class PartialBaseWidget ( BaseWidget ) : <EOL> def get_view_class ( self , view_class , model = None , ** opts ) : <EOL> admin_class = self . admin_site . _registry . get ( model ) if model else None <EOL> return self . admin_site . get_view_class ( view_class , admin_class , ** opts ) <EOL> def get_factory ( self ) : <EOL> return RequestFactory ( ) <EOL> def setup_request ( self , request ) : <EOL> request . user = self . user <EOL> request . session = self . request . session <EOL> return request <EOL> def make_get_request ( self , path , data = { } , ** extra ) : <EOL> req = self . get_factory ( ) . get ( path , data , ** extra ) <EOL> return self . setup_request ( req ) <EOL> def make_post_request ( self , path , data = { } , ** extra ) : <EOL> req = self . get_factory ( ) . post ( path , data , ** extra ) <EOL> return self . setup_request ( req ) <EOL> @ widget_manager . register <EOL> class QuickBtnWidget ( BaseWidget ) : <EOL> widget_type = '<STR_LIT>' <EOL> description = _ ( u'<STR_LIT>' ) <EOL> template = "<STR_LIT>" <EOL> base_title = _ ( u"<STR_LIT>" ) <EOL> widget_icon = '<STR_LIT>' <EOL> def convert ( self , data ) : <EOL> self . q_btns = data . pop ( '<STR_LIT>' , [ ] ) <EOL> def get_model ( self , model_or_label ) : <EOL> if isinstance ( model_or_label , ModelBase ) : <EOL> return model_or_label <EOL> else : <EOL> return apps . get_model ( * model_or_label . lower ( ) . split ( '<STR_LIT:.>' ) ) <EOL> def context ( self , context ) : <EOL> btns = [ ] <EOL> for b in self . q_btns : <EOL> btn = { } <EOL> if '<STR_LIT>' in b : <EOL> model = self . get_model ( b [ '<STR_LIT>' ] ) <EOL> if not self . user . has_perm ( "<STR_LIT>" % ( model . _meta . app_label , model . _meta . model_name ) ) : <EOL> continue <EOL> btn [ '<STR_LIT:url>' ] = reverse ( "<STR_LIT>" % ( self . admin_site . app_name , model . _meta . app_label , <EOL> model . _meta . model_name , b . get ( '<STR_LIT>' , '<STR_LIT>' ) ) ) <EOL> btn [ '<STR_LIT:title>' ] = model . _meta . verbose_name <EOL> btn [ '<STR_LIT>' ] = self . dashboard . get_model_icon ( model ) <EOL> else : <EOL> try : <EOL> btn [ '<STR_LIT:url>' ] = reverse ( b [ '<STR_LIT:url>' ] ) <EOL> except NoReverseMatch : <EOL> btn [ '<STR_LIT:url>' ] = b [ '<STR_LIT:url>' ] <EOL> if '<STR_LIT:title>' in b : <EOL> btn [ '<STR_LIT:title>' ] = b [ '<STR_LIT:title>' ] <EOL> if '<STR_LIT>' in b : <EOL> btn [ '<STR_LIT>' ] = b [ '<STR_LIT>' ] <EOL> btns . append ( btn ) <EOL> context . update ( { '<STR_LIT>' : btns } ) <EOL> def has_perm ( self ) : <EOL> return True <EOL> @ widget_manager . register <EOL> class ListWidget ( ModelBaseWidget , PartialBaseWidget ) : <EOL> widget_type = '<STR_LIT:list>' <EOL> description = _ ( u'<STR_LIT>' ) <EOL> template = "<STR_LIT>" <EOL> model_perm = '<STR_LIT>' <EOL> widget_icon = '<STR_LIT>' <EOL> def convert ( self , data ) : <EOL> self . list_params = data . pop ( '<STR_LIT>' , { } ) <EOL> self . list_count = data . pop ( '<STR_LIT:count>' , <NUM_LIT:10> ) <EOL> def setup ( self ) : <EOL> super ( ListWidget , self ) . setup ( ) <EOL> if not self . title : <EOL> self . title = self . model . _meta . verbose_name_plural <EOL> req = self . make_get_request ( "<STR_LIT>" , self . list_params ) <EOL> self . list_view = self . get_view_class ( ListAdminView , self . model ) ( req ) <EOL> if self . list_count : <EOL> self . list_view . list_per_page = self . list_count <EOL> def context ( self , context ) : <EOL> list_view = self . list_view <EOL> list_view . make_result_list ( ) <EOL> base_fields = list_view . base_list_display <EOL> if len ( base_fields ) > <NUM_LIT:5> : <EOL> base_fields = base_fields [ <NUM_LIT:0> : <NUM_LIT:5> ] <EOL> context [ '<STR_LIT>' ] = [ c for c in list_view . result_headers ( <EOL> ) . cells if c . field_name in base_fields ] <EOL> context [ '<STR_LIT>' ] = [ [ o for i , o in <EOL> enumerate ( filter ( lambda c : c . field_name in base_fields , r . cells ) ) ] <EOL> for r in list_view . results ( ) ] <EOL> context [ '<STR_LIT>' ] = list_view . result_count <EOL> context [ '<STR_LIT>' ] = self . model_admin_url ( '<STR_LIT>' ) + "<STR_LIT:?>" + urlencode ( self . list_params ) <EOL> @ widget_manager . register <EOL> class AddFormWidget ( ModelBaseWidget , PartialBaseWidget ) : <EOL> widget_type = '<STR_LIT>' <EOL> description = _ ( u'<STR_LIT>' ) <EOL> template = "<STR_LIT>" <EOL> model_perm = '<STR_LIT>' <EOL> widget_icon = '<STR_LIT>' <EOL> def setup ( self ) : <EOL> super ( AddFormWidget , self ) . setup ( ) <EOL> if self . title is None : <EOL> self . title = _ ( '<STR_LIT>' ) % self . model . _meta . verbose_name <EOL> req = self . make_get_request ( "<STR_LIT>" ) <EOL> self . add_view = self . get_view_class ( <EOL> CreateAdminView , self . model , list_per_page = <NUM_LIT:10> ) ( req ) <EOL> self . add_view . instance_forms ( ) <EOL> def context ( self , context ) : <EOL> helper = FormHelper ( ) <EOL> helper . form_tag = False <EOL> context . update ( { <EOL> '<STR_LIT>' : self . add_view . form_obj , <EOL> '<STR_LIT>' : helper , <EOL> '<STR_LIT>' : self . add_view . model_admin_url ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : self . model <EOL> } ) <EOL> def media ( self ) : <EOL> return self . add_view . media + self . add_view . form_obj . media + self . vendor ( '<STR_LIT>' ) <EOL> class Dashboard ( CommAdminView ) : <EOL> widget_customiz = True <EOL> widgets = [ ] <EOL> title = _ ( u"<STR_LIT>" ) <EOL> icon = None <EOL> def get_page_id ( self ) : <EOL> return self . request . path <EOL> def get_portal_key ( self ) : <EOL> return "<STR_LIT>" % self . get_page_id ( ) <EOL> @ filter_hook <EOL> def get_widget ( self , widget_or_id , data = None ) : <EOL> try : <EOL> if isinstance ( widget_or_id , UserWidget ) : <EOL> widget = widget_or_id <EOL> else : <EOL> widget = UserWidget . objects . get ( user = self . user , page_id = self . get_page_id ( ) , id = widget_or_id ) <EOL> wid = widget_manager . get ( widget . widget_type ) <EOL> class widget_with_perm ( wid ) : <EOL> def context ( self , context ) : <EOL> super ( widget_with_perm , self ) . context ( context ) <EOL> context . update ( { '<STR_LIT>' : self . request . user . has_perm ( '<STR_LIT>' ) } ) <EOL> wid_instance = widget_with_perm ( self , data or widget . get_value ( ) ) <EOL> return wid_instance <EOL> except UserWidget . DoesNotExist : <EOL> return None <EOL> @ filter_hook <EOL> def get_init_widget ( self ) : <EOL> portal = [ ] <EOL> widgets = self . widgets <EOL> for col in widgets : <EOL> portal_col = [ ] <EOL> for opts in col : <EOL> try : <EOL> widget = UserWidget ( user = self . user , page_id = self . get_page_id ( ) , widget_type = opts [ '<STR_LIT:type>' ] ) <EOL> widget . set_value ( opts ) <EOL> widget . save ( ) <EOL> portal_col . append ( self . get_widget ( widget ) ) <EOL> except ( PermissionDenied , WidgetDataError ) : <EOL> widget . delete ( ) <EOL> continue <EOL> portal . append ( portal_col ) <EOL> UserSettings ( <EOL> user = self . user , key = "<STR_LIT>" % self . get_page_id ( ) , <EOL> value = '<STR_LIT:|>' . join ( [ '<STR_LIT:U+002C>' . join ( [ str ( w . id ) for w in col ] ) for col in portal ] ) ) . save ( ) <EOL> return portal <EOL> @ filter_hook <EOL> def get_widgets ( self ) : <EOL> if self . widget_customiz : <EOL> portal_pos = UserSettings . objects . filter ( <EOL> user = self . user , key = self . get_portal_key ( ) ) <EOL> if len ( portal_pos ) : <EOL> portal_pos = portal_pos [ <NUM_LIT:0> ] . value <EOL> widgets = [ ] <EOL> if portal_pos : <EOL> user_widgets = dict ( [ ( uw . id , uw ) for uw in UserWidget . objects . filter ( user = self . user , page_id = self . get_page_id ( ) ) ] ) <EOL> for col in portal_pos . split ( '<STR_LIT:|>' ) : <EOL> ws = [ ] <EOL> for wid in col . split ( '<STR_LIT:U+002C>' ) : <EOL> try : <EOL> widget = user_widgets . get ( int ( wid ) ) <EOL> if widget : <EOL> ws . append ( self . get_widget ( widget ) ) <EOL> except Exception , e : <EOL> import logging <EOL> logging . error ( e , exc_info = True ) <EOL> widgets . append ( ws ) <EOL> return widgets <EOL> return self . get_init_widget ( ) <EOL> @ filter_hook <EOL> def get_title ( self ) : <EOL> return self . title <EOL> @ filter_hook <EOL> def get_context ( self ) : <EOL> new_context = { <EOL> '<STR_LIT:title>' : self . get_title ( ) , <EOL> '<STR_LIT>' : self . icon , <EOL> '<STR_LIT>' : self . get_portal_key ( ) , <EOL> '<STR_LIT>' : [ ( '<STR_LIT>' % int ( <NUM_LIT:12> / len ( self . widgets ) ) , ws ) for ws in self . widgets ] , <EOL> '<STR_LIT>' : self . has_model_perm ( UserWidget , '<STR_LIT>' ) and self . widget_customiz , <EOL> '<STR_LIT>' : self . get_admin_url ( '<STR_LIT>' % ( UserWidget . _meta . app_label , UserWidget . _meta . model_name ) ) + <EOL> "<STR_LIT>" % ( self . user . id , self . get_page_id ( ) , urlquote ( self . request . get_full_path ( ) ) ) <EOL> } <EOL> context = super ( Dashboard , self ) . get_context ( ) <EOL> context . update ( new_context ) <EOL> return context <EOL> @ never_cache <EOL> def get ( self , request , * args , ** kwargs ) : <EOL> self . widgets = self . get_widgets ( ) <EOL> return self . template_response ( '<STR_LIT>' , self . get_context ( ) ) <EOL> @ csrf_protect_m <EOL> def post ( self , request , * args , ** kwargs ) : <EOL> if '<STR_LIT:id>' in request . POST : <EOL> widget_id = request . POST [ '<STR_LIT:id>' ] <EOL> if request . POST . get ( '<STR_LIT>' , None ) != '<STR_LIT>' : <EOL> widget = self . get_widget ( widget_id , request . POST . copy ( ) ) <EOL> widget . save ( ) <EOL> else : <EOL> try : <EOL> widget = UserWidget . objects . get ( <EOL> user = self . user , page_id = self . get_page_id ( ) , id = widget_id ) <EOL> widget . delete ( ) <EOL> try : <EOL> portal_pos = UserSettings . objects . get ( user = self . user , key = "<STR_LIT>" % self . get_page_id ( ) ) <EOL> pos = [ [ w for w in col . split ( '<STR_LIT:U+002C>' ) if w != str ( <EOL> widget_id ) ] for col in portal_pos . value . split ( '<STR_LIT:|>' ) ] <EOL> portal_pos . value = '<STR_LIT:|>' . join ( [ '<STR_LIT:U+002C>' . join ( col ) for col in pos ] ) <EOL> portal_pos . save ( ) <EOL> except Exception : <EOL> pass <EOL> except UserWidget . DoesNotExist : <EOL> pass <EOL> return self . get ( request ) <EOL> @ filter_hook <EOL> def get_media ( self ) : <EOL> media = super ( Dashboard , self ) . get_media ( ) + self . vendor ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> if self . widget_customiz : <EOL> media = media + self . vendor ( '<STR_LIT>' ) <EOL> for ws in self . widgets : <EOL> for widget in ws : <EOL> media = media + widget . media ( ) <EOL> return media <EOL> class ModelDashboard ( Dashboard , ModelAdminView ) : <EOL> title = _ ( u"<STR_LIT>" ) <EOL> def get_page_id ( self ) : <EOL> return '<STR_LIT>' % self . model_info <EOL> @ filter_hook <EOL> def get_title ( self ) : <EOL> return self . title % force_unicode ( self . obj ) <EOL> def init_request ( self , object_id , * args , ** kwargs ) : <EOL> self . obj = self . get_object ( unquote ( object_id ) ) <EOL> if not self . has_view_permission ( self . obj ) : <EOL> raise PermissionDenied <EOL> if self . obj is None : <EOL> raise Http404 ( _ ( '<STR_LIT>' ) % <EOL> { '<STR_LIT:name>' : force_unicode ( self . opts . verbose_name ) , '<STR_LIT:key>' : escape ( object_id ) } ) <EOL> @ filter_hook <EOL> def get_context ( self ) : <EOL> new_context = { <EOL> '<STR_LIT>' : self . has_change_permission ( self . obj ) , <EOL> '<STR_LIT:object>' : self . obj , <EOL> } <EOL> context = Dashboard . get_context ( self ) <EOL> context . update ( ModelAdminView . get_context ( self ) ) <EOL> context . update ( new_context ) <EOL> return context <EOL> @ never_cache <EOL> def get ( self , request , * args , ** kwargs ) : <EOL> self . widgets = self . get_widgets ( ) <EOL> return self . template_response ( self . get_template_list ( '<STR_LIT>' ) , self . get_context ( ) ) </s>
