<s> from django . core . urlresolvers import reverse <EOL> from modularodm import Q <EOL> from modularodm . exceptions import NoResultsFound <EOL> from modularodm . query . query import RawQuery <EOL> from modularodm . storage . mongostorage import MongoQuerySet <EOL> class AffiliatedInstitutionsList ( list ) : <EOL> '''<STR_LIT>''' <EOL> def __init__ ( self , init , obj , private_target ) : <EOL> super ( AffiliatedInstitutionsList , self ) . __init__ ( init or [ ] ) <EOL> self . obj = obj <EOL> self . target = private_target <EOL> def append ( self , to_append ) : <EOL> temp_list = getattr ( self . obj , self . target ) <EOL> temp_list . append ( to_append . node ) <EOL> setattr ( self . obj , self . target , temp_list ) <EOL> def remove ( self , to_remove ) : <EOL> temp_list = getattr ( self . obj , self . target ) <EOL> temp_list . remove ( to_remove . node ) <EOL> setattr ( self . obj , self . target , temp_list ) <EOL> class InstitutionQuerySet ( MongoQuerySet ) : <EOL> def __init__ ( self , queryset ) : <EOL> super ( InstitutionQuerySet , self ) . __init__ ( queryset . schema , queryset . data ) <EOL> def __iter__ ( self ) : <EOL> for each in super ( InstitutionQuerySet , self ) . __iter__ ( ) : <EOL> yield Institution ( each ) <EOL> def _do_getitem ( self , index ) : <EOL> item = super ( InstitutionQuerySet , self ) . _do_getitem ( index ) <EOL> if isinstance ( item , MongoQuerySet ) : <EOL> return self . __class__ ( item ) <EOL> return Institution ( item ) <EOL> class Institution ( object ) : <EOL> '''<STR_LIT>''' <EOL> attribute_map = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:name>' : '<STR_LIT:title>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:description>' : '<STR_LIT:description>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> def __init__ ( self , node = None ) : <EOL> self . node = node <EOL> if node is None : <EOL> return <EOL> for key , value in self . attribute_map . iteritems ( ) : <EOL> setattr ( self , key , getattr ( node , value ) ) <EOL> def __getattr__ ( self , item ) : <EOL> return getattr ( self . node , item ) <EOL> def __eq__ ( self , other ) : <EOL> if not isinstance ( other , self . __class__ ) : <EOL> return False <EOL> return self . _id == other . _id <EOL> def save ( self ) : <EOL> from website . search . search import update_institution <EOL> update_institution ( self ) <EOL> for key , value in self . attribute_map . iteritems ( ) : <EOL> if getattr ( self , key ) != getattr ( self . node , value ) : <EOL> setattr ( self . node , value , getattr ( self , key ) ) <EOL> self . node . save ( ) <EOL> @ classmethod <EOL> def find ( cls , query = None , deleted = False , ** kwargs ) : <EOL> from website . models import Node <EOL> if query and getattr ( query , '<STR_LIT>' , False ) : <EOL> for node in query . nodes : <EOL> replacement_attr = cls . attribute_map . get ( node . attribute , False ) <EOL> node . attribute = replacement_attr or node . attribute <EOL> elif isinstance ( query , RawQuery ) : <EOL> replacement_attr = cls . attribute_map . get ( query . attribute , False ) <EOL> query . attribute = replacement_attr or query . attribute <EOL> query = query & Q ( '<STR_LIT>' , '<STR_LIT>' , None ) if query else Q ( '<STR_LIT>' , '<STR_LIT>' , None ) <EOL> query = query & Q ( '<STR_LIT>' , '<STR_LIT>' , True ) if not deleted else query <EOL> nodes = Node . find ( query , allow_institution = True , ** kwargs ) <EOL> return InstitutionQuerySet ( nodes ) <EOL> @ classmethod <EOL> def find_one ( cls , query = None , deleted = False , ** kwargs ) : <EOL> from website . models import Node <EOL> if query and getattr ( query , '<STR_LIT>' , False ) : <EOL> for node in query . nodes : <EOL> replacement_attr = cls . attribute_map . get ( node . attribute , False ) <EOL> node . attribute = replacement_attr if replacement_attr else node . attribute <EOL> elif isinstance ( query , RawQuery ) : <EOL> replacement_attr = cls . attribute_map . get ( query . attribute , False ) <EOL> query . attribute = replacement_attr if replacement_attr else query . attribute <EOL> query = query & Q ( '<STR_LIT>' , '<STR_LIT>' , None ) if query else Q ( '<STR_LIT>' , '<STR_LIT>' , None ) <EOL> query = query & Q ( '<STR_LIT>' , '<STR_LIT>' , True ) if not deleted else query <EOL> node = Node . find_one ( query , allow_institution = True , ** kwargs ) <EOL> return cls ( node ) <EOL> @ classmethod <EOL> def load ( cls , key ) : <EOL> from website . models import Node <EOL> try : <EOL> node = Node . find_one ( Q ( '<STR_LIT>' , '<STR_LIT>' , key ) , allow_institution = True ) <EOL> return cls ( node ) <EOL> except NoResultsFound : <EOL> return None <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' . format ( self . name , self . _id ) <EOL> @ property <EOL> def pk ( self ) : <EOL> return self . _id <EOL> @ property <EOL> def api_v2_url ( self ) : <EOL> return reverse ( '<STR_LIT>' , kwargs = { '<STR_LIT>' : self . _id } ) <EOL> @ property <EOL> def absolute_api_v2_url ( self ) : <EOL> from api . base . utils import absolute_reverse <EOL> return absolute_reverse ( '<STR_LIT>' , kwargs = { '<STR_LIT>' : self . _id } ) <EOL> @ property <EOL> def logo_path ( self ) : <EOL> if self . logo_name : <EOL> return '<STR_LIT>' . format ( self . logo_name ) <EOL> else : <EOL> return None <EOL> @ property <EOL> def banner_path ( self ) : <EOL> if self . banner_name : <EOL> return '<STR_LIT>' . format ( self . banner_name ) <EOL> else : <EOL> return None </s>
