<s> from django . core . context_processors import csrf <EOL> from django . core . urlresolvers import reverse <EOL> from django . http import HttpResponseRedirect , HttpResponse , HttpResponseNotModified , Http404 <EOL> from django . shortcuts import render_to_response <EOL> from django . template import RequestContext <EOL> from django . template . loader import render_to_string <EOL> from django . utils . datastructures import SortedDict <EOL> from django . utils . http import http_date <EOL> from django . views . decorators . cache import never_cache <EOL> from django . views . decorators . csrf import csrf_protect <EOL> from django . views . static import was_modified_since <EOL> try : <EOL> from django . utils . functional import update_wrapper <EOL> except ImportError : <EOL> from functools import update_wrapper <EOL> from nexus import conf <EOL> import mimetypes <EOL> import os <EOL> import os . path <EOL> import posixpath <EOL> import stat <EOL> import urllib <EOL> NEXUS_ROOT = os . path . normpath ( os . path . dirname ( __file__ ) ) <EOL> try : <EOL> from django . views . decorators . csrf import ensure_csrf_cookie <EOL> except ImportError : <EOL> from django . views . decorators . csrf import CsrfViewMiddleware <EOL> from django . middleware . csrf import get_token <EOL> from django . utils . decorators import decorator_from_middleware <EOL> class _EnsureCsrfCookie ( CsrfViewMiddleware ) : <EOL> def _reject ( self , request , reason ) : <EOL> return None <EOL> def process_view ( self , request , callback , callback_args , callback_kwargs ) : <EOL> retval = super ( _EnsureCsrfCookie , self ) . process_view ( request , callback , callback_args , callback_kwargs ) <EOL> get_token ( request ) <EOL> return retval <EOL> ensure_csrf_cookie = decorator_from_middleware ( _EnsureCsrfCookie ) <EOL> ensure_csrf_cookie . __name__ = '<STR_LIT>' <EOL> ensure_csrf_cookie . __doc__ = """<STR_LIT>""" <EOL> class NexusSite ( object ) : <EOL> def __init__ ( self , name = None , app_name = '<STR_LIT>' ) : <EOL> self . _registry = { } <EOL> self . _categories = SortedDict ( ) <EOL> if name is None : <EOL> self . name = '<STR_LIT>' <EOL> else : <EOL> self . name = name <EOL> self . app_name = app_name <EOL> def register_category ( self , category , label , index = None ) : <EOL> if index : <EOL> self . _categories . insert ( index , category , label ) <EOL> else : <EOL> self . _categories [ category ] = label <EOL> def register ( self , module , namespace = None , category = None ) : <EOL> module = module ( self , category ) <EOL> if not namespace : <EOL> namespace = module . get_namespace ( ) <EOL> if namespace : <EOL> module . app_name = module . name = namespace <EOL> self . _registry [ namespace ] = ( module , category ) <EOL> return module <EOL> def unregister ( self , namespace ) : <EOL> if namespace in self . _registry : <EOL> del self . _registry [ namespace ] <EOL> def get_urls ( self ) : <EOL> try : <EOL> from django . conf . urls import patterns , url , include <EOL> except ImportError : <EOL> from django . conf . urls . defaults import patterns , url , include <EOL> base_urls = patterns ( '<STR_LIT>' , <EOL> url ( r'<STR_LIT>' , self . media , name = '<STR_LIT>' ) , <EOL> url ( r'<STR_LIT>' , self . as_view ( self . dashboard ) , name = '<STR_LIT:index>' ) , <EOL> url ( r'<STR_LIT>' , self . login , name = '<STR_LIT>' ) , <EOL> url ( r'<STR_LIT>' , self . as_view ( self . logout ) , name = '<STR_LIT>' ) , <EOL> ) , self . app_name , self . name <EOL> urlpatterns = patterns ( '<STR_LIT>' , <EOL> url ( r'<STR_LIT>' , include ( base_urls ) ) , <EOL> ) <EOL> for namespace , module in self . get_modules ( ) : <EOL> urlpatterns += patterns ( '<STR_LIT>' , <EOL> url ( r'<STR_LIT>' % namespace , include ( module . urls ) ) , <EOL> ) <EOL> return urlpatterns <EOL> def urls ( self ) : <EOL> return self . get_urls ( ) <EOL> urls = property ( urls ) <EOL> def has_permission ( self , request , extra_permission = None ) : <EOL> """<STR_LIT>""" <EOL> permission = request . user . is_active and request . user . is_staff <EOL> if extra_permission : <EOL> permission = permission and request . user . has_perm ( extra_permission ) <EOL> return permission <EOL> def as_view ( self , view , cacheable = False , extra_permission = None ) : <EOL> """<STR_LIT>""" <EOL> def inner ( request , * args , ** kwargs ) : <EOL> if not self . has_permission ( request , extra_permission ) : <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> inner = ensure_csrf_cookie ( inner ) <EOL> return update_wrapper ( inner , view ) <EOL> def get_context ( self , request ) : <EOL> context = csrf ( request ) <EOL> context . update ( { <EOL> '<STR_LIT>' : request , <EOL> '<STR_LIT>' : self , <EOL> '<STR_LIT>' : conf . MEDIA_PREFIX . rstrip ( '<STR_LIT:/>' ) , <EOL> } ) <EOL> return context <EOL> def get_modules ( self ) : <EOL> for k , v in self . _registry . iteritems ( ) : <EOL> yield k , v [ <NUM_LIT:0> ] <EOL> def get_module ( self , module ) : <EOL> return self . _registry [ module ] [ <NUM_LIT:0> ] <EOL> def get_categories ( self ) : <EOL> for k , v in self . _categories . iteritems ( ) : <EOL> yield k , v <EOL> def get_category_label ( self , category ) : <EOL> return self . _categories . get ( category , category . title ( ) . replace ( '<STR_LIT:_>' , '<STR_LIT:U+0020>' ) ) <EOL> def render_to_string ( self , template , context , request , current_app = None ) : <EOL> if not current_app : <EOL> current_app = self . name <EOL> else : <EOL> current_app = '<STR_LIT>' % ( self . name , current_app ) <EOL> if request : <EOL> context_instance = RequestContext ( request , current_app = current_app ) <EOL> else : <EOL> context_instance = None <EOL> context . update ( self . get_context ( request ) ) <EOL> return render_to_string ( template , context , <EOL> context_instance = context_instance <EOL> ) <EOL> def render_to_response ( self , template , context , request , current_app = None ) : <EOL> "<STR_LIT>" <EOL> if not current_app : <EOL> current_app = self . name <EOL> else : <EOL> current_app = '<STR_LIT>' % ( self . name , current_app ) <EOL> if request : <EOL> context_instance = RequestContext ( request , current_app = current_app ) <EOL> else : <EOL> context_instance = None <EOL> context . update ( self . get_context ( request ) ) <EOL> return render_to_response ( template , context , <EOL> context_instance = context_instance <EOL> ) <EOL> def media ( self , request , module , path ) : <EOL> """<STR_LIT>""" <EOL> if module == '<STR_LIT>' : <EOL> document_root = os . path . join ( NEXUS_ROOT , '<STR_LIT>' ) <EOL> else : <EOL> document_root = self . get_module ( module ) . media_root <EOL> path = posixpath . normpath ( urllib . unquote ( path ) ) <EOL> path = path . lstrip ( '<STR_LIT:/>' ) <EOL> newpath = '<STR_LIT>' <EOL> for part in path . split ( '<STR_LIT:/>' ) : <EOL> if not part : <EOL> continue <EOL> drive , part = os . path . splitdrive ( part ) <EOL> head , part = os . path . split ( part ) <EOL> if part in ( os . curdir , os . pardir ) : <EOL> continue <EOL> newpath = os . path . join ( newpath , part ) . replace ( '<STR_LIT:\\>' , '<STR_LIT:/>' ) <EOL> if newpath and path != newpath : <EOL> return HttpResponseRedirect ( newpath ) <EOL> fullpath = os . path . join ( document_root , newpath ) <EOL> if os . path . isdir ( fullpath ) : <EOL> raise Http404 ( "<STR_LIT>" ) <EOL> if not os . path . exists ( fullpath ) : <EOL> raise Http404 ( '<STR_LIT>' % fullpath ) <EOL> statobj = os . stat ( fullpath ) <EOL> mimetype = mimetypes . guess_type ( fullpath ) [ <NUM_LIT:0> ] or '<STR_LIT>' <EOL> if not was_modified_since ( request . META . get ( '<STR_LIT>' ) , <EOL> statobj [ stat . ST_MTIME ] , statobj [ stat . ST_SIZE ] ) : <EOL> return HttpResponseNotModified ( content_type = mimetype ) <EOL> contents = open ( fullpath , '<STR_LIT:rb>' ) . read ( ) <EOL> response = HttpResponse ( contents , content_type = mimetype ) <EOL> response [ "<STR_LIT>" ] = http_date ( statobj [ stat . ST_MTIME ] ) <EOL> response [ "<STR_LIT>" ] = len ( contents ) <EOL> return response <EOL> def login ( self , request , form_class = None ) : <EOL> "<STR_LIT>" <EOL> from django . contrib . auth import login as login_ <EOL> from django . contrib . auth . forms import AuthenticationForm <EOL> if form_class is None : <EOL> form_class = AuthenticationForm <EOL> if request . POST : <EOL> form = form_class ( request , request . POST ) <EOL> if form . is_valid ( ) : <EOL> login_ ( request , form . get_user ( ) ) <EOL> request . session . save ( ) <EOL> return HttpResponseRedirect ( request . POST . get ( '<STR_LIT>' ) or reverse ( '<STR_LIT>' , current_app = self . name ) ) <EOL> else : <EOL> request . session . set_test_cookie ( ) <EOL> else : <EOL> form = form_class ( request ) <EOL> request . session . set_test_cookie ( ) <EOL> return self . render_to_response ( '<STR_LIT>' , { <EOL> '<STR_LIT>' : form , <EOL> } , request ) <EOL> login = never_cache ( login ) <EOL> def logout ( self , request ) : <EOL> "<STR_LIT>" <EOL> from django . contrib . auth import logout <EOL> logout ( request ) <EOL> return HttpResponseRedirect ( reverse ( '<STR_LIT>' , current_app = self . name ) ) <EOL> def dashboard ( self , request ) : <EOL> "<STR_LIT>" <EOL> module_set = [ ] <EOL> for namespace , module in self . get_modules ( ) : <EOL> home_url = module . get_home_url ( request ) <EOL> if hasattr ( module , '<STR_LIT>' ) : <EOL> if not module . permission or request . user . has_perm ( module . permission ) : <EOL> module_set . append ( ( module . get_dashboard_title ( ) , module . render_on_dashboard ( request ) , home_url ) ) <EOL> return self . render_to_response ( '<STR_LIT>' , { <EOL> '<STR_LIT>' : module_set , <EOL> } , request ) <EOL> site = NexusSite ( ) </s>
