<s> from django . utils . datastructures import SortedDict <EOL> from hyperadmin . endpoints import RootEndpoint <EOL> from hyperadmin . resources . directory import ResourceDirectory <EOL> from hyperadmin . resources . auth import AuthResource <EOL> from hyperadmin . throttle import Throttle <EOL> import collections <EOL> class Registry ( dict ) : <EOL> def __init__ ( self , resource_site ) : <EOL> self . resource_site = resource_site <EOL> def __getitem__ ( self , key ) : <EOL> item = super ( Registry , self ) . __getitem__ ( key ) <EOL> if self . resource_site . api_request : <EOL> return self . resource_site . api_request . get_endpoint ( item . get_url_name ( ) ) <EOL> return item <EOL> def items ( self ) : <EOL> items = super ( Registry , self ) . items ( ) <EOL> if self . resource_site . api_request : <EOL> items = [ ( key , self [ key ] ) for key , val in items ] <EOL> return items <EOL> class BaseResourceSite ( RootEndpoint ) : <EOL> directory_resource_class = ResourceDirectory <EOL> throttle = Throttle ( throttle_at = <NUM_LIT> ) <EOL> name = '<STR_LIT>' <EOL> def __init__ ( self , ** kwargs ) : <EOL> self . registry = dict ( ) <EOL> kwargs . setdefault ( '<STR_LIT>' , kwargs . get ( '<STR_LIT:name>' , self . name ) ) <EOL> super ( BaseResourceSite , self ) . __init__ ( ** kwargs ) <EOL> def post_register ( self ) : <EOL> super ( BaseResourceSite , self ) . post_register ( ) <EOL> self . directory_resource = self . create_directory_resource ( base_url_name_suffix = self . base_url_name_suffix ) <EOL> def get_directory_resource_kwargs ( self , ** kwargs ) : <EOL> kwargs . setdefault ( '<STR_LIT>' , self . name ) <EOL> return self . get_resource_kwargs ( ** kwargs ) <EOL> def create_directory_resource ( self , ** kwargs ) : <EOL> params = self . get_directory_resource_kwargs ( ** kwargs ) <EOL> return self . directory_resource_class ( ** params ) <EOL> def get_children_endpoints ( self ) : <EOL> return [ self . directory_resource ] <EOL> def get_index_endpoint ( self ) : <EOL> return self . directory_resource <EOL> def get_resource_kwargs ( self , ** kwargs ) : <EOL> params = { '<STR_LIT>' : self , <EOL> '<STR_LIT>' : self . api_request , } <EOL> params . update ( kwargs ) <EOL> return params <EOL> def get_endpoint_kwargs ( self , ** kwargs ) : <EOL> kwargs . setdefault ( '<STR_LIT>' , self . directory_resource ) <EOL> params = self . get_resource_kwargs ( ** kwargs ) <EOL> return params <EOL> def fork ( self , ** kwargs ) : <EOL> ret = super ( BaseResourceSite , self ) . fork ( ** kwargs ) <EOL> ret . registry . update ( self . registry ) <EOL> ret . directory_resource . resource_adaptor . update ( self . directory_resource . resource_adaptor ) <EOL> return ret <EOL> def register_endpoint ( self , klass , ** options ) : <EOL> kwargs = self . get_endpoint_kwargs ( ** options ) <EOL> endpoint = klass ( ** kwargs ) <EOL> if '<STR_LIT>' in kwargs : <EOL> self . registry [ kwargs [ '<STR_LIT>' ] ] = endpoint <EOL> self . directory_resource . register_resource ( endpoint ) <EOL> return endpoint <EOL> def register_builtin_media_types ( self ) : <EOL> from mediatypes import BUILTIN_MEDIA_TYPES <EOL> for key , value in BUILTIN_MEDIA_TYPES . iteritems ( ) : <EOL> self . register_media_type ( key , value ) <EOL> def get_html_type_from_field ( self , field ) : <EOL> from django . forms import widgets <EOL> from django . forms import FileField <EOL> if hasattr ( field , '<STR_LIT>' ) : <EOL> field = field . field <EOL> widget = field . widget <EOL> if isinstance ( widget , widgets . Input ) : <EOL> return widget . input_type <EOL> if isinstance ( widget , widgets . CheckboxInput ) : <EOL> return '<STR_LIT>' <EOL> if isinstance ( widget , widgets . Select ) : <EOL> return '<STR_LIT>' <EOL> if isinstance ( field , FileField ) : <EOL> return '<STR_LIT:file>' <EOL> self . get_logger ( ) . warning ( '<STR_LIT>' % type ( widget ) ) <EOL> return '<STR_LIT:text>' <EOL> def get_media_resource_urlname ( self ) : <EOL> return '<STR_LIT>' % self . base_url_name_suffix <EOL> def get_media_resource ( self ) : <EOL> urlname = self . get_media_resource_urlname ( ) <EOL> return self . api_request . get_endpoint ( urlname ) <EOL> def get_related_resource_from_field ( self , field ) : <EOL> from django . forms import FileField <EOL> from django . forms . models import ModelChoiceField <EOL> if hasattr ( field , '<STR_LIT>' ) : <EOL> field = field . field <EOL> if isinstance ( field , ModelChoiceField ) : <EOL> model = field . queryset . model <EOL> if model in self . registry : <EOL> resource = self . registry [ model ] <EOL> return self . api_request . get_endpoint ( resource . get_url_name ( ) ) <EOL> if isinstance ( field , FileField ) : <EOL> return self . get_media_resource ( ) . link_prototypes [ '<STR_LIT>' ] . get_url ( ) <EOL> return None <EOL> def api_permission_check ( self , api_request , endpoint ) : <EOL> response = self . throttle . throttle_check ( api_request , endpoint ) <EOL> if response : <EOL> return response <EOL> return super ( BaseResourceSite , self ) . api_permission_check ( api_request , endpoint ) <EOL> class ResourceSite ( BaseResourceSite ) : <EOL> '''<STR_LIT>''' <EOL> auth_resource_class = AuthResource <EOL> name = '<STR_LIT>' <EOL> base_url_name_suffix = '<STR_LIT>' <EOL> def post_register ( self ) : <EOL> super ( ResourceSite , self ) . post_register ( ) <EOL> self . auth_resource = self . register_endpoint ( self . auth_resource_class ) <EOL> @ property <EOL> def applications ( self ) : <EOL> return self . directory_resource . resource_adaptor <EOL> def register ( self , model_or_iterable , admin_class , ** options ) : <EOL> if isinstance ( model_or_iterable , collections . Iterable ) and not isinstance ( model_or_iterable , basestring ) : <EOL> resources = list ( ) <EOL> for model in model_or_iterable : <EOL> resources . append ( self . register ( model , admin_class , ** options ) ) <EOL> return resources <EOL> model = model_or_iterable <EOL> app_name = options . pop ( '<STR_LIT>' ) <EOL> app_resource = self . register_application ( app_name ) <EOL> options . setdefault ( '<STR_LIT>' , app_resource ) <EOL> kwargs = self . get_resource_kwargs ( resource_adaptor = model , ** options ) <EOL> resource = admin_class ( ** kwargs ) <EOL> self . applications [ app_name ] . register_resource ( resource ) <EOL> self . registry [ model ] = resource <EOL> return resource <EOL> def register_application ( self , app_name , app_class = None , ** options ) : <EOL> if app_name not in self . applications : <EOL> if app_class is None : <EOL> app_class = self . directory_resource_class <EOL> app_resource = self . register_endpoint ( app_class , app_name = self . name , resource_name = app_name ) <EOL> assert app_name in self . applications <EOL> return self . applications [ app_name ] <EOL> def get_login_link ( self , api_request , ** kwargs ) : <EOL> auth_resource = api_request . get_endpoint ( self . auth_resource . get_url_name ( ) ) <EOL> return auth_resource . get_link ( ** kwargs ) <EOL> def api_permission_check ( self , api_request , endpoint ) : <EOL> user = api_request . user <EOL> if not user . is_authenticated ( ) : <EOL> return self . get_login_link ( api_request , prompt = '<STR_LIT>' ) <EOL> if not user . is_staff : <EOL> return self . get_login_link ( api_request , prompt = '<STR_LIT>' , http_status = <NUM_LIT> ) <EOL> return super ( ResourceSite , self ) . api_permission_check ( api_request , endpoint ) <EOL> def get_actions ( self , request ) : <EOL> return SortedDict ( ) <EOL> def install_models_from_site ( self , site ) : <EOL> from hyperadmin . resources . models . autoload import DEFAULT_LOADER <EOL> loader = DEFAULT_LOADER ( root_endpoint = self , admin_site = site ) <EOL> loader . register_resources ( ) <EOL> def install_storage_resources ( self , media_resource_class = None , static_resource_class = None ) : <EOL> from hyperadmin . resources . storages import StorageResource <EOL> from django . core . files . storage import default_storage as media_storage <EOL> try : <EOL> from django . contrib . staticfiles . storage import staticfiles_storage as static_storage <EOL> except ImportError : <EOL> from django . conf import settings <EOL> from django . core . files . storage import get_storage_class <EOL> static_storage = get_storage_class ( settings . STATICFILES_STORAGE ) ( ) <EOL> if media_resource_class is None : <EOL> media_resource_class = StorageResource <EOL> if static_resource_class is None : <EOL> static_resource_class = StorageResource <EOL> app_name = '<STR_LIT>' <EOL> self . register ( media_storage , media_resource_class , resource_name = '<STR_LIT>' , app_name = app_name ) <EOL> self . register ( static_storage , static_resource_class , resource_name = '<STR_LIT>' , app_name = app_name ) <EOL> class GlobalSite ( BaseResourceSite ) : <EOL> '''<STR_LIT>''' <EOL> name = '<STR_LIT>' <EOL> def get_resolver ( self ) : <EOL> from django . core . urlresolvers import get_resolver <EOL> return get_resolver ( None ) <EOL> site = ResourceSite ( ) <EOL> site . register_builtin_media_types ( ) <EOL> global_site = GlobalSite ( ) <EOL> global_site . register_builtin_media_types ( ) </s>
