<s> import inspect <EOL> import os <EOL> import re <EOL> from django import template <EOL> from django . template import RequestContext <EOL> from django . conf import settings <EOL> from django . contrib . admin . views . decorators import staff_member_required <EOL> from django . db import models <EOL> from django . shortcuts import render_to_response <EOL> from django . core . exceptions import ImproperlyConfigured , ViewDoesNotExist <EOL> from django . http import Http404 <EOL> from django . core import urlresolvers <EOL> from django . contrib . admindocs import utils <EOL> from django . contrib . sites . models import Site <EOL> from django . utils . importlib import import_module <EOL> from django . utils . translation import ugettext as _ <EOL> from django . utils . safestring import mark_safe <EOL> MODEL_METHODS_EXCLUDE = ( '<STR_LIT:_>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> class GenericSite ( object ) : <EOL> domain = '<STR_LIT>' <EOL> name = '<STR_LIT>' <EOL> @ staff_member_required <EOL> def doc_index ( request ) : <EOL> if not utils . docutils_is_available : <EOL> return missing_docutils_page ( request ) <EOL> return render_to_response ( '<STR_LIT>' , { <EOL> '<STR_LIT>' : urlresolvers . reverse ( '<STR_LIT>' ) , <EOL> } , context_instance = RequestContext ( request ) ) <EOL> @ staff_member_required <EOL> def bookmarklets ( request ) : <EOL> admin_root = urlresolvers . reverse ( '<STR_LIT>' ) <EOL> return render_to_response ( '<STR_LIT>' , { <EOL> '<STR_LIT>' : admin_root , <EOL> '<STR_LIT>' : mark_safe ( "<STR_LIT>" % ( request . is_secure ( ) and '<STR_LIT>' or '<STR_LIT:http>' , request . get_host ( ) , admin_root ) ) , <EOL> } , context_instance = RequestContext ( request ) ) <EOL> @ staff_member_required <EOL> def template_tag_index ( request ) : <EOL> if not utils . docutils_is_available : <EOL> return missing_docutils_page ( request ) <EOL> load_all_installed_template_libraries ( ) <EOL> tags = [ ] <EOL> app_libs = template . libraries . items ( ) <EOL> builtin_libs = [ ( None , lib ) for lib in template . builtins ] <EOL> for module_name , library in builtin_libs + app_libs : <EOL> for tag_name , tag_func in library . tags . items ( ) : <EOL> title , body , metadata = utils . parse_docstring ( tag_func . __doc__ ) <EOL> if title : <EOL> title = utils . parse_rst ( title , '<STR_LIT>' , _ ( '<STR_LIT>' ) + tag_name ) <EOL> if body : <EOL> body = utils . parse_rst ( body , '<STR_LIT>' , _ ( '<STR_LIT>' ) + tag_name ) <EOL> for key in metadata : <EOL> metadata [ key ] = utils . parse_rst ( metadata [ key ] , '<STR_LIT>' , _ ( '<STR_LIT>' ) + tag_name ) <EOL> if library in template . builtins : <EOL> tag_library = None <EOL> else : <EOL> tag_library = module_name . split ( '<STR_LIT:.>' ) [ - <NUM_LIT:1> ] <EOL> tags . append ( { <EOL> '<STR_LIT:name>' : tag_name , <EOL> '<STR_LIT:title>' : title , <EOL> '<STR_LIT:body>' : body , <EOL> '<STR_LIT>' : metadata , <EOL> '<STR_LIT>' : tag_library , <EOL> } ) <EOL> return render_to_response ( '<STR_LIT>' , { <EOL> '<STR_LIT>' : urlresolvers . reverse ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : tags <EOL> } , context_instance = RequestContext ( request ) ) <EOL> @ staff_member_required <EOL> def template_filter_index ( request ) : <EOL> if not utils . docutils_is_available : <EOL> return missing_docutils_page ( request ) <EOL> load_all_installed_template_libraries ( ) <EOL> filters = [ ] <EOL> app_libs = template . libraries . items ( ) <EOL> builtin_libs = [ ( None , lib ) for lib in template . builtins ] <EOL> for module_name , library in builtin_libs + app_libs : <EOL> for filter_name , filter_func in library . filters . items ( ) : <EOL> title , body , metadata = utils . parse_docstring ( filter_func . __doc__ ) <EOL> if title : <EOL> title = utils . parse_rst ( title , '<STR_LIT>' , _ ( '<STR_LIT>' ) + filter_name ) <EOL> if body : <EOL> body = utils . parse_rst ( body , '<STR_LIT>' , _ ( '<STR_LIT>' ) + filter_name ) <EOL> for key in metadata : <EOL> metadata [ key ] = utils . parse_rst ( metadata [ key ] , '<STR_LIT>' , _ ( '<STR_LIT>' ) + filter_name ) <EOL> if library in template . builtins : <EOL> tag_library = None <EOL> else : <EOL> tag_library = module_name . split ( '<STR_LIT:.>' ) [ - <NUM_LIT:1> ] <EOL> filters . append ( { <EOL> '<STR_LIT:name>' : filter_name , <EOL> '<STR_LIT:title>' : title , <EOL> '<STR_LIT:body>' : body , <EOL> '<STR_LIT>' : metadata , <EOL> '<STR_LIT>' : tag_library , <EOL> } ) <EOL> return render_to_response ( '<STR_LIT>' , { <EOL> '<STR_LIT>' : urlresolvers . reverse ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : filters <EOL> } , context_instance = RequestContext ( request ) ) <EOL> @ staff_member_required <EOL> def view_index ( request ) : <EOL> if not utils . docutils_is_available : <EOL> return missing_docutils_page ( request ) <EOL> if settings . ADMIN_FOR : <EOL> settings_modules = [ import_module ( m ) for m in settings . ADMIN_FOR ] <EOL> else : <EOL> settings_modules = [ settings ] <EOL> views = [ ] <EOL> for settings_mod in settings_modules : <EOL> urlconf = import_module ( settings_mod . ROOT_URLCONF ) <EOL> view_functions = extract_views_from_urlpatterns ( urlconf . urlpatterns ) <EOL> if Site . _meta . installed : <EOL> site_obj = Site . objects . get ( pk = settings_mod . SITE_ID ) <EOL> else : <EOL> site_obj = GenericSite ( ) <EOL> for ( func , regex ) in view_functions : <EOL> views . append ( { <EOL> '<STR_LIT>' : '<STR_LIT>' % ( func . __module__ , getattr ( func , '<STR_LIT>' , func . __class__ . __name__ ) ) , <EOL> '<STR_LIT>' : settings_mod . SITE_ID , <EOL> '<STR_LIT>' : site_obj , <EOL> '<STR_LIT:url>' : simplify_regex ( regex ) , <EOL> } ) <EOL> return render_to_response ( '<STR_LIT>' , { <EOL> '<STR_LIT>' : urlresolvers . reverse ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : views <EOL> } , context_instance = RequestContext ( request ) ) <EOL> @ staff_member_required <EOL> def view_detail ( request , view ) : <EOL> if not utils . docutils_is_available : <EOL> return missing_docutils_page ( request ) <EOL> mod , func = urlresolvers . get_mod_func ( view ) <EOL> try : <EOL> view_func = getattr ( import_module ( mod ) , func ) <EOL> except ( ImportError , AttributeError ) : <EOL> raise Http404 <EOL> title , body , metadata = utils . parse_docstring ( view_func . __doc__ ) <EOL> if title : <EOL> title = utils . parse_rst ( title , '<STR_LIT>' , _ ( '<STR_LIT>' ) + view ) <EOL> if body : <EOL> body = utils . parse_rst ( body , '<STR_LIT>' , _ ( '<STR_LIT>' ) + view ) <EOL> for key in metadata : <EOL> metadata [ key ] = utils . parse_rst ( metadata [ key ] , '<STR_LIT>' , _ ( '<STR_LIT>' ) + view ) <EOL> return render_to_response ( '<STR_LIT>' , { <EOL> '<STR_LIT>' : urlresolvers . reverse ( '<STR_LIT>' ) , <EOL> '<STR_LIT:name>' : view , <EOL> '<STR_LIT>' : title , <EOL> '<STR_LIT:body>' : body , <EOL> '<STR_LIT>' : metadata , <EOL> } , context_instance = RequestContext ( request ) ) <EOL> @ staff_member_required <EOL> def model_index ( request ) : <EOL> if not utils . docutils_is_available : <EOL> return missing_docutils_page ( request ) <EOL> m_list = [ m . _meta for m in models . get_models ( ) ] <EOL> return render_to_response ( '<STR_LIT>' , { <EOL> '<STR_LIT>' : urlresolvers . reverse ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : m_list <EOL> } , context_instance = RequestContext ( request ) ) <EOL> @ staff_member_required <EOL> def model_detail ( request , app_label , model_name ) : <EOL> if not utils . docutils_is_available : <EOL> return missing_docutils_page ( request ) <EOL> try : <EOL> app_mod = models . get_app ( app_label ) <EOL> except ImproperlyConfigured : <EOL> raise Http404 ( _ ( "<STR_LIT>" ) % app_label ) <EOL> model = None <EOL> for m in models . get_models ( app_mod ) : <EOL> if m . _meta . object_name . lower ( ) == model_name : <EOL> model = m <EOL> break <EOL> if model is None : <EOL> raise Http404 ( _ ( "<STR_LIT>" ) % { '<STR_LIT>' : model_name , '<STR_LIT>' : app_label } ) <EOL> opts = model . _meta <EOL> fields = [ ] <EOL> for field in opts . fields : <EOL> if isinstance ( field , models . ForeignKey ) : <EOL> data_type = field . rel . to . __name__ <EOL> app_label = field . rel . to . _meta . app_label <EOL> verbose = utils . parse_rst ( ( _ ( "<STR_LIT>" ) % { '<STR_LIT>' : app_label , '<STR_LIT>' : data_type } ) , '<STR_LIT>' , _ ( '<STR_LIT>' ) + data_type ) <EOL> else : <EOL> data_type = get_readable_field_data_type ( field ) <EOL> verbose = field . verbose_name <EOL> fields . append ( { <EOL> '<STR_LIT:name>' : field . name , <EOL> '<STR_LIT>' : data_type , <EOL> '<STR_LIT>' : verbose , <EOL> '<STR_LIT>' : field . help_text , <EOL> } ) <EOL> for field in opts . many_to_many : <EOL> data_type = field . rel . to . __name__ <EOL> app_label = field . rel . to . _meta . app_label <EOL> verbose = _ ( "<STR_LIT>" ) % { '<STR_LIT>' : app_label , '<STR_LIT:object_name>' : data_type } <EOL> fields . append ( { <EOL> '<STR_LIT:name>' : "<STR_LIT>" % field . name , <EOL> "<STR_LIT>" : '<STR_LIT>' , <EOL> '<STR_LIT>' : utils . parse_rst ( _ ( "<STR_LIT>" ) % verbose , '<STR_LIT>' , _ ( '<STR_LIT>' ) + opts . module_name ) , <EOL> } ) <EOL> fields . append ( { <EOL> '<STR_LIT:name>' : "<STR_LIT>" % field . name , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : utils . parse_rst ( _ ( "<STR_LIT>" ) % verbose , '<STR_LIT>' , _ ( '<STR_LIT>' ) + opts . module_name ) , <EOL> } ) <EOL> for func_name , func in model . __dict__ . items ( ) : <EOL> if ( inspect . isfunction ( func ) and len ( inspect . getargspec ( func ) [ <NUM_LIT:0> ] ) == <NUM_LIT:1> ) : <EOL> try : <EOL> for exclude in MODEL_METHODS_EXCLUDE : <EOL> if func_name . startswith ( exclude ) : <EOL> raise StopIteration <EOL> except StopIteration : <EOL> continue <EOL> verbose = func . __doc__ <EOL> if verbose : <EOL> verbose = utils . parse_rst ( utils . trim_docstring ( verbose ) , '<STR_LIT>' , _ ( '<STR_LIT>' ) + opts . module_name ) <EOL> fields . append ( { <EOL> '<STR_LIT:name>' : func_name , <EOL> '<STR_LIT>' : get_return_data_type ( func_name ) , <EOL> '<STR_LIT>' : verbose , <EOL> } ) <EOL> for rel in opts . get_all_related_objects ( ) + opts . get_all_related_many_to_many_objects ( ) : <EOL> verbose = _ ( "<STR_LIT>" ) % { '<STR_LIT>' : rel . opts . app_label , '<STR_LIT:object_name>' : rel . opts . object_name } <EOL> accessor = rel . get_accessor_name ( ) <EOL> fields . append ( { <EOL> '<STR_LIT:name>' : "<STR_LIT>" % accessor , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : utils . parse_rst ( _ ( "<STR_LIT>" ) % verbose , '<STR_LIT>' , _ ( '<STR_LIT>' ) + opts . module_name ) , <EOL> } ) <EOL> fields . append ( { <EOL> '<STR_LIT:name>' : "<STR_LIT>" % accessor , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : utils . parse_rst ( _ ( "<STR_LIT>" ) % verbose , '<STR_LIT>' , _ ( '<STR_LIT>' ) + opts . module_name ) , <EOL> } ) <EOL> return render_to_response ( '<STR_LIT>' , { <EOL> '<STR_LIT>' : urlresolvers . reverse ( '<STR_LIT>' ) , <EOL> '<STR_LIT:name>' : '<STR_LIT>' % ( opts . app_label , opts . object_name ) , <EOL> '<STR_LIT>' : _ ( "<STR_LIT>" ) % opts . object_name , <EOL> '<STR_LIT:description>' : model . __doc__ , <EOL> '<STR_LIT>' : fields , <EOL> } , context_instance = RequestContext ( request ) ) <EOL> @ staff_member_required <EOL> def template_detail ( request , template ) : <EOL> templates = [ ] <EOL> for site_settings_module in settings . ADMIN_FOR : <EOL> settings_mod = import_module ( site_settings_module ) <EOL> if Site . _meta . installed : <EOL> site_obj = Site . objects . get ( pk = settings_mod . SITE_ID ) <EOL> else : <EOL> site_obj = GenericSite ( ) <EOL> for dir in settings_mod . TEMPLATE_DIRS : <EOL> template_file = os . path . join ( dir , template ) <EOL> templates . append ( { <EOL> '<STR_LIT:file>' : template_file , <EOL> '<STR_LIT>' : os . path . exists ( template_file ) , <EOL> '<STR_LIT>' : lambda : os . path . exists ( template_file ) and open ( template_file ) . read ( ) or '<STR_LIT>' , <EOL> '<STR_LIT>' : settings_mod . SITE_ID , <EOL> '<STR_LIT>' : site_obj , <EOL> '<STR_LIT>' : list ( settings_mod . TEMPLATE_DIRS ) . index ( dir ) , <EOL> } ) <EOL> return render_to_response ( '<STR_LIT>' , { <EOL> '<STR_LIT>' : urlresolvers . reverse ( '<STR_LIT>' ) , <EOL> '<STR_LIT:name>' : template , <EOL> '<STR_LIT>' : templates , <EOL> } , context_instance = RequestContext ( request ) ) <EOL> def missing_docutils_page ( request ) : <EOL> """<STR_LIT>""" <EOL> return render_to_response ( '<STR_LIT>' ) <EOL> def load_all_installed_template_libraries ( ) : <EOL> for module_name in template . get_templatetags_modules ( ) : <EOL> mod = import_module ( module_name ) <EOL> try : <EOL> libraries = [ <EOL> os . path . splitext ( p ) [ <NUM_LIT:0> ] <EOL> for p in os . listdir ( os . path . dirname ( mod . __file__ ) ) <EOL> if p . endswith ( '<STR_LIT>' ) and p [ <NUM_LIT:0> ] . isalpha ( ) <EOL> ] <EOL> except OSError : <EOL> libraries = [ ] <EOL> for library_name in libraries : <EOL> try : <EOL> lib = template . get_library ( library_name ) <EOL> except template . InvalidTemplateLibrary , e : <EOL> pass <EOL> def get_return_data_type ( func_name ) : <EOL> """<STR_LIT>""" <EOL> if func_name . startswith ( '<STR_LIT>' ) : <EOL> if func_name . endswith ( '<STR_LIT>' ) : <EOL> return '<STR_LIT>' <EOL> elif func_name . endswith ( '<STR_LIT>' ) : <EOL> return '<STR_LIT>' <EOL> return '<STR_LIT>' <EOL> def get_readable_field_data_type ( field ) : <EOL> """<STR_LIT>""" <EOL> return field . description % field . __dict__ <EOL> def extract_views_from_urlpatterns ( urlpatterns , base = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> views = [ ] <EOL> for p in urlpatterns : <EOL> if hasattr ( p , '<STR_LIT>' ) : <EOL> try : <EOL> patterns = p . url_patterns <EOL> except ImportError : <EOL> continue <EOL> views . extend ( extract_views_from_urlpatterns ( patterns , base + p . regex . pattern ) ) <EOL> elif hasattr ( p , '<STR_LIT>' ) : <EOL> try : <EOL> views . append ( ( p . callback , base + p . regex . pattern ) ) <EOL> except ViewDoesNotExist : <EOL> continue <EOL> else : <EOL> raise TypeError ( _ ( "<STR_LIT>" ) % p ) <EOL> return views <EOL> named_group_matcher = re . compile ( r'<STR_LIT>' ) <EOL> non_named_group_matcher = re . compile ( r'<STR_LIT>' ) <EOL> def simplify_regex ( pattern ) : <EOL> """<STR_LIT>""" <EOL> pattern = named_group_matcher . sub ( lambda m : m . group ( <NUM_LIT:1> ) , pattern ) <EOL> pattern = non_named_group_matcher . sub ( "<STR_LIT>" , pattern ) <EOL> pattern = pattern . replace ( '<STR_LIT>' , '<STR_LIT>' ) . replace ( '<STR_LIT:$>' , '<STR_LIT>' ) . replace ( '<STR_LIT:?>' , '<STR_LIT>' ) . replace ( '<STR_LIT>' , '<STR_LIT:/>' ) . replace ( '<STR_LIT:\\>' , '<STR_LIT>' ) <EOL> if not pattern . startswith ( '<STR_LIT:/>' ) : <EOL> pattern = '<STR_LIT:/>' + pattern <EOL> return pattern </s>
