<s> import re <EOL> from django import http , template <EOL> from django . contrib . admin import ModelAdmin <EOL> from django . contrib . admin import actions <EOL> from django . contrib . auth import authenticate , login <EOL> from django . views . decorators . csrf import csrf_protect <EOL> from django . db . models . base import ModelBase <EOL> from django . core . exceptions import ImproperlyConfigured <EOL> from django . core . urlresolvers import reverse <EOL> from django . shortcuts import render_to_response <EOL> from django . utils . functional import update_wrapper <EOL> from django . utils . safestring import mark_safe <EOL> from django . utils . text import capfirst <EOL> from django . utils . translation import ugettext_lazy , ugettext as _ <EOL> from django . views . decorators . cache import never_cache <EOL> from django . conf import settings <EOL> ERROR_MESSAGE = ugettext_lazy ( "<STR_LIT>" ) <EOL> LOGIN_FORM_KEY = '<STR_LIT>' <EOL> class AlreadyRegistered ( Exception ) : <EOL> pass <EOL> class NotRegistered ( Exception ) : <EOL> pass <EOL> class AdminSite ( object ) : <EOL> """<STR_LIT>""" <EOL> index_template = None <EOL> app_index_template = None <EOL> login_template = None <EOL> logout_template = None <EOL> password_change_template = None <EOL> password_change_done_template = None <EOL> def __init__ ( self , name = None , app_name = '<STR_LIT>' ) : <EOL> self . _registry = { } <EOL> self . root_path = None <EOL> if name is None : <EOL> self . name = '<STR_LIT>' <EOL> else : <EOL> self . name = name <EOL> self . app_name = app_name <EOL> self . _actions = { '<STR_LIT>' : actions . delete_selected } <EOL> self . _global_actions = self . _actions . copy ( ) <EOL> def register ( self , model_or_iterable , admin_class = None , ** options ) : <EOL> """<STR_LIT>""" <EOL> if not admin_class : <EOL> admin_class = ModelAdmin <EOL> if admin_class and settings . DEBUG : <EOL> from django . contrib . admin . validation import validate <EOL> else : <EOL> validate = lambda model , adminclass : None <EOL> if isinstance ( model_or_iterable , ModelBase ) : <EOL> model_or_iterable = [ model_or_iterable ] <EOL> for model in model_or_iterable : <EOL> if model in self . _registry : <EOL> raise AlreadyRegistered ( '<STR_LIT>' % model . __name__ ) <EOL> if options : <EOL> options [ '<STR_LIT>' ] = __name__ <EOL> admin_class = type ( "<STR_LIT>" % model . __name__ , ( admin_class , ) , options ) <EOL> validate ( admin_class , model ) <EOL> self . _registry [ model ] = admin_class ( model , self ) <EOL> def unregister ( self , model_or_iterable ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( model_or_iterable , ModelBase ) : <EOL> model_or_iterable = [ model_or_iterable ] <EOL> for model in model_or_iterable : <EOL> if model not in self . _registry : <EOL> raise NotRegistered ( '<STR_LIT>' % model . __name__ ) <EOL> del self . _registry [ model ] <EOL> def add_action ( self , action , name = None ) : <EOL> """<STR_LIT>""" <EOL> name = name or action . __name__ <EOL> self . _actions [ name ] = action <EOL> self . _global_actions [ name ] = action <EOL> def disable_action ( self , name ) : <EOL> """<STR_LIT>""" <EOL> del self . _actions [ name ] <EOL> def get_action ( self , name ) : <EOL> """<STR_LIT>""" <EOL> return self . _global_actions [ name ] <EOL> def actions ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _actions . iteritems ( ) <EOL> actions = property ( actions ) <EOL> def has_permission ( self , request ) : <EOL> """<STR_LIT>""" <EOL> return request . user . is_active and request . user . is_staff <EOL> def check_dependencies ( self ) : <EOL> """<STR_LIT>""" <EOL> from django . contrib . admin . models import LogEntry <EOL> from django . contrib . contenttypes . models import ContentType <EOL> if not LogEntry . _meta . installed : <EOL> raise ImproperlyConfigured ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> if not ContentType . _meta . installed : <EOL> raise ImproperlyConfigured ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> if not ( '<STR_LIT>' in settings . TEMPLATE_CONTEXT_PROCESSORS or <EOL> '<STR_LIT>' in settings . TEMPLATE_CONTEXT_PROCESSORS ) : <EOL> raise ImproperlyConfigured ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> def admin_view ( self , view , cacheable = False ) : <EOL> """<STR_LIT>""" <EOL> def inner ( request , * args , ** kwargs ) : <EOL> if not self . has_permission ( request ) : <EOL> return self . login ( request ) <EOL> return view ( request , * args , ** kwargs ) <EOL> if not cacheable : <EOL> inner = never_cache ( inner ) <EOL> if not getattr ( view , '<STR_LIT>' , False ) : <EOL> inner = csrf_protect ( inner ) <EOL> return update_wrapper ( inner , view ) <EOL> def get_urls ( self ) : <EOL> from django . conf . urls . defaults import patterns , url , include <EOL> if settings . DEBUG : <EOL> self . check_dependencies ( ) <EOL> def wrap ( view , cacheable = False ) : <EOL> def wrapper ( * args , ** kwargs ) : <EOL> return self . admin_view ( view , cacheable ) ( * args , ** kwargs ) <EOL> return update_wrapper ( wrapper , view ) <EOL> urlpatterns = patterns ( '<STR_LIT>' , <EOL> url ( r'<STR_LIT>' , <EOL> wrap ( self . index ) , <EOL> name = '<STR_LIT:index>' ) , <EOL> url ( r'<STR_LIT>' , <EOL> wrap ( self . logout ) , <EOL> name = '<STR_LIT>' ) , <EOL> url ( r'<STR_LIT>' , <EOL> wrap ( self . password_change , cacheable = True ) , <EOL> name = '<STR_LIT>' ) , <EOL> url ( r'<STR_LIT>' , <EOL> wrap ( self . password_change_done , cacheable = True ) , <EOL> name = '<STR_LIT>' ) , <EOL> url ( r'<STR_LIT>' , <EOL> wrap ( self . i18n_javascript , cacheable = True ) , <EOL> name = '<STR_LIT>' ) , <EOL> url ( r'<STR_LIT>' , <EOL> '<STR_LIT>' ) , <EOL> url ( r'<STR_LIT>' , <EOL> wrap ( self . app_index ) , <EOL> name = '<STR_LIT>' ) <EOL> ) <EOL> for model , model_admin in self . _registry . iteritems ( ) : <EOL> urlpatterns += patterns ( '<STR_LIT>' , <EOL> url ( r'<STR_LIT>' % ( model . _meta . app_label , model . _meta . module_name ) , <EOL> include ( model_admin . urls ) ) <EOL> ) <EOL> return urlpatterns <EOL> def urls ( self ) : <EOL> return self . get_urls ( ) , self . app_name , self . name <EOL> urls = property ( urls ) <EOL> def password_change ( self , request ) : <EOL> """<STR_LIT>""" <EOL> from django . contrib . auth . views import password_change <EOL> if self . root_path is not None : <EOL> url = '<STR_LIT>' % self . root_path <EOL> else : <EOL> url = reverse ( '<STR_LIT>' , current_app = self . name ) <EOL> defaults = { <EOL> '<STR_LIT>' : url <EOL> } <EOL> if self . password_change_template is not None : <EOL> defaults [ '<STR_LIT>' ] = self . password_change_template <EOL> return password_change ( request , ** defaults ) <EOL> def password_change_done ( self , request ) : <EOL> """<STR_LIT>""" <EOL> from django . contrib . auth . views import password_change_done <EOL> defaults = { } <EOL> if self . password_change_done_template is not None : <EOL> defaults [ '<STR_LIT>' ] = self . password_change_done_template <EOL> return password_change_done ( request , ** defaults ) <EOL> def i18n_javascript ( self , request ) : <EOL> """<STR_LIT>""" <EOL> if settings . USE_I18N : <EOL> from django . views . i18n import javascript_catalog <EOL> else : <EOL> from django . views . i18n import null_javascript_catalog as javascript_catalog <EOL> return javascript_catalog ( request , packages = '<STR_LIT>' ) <EOL> def logout ( self , request ) : <EOL> """<STR_LIT>""" <EOL> from django . contrib . auth . views import logout <EOL> defaults = { } <EOL> if self . logout_template is not None : <EOL> defaults [ '<STR_LIT>' ] = self . logout_template <EOL> return logout ( request , ** defaults ) <EOL> logout = never_cache ( logout ) <EOL> def login ( self , request ) : <EOL> """<STR_LIT>""" <EOL> from django . contrib . auth . models import User <EOL> if not request . POST . has_key ( LOGIN_FORM_KEY ) : <EOL> if request . POST : <EOL> message = _ ( "<STR_LIT>" ) <EOL> else : <EOL> message = "<STR_LIT>" <EOL> return self . display_login_form ( request , message ) <EOL> if not request . session . test_cookie_worked ( ) : <EOL> message = _ ( "<STR_LIT>" ) <EOL> return self . display_login_form ( request , message ) <EOL> else : <EOL> request . session . delete_test_cookie ( ) <EOL> username = request . POST . get ( '<STR_LIT:username>' , None ) <EOL> password = request . POST . get ( '<STR_LIT:password>' , None ) <EOL> user = authenticate ( username = username , password = password ) <EOL> if user is None : <EOL> message = ERROR_MESSAGE <EOL> if username is not None and u'<STR_LIT:@>' in username : <EOL> try : <EOL> user = User . objects . get ( email = username ) <EOL> except ( User . DoesNotExist , User . MultipleObjectsReturned ) : <EOL> pass <EOL> else : <EOL> if user . check_password ( password ) : <EOL> message = _ ( "<STR_LIT>" <EOL> "<STR_LIT>" ) % user . username <EOL> return self . display_login_form ( request , message ) <EOL> else : <EOL> if user . is_active and user . is_staff : <EOL> login ( request , user ) <EOL> return http . HttpResponseRedirect ( request . get_full_path ( ) ) <EOL> else : <EOL> return self . display_login_form ( request , ERROR_MESSAGE ) <EOL> login = never_cache ( login ) <EOL> def index ( self , request , extra_context = None ) : <EOL> """<STR_LIT>""" <EOL> app_dict = { } <EOL> user = request . user <EOL> for model , model_admin in self . _registry . items ( ) : <EOL> app_label = model . _meta . app_label <EOL> has_module_perms = user . has_module_perms ( app_label ) <EOL> if has_module_perms : <EOL> perms = model_admin . get_model_perms ( request ) <EOL> if True in perms . values ( ) : <EOL> model_dict = { <EOL> '<STR_LIT:name>' : capfirst ( model . _meta . verbose_name_plural ) , <EOL> '<STR_LIT>' : mark_safe ( '<STR_LIT>' % ( app_label , model . __name__ . lower ( ) ) ) , <EOL> '<STR_LIT>' : perms , <EOL> } <EOL> if app_label in app_dict : <EOL> app_dict [ app_label ] [ '<STR_LIT>' ] . append ( model_dict ) <EOL> else : <EOL> app_dict [ app_label ] = { <EOL> '<STR_LIT:name>' : app_label . title ( ) , <EOL> '<STR_LIT>' : app_label + '<STR_LIT:/>' , <EOL> '<STR_LIT>' : has_module_perms , <EOL> '<STR_LIT>' : [ model_dict ] , <EOL> } <EOL> app_list = app_dict . values ( ) <EOL> app_list . sort ( lambda x , y : cmp ( x [ '<STR_LIT:name>' ] , y [ '<STR_LIT:name>' ] ) ) <EOL> for app in app_list : <EOL> app [ '<STR_LIT>' ] . sort ( lambda x , y : cmp ( x [ '<STR_LIT:name>' ] , y [ '<STR_LIT:name>' ] ) ) <EOL> context = { <EOL> '<STR_LIT:title>' : _ ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : app_list , <EOL> '<STR_LIT>' : self . root_path , <EOL> } <EOL> context . update ( extra_context or { } ) <EOL> context_instance = template . RequestContext ( request , current_app = self . name ) <EOL> return render_to_response ( self . index_template or '<STR_LIT>' , context , <EOL> context_instance = context_instance <EOL> ) <EOL> index = never_cache ( index ) <EOL> def display_login_form ( self , request , error_message = '<STR_LIT>' , extra_context = None ) : <EOL> request . session . set_test_cookie ( ) <EOL> context = { <EOL> '<STR_LIT:title>' : _ ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : request . get_full_path ( ) , <EOL> '<STR_LIT>' : error_message , <EOL> '<STR_LIT>' : self . root_path , <EOL> } <EOL> context . update ( extra_context or { } ) <EOL> context_instance = template . RequestContext ( request , current_app = self . name ) <EOL> return render_to_response ( self . login_template or '<STR_LIT>' , context , <EOL> context_instance = context_instance <EOL> ) <EOL> def app_index ( self , request , app_label , extra_context = None ) : <EOL> user = request . user <EOL> has_module_perms = user . has_module_perms ( app_label ) <EOL> app_dict = { } <EOL> for model , model_admin in self . _registry . items ( ) : <EOL> if app_label == model . _meta . app_label : <EOL> if has_module_perms : <EOL> perms = model_admin . get_model_perms ( request ) <EOL> if True in perms . values ( ) : <EOL> model_dict = { <EOL> '<STR_LIT:name>' : capfirst ( model . _meta . verbose_name_plural ) , <EOL> '<STR_LIT>' : '<STR_LIT>' % model . __name__ . lower ( ) , <EOL> '<STR_LIT>' : perms , <EOL> } <EOL> if app_dict : <EOL> app_dict [ '<STR_LIT>' ] . append ( model_dict ) , <EOL> else : <EOL> app_dict = { <EOL> '<STR_LIT:name>' : app_label . title ( ) , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : has_module_perms , <EOL> '<STR_LIT>' : [ model_dict ] , <EOL> } <EOL> if not app_dict : <EOL> raise http . Http404 ( '<STR_LIT>' ) <EOL> app_dict [ '<STR_LIT>' ] . sort ( lambda x , y : cmp ( x [ '<STR_LIT:name>' ] , y [ '<STR_LIT:name>' ] ) ) <EOL> context = { <EOL> '<STR_LIT:title>' : _ ( '<STR_LIT>' ) % capfirst ( app_label ) , <EOL> '<STR_LIT>' : [ app_dict ] , <EOL> '<STR_LIT>' : self . root_path , <EOL> } <EOL> context . update ( extra_context or { } ) <EOL> context_instance = template . RequestContext ( request , current_app = self . name ) <EOL> return render_to_response ( self . app_index_template or ( '<STR_LIT>' % app_label , <EOL> '<STR_LIT>' ) , context , <EOL> context_instance = context_instance <EOL> ) <EOL> def root ( self , request , url ) : <EOL> """<STR_LIT>""" <EOL> import warnings <EOL> warnings . warn ( <EOL> "<STR_LIT>" , <EOL> DeprecationWarning <EOL> ) <EOL> if request . method == '<STR_LIT:GET>' and not request . path . endswith ( '<STR_LIT:/>' ) : <EOL> return http . HttpResponseRedirect ( request . path + '<STR_LIT:/>' ) <EOL> if settings . DEBUG : <EOL> self . check_dependencies ( ) <EOL> self . root_path = re . sub ( re . escape ( url ) + '<STR_LIT:$>' , '<STR_LIT>' , request . path ) <EOL> url = url . rstrip ( '<STR_LIT:/>' ) <EOL> if url == '<STR_LIT>' : <EOL> return self . logout ( request ) <EOL> if not self . has_permission ( request ) : <EOL> return self . login ( request ) <EOL> if url == '<STR_LIT>' : <EOL> return self . index ( request ) <EOL> elif url == '<STR_LIT>' : <EOL> return self . password_change ( request ) <EOL> elif url == '<STR_LIT>' : <EOL> return self . password_change_done ( request ) <EOL> elif url == '<STR_LIT>' : <EOL> return self . i18n_javascript ( request ) <EOL> elif url . startswith ( '<STR_LIT>' ) : <EOL> from django . contrib . contenttypes . views import shortcut <EOL> return shortcut ( request , * url . split ( '<STR_LIT:/>' ) [ <NUM_LIT:1> : ] ) <EOL> else : <EOL> if '<STR_LIT:/>' in url : <EOL> return self . model_page ( request , * url . split ( '<STR_LIT:/>' , <NUM_LIT:2> ) ) <EOL> else : <EOL> return self . app_index ( request , url ) <EOL> raise http . Http404 ( '<STR_LIT>' ) <EOL> def model_page ( self , request , app_label , model_name , rest_of_url = None ) : <EOL> """<STR_LIT>""" <EOL> from django . db import models <EOL> model = models . get_model ( app_label , model_name ) <EOL> if model is None : <EOL> raise http . Http404 ( "<STR_LIT>" % ( app_label , model_name ) ) <EOL> try : <EOL> admin_obj = self . _registry [ model ] <EOL> except KeyError : <EOL> raise http . Http404 ( "<STR_LIT>" ) <EOL> return admin_obj ( request , rest_of_url ) <EOL> model_page = never_cache ( model_page ) <EOL> site = AdminSite ( ) </s>
