<s> import httplib as http <EOL> from modularodm import fields <EOL> from framework . auth . decorators import Auth <EOL> from framework . exceptions import HTTPError <EOL> from website . addons . base import ( <EOL> AddonOAuthNodeSettingsBase , AddonOAuthUserSettingsBase , exceptions , <EOL> ) <EOL> from website . addons . base import StorageAddonBase <EOL> from website . addons . dataverse . client import connect_from_settings_or_401 <EOL> from website . addons . dataverse . serializer import DataverseSerializer <EOL> from website . addons . dataverse . utils import DataverseNodeLogger <EOL> class DataverseProvider ( object ) : <EOL> """<STR_LIT>""" <EOL> name = '<STR_LIT>' <EOL> short_name = '<STR_LIT>' <EOL> serializer = DataverseSerializer <EOL> def __init__ ( self , account = None ) : <EOL> super ( DataverseProvider , self ) . __init__ ( ) <EOL> self . account = account <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' . format ( <EOL> name = self . __class__ . __name__ , <EOL> status = self . account . provider_id if self . account else '<STR_LIT>' <EOL> ) <EOL> class AddonDataverseUserSettings ( AddonOAuthUserSettingsBase ) : <EOL> oauth_provider = DataverseProvider <EOL> serializer = DataverseSerializer <EOL> class AddonDataverseNodeSettings ( StorageAddonBase , AddonOAuthNodeSettingsBase ) : <EOL> oauth_provider = DataverseProvider <EOL> serializer = DataverseSerializer <EOL> dataverse_alias = fields . StringField ( ) <EOL> dataverse = fields . StringField ( ) <EOL> dataset_doi = fields . StringField ( ) <EOL> _dataset_id = fields . StringField ( ) <EOL> dataset = fields . StringField ( ) <EOL> @ property <EOL> def folder_name ( self ) : <EOL> return self . dataset <EOL> @ property <EOL> def dataset_id ( self ) : <EOL> if self . _dataset_id is None and ( self . dataverse_alias and self . dataset_doi ) : <EOL> connection = connect_from_settings_or_401 ( self ) <EOL> dataverse = connection . get_dataverse ( self . dataverse_alias ) <EOL> dataset = dataverse . get_dataset_by_doi ( self . dataset_doi ) <EOL> self . _dataset_id = dataset . id <EOL> self . save ( ) <EOL> return self . _dataset_id <EOL> @ property <EOL> def complete ( self ) : <EOL> return bool ( self . has_auth and self . dataset_doi is not None ) <EOL> @ property <EOL> def folder_id ( self ) : <EOL> return self . dataset_id <EOL> @ property <EOL> def folder_path ( self ) : <EOL> pass <EOL> @ property <EOL> def nodelogger ( self ) : <EOL> auth = None <EOL> if self . user_settings : <EOL> auth = Auth ( self . user_settings . owner ) <EOL> return DataverseNodeLogger ( <EOL> node = self . owner , <EOL> auth = auth <EOL> ) <EOL> def set_folder ( self , dataverse , dataset , auth = None ) : <EOL> self . dataverse_alias = dataverse . alias <EOL> self . dataverse = dataverse . title <EOL> self . dataset_doi = dataset . doi <EOL> self . _dataset_id = dataset . id <EOL> self . dataset = dataset . title <EOL> self . save ( ) <EOL> if auth : <EOL> self . owner . add_log ( <EOL> action = '<STR_LIT>' , <EOL> params = { <EOL> '<STR_LIT>' : self . owner . parent_id , <EOL> '<STR_LIT>' : self . owner . _id , <EOL> '<STR_LIT>' : dataset . title , <EOL> } , <EOL> auth = auth , <EOL> ) <EOL> def _get_fileobj_child_metadata ( self , filenode , user , cookie = None , version = None ) : <EOL> try : <EOL> return super ( AddonDataverseNodeSettings , self ) . _get_fileobj_child_metadata ( filenode , user , cookie = cookie , version = version ) <EOL> except HTTPError as e : <EOL> if e . code == http . NOT_FOUND and version == '<STR_LIT>' : <EOL> return [ ] <EOL> raise <EOL> def clear_settings ( self ) : <EOL> """<STR_LIT>""" <EOL> self . dataverse_alias = None <EOL> self . dataverse = None <EOL> self . dataset_doi = None <EOL> self . _dataset_id = None <EOL> self . dataset = None <EOL> def deauthorize ( self , auth = None , add_log = True ) : <EOL> """<STR_LIT>""" <EOL> self . clear_settings ( ) <EOL> self . clear_auth ( ) <EOL> if add_log and auth : <EOL> node = self . owner <EOL> self . owner . add_log ( <EOL> action = '<STR_LIT>' , <EOL> params = { <EOL> '<STR_LIT>' : node . parent_id , <EOL> '<STR_LIT>' : node . _id , <EOL> } , <EOL> auth = auth , <EOL> ) <EOL> def serialize_waterbutler_credentials ( self ) : <EOL> if not self . has_auth : <EOL> raise exceptions . AddonError ( '<STR_LIT>' ) <EOL> return { '<STR_LIT>' : self . external_account . oauth_secret } <EOL> def serialize_waterbutler_settings ( self ) : <EOL> if not self . folder_id : <EOL> raise exceptions . AddonError ( "<STR_LIT>" ) <EOL> return { <EOL> '<STR_LIT:host>' : self . external_account . oauth_key , <EOL> '<STR_LIT>' : self . dataset_doi , <EOL> '<STR_LIT:id>' : self . dataset_id , <EOL> '<STR_LIT:name>' : self . dataset , <EOL> } <EOL> def create_waterbutler_log ( self , auth , action , metadata ) : <EOL> url = self . owner . web_url_for ( '<STR_LIT>' , path = metadata [ '<STR_LIT:path>' ] , provider = '<STR_LIT>' ) <EOL> self . owner . add_log ( <EOL> '<STR_LIT>' . format ( action ) , <EOL> auth = auth , <EOL> params = { <EOL> '<STR_LIT>' : self . owner . parent_id , <EOL> '<STR_LIT>' : self . owner . _id , <EOL> '<STR_LIT>' : self . dataset , <EOL> '<STR_LIT:filename>' : metadata [ '<STR_LIT>' ] . strip ( '<STR_LIT:/>' ) , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : url , <EOL> '<STR_LIT>' : url + '<STR_LIT>' <EOL> } , <EOL> } , <EOL> ) <EOL> def after_delete ( self , node , user ) : <EOL> self . deauthorize ( Auth ( user = user ) , add_log = True ) <EOL> self . save ( ) <EOL> def on_delete ( self ) : <EOL> self . deauthorize ( add_log = False ) <EOL> self . save ( ) </s>
