<s> from copy import copy <EOL> from django . utils . http import urlencode <EOL> from django . utils . datastructures import MergeDict <EOL> from django . http import QueryDict <EOL> from hyperadmin . links import LinkCollectionProvider , LinkCollectorMixin <EOL> class State ( MergeDict ) : <EOL> def __init__ ( self , substates = [ ] , data = { } ) : <EOL> self . active_dictionary = dict ( ) <EOL> self . substates = substates <EOL> dictionaries = self . get_dictionaries ( ) <EOL> super ( State , self ) . __init__ ( * dictionaries ) <EOL> self . update ( data ) <EOL> def get_dictionaries ( self ) : <EOL> return [ self . active_dictionary ] + self . substates <EOL> def __copy__ ( self ) : <EOL> substates = self . get_dictionaries ( ) <EOL> ret = self . __class__ ( substates = substates ) <EOL> return ret <EOL> def __setitem__ ( self , key , value ) : <EOL> self . active_dictionary [ key ] = value <EOL> def __delitem__ ( self , key ) : <EOL> del self . active_dictionary [ key ] <EOL> def pop ( self , key , default = None ) : <EOL> return self . active_dictionary . pop ( key , default ) <EOL> def update ( self , other_dict ) : <EOL> self . active_dictionary . update ( other_dict ) <EOL> class EndpointStateLinkCollectionProvider ( LinkCollectionProvider ) : <EOL> def _get_link_functions ( self , attr ) : <EOL> functions = super ( EndpointStateLinkCollectionProvider , self ) . _get_link_functions ( attr ) <EOL> key_name = attr [ len ( '<STR_LIT>' ) : ] <EOL> functions . append ( lambda * args , ** kwargs : self . container [ '<STR_LIT>' ] . get ( key_name , [ ] ) ) <EOL> return functions <EOL> def add_link ( self , key , link ) : <EOL> self . container [ '<STR_LIT>' ] . setdefault ( key , list ( ) ) <EOL> self . container [ '<STR_LIT>' ] [ key ] . append ( link ) <EOL> class EndpointState ( LinkCollectorMixin , State ) : <EOL> """<STR_LIT>""" <EOL> link_collector_class = EndpointStateLinkCollectionProvider <EOL> def __init__ ( self , endpoint , meta , substates = [ ] , data = { } ) : <EOL> self . endpoint = endpoint <EOL> super ( EndpointState , self ) . __init__ ( substates = substates , data = data ) <EOL> self . meta = meta <EOL> self . links = self . get_link_collector ( ) <EOL> self . update ( { '<STR_LIT>' : { } , <EOL> '<STR_LIT>' : { } , <EOL> '<STR_LIT>' : self . endpoint , } ) <EOL> @ property <EOL> def api_request ( self ) : <EOL> return self . endpoint . api_request <EOL> def get_link_collector_kwargs ( self , ** kwargs ) : <EOL> params = super ( EndpointState , self ) . get_link_collector_kwargs ( ** kwargs ) <EOL> params [ '<STR_LIT>' ] = self . endpoint . links <EOL> return params <EOL> def get_dictionaries ( self ) : <EOL> return [ self . active_dictionary ] + self . substates + [ self . endpoint . api_request . session_state ] <EOL> @ property <EOL> def resource ( self ) : <EOL> return getattr ( self . endpoint , '<STR_LIT>' , self . endpoint ) <EOL> @ property <EOL> def site ( self ) : <EOL> return self . get ( '<STR_LIT>' , self . endpoint . site ) <EOL> def reverse ( self , name , * args , ** kwargs ) : <EOL> return self . api_request . reverse ( name , * args , ** kwargs ) <EOL> def _set_item ( self , val ) : <EOL> self [ '<STR_LIT>' ] = val <EOL> def _get_item ( self ) : <EOL> return self . get ( '<STR_LIT>' , None ) <EOL> item = property ( _get_item , _set_item ) <EOL> def _set_meta ( self , val ) : <EOL> self [ '<STR_LIT>' ] = val <EOL> def _get_meta ( self ) : <EOL> return self . get ( '<STR_LIT>' , { } ) <EOL> meta = property ( _get_meta , _set_meta ) <EOL> def get_link_url ( self , link ) : <EOL> url = link . get_base_url ( ) <EOL> params = self . get ( '<STR_LIT>' , None ) or QueryDict ( '<STR_LIT>' , mutable = True ) <EOL> if params : <EOL> params = copy ( params ) <EOL> if '<STR_LIT:?>' in url : <EOL> url , get_string = url . split ( '<STR_LIT:?>' , <NUM_LIT:1> ) <EOL> url_args = QueryDict ( get_string ) <EOL> if hasattr ( params , '<STR_LIT>' ) : <EOL> for key , value in url_args . iterlists ( ) : <EOL> params . setlist ( key , value ) <EOL> else : <EOL> params . update ( url_args ) <EOL> if hasattr ( params , '<STR_LIT>' ) : <EOL> params = params . urlencode ( ) <EOL> else : <EOL> params = urlencode ( params ) <EOL> url += '<STR_LIT:?>' + params <EOL> return url <EOL> def has_view_class ( self , cls ) : <EOL> view_classes = self . get ( '<STR_LIT>' , [ ] ) <EOL> return cls in view_classes <EOL> @ property <EOL> def params ( self ) : <EOL> """<STR_LIT>""" <EOL> if '<STR_LIT>' in self : <EOL> return self [ '<STR_LIT>' ] <EOL> if '<STR_LIT>' in self : <EOL> return self [ '<STR_LIT>' ] . GET <EOL> return { } <EOL> @ property <EOL> def namespace ( self ) : <EOL> return self . get ( '<STR_LIT>' , None ) <EOL> def get_resource_items ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . item is not None : <EOL> return self . item . get_resource_items ( ) <EOL> return self . endpoint . get_resource_items ( ) <EOL> def get_query_string ( self , new_params = None , remove = None ) : <EOL> if new_params is None : new_params = { } <EOL> if remove is None : remove = [ ] <EOL> p = copy ( self . params ) <EOL> for r in remove : <EOL> for k in p . keys ( ) : <EOL> if k . startswith ( r ) : <EOL> del p [ k ] <EOL> for k , v in new_params . items ( ) : <EOL> if v is None : <EOL> if k in p : <EOL> del p [ k ] <EOL> else : <EOL> p [ k ] = v <EOL> if hasattr ( p , '<STR_LIT>' ) : <EOL> return '<STR_LIT>' % p . urlencode ( ) <EOL> return '<STR_LIT>' % urlencode ( p ) <EOL> def get_namespaces ( self ) : <EOL> return self . endpoint . get_namespaces ( ) <EOL> def __copy__ ( self ) : <EOL> substates = self . get_dictionaries ( ) <EOL> ret = self . __class__ ( self . endpoint , copy ( self . meta ) , substates = substates ) <EOL> return ret </s>
