<s> from __future__ import unicode_literals <EOL> import django . forms . models as model_forms <EOL> import json <EOL> import six <EOL> from django import forms <EOL> from django . conf import settings <EOL> from django . conf . urls import patterns , url <EOL> from django . contrib import messages <EOL> from django . contrib . auth import REDIRECT_FIELD_NAME , get_user_model <EOL> from django . core . exceptions import ImproperlyConfigured <EOL> from django . core . urlresolvers import reverse <EOL> from django . db import IntegrityError <EOL> from django . db . models import Q <EOL> from django . http import HttpResponseRedirect , HttpResponse <EOL> from django . utils . encoding import force_text <EOL> from django . utils . http import urlquote <EOL> from django . utils . translation import ugettext_lazy as _ <EOL> from django . views . generic . edit import ModelFormMixin , UpdateView , CreateView , ProcessFormView , FormView <EOL> from django . views . generic . base import TemplateView <EOL> from django . views . generic import DetailView , ListView <EOL> from guardian . shortcuts import get_objects_for_user , assign_perm <EOL> from guardian . utils import get_anonymous_user <EOL> from smartmin . csv_imports . models import ImportTask <EOL> from . import widgets <EOL> def smart_url ( url , obj = None ) : <EOL> """<STR_LIT>""" <EOL> if url . find ( "<STR_LIT:@>" ) >= <NUM_LIT:0> : <EOL> ( args , value ) = url . split ( '<STR_LIT:@>' ) <EOL> if args : <EOL> val = getattr ( obj , args , None ) <EOL> return reverse ( value , args = [ val ] ) <EOL> else : <EOL> return reverse ( value ) <EOL> else : <EOL> if obj is None : <EOL> return url <EOL> else : <EOL> return url % obj . id <EOL> class SmartView ( object ) : <EOL> fields = None <EOL> exclude = None <EOL> field_config = { } <EOL> title = None <EOL> refresh = <NUM_LIT:0> <EOL> template_name = None <EOL> pjax = None <EOL> url_name = None <EOL> crudl = None <EOL> def __init__ ( self , * args ) : <EOL> """<STR_LIT>""" <EOL> self . extra_context = { } <EOL> super ( SmartView , self ) . __init__ ( ) <EOL> def derive_title ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . title <EOL> @ classmethod <EOL> def derive_url_pattern ( cls , path , action ) : <EOL> """<STR_LIT>""" <EOL> return r'<STR_LIT>' % ( path , action ) <EOL> def has_permission ( self , request , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> self . kwargs = kwargs <EOL> self . args = args <EOL> self . request = request <EOL> if not getattr ( self , '<STR_LIT>' , None ) : <EOL> return True <EOL> else : <EOL> real_anon = get_anonymous_user ( ) <EOL> has_perm = real_anon . has_perm ( self . permission ) <EOL> if not has_perm : <EOL> has_perm = request . user . has_perm ( self . permission ) <EOL> if not has_perm : <EOL> has_perm = self . has_object_permission ( '<STR_LIT>' ) <EOL> if not has_perm : <EOL> has_perm = self . has_object_permission ( '<STR_LIT>' ) <EOL> return has_perm <EOL> def has_object_permission ( self , getter_name ) : <EOL> """<STR_LIT>""" <EOL> obj = None <EOL> obj_getter = getattr ( self , getter_name , None ) <EOL> if getter_name == "<STR_LIT>" and '<STR_LIT>' not in self . kwargs : <EOL> return False <EOL> if obj_getter : <EOL> obj = obj_getter ( ) <EOL> if obj : <EOL> return self . request . user . has_perm ( getattr ( self , '<STR_LIT>' , None ) , obj ) <EOL> def dispatch ( self , request , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> def wrapper ( request , * args , ** kwargs ) : <EOL> if not self . has_permission ( request , * args , ** kwargs ) : <EOL> path = urlquote ( request . get_full_path ( ) ) <EOL> login_url = kwargs . pop ( '<STR_LIT>' , settings . LOGIN_URL ) <EOL> redirect_field_name = kwargs . pop ( '<STR_LIT>' , REDIRECT_FIELD_NAME ) <EOL> return HttpResponseRedirect ( "<STR_LIT>" % ( login_url , redirect_field_name , path ) ) <EOL> else : <EOL> response = self . pre_process ( request , * args , ** kwargs ) <EOL> if not response : <EOL> return super ( SmartView , self ) . dispatch ( request , * args , ** kwargs ) <EOL> else : <EOL> return response <EOL> return wrapper ( request , * args , ** kwargs ) <EOL> def pre_process ( self , request , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> return None <EOL> def lookup_obj_attribute ( self , obj , field ) : <EOL> """<STR_LIT>""" <EOL> curr_field = field . encode ( '<STR_LIT:ascii>' , '<STR_LIT:ignore>' ) . decode ( "<STR_LIT:utf-8>" ) <EOL> rest = None <EOL> if field . find ( '<STR_LIT:.>' ) >= <NUM_LIT:0> : <EOL> curr_field = field . split ( '<STR_LIT:.>' ) [ <NUM_LIT:0> ] <EOL> rest = '<STR_LIT:.>' . join ( field . split ( '<STR_LIT:.>' ) [ <NUM_LIT:1> : ] ) <EOL> obj_field = getattr ( obj , curr_field , None ) <EOL> if obj_field and getattr ( obj_field , '<STR_LIT>' , None ) : <EOL> obj_field = obj_field ( ) <EOL> if obj_field and rest : <EOL> return self . lookup_obj_attribute ( obj_field , rest ) <EOL> else : <EOL> return obj_field <EOL> def lookup_field_value ( self , context , obj , field ) : <EOL> """<STR_LIT>""" <EOL> curr_field = field . encode ( '<STR_LIT:ascii>' , '<STR_LIT:ignore>' ) . decode ( "<STR_LIT:utf-8>" ) <EOL> if field . find ( '<STR_LIT:.>' ) == - <NUM_LIT:1> : <EOL> view_method = getattr ( self , '<STR_LIT>' % curr_field , None ) <EOL> if view_method : <EOL> return view_method ( obj ) <EOL> return self . lookup_obj_attribute ( obj , field ) <EOL> def lookup_field_label ( self , context , field , default = None ) : <EOL> """<STR_LIT>""" <EOL> if field . find ( '<STR_LIT:.>' ) >= <NUM_LIT:0> : <EOL> return self . lookup_field_label ( context , field . split ( '<STR_LIT:.>' ) [ - <NUM_LIT:1> ] , default ) <EOL> label = None <EOL> if field in self . field_config and '<STR_LIT:label>' in self . field_config [ field ] : <EOL> label = self . field_config [ field ] [ '<STR_LIT:label>' ] <EOL> elif default : <EOL> label = default <EOL> else : <EOL> for model_field in self . model . _meta . fields : <EOL> if model_field . name == field : <EOL> return model_field . verbose_name . title ( ) <EOL> if label is None : <EOL> label = self . derive_field_label ( field ) <EOL> return label <EOL> def lookup_field_help ( self , field , default = None ) : <EOL> """<STR_LIT>""" <EOL> help = None <EOL> if field in self . field_config and '<STR_LIT>' in self . field_config [ field ] : <EOL> help = self . field_config [ field ] [ '<STR_LIT>' ] <EOL> elif default : <EOL> help = default <EOL> elif hasattr ( self , '<STR_LIT>' ) : <EOL> for model_field in self . model . _meta . fields : <EOL> if model_field . name == field : <EOL> help = model_field . help_text <EOL> break <EOL> return help <EOL> def lookup_field_class ( self , field , obj = None , default = None ) : <EOL> """<STR_LIT>""" <EOL> css = "<STR_LIT>" <EOL> if field in self . field_config and '<STR_LIT:class>' in self . field_config [ field ] : <EOL> css = self . field_config [ field ] [ '<STR_LIT:class>' ] <EOL> elif default : <EOL> css = default <EOL> return css <EOL> def derive_field_label ( self , field , obj = None ) : <EOL> """<STR_LIT>""" <EOL> label = field . replace ( '<STR_LIT:_>' , '<STR_LIT:U+0020>' ) . title ( ) <EOL> return label <EOL> def derive_field_config ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . field_config <EOL> def get_template_names ( self ) : <EOL> """<STR_LIT>""" <EOL> templates = [ ] <EOL> if getattr ( self , '<STR_LIT>' , None ) : <EOL> templates . append ( self . template_name ) <EOL> if getattr ( self , '<STR_LIT>' , None ) : <EOL> templates . append ( self . default_template ) <EOL> else : <EOL> templates = super ( SmartView , self ) . get_template_names ( ) <EOL> return templates <EOL> def derive_fields ( self ) : <EOL> """<STR_LIT>""" <EOL> fields = [ ] <EOL> if self . fields : <EOL> fields . append ( self . fields ) <EOL> return fields <EOL> def derive_exclude ( self ) : <EOL> """<STR_LIT>""" <EOL> exclude = [ ] <EOL> if self . exclude : <EOL> exclude += self . exclude <EOL> return exclude <EOL> def derive_refresh ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . refresh <EOL> def get_context_data ( self , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> context = super ( SmartView , self ) . get_context_data ( ** kwargs ) <EOL> self . field_config = self . derive_field_config ( ) <EOL> self . fields = self . derive_fields ( ) <EOL> url_params = "<STR_LIT:?>" <EOL> order_params = "<STR_LIT>" <EOL> for key in self . request . GET . keys ( ) : <EOL> if key != '<STR_LIT>' and key != '<STR_LIT>' and key [ <NUM_LIT:0> ] != '<STR_LIT:_>' : <EOL> for value in self . request . GET . getlist ( key ) : <EOL> url_params += "<STR_LIT>" % ( key , urlquote ( value ) ) <EOL> elif key == '<STR_LIT>' : <EOL> order_params = "<STR_LIT:&>" . join ( [ "<STR_LIT>" % ( key , _ ) for _ in self . request . GET . getlist ( key ) ] ) <EOL> context [ '<STR_LIT>' ] = url_params <EOL> context [ '<STR_LIT>' ] = order_params + "<STR_LIT:&>" <EOL> context [ '<STR_LIT>' ] = self . pjax <EOL> context [ '<STR_LIT>' ] = dict ( ) <EOL> context [ '<STR_LIT>' ] = self . fields <EOL> context [ '<STR_LIT>' ] = self <EOL> context [ '<STR_LIT>' ] = self . field_config <EOL> context [ '<STR_LIT:title>' ] = self . derive_title ( ) <EOL> context . update ( self . extra_context ) <EOL> base_template = "<STR_LIT>" <EOL> if '<STR_LIT>' in self . request . GET or '<STR_LIT>' in self . request . POST : <EOL> base_template = "<STR_LIT>" <EOL> if '<STR_LIT>' in self . request . META : <EOL> base_template = "<STR_LIT>" <EOL> context [ '<STR_LIT>' ] = base_template <EOL> refresh = self . derive_refresh ( ) <EOL> if refresh : <EOL> context [ '<STR_LIT>' ] = refresh <EOL> return context <EOL> def as_json ( self , context ) : <EOL> """<STR_LIT>""" <EOL> return context <EOL> def render_to_response ( self , context , ** response_kwargs ) : <EOL> """<STR_LIT>""" <EOL> if '<STR_LIT>' in self . request . GET and self . request . GET [ '<STR_LIT>' ] == '<STR_LIT>' : <EOL> json_data = self . as_json ( context ) <EOL> return HttpResponse ( json . dumps ( json_data ) , content_type = '<STR_LIT>' ) <EOL> else : <EOL> return super ( SmartView , self ) . render_to_response ( context ) <EOL> class SmartTemplateView ( SmartView , TemplateView ) : <EOL> pass <EOL> class SmartReadView ( SmartView , DetailView ) : <EOL> default_template = '<STR_LIT>' <EOL> edit_button = None <EOL> field_config = { '<STR_LIT>' : dict ( label = "<STR_LIT>" ) , <EOL> '<STR_LIT>' : dict ( label = "<STR_LIT>" ) } <EOL> def derive_queryset ( self ) : <EOL> return super ( SmartReadView , self ) . get_queryset ( ) <EOL> def get_queryset ( self ) : <EOL> self . queryset = self . derive_queryset ( ) <EOL> return self . queryset <EOL> def derive_title ( self ) : <EOL> """<STR_LIT>""" <EOL> return str ( self . object ) <EOL> @ classmethod <EOL> def derive_url_pattern ( cls , path , action ) : <EOL> """<STR_LIT>""" <EOL> return r'<STR_LIT>' % ( path , action ) <EOL> def derive_fields ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . fields : <EOL> return list ( self . fields ) <EOL> else : <EOL> fields = [ ] <EOL> for field in self . object . _meta . fields : <EOL> fields . append ( field . name ) <EOL> exclude = self . derive_exclude ( ) <EOL> fields = [ field for field in fields if field not in exclude ] <EOL> return fields <EOL> def get_modified_blurb ( self , obj ) : <EOL> return "<STR_LIT>" % ( obj . modified_on . strftime ( "<STR_LIT>" ) , obj . modified_by ) <EOL> def get_created_blurb ( self , obj ) : <EOL> return "<STR_LIT>" % ( obj . created_on . strftime ( "<STR_LIT>" ) , obj . created_by ) <EOL> class SmartDeleteView ( SmartView , DetailView , ProcessFormView ) : <EOL> default_template = '<STR_LIT>' <EOL> name_field = '<STR_LIT:name>' <EOL> cancel_url = None <EOL> redirect_url = None <EOL> @ classmethod <EOL> def derive_url_pattern ( cls , path , action ) : <EOL> """<STR_LIT>""" <EOL> return r'<STR_LIT>' % ( path , action ) <EOL> def get_cancel_url ( self ) : <EOL> if not self . cancel_url : <EOL> raise ImproperlyConfigured ( "<STR_LIT>" ) <EOL> return smart_url ( self . cancel_url , self . object ) <EOL> def pre_delete ( self , obj ) : <EOL> if hasattr ( obj , '<STR_LIT>' ) and self . request . user . id >= <NUM_LIT:0> : <EOL> obj . modified_by = self . request . user <EOL> def post ( self , request , * args , ** kwargs ) : <EOL> self . object = self . get_object ( ) <EOL> self . pre_delete ( self . object ) <EOL> redirect_url = self . get_redirect_url ( ) <EOL> self . object . delete ( ) <EOL> return HttpResponseRedirect ( redirect_url ) <EOL> def get_redirect_url ( self , ** kwargs ) : <EOL> if not self . redirect_url : <EOL> raise ImproperlyConfigured ( "<STR_LIT>" ) <EOL> return smart_url ( self . redirect_url ) <EOL> def get_context_data ( self , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> context = super ( SmartDeleteView , self ) . get_context_data ( ** kwargs ) <EOL> context [ '<STR_LIT>' ] = self . name_field <EOL> context [ '<STR_LIT>' ] = self . get_cancel_url ( ) <EOL> return context <EOL> class SmartListView ( SmartView , ListView ) : <EOL> default_template = '<STR_LIT>' <EOL> link_url = None <EOL> link_fields = None <EOL> add_button = None <EOL> search_fields = None <EOL> paginate_by = <NUM_LIT> <EOL> field_config = { '<STR_LIT>' : dict ( label = '<STR_LIT>' ) , } <EOL> default_order = None <EOL> select_related = None <EOL> list_permission = None <EOL> @ classmethod <EOL> def derive_url_pattern ( cls , path , action ) : <EOL> if action == '<STR_LIT:list>' : <EOL> return r'<STR_LIT>' % ( path ) <EOL> else : <EOL> return r'<STR_LIT>' % ( path , action ) <EOL> def derive_search_fields ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . search_fields <EOL> def derive_title ( self ) : <EOL> """<STR_LIT>""" <EOL> title = super ( SmartListView , self ) . derive_title ( ) <EOL> if not title : <EOL> return force_text ( self . model . _meta . verbose_name_plural ) . title ( ) <EOL> else : <EOL> return title <EOL> def derive_link_fields ( self , context ) : <EOL> """<STR_LIT>""" <EOL> if not self . link_fields is None : <EOL> return self . link_fields <EOL> else : <EOL> link_fields = set ( ) <EOL> if self . fields : <EOL> for field in self . fields : <EOL> if field != '<STR_LIT>' : <EOL> link_fields . add ( field ) <EOL> break <EOL> return link_fields <EOL> def lookup_field_link ( self , context , field , obj ) : <EOL> """<STR_LIT>""" <EOL> return smart_url ( self . link_url , obj ) <EOL> def lookup_field_orderable ( self , field ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> self . model . _meta . get_field_by_name ( field ) <EOL> return True <EOL> except : <EOL> return False <EOL> def get_context_data ( self , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> context = super ( SmartListView , self ) . get_context_data ( ** kwargs ) <EOL> self . link_fields = self . derive_link_fields ( context ) <EOL> context [ '<STR_LIT>' ] = self . link_fields <EOL> if '<STR_LIT>' in self . request . GET : <EOL> context [ '<STR_LIT>' ] = self . request . GET [ '<STR_LIT>' ] <EOL> order = self . derive_ordering ( ) <EOL> if order : <EOL> if order [ <NUM_LIT:0> ] == '<STR_LIT:->' : <EOL> context [ '<STR_LIT>' ] = order [ <NUM_LIT:1> : ] <EOL> context [ '<STR_LIT>' ] = False <EOL> else : <EOL> context [ '<STR_LIT>' ] = order <EOL> context [ '<STR_LIT>' ] = True <EOL> return context <EOL> def derive_select_related ( self ) : <EOL> return self . select_related <EOL> def derive_queryset ( self , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> queryset = super ( SmartListView , self ) . get_queryset ( ** kwargs ) <EOL> search_fields = self . derive_search_fields ( ) <EOL> if search_fields and '<STR_LIT>' in self . request . GET : <EOL> terms = self . request . GET [ '<STR_LIT>' ] . split ( ) <EOL> query = Q ( pk__gt = <NUM_LIT:0> ) <EOL> for term in terms : <EOL> term_query = Q ( pk__lt = <NUM_LIT:0> ) <EOL> for field in search_fields : <EOL> term_query |= Q ( ** { field : term } ) <EOL> query &= term_query <EOL> queryset = queryset . filter ( query ) <EOL> related = self . derive_select_related ( ) <EOL> if related : <EOL> queryset = queryset . select_related ( * related ) <EOL> return queryset <EOL> def get_queryset ( self , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> queryset = self . derive_queryset ( ** kwargs ) <EOL> if self . list_permission : <EOL> if not self . request . user . has_perm ( self . list_permission ) : <EOL> user = self . request . user <EOL> if settings . ANONYMOUS_USER_ID and user . is_anonymous ( ) : <EOL> user = get_user_model ( ) . objects . get ( pk = settings . ANONYMOUS_USER_ID ) <EOL> queryset = queryset . filter ( id__in = get_objects_for_user ( user , self . list_permission ) ) <EOL> return self . order_queryset ( queryset ) <EOL> def derive_ordering ( self ) : <EOL> """<STR_LIT>""" <EOL> if '<STR_LIT>' in self . request . GET : <EOL> return self . request . GET [ '<STR_LIT>' ] <EOL> elif self . default_order : <EOL> return self . default_order <EOL> else : <EOL> return None <EOL> def order_queryset ( self , queryset ) : <EOL> """<STR_LIT>""" <EOL> order = self . derive_ordering ( ) <EOL> if '<STR_LIT>' in self . request . GET : <EOL> if order not in self . derive_fields ( ) : <EOL> order = None <EOL> if order : <EOL> if isinstance ( order , six . string_types ) : <EOL> order = ( order , ) <EOL> queryset = queryset . order_by ( * order ) <EOL> return queryset <EOL> def derive_fields ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . fields : <EOL> return self . fields <EOL> else : <EOL> fields = [ ] <EOL> for field in self . object_list . model . _meta . fields : <EOL> if field . name != '<STR_LIT:id>' : <EOL> fields . append ( field . name ) <EOL> return fields <EOL> def get_is_active ( self , obj ) : <EOL> """<STR_LIT>""" <EOL> if obj . is_active : <EOL> return '<STR_LIT>' <EOL> else : <EOL> return '<STR_LIT>' <EOL> def render_to_response ( self , context , ** response_kwargs ) : <EOL> """<STR_LIT>""" <EOL> if self . request . GET . get ( '<STR_LIT>' , '<STR_LIT:html>' ) == '<STR_LIT>' : <EOL> results = [ ] <EOL> for obj in context [ '<STR_LIT>' ] : <EOL> result = None <EOL> if hasattr ( obj , '<STR_LIT>' ) : <EOL> result = obj . as_select2 ( ) <EOL> if not result : <EOL> result = dict ( id = obj . pk , text = "<STR_LIT:%s>" % obj ) <EOL> results . append ( result ) <EOL> json_data = dict ( results = results , err = '<STR_LIT>' , more = context [ '<STR_LIT>' ] . has_next ( ) ) <EOL> return HttpResponse ( json . dumps ( json_data ) , content_type = '<STR_LIT>' ) <EOL> else : <EOL> return super ( SmartListView , self ) . render_to_response ( context ) <EOL> class SmartCsvView ( SmartListView ) : <EOL> def derive_filename ( self ) : <EOL> filename = getattr ( self , '<STR_LIT:filename>' , None ) <EOL> if not filename : <EOL> filename = "<STR_LIT>" % self . model . _meta . verbose_name . lower ( ) <EOL> return filename <EOL> def render_to_response ( self , context , ** response_kwargs ) : <EOL> import csv <EOL> response = HttpResponse ( content_type = '<STR_LIT>' ) <EOL> response [ '<STR_LIT>' ] = '<STR_LIT>' % self . derive_filename ( ) <EOL> writer = csv . writer ( response , quoting = csv . QUOTE_ALL ) <EOL> fields = self . derive_fields ( ) <EOL> header = [ ] <EOL> for field in fields : <EOL> header . append ( six . text_type ( self . lookup_field_label ( dict ( ) , field ) ) ) <EOL> writer . writerow ( [ s . encode ( "<STR_LIT:utf-8>" ) for s in header ] ) <EOL> for obj in self . object_list : <EOL> row = [ ] <EOL> for field in fields : <EOL> row . append ( six . text_type ( self . lookup_field_value ( dict ( ) , obj , field ) ) ) <EOL> writer . writerow ( [ s . encode ( "<STR_LIT:utf-8>" ) for s in row ] ) <EOL> return response <EOL> class SmartXlsView ( SmartListView ) : <EOL> def derive_filename ( self ) : <EOL> filename = getattr ( self , '<STR_LIT:filename>' , None ) <EOL> if not filename : <EOL> filename = "<STR_LIT>" % self . model . _meta . verbose_name . lower ( ) <EOL> return filename <EOL> def render_to_response ( self , context , ** response_kwargs ) : <EOL> from xlwt import Workbook <EOL> book = Workbook ( ) <EOL> sheet1 = book . add_sheet ( self . derive_title ( ) ) <EOL> fields = self . derive_fields ( ) <EOL> for col in range ( len ( fields ) ) : <EOL> field = fields [ col ] <EOL> sheet1 . write ( <NUM_LIT:0> , col , six . text_type ( self . lookup_field_label ( dict ( ) , field ) ) ) <EOL> for row in range ( len ( self . object_list ) ) : <EOL> obj = self . object_list [ row ] <EOL> for col in range ( len ( fields ) ) : <EOL> field = fields [ col ] <EOL> value = six . text_type ( self . lookup_field_value ( dict ( ) , obj , field ) ) <EOL> sheet1 . write ( row + <NUM_LIT:1> , col , value ) <EOL> response = HttpResponse ( content_type = '<STR_LIT>' ) <EOL> response [ '<STR_LIT>' ] = '<STR_LIT>' % self . derive_filename ( ) <EOL> book . save ( response ) <EOL> return response <EOL> class SmartFormMixin ( object ) : <EOL> readonly = ( ) <EOL> field_config = { '<STR_LIT>' : dict ( label = "<STR_LIT>" ) , <EOL> '<STR_LIT>' : dict ( label = "<STR_LIT>" ) } <EOL> success_message = None <EOL> submit_button_name = _ ( "<STR_LIT>" ) <EOL> def derive_title ( self ) : <EOL> """<STR_LIT>""" <EOL> if not self . title : <EOL> return _ ( "<STR_LIT>" ) <EOL> else : <EOL> return self . title <EOL> def derive_success_message ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . success_message <EOL> def get_form ( self , form_class = None ) : <EOL> """<STR_LIT>""" <EOL> self . form = super ( SmartFormMixin , self ) . get_form ( form_class ) <EOL> fields = list ( self . derive_fields ( ) ) <EOL> exclude = self . derive_exclude ( ) <EOL> exclude += self . derive_readonly ( ) <EOL> for field in exclude : <EOL> if field in self . form . fields : <EOL> del self . form . fields [ field ] <EOL> if fields is not None : <EOL> for name , field in self . form . fields . items ( ) : <EOL> if not name in fields : <EOL> del self . form . fields [ name ] <EOL> location = forms . CharField ( widget = forms . widgets . HiddenInput ( ) , required = False ) <EOL> if ( '<STR_LIT>' in self . request . META ) : <EOL> location . initial = self . request . META [ '<STR_LIT>' ] <EOL> self . form . fields [ '<STR_LIT>' ] = location <EOL> if fields : <EOL> fields . append ( '<STR_LIT>' ) <EOL> for ( name , field ) in self . form . fields . items ( ) : <EOL> field = self . customize_form_field ( name , field ) <EOL> self . form . fields [ name ] = field <EOL> return self . form <EOL> def customize_form_field ( self , name , field ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( field , forms . fields . DateField ) and isinstance ( field . widget , forms . widgets . DateInput ) : <EOL> field . widget = widgets . DatePickerWidget ( ) <EOL> if isinstance ( field , forms . fields . ImageField ) and isinstance ( field . widget , forms . widgets . ClearableFileInput ) : <EOL> field . widget = widgets . ImageThumbnailWidget ( ) <EOL> return field <EOL> def lookup_field_label ( self , context , field , default = None ) : <EOL> """<STR_LIT>""" <EOL> default = None <EOL> for form_field in self . form : <EOL> if form_field . name == field : <EOL> default = form_field . label <EOL> break <EOL> return super ( SmartFormMixin , self ) . lookup_field_label ( context , field , default = default ) <EOL> def lookup_field_help ( self , field , default = None ) : <EOL> """<STR_LIT>""" <EOL> default = None <EOL> for form_field in self . form : <EOL> if form_field . name == field : <EOL> default = form_field . help_text <EOL> break <EOL> return super ( SmartFormMixin , self ) . lookup_field_help ( field , default = default ) <EOL> def derive_readonly ( self ) : <EOL> """<STR_LIT>""" <EOL> readonly = list ( self . readonly ) <EOL> for key , value in self . field_config . items ( ) : <EOL> if '<STR_LIT>' in value and value [ '<STR_LIT>' ] : <EOL> readonly . append ( key ) <EOL> return readonly <EOL> def derive_fields ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . fields is not None : <EOL> fields = list ( self . fields ) <EOL> else : <EOL> form = self . form <EOL> fields = [ ] <EOL> for field in form : <EOL> fields . append ( field . name ) <EOL> readonly = self . derive_readonly ( ) <EOL> if readonly : <EOL> fields += readonly <EOL> for exclude in self . derive_exclude ( ) : <EOL> if exclude in fields : <EOL> fields . remove ( exclude ) <EOL> return fields <EOL> def get_form_class ( self ) : <EOL> """<STR_LIT>""" <EOL> form_class = None <EOL> if self . form_class : <EOL> form_class = self . form_class <EOL> else : <EOL> if self . model is not None : <EOL> model = self . model <EOL> elif hasattr ( self , '<STR_LIT:object>' ) and self . object is not None : <EOL> model = self . object . __class__ <EOL> else : <EOL> model = self . get_queryset ( ) . model <EOL> factory_kwargs = self . get_factory_kwargs ( ) <EOL> form_class = model_forms . modelform_factory ( model , ** factory_kwargs ) <EOL> return form_class <EOL> def get_factory_kwargs ( self ) : <EOL> """<STR_LIT>""" <EOL> params = dict ( ) <EOL> exclude = self . derive_exclude ( ) <EOL> exclude += self . derive_readonly ( ) <EOL> if self . fields : <EOL> fields = list ( self . fields ) <EOL> for ex in exclude : <EOL> if ex in fields : <EOL> fields . remove ( ex ) <EOL> params [ '<STR_LIT>' ] = fields <EOL> if exclude : <EOL> params [ '<STR_LIT>' ] = exclude <EOL> return params <EOL> def get_success_url ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . success_url : <EOL> if self . success_url . find ( '<STR_LIT:@>' ) > <NUM_LIT:0> : <EOL> return smart_url ( self . success_url , self . object ) <EOL> else : <EOL> return smart_url ( self . success_url , None ) <EOL> elif '<STR_LIT>' in self . form . cleaned_data : <EOL> return self . form . cleaned_data [ '<STR_LIT>' ] <EOL> raise ImproperlyConfigured ( "<STR_LIT>" ) <EOL> def derive_initial ( self ) : <EOL> """<STR_LIT>""" <EOL> return dict ( ) <EOL> def get_form_kwargs ( self ) : <EOL> """<STR_LIT>""" <EOL> kwargs = super ( SmartFormMixin , self ) . get_form_kwargs ( ) <EOL> kwargs [ '<STR_LIT>' ] = self . derive_initial ( ) <EOL> return kwargs <EOL> def derive_submit_button_name ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . submit_button_name <EOL> def get_context_data ( self , ** kwargs ) : <EOL> context = super ( SmartFormMixin , self ) . get_context_data ( ** kwargs ) <EOL> context [ '<STR_LIT>' ] = self . derive_submit_button_name ( ) <EOL> return context <EOL> class SmartFormView ( SmartFormMixin , SmartView , FormView ) : <EOL> default_template = '<STR_LIT>' <EOL> def form_valid ( self , form ) : <EOL> messages . success ( self . request , self . derive_success_message ( ) ) <EOL> return super ( SmartFormView , self ) . form_valid ( form ) <EOL> class SmartModelFormView ( SmartFormMixin , SmartView , ModelFormMixin ) : <EOL> grant_permissions = None <EOL> javascript_submit = None <EOL> field_config = { '<STR_LIT>' : dict ( label = "<STR_LIT>" ) , <EOL> '<STR_LIT>' : dict ( label = "<STR_LIT>" ) } <EOL> def derive_title ( self ) : <EOL> """<STR_LIT>""" <EOL> if not self . title : <EOL> return _ ( "<STR_LIT>" ) % force_text ( self . model . _meta . verbose_name ) . title ( ) <EOL> else : <EOL> return self . title <EOL> def pre_save ( self , obj ) : <EOL> """<STR_LIT>""" <EOL> return obj <EOL> def save ( self , obj ) : <EOL> """<STR_LIT>""" <EOL> self . object . save ( ) <EOL> self . save_m2m ( ) <EOL> def form_valid ( self , form ) : <EOL> self . object = form . save ( commit = False ) <EOL> try : <EOL> self . object = self . pre_save ( self . object ) <EOL> self . save ( self . object ) <EOL> self . object = self . post_save ( self . object ) <EOL> messages . success ( self . request , self . derive_success_message ( ) ) <EOL> if '<STR_LIT>' not in self . request . META : <EOL> return HttpResponseRedirect ( self . get_success_url ( ) ) <EOL> else : <EOL> response = self . render_to_response ( self . get_context_data ( form = form ) ) <EOL> response [ '<STR_LIT>' ] = self . get_success_url ( ) <EOL> return response <EOL> except IntegrityError as e : <EOL> message = str ( e ) . capitalize ( ) <EOL> errors = self . form . _errors . setdefault ( forms . forms . NON_FIELD_ERRORS , forms . utils . ErrorList ( ) ) <EOL> errors . append ( message ) <EOL> return self . render_to_response ( self . get_context_data ( form = form ) ) <EOL> def save_m2m ( self ) : <EOL> """<STR_LIT>""" <EOL> self . form . save_m2m ( ) <EOL> def post_save ( self , obj ) : <EOL> """<STR_LIT>""" <EOL> if self . grant_permissions : <EOL> for permission in self . grant_permissions : <EOL> if not self . request . user . has_perm ( permission ) : <EOL> assign_perm ( permission , self . request . user , self . object ) <EOL> return obj <EOL> def get_context_data ( self , ** kwargs ) : <EOL> context = super ( SmartModelFormView , self ) . get_context_data ( ** kwargs ) <EOL> context [ '<STR_LIT>' ] = self . javascript_submit <EOL> return context <EOL> class SmartUpdateView ( SmartModelFormView , UpdateView ) : <EOL> default_template = '<STR_LIT>' <EOL> exclude = ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> submit_button_name = _ ( "<STR_LIT>" ) <EOL> delete_url = None <EOL> def derive_queryset ( self ) : <EOL> return super ( SmartUpdateView , self ) . get_queryset ( ) <EOL> def get_queryset ( self ) : <EOL> self . queryset = self . derive_queryset ( ) <EOL> return self . queryset <EOL> @ classmethod <EOL> def derive_url_pattern ( cls , path , action ) : <EOL> """<STR_LIT>""" <EOL> return r'<STR_LIT>' % ( path , action ) <EOL> def derive_success_message ( self ) : <EOL> if self . success_message is None : <EOL> return "<STR_LIT>" % self . model . _meta . verbose_name <EOL> else : <EOL> return self . success_message <EOL> def pre_save ( self , obj ) : <EOL> if hasattr ( obj , '<STR_LIT>' ) and self . request . user . id >= <NUM_LIT:0> : <EOL> obj . modified_by = self . request . user <EOL> return obj <EOL> def get_context_data ( self , ** kwargs ) : <EOL> context = super ( SmartUpdateView , self ) . get_context_data ( ** kwargs ) <EOL> if self . delete_url : <EOL> context [ '<STR_LIT>' ] = smart_url ( self . delete_url , self . object ) <EOL> return context <EOL> def get_modified_blurb ( self , obj ) : <EOL> return "<STR_LIT>" % ( obj . modified_on . strftime ( "<STR_LIT>" ) , obj . modified_by ) <EOL> def get_created_blurb ( self , obj ) : <EOL> return "<STR_LIT>" % ( obj . created_on . strftime ( "<STR_LIT>" ) , obj . created_by ) <EOL> class SmartMultiFormView ( SmartView , TemplateView ) : <EOL> default_template = '<STR_LIT>' <EOL> forms = { } <EOL> delete_url = None <EOL> def get ( self , request , * args , ** kwargs ) : <EOL> context = self . get_context_data ( ** kwargs ) <EOL> page_forms = [ ] <EOL> for prefix , form in self . forms . items ( ) : <EOL> f = form ( prefix = prefix ) <EOL> page_forms . append ( f ) <EOL> context [ '<STR_LIT>' ] = page_forms <EOL> return self . render_to_response ( context ) <EOL> def post ( self , request , * args , ** kwargs ) : <EOL> context = self . get_context_data ( ** kwargs ) <EOL> page_forms = [ ] <EOL> valid = True <EOL> for prefix , form in self . forms . items ( ) : <EOL> f = form ( request . POST , prefix = prefix ) <EOL> valid = valid and f . is_valid ( ) <EOL> page_forms . append ( f ) <EOL> if not valid : <EOL> context [ '<STR_LIT>' ] = page_forms <EOL> return self . render_to_response ( context ) <EOL> else : <EOL> pass <EOL> def get_context_data ( self , ** kwargs ) : <EOL> context = super ( SmartMultiFormView , self ) . get_context_data ( ** kwargs ) <EOL> if self . delete_url : <EOL> context [ '<STR_LIT>' ] = smart_url ( self . delete_url , self . object ) <EOL> return context <EOL> class SmartCreateView ( SmartModelFormView , CreateView ) : <EOL> default_template = '<STR_LIT>' <EOL> exclude = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> submit_button_name = _ ( "<STR_LIT>" ) <EOL> def pre_save ( self , obj ) : <EOL> if hasattr ( obj , '<STR_LIT>' ) and self . request . user . id >= <NUM_LIT:0> : <EOL> obj . created_by = self . request . user <EOL> if hasattr ( obj , '<STR_LIT>' ) and self . request . user . id >= <NUM_LIT:0> : <EOL> obj . modified_by = self . request . user <EOL> return obj <EOL> def derive_success_message ( self ) : <EOL> if self . success_message is None : <EOL> return _ ( "<STR_LIT>" ) % self . model . _meta . verbose_name <EOL> else : <EOL> return self . success_message <EOL> def derive_title ( self ) : <EOL> """<STR_LIT>""" <EOL> if not self . title : <EOL> return _ ( "<STR_LIT>" ) % force_text ( self . model . _meta . verbose_name ) . title ( ) <EOL> else : <EOL> return self . title <EOL> class SmartCSVImportView ( SmartCreateView ) : <EOL> success_url = '<STR_LIT>' <EOL> fields = ( '<STR_LIT>' , ) <EOL> def derive_title ( self ) : <EOL> return _ ( "<STR_LIT>" ) % self . crudl . model . _meta . verbose_name_plural . title ( ) <EOL> def pre_save ( self , obj ) : <EOL> obj = super ( SmartCSVImportView , self ) . pre_save ( obj ) <EOL> obj . model_class = "<STR_LIT>" % ( self . crudl . model . __module__ , self . crudl . model . __name__ ) <EOL> return obj <EOL> def post_save ( self , task ) : <EOL> task = super ( SmartCSVImportView , self ) . post_save ( task ) <EOL> task . import_params = json . dumps ( self . form . data ) <EOL> task . start ( ) <EOL> return task <EOL> class SmartCRUDL ( object ) : <EOL> actions = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:list>' ) <EOL> model_name = None <EOL> app_name = None <EOL> module_name = None <EOL> path = None <EOL> permissions = True <EOL> def __init__ ( self , model = None , path = None , actions = None ) : <EOL> if model : <EOL> self . model = model <EOL> if not self . model_name : <EOL> self . model_name = self . model . _meta . object_name <EOL> if not self . app_name : <EOL> self . app_name = self . model . _meta . app_label <EOL> if not path and not self . path : <EOL> self . path = self . model_name . lower ( ) <EOL> if not self . module_name : <EOL> parts = self . __class__ . __module__ . split ( "<STR_LIT:.>" ) <EOL> self . module_name = parts [ - <NUM_LIT:2> ] <EOL> if self . module_name == '<STR_LIT>' and len ( parts ) >= <NUM_LIT:3> : <EOL> self . module_name = parts [ - <NUM_LIT:3> ] <EOL> if actions : <EOL> self . actions = actions <EOL> def permission_for_action ( self , action ) : <EOL> """<STR_LIT>""" <EOL> return "<STR_LIT>" % ( self . app_name . lower ( ) , self . model_name . lower ( ) , action ) <EOL> def template_for_action ( self , action ) : <EOL> """<STR_LIT>""" <EOL> return "<STR_LIT>" % ( self . module_name . lower ( ) , self . model_name . lower ( ) , action ) <EOL> def url_name_for_action ( self , action ) : <EOL> """<STR_LIT>""" <EOL> return "<STR_LIT>" % ( self . module_name . lower ( ) , self . model_name . lower ( ) , action ) <EOL> def view_for_action ( self , action ) : <EOL> """<STR_LIT>""" <EOL> class_name = "<STR_LIT>" . join ( [ word . capitalize ( ) for word in action . split ( "<STR_LIT:_>" ) ] ) <EOL> view = None <EOL> if hasattr ( self , class_name ) : <EOL> view = getattr ( self , class_name ) <EOL> if not getattr ( view , '<STR_LIT>' , None ) : <EOL> view . model = self . model <EOL> if not hasattr ( view , '<STR_LIT>' ) and self . permissions : <EOL> view . permission = self . permission_for_action ( action ) <EOL> if not getattr ( view , '<STR_LIT>' , None ) : <EOL> if '<STR_LIT>' in self . actions : <EOL> view . link_url = '<STR_LIT>' % self . url_name_for_action ( '<STR_LIT>' ) <EOL> elif '<STR_LIT>' in self . actions : <EOL> view . link_url = '<STR_LIT>' % self . url_name_for_action ( '<STR_LIT>' ) <EOL> if not getattr ( view , '<STR_LIT>' , None ) and '<STR_LIT>' not in view . __dict__ : <EOL> view . link_fields = ( ) <EOL> if action == '<STR_LIT:list>' and getattr ( view , '<STR_LIT>' , None ) is None : <EOL> view . add_button = '<STR_LIT>' in self . actions <EOL> if action == '<STR_LIT>' and getattr ( view , '<STR_LIT>' , None ) is None : <EOL> view . edit_button = '<STR_LIT>' in self . actions <EOL> if not getattr ( view , '<STR_LIT>' , None ) and ( action == '<STR_LIT>' or action == '<STR_LIT>' ) : <EOL> view . success_url = '<STR_LIT>' % self . url_name_for_action ( '<STR_LIT:list>' ) <EOL> else : <EOL> options = dict ( model = self . model ) <EOL> if action == '<STR_LIT>' or action == '<STR_LIT>' and '<STR_LIT:list>' in self . actions : <EOL> options [ '<STR_LIT>' ] = '<STR_LIT>' % self . url_name_for_action ( '<STR_LIT:list>' ) <EOL> if self . permissions : <EOL> options [ '<STR_LIT>' ] = self . permission_for_action ( action ) <EOL> if action == '<STR_LIT>' : <EOL> view = type ( str ( "<STR_LIT>" % self . model_name ) , ( SmartCreateView , ) , options ) <EOL> elif action == '<STR_LIT>' : <EOL> if '<STR_LIT>' in self . actions : <EOL> options [ '<STR_LIT>' ] = True <EOL> view = type ( str ( "<STR_LIT>" % self . model_name ) , ( SmartReadView , ) , options ) <EOL> elif action == '<STR_LIT>' : <EOL> if '<STR_LIT>' in self . actions : <EOL> options [ '<STR_LIT>' ] = '<STR_LIT>' % self . url_name_for_action ( '<STR_LIT>' ) <EOL> view = type ( str ( "<STR_LIT>" % self . model_name ) , ( SmartUpdateView , ) , options ) <EOL> elif action == '<STR_LIT>' : <EOL> if '<STR_LIT:list>' in self . actions : <EOL> options [ '<STR_LIT>' ] = '<STR_LIT>' % self . url_name_for_action ( '<STR_LIT:list>' ) <EOL> options [ '<STR_LIT>' ] = '<STR_LIT>' % self . url_name_for_action ( '<STR_LIT:list>' ) <EOL> elif '<STR_LIT>' in self . actions : <EOL> options [ '<STR_LIT>' ] = '<STR_LIT>' % self . url_name_for_action ( '<STR_LIT>' ) <EOL> view = type ( str ( "<STR_LIT>" % self . model_name ) , ( SmartDeleteView , ) , options ) <EOL> elif action == '<STR_LIT:list>' : <EOL> if '<STR_LIT>' in self . actions : <EOL> options [ '<STR_LIT>' ] = '<STR_LIT>' % self . url_name_for_action ( '<STR_LIT>' ) <EOL> elif '<STR_LIT>' in self . actions : <EOL> options [ '<STR_LIT>' ] = '<STR_LIT>' % self . url_name_for_action ( '<STR_LIT>' ) <EOL> else : <EOL> options [ '<STR_LIT>' ] = ( ) <EOL> if '<STR_LIT>' in self . actions : <EOL> options [ '<STR_LIT>' ] = True <EOL> view = type ( str ( "<STR_LIT>" % self . model_name ) , ( SmartListView , ) , options ) <EOL> elif action == '<STR_LIT>' : <EOL> options [ '<STR_LIT>' ] = ImportTask <EOL> view = type ( str ( "<STR_LIT>" % self . model_name ) , ( SmartCSVImportView , ) , options ) <EOL> if not view : <EOL> raise Exception ( "<STR_LIT>" % action ) <EOL> view . url_name = self . url_name_for_action ( action ) <EOL> if not getattr ( view , '<STR_LIT>' , None ) : <EOL> view . template_name = self . template_for_action ( action ) <EOL> view . crudl = self <EOL> return view <EOL> def pattern_for_view ( self , view , action ) : <EOL> """<STR_LIT>""" <EOL> if getattr ( view , '<STR_LIT>' , None ) : <EOL> return view . derive_url_pattern ( self . path , action ) <EOL> else : <EOL> return r'<STR_LIT>' % ( self . path , action ) <EOL> def as_urlpatterns ( self ) : <EOL> """<STR_LIT>""" <EOL> urlpatterns = patterns ( '<STR_LIT>' ) <EOL> for action in self . actions : <EOL> view_class = self . view_for_action ( action ) <EOL> view_pattern = self . pattern_for_view ( view_class , action ) <EOL> name = self . url_name_for_action ( action ) <EOL> urlpatterns += patterns ( '<STR_LIT>' , url ( view_pattern , view_class . as_view ( ) , name = name ) ) <EOL> return urlpatterns </s>
