<s> import abc <EOL> from framework . auth . decorators import collect_auth <EOL> from website . util import api_url_for , web_url_for <EOL> class AddonSerializer ( object ) : <EOL> __metaclass__ = abc . ABCMeta <EOL> def __init__ ( self , node_settings = None , user_settings = None ) : <EOL> self . node_settings = node_settings <EOL> self . user_settings = user_settings <EOL> @ abc . abstractproperty <EOL> def addon_short_name ( self ) : <EOL> pass <EOL> @ abc . abstractproperty <EOL> def addon_serialized_urls ( self ) : <EOL> pass <EOL> @ abc . abstractproperty <EOL> def serialized_urls ( self ) : <EOL> pass <EOL> @ abc . abstractproperty <EOL> def user_is_owner ( self ) : <EOL> pass <EOL> @ abc . abstractproperty <EOL> def credentials_owner ( self ) : <EOL> pass <EOL> @ property <EOL> def serialized_node_settings ( self ) : <EOL> result = { <EOL> '<STR_LIT>' : self . node_settings . has_auth , <EOL> '<STR_LIT>' : self . user_is_owner , <EOL> '<STR_LIT>' : self . serialized_urls , <EOL> } <EOL> if self . user_settings : <EOL> result [ '<STR_LIT>' ] = self . user_settings . has_auth <EOL> else : <EOL> result [ '<STR_LIT>' ] = False <EOL> if self . node_settings . has_auth : <EOL> owner = self . credentials_owner <EOL> if owner : <EOL> result [ '<STR_LIT>' ] [ '<STR_LIT>' ] = web_url_for ( '<STR_LIT>' , <EOL> uid = owner . _primary_key ) <EOL> result [ '<STR_LIT>' ] = owner . fullname <EOL> return result <EOL> @ property <EOL> def serialized_user_settings ( self ) : <EOL> return { } <EOL> class OAuthAddonSerializer ( AddonSerializer ) : <EOL> @ property <EOL> def credentials_owner ( self ) : <EOL> return self . user_settings . owner if self . user_settings else None <EOL> @ property <EOL> def user_is_owner ( self ) : <EOL> if self . user_settings is None or self . node_settings is None : <EOL> return False <EOL> user_accounts = self . user_settings . external_accounts <EOL> return bool ( <EOL> self . node_settings . has_auth and <EOL> self . node_settings . external_account in user_accounts <EOL> ) <EOL> @ property <EOL> def serialized_urls ( self ) : <EOL> ret = self . addon_serialized_urls <EOL> for url in self . REQUIRED_URLS : <EOL> msg = "<STR_LIT>" . format ( url ) <EOL> assert url in ret , msg <EOL> ret . update ( { '<STR_LIT>' : web_url_for ( '<STR_LIT>' ) } ) <EOL> return ret <EOL> @ property <EOL> def serialized_accounts ( self ) : <EOL> return [ <EOL> self . serialize_account ( each ) <EOL> for each in self . user_settings . external_accounts <EOL> ] <EOL> @ property <EOL> def serialized_user_settings ( self ) : <EOL> retval = super ( OAuthAddonSerializer , self ) . serialized_user_settings <EOL> retval [ '<STR_LIT>' ] = [ ] <EOL> if self . user_settings : <EOL> retval [ '<STR_LIT>' ] = self . serialized_accounts <EOL> return retval <EOL> def serialize_account ( self , external_account ) : <EOL> if external_account is None : <EOL> return None <EOL> return { <EOL> '<STR_LIT:id>' : external_account . _id , <EOL> '<STR_LIT>' : external_account . provider_id , <EOL> '<STR_LIT>' : external_account . provider_name , <EOL> '<STR_LIT>' : external_account . provider , <EOL> '<STR_LIT>' : external_account . display_name , <EOL> '<STR_LIT>' : external_account . profile_url , <EOL> '<STR_LIT>' : [ <EOL> self . serialize_granted_node ( node ) <EOL> for node in self . user_settings . get_attached_nodes ( <EOL> external_account = external_account <EOL> ) <EOL> ] , <EOL> } <EOL> @ collect_auth <EOL> def serialize_granted_node ( self , node , auth ) : <EOL> node_settings = node . get_addon ( <EOL> self . user_settings . oauth_provider . short_name <EOL> ) <EOL> serializer = node_settings . serializer ( node_settings = node_settings ) <EOL> urls = serializer . addon_serialized_urls <EOL> urls [ '<STR_LIT>' ] = node . url <EOL> return { <EOL> '<STR_LIT:id>' : node . _id , <EOL> '<STR_LIT:title>' : node . title if node . can_view ( auth ) else None , <EOL> '<STR_LIT>' : urls , <EOL> } <EOL> class StorageAddonSerializer ( OAuthAddonSerializer ) : <EOL> REQUIRED_URLS = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> @ abc . abstractmethod <EOL> def credentials_are_valid ( self , user_settings ) : <EOL> pass <EOL> @ abc . abstractmethod <EOL> def serialized_folder ( self , node_settings ) : <EOL> pass <EOL> def serialize_settings ( self , node_settings , current_user , client = None ) : <EOL> user_settings = node_settings . user_settings <EOL> self . node_settings = node_settings <EOL> current_user_settings = current_user . get_addon ( self . addon_short_name ) <EOL> user_is_owner = user_settings is not None and user_settings . owner == current_user <EOL> valid_credentials = self . credentials_are_valid ( user_settings , client ) <EOL> result = { <EOL> '<STR_LIT>' : user_is_owner , <EOL> '<STR_LIT>' : node_settings . has_auth , <EOL> '<STR_LIT>' : self . serialized_urls , <EOL> '<STR_LIT>' : valid_credentials , <EOL> '<STR_LIT>' : current_user_settings is not None and current_user_settings . has_auth , <EOL> } <EOL> if node_settings . has_auth : <EOL> result [ '<STR_LIT>' ] [ '<STR_LIT>' ] = web_url_for ( <EOL> '<STR_LIT>' , <EOL> uid = user_settings . owner . _id <EOL> ) <EOL> result [ '<STR_LIT>' ] = user_settings . owner . fullname <EOL> if node_settings . folder_id is None : <EOL> result [ '<STR_LIT>' ] = { '<STR_LIT:name>' : None , '<STR_LIT:path>' : None } <EOL> elif valid_credentials : <EOL> result [ '<STR_LIT>' ] = self . serialized_folder ( node_settings ) <EOL> return result <EOL> class CitationsAddonSerializer ( OAuthAddonSerializer ) : <EOL> REQUIRED_URLS = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> serialized_root_folder = { <EOL> '<STR_LIT:name>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : None , <EOL> '<STR_LIT:id>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> @ property <EOL> def serialized_urls ( self ) : <EOL> external_account = self . node_settings . external_account <EOL> ret = { <EOL> '<STR_LIT>' : api_url_for ( '<STR_LIT>' , <EOL> service_name = self . addon_short_name ) , <EOL> '<STR_LIT>' : self . node_settings . owner . url , <EOL> } <EOL> if external_account and external_account . profile_url : <EOL> ret [ '<STR_LIT>' ] = external_account . profile_url <EOL> ret . update ( super ( CitationsAddonSerializer , self ) . serialized_urls ) <EOL> return ret <EOL> @ property <EOL> def serialized_node_settings ( self ) : <EOL> result = super ( CitationsAddonSerializer , self ) . serialized_node_settings <EOL> result [ '<STR_LIT>' ] = { <EOL> '<STR_LIT:name>' : self . node_settings . fetch_folder_name <EOL> } <EOL> return result <EOL> @ property <EOL> def credentials_owner ( self ) : <EOL> return self . node_settings . user_settings . owner <EOL> def serialize_folder ( self , folder ) : <EOL> return { <EOL> '<STR_LIT:data>' : folder , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:name>' : folder [ '<STR_LIT:name>' ] , <EOL> '<STR_LIT:id>' : folder [ '<STR_LIT:id>' ] , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : self . node_settings . owner . api_url_for ( <EOL> '<STR_LIT>' . format ( self . addon_short_name ) , <EOL> list_id = folder [ '<STR_LIT:id>' ] <EOL> ) , <EOL> } , <EOL> } <EOL> @ property <EOL> def addon_serialized_urls ( self ) : <EOL> node = self . node_settings . owner <EOL> return { <EOL> '<STR_LIT>' : node . api_url_for ( '<STR_LIT>' . format ( self . addon_short_name ) ) , <EOL> '<STR_LIT>' : node . api_url_for ( '<STR_LIT>' . format ( self . addon_short_name ) ) , <EOL> '<STR_LIT>' : node . api_url_for ( '<STR_LIT>' . format ( self . addon_short_name ) ) , <EOL> '<STR_LIT>' : node . api_url_for ( '<STR_LIT>' . format ( self . addon_short_name ) ) , <EOL> '<STR_LIT>' : node . api_url_for ( '<STR_LIT>' . format ( self . addon_short_name ) ) , <EOL> } <EOL> def serialize_citation ( self , citation ) : <EOL> return { <EOL> '<STR_LIT>' : citation , <EOL> '<STR_LIT>' : '<STR_LIT:file>' , <EOL> '<STR_LIT:id>' : citation [ '<STR_LIT:id>' ] , <EOL> } </s>
