class Sheila(StoredObject): _id = fields.StringField(primary=True) _meta = {'optimistic': True} # Simple fields sheila_str = fields.StringField(default='sheila', validate=True, required=True) sheila_int = fields.IntegerField(default=7, validate=MaxValueValidator(9)) sheila_now = fields.DateTimeField() sheila_url = fields.StringField(validate=URLValidator()) sheila_foostop = fields.StringField(required=True, validate=RegexValidator(r'foo$'), list=True) created = fields.DateTimeField(auto_now_add=True) modified = fields.DateTimeField(auto_now=True) # List fields sheila_strs = fields.StringField(list=True, validate=MinLengthValidator(5), list_validate=MinLengthValidator(3)) sheila_nows = fields.DateTimeField(list=True) #, default=[]) sheila_urls = fields.StringField( list=True, validate=[URLValidator(), MinLengthValidator(20)], list_validate=MinLengthValidator(2)) sheila_ints = fields.IntegerField(list=True, validate=MinValueValidator(3), list_validate=MinLengthValidator(2)) # Foreign fields sheila_ron = fields.ForeignField('Ron', backref='ron') sheila_rons = fields.ForeignField('Ron', backref='rons', list=True)
class OsfStorageFileNode(StoredObject): _id = fields.StringField(primary=True, default=lambda: str(bson.ObjectId())) is_deleted = fields.BooleanField(default=False) name = fields.StringField(required=True, index=True) kind = fields.StringField(required=True, index=True) parent = fields.ForeignField('OsfStorageFileNode', index=True) versions = fields.ForeignField('OsfStorageFileVersion', list=True) node_settings = fields.ForeignField('OsfStorageNodeSettings', required=True, index=True) def materialized_path(self): def lineage(): current = self while current: yield current current = current.parent path = os.path.join(*reversed([x.name for x in lineage()])) if self.kind == 'folder': return '/{}/'.format(path) return '/{}'.format(path) def append_file(self, name, save=True): assert self.kind == 'folder' child = OsfStorageFileNode(name=name, kind='file', parent=self, node_settings=self.node_settings) if save: child.save() return child def find_child_by_name(self, name): assert self.kind == 'folder' return self.__class__.find_one( Q('name', 'eq', name) & Q('kind', 'eq', 'file') & Q('parent', 'eq', self)) @property def path(self): return '/{}{}'.format(self._id, '/' if self.kind == 'folder' else '') def get_download_count(self, version=None): """ :param int version: Optional one-based version index """ parts = ['download', self.node_settings.owner._id, self._id] if version is not None: parts.append(version) page = ':'.join([format(part) for part in parts]) _, count = get_basic_counters(page) return count or 0
class OsfStorageTrashedFileNode(StoredObject): """The graveyard for all deleted OsfStorageFileNodes""" _id = fields.StringField(primary=True) name = fields.StringField(required=True, index=True) kind = fields.StringField(required=True, index=True) parent = fields.ForeignField('OsfStorageFileNode', index=True) versions = fields.ForeignField('OsfStorageFileVersion', list=True) node_settings = fields.ForeignField('OsfStorageNodeSettings', required=True, index=True)
class Bar(StoredObject): _id = fields.StringField() ref = fields.ForeignField('foo', backref='my_ref') abs_ref = fields.AbstractForeignField(backref='my_abs_ref') ref_list = fields.ForeignField('foo', backref='my_ref_list', list=True) abs_ref_list = fields.AbstractForeignField(backref='my_abs_ref_list', list=True) _meta = { 'optimistic': True, }
class TrashedFileNode(StoredObject): """The graveyard for all deleted FileNodes""" _id = fields.StringField(primary=True) last_touched = fields.DateTimeField() history = fields.DictionaryField(list=True) versions = fields.ForeignField('FileVersion', list=True) node = fields.ForeignField('node', required=True) parent = fields.AbstractForeignField(default=None) is_file = fields.BooleanField(default=True) provider = fields.StringField(required=True) name = fields.StringField(required=True) path = fields.StringField(required=True) materialized_path = fields.StringField(required=True) checkout = fields.AbstractForeignField('User') deleted_by = fields.AbstractForeignField('User') deleted_on = fields.DateTimeField(auto_now_add=True) tags = fields.ForeignField('Tag', list=True) @property def deep_url(self): """Allows deleted files to resolve to a view that will provide a nice error message and http.GONE """ return self.node.web_url_for('addon_deleted_file', trashed_id=self._id) def restore(self, recursive=True, parent=None): """Recreate a StoredFileNode from the data in this object Will re-point all guids and finally remove itself :raises KeyExistsException: """ data = self.to_storage() data.pop('deleted_on') data.pop('deleted_by') if parent: data['parent'] = parent._id elif data['parent']: # parent is an AbstractForeignField, so it gets stored as tuple data['parent'] = data['parent'][0] restored = FileNode.resolve_class(self.provider, int(self.is_file))(**data) if not restored.parent: raise ValueError('No parent to restore to') restored.save() if recursive: for child in TrashedFileNode.find(Q('parent', 'eq', self)): child.restore(recursive=recursive, parent=restored) TrashedFileNode.remove_one(self) return restored
class NodeWikiPage(GuidStoredObject): redirect_mode = 'redirect' _id = fields.StringField(primary=True) page_name = fields.StringField(validate=validate_page_name) version = fields.IntegerField() date = fields.DateTimeField(auto_now_add=datetime.datetime.utcnow) is_current = fields.BooleanField() content = fields.StringField(default='') user = fields.ForeignField('user') node = fields.ForeignField('node') @property def deep_url(self): return '{}wiki/{}/'.format(self.node.deep_url, self.page_name) @property def url(self): return '{}wiki/{}/'.format(self.node.url, self.page_name) def html(self, node): """The cleaned HTML of the page""" sanitized_content = render_content(self.content, node=node) try: return linkify( sanitized_content, [ nofollow, ], ) except TypeError: logger.warning('Returning unlinkified content.') return sanitized_content def raw_text(self, node): """ The raw text of the page, suitable for using in a test search""" return sanitize(self.html(node), tags=[], strip=True) def save(self, *args, **kwargs): rv = super(NodeWikiPage, self).save(*args, **kwargs) if self.node: self.node.update_search() return rv def rename(self, new_name, save=True): self.page_name = new_name if save: self.save() def to_json(self): return {}
class Blog(StoredObject): _id = fields.StringField(primary=True, optimistic=True) body = fields.StringField(default='blog body') title = fields.StringField(default='asdfasdfasdf', validate=MinLengthValidator(8)) tag = fields.ForeignField('Tag', backref='tagged') tags = fields.ForeignField('Tag', list=True, backref='taggeds') _meta = { 'optimistic': True, 'log_level': logging.DEBUG, }
class NotificationSubscription(StoredObject): _id = fields.StringField( primary=True) # pxyz_wiki_updated, uabc_comment_replies event_name = fields.StringField() # wiki_updated, comment_replies owner = fields.AbstractForeignField() # Notification types none = fields.ForeignField('user', list=True) email_digest = fields.ForeignField('user', list=True) email_transactional = fields.ForeignField('user', list=True) def add_user_to_subscription(self, user, notification_type, save=True): for nt in NOTIFICATION_TYPES: if user in getattr(self, nt): if nt != notification_type: getattr(self, nt).remove(user) else: if nt == notification_type: getattr(self, nt).append(user) if notification_type != 'none' and isinstance( self.owner, Node) and self.owner.parent_node: user_subs = self.owner.parent_node.child_node_subscriptions if self.owner._id not in user_subs.setdefault(user._id, []): user_subs[user._id].append(self.owner._id) self.owner.parent_node.save() if save: self.save() def remove_user_from_subscription(self, user, save=True): for notification_type in NOTIFICATION_TYPES: try: getattr(self, notification_type, []).remove(user) except ValueError: pass if isinstance(self.owner, Node) and self.owner.parent_node: try: self.owner.parent_node.child_node_subscriptions.get( user._id, []).remove(self.owner._id) self.owner.parent_node.save() except ValueError: pass if save: self.save()
class Conference(StoredObject): #: Determines the email address for submission and the OSF url # Example: If endpoint is spsp2014, then submission email will be # [email protected] or [email protected] and the OSF url will # be osf.io/view/spsp2014 endpoint = fields.StringField(primary=True, required=True, unique=True) #: Full name, e.g. "SPSP 2014" name = fields.StringField(required=True) info_url = fields.StringField(required=False, default=None) logo_url = fields.StringField(required=False, default=None) active = fields.BooleanField(required=True) admins = fields.ForeignField('user', list=True, required=False, default=None) #: Whether to make submitted projects public public_projects = fields.BooleanField(required=False, default=True) poster = fields.BooleanField(default=True) talk = fields.BooleanField(default=True) @classmethod def get_by_endpoint(cls, endpoint, active=True): query = Q('endpoint', 'iexact', endpoint) if active: query &= Q('active', 'eq', True) try: return Conference.find_one(query) except ModularOdmException: raise ConferenceError('Endpoint {0} not found'.format(endpoint))
class OsfStorageNodeSettings(AddonNodeSettingsBase): file_tree = fields.ForeignField('OsfStorageFileTree') def copy_contents_to(self, dest): """Copy file tree and contents to destination. Note: destination must be saved before copying so that copied items can refer to it. :param OsfStorageNodeSettings dest: Destination settings object """ dest.save() if self.file_tree: dest.file_tree = copy_file_tree_stable(self.file_tree, dest) dest.save() def after_fork(self, node, fork, user, save=True): clone, message = super(OsfStorageNodeSettings, self).after_fork(node=node, fork=fork, user=user, save=False) self.copy_contents_to(clone) return clone, message def after_register(self, node, registration, user, save=True): clone, message = super(OsfStorageNodeSettings, self).after_register(node=node, registration=registration, user=user, save=False) self.copy_contents_to(clone) return clone, message
class Subject(StoredObject): _id = fields.StringField(primary=True, default=lambda: str(ObjectId())) text = fields.StringField(required=True) parents = fields.ForeignField('subject', list=True) children = fields.ForeignField('subject', list=True) @property def absolute_api_v2_url(self): return api_v2_url('taxonomies/{}/'.format(self._id)) @property def child_count(self): return len(self.children) def get_absolute_url(self): return self.absolute_api_v2_url
class AddonUserSettingsBase(AddonSettingsBase): owner = fields.ForeignField('user', index=True) _meta = { 'abstract': True, } def __repr__(self): if self.owner: return '<{cls} owned by user {uid}>'.format(cls=self.__class__.__name__, uid=self.owner._id) else: return '<{cls} with no owner>'.format(cls=self.__class__.__name__) @property def public_id(self): return None @property def has_auth(self): """Whether the user has added credentials for this addon.""" return False # TODO: Test me @asmacdo @property def nodes_authorized(self): """Get authorized, non-deleted nodes. Returns an empty list if the attached add-on does not include a node model. """ try: schema = self.config.settings_models['node'] except KeyError: return [] return [ node_addon.owner for node_addon in schema.find(Q('user_settings', 'eq', self)) if node_addon.owner and not node_addon.owner.is_deleted ] @property def can_be_merged(self): return hasattr(self, 'merge') def to_json(self, user): ret = super(AddonUserSettingsBase, self).to_json(user) ret['has_auth'] = self.has_auth ret.update({ 'nodes': [ { '_id': node._id, 'url': node.url, 'title': node.title, 'registered': node.is_registration, 'api_url': node.api_url } for node in self.nodes_authorized ] }) return ret
class Conference(StoredObject): #: Determines the email address for submission and the OSF url # Example: If endpoint is spsp2014, then submission email will be # [email protected] or [email protected] and the OSF url will # be osf.io/view/spsp2014 endpoint = fields.StringField(primary=True, required=True, unique=True) #: Full name, e.g. "SPSP 2014" name = fields.StringField(required=True) info_url = fields.StringField(required=False, default=None) logo_url = fields.StringField(required=False, default=None) location = fields.StringField(required=False, default=None) start_date = fields.DateTimeField(default=None) end_date = fields.DateTimeField(default=None) active = fields.BooleanField(required=True) admins = fields.ForeignField('user', list=True, required=False, default=None) #: Whether to make submitted projects public public_projects = fields.BooleanField(required=False, default=True) poster = fields.BooleanField(default=True) talk = fields.BooleanField(default=True) # field_names are used to customize the text on the conference page, the categories # of submissions, and the email adress to send material to. field_names = fields.DictionaryField(default=lambda: DEFAULT_FIELD_NAMES) # Cached number of submissions num_submissions = fields.IntegerField(default=0) @classmethod def get_by_endpoint(cls, endpoint, active=True): query = Q('endpoint', 'iexact', endpoint) if active: query &= Q('active', 'eq', True) try: return Conference.find_one(query) except ModularOdmException: raise ConferenceError('Endpoint {0} not found'.format(endpoint))
class NodeLicenseRecord(StoredObject): _id = fields.StringField(primary=True, default=lambda: str(ObjectId())) node_license = fields.ForeignField('nodelicense', required=True) # Deliberately left as a StringField to support year ranges (e.g. 2012-2015) year = fields.StringField() copyright_holders = fields.StringField(list=True) @property def name(self): return self.node_license.name if self.node_license else None @property def text(self): return self.node_license.text if self.node_license else None @property def id(self): return self.node_license.id if self.node_license else None def to_json(self): return serialize_node_license_record(self) def copy(self): copied = NodeLicenseRecord(node_license=self.node_license, year=self.year, copyright_holders=self.copyright_holders) copied.save() return copied
class SanctionTestClass(TokenApprovableSanction): DISPLAY_NAME = 'test class' initiated_by = fields.ForeignField('user', backref='tested') def _validate_authorizer(self, user): return 'flag' in user.system_tags
class OsfStorageNodeSettings(AddonNodeSettingsBase): file_tree = fields.ForeignField('OsfStorageFileTree') def copy_contents_to(self, dest): """Copy file tree and contents to destination. Note: destination must be saved before copying so that copied items can refer to it. :param OsfStorageNodeSettings dest: Destination settings object """ dest.save() if self.file_tree: dest.file_tree = copy_file_tree(self.file_tree, dest) dest.save() def after_fork(self, node, fork, user, save=True): clone, message = super(OsfStorageNodeSettings, self).after_fork(node=node, fork=fork, user=user, save=False) self.copy_contents_to(clone) return clone, message def after_register(self, node, registration, user, save=True): clone, message = super(OsfStorageNodeSettings, self).after_register(node=node, registration=registration, user=user, save=False) self.copy_contents_to(clone) return clone, message def serialize_waterbutler_settings(self): ret = { 'callback': self.owner.api_url_for( 'osf_storage_update_metadata_hook', _absolute=True, ), 'metadata': self.owner.api_url_for( 'osf_storage_get_metadata_hook', _absolute=True, ), } ret.update(settings.WATERBUTLER_SETTINGS) return ret def serialize_waterbutler_credentials(self): return settings.WATERBUTLER_CREDENTIALS def create_waterbutler_log(self, auth, action, metadata): pass def get_waterbutler_render_url(self, path, **kwargs): return self.owner.web_url_for('osf_storage_view_file', path=path)
class OsfStorageFileVersion(StoredObject): """A version of an OsfStorageFileNode. contains information about where the file is located, hashes and datetimes """ _id = fields.StringField(primary=True, default=lambda: str(bson.ObjectId())) creator = fields.ForeignField('user', required=True) # Date version record was created. This is the date displayed to the user. date_created = fields.DateTimeField(auto_now_add=True) # Dictionary specifying all information needed to locate file on backend # { # 'service': 'cloudfiles', # required # 'container': 'osf', # required # 'object': '20c53b', # required # 'worker_url': '127.0.0.1', # 'worker_host': 'upload-service-1', # } location = fields.DictionaryField(validate=utils.validate_location) # Dictionary containing raw metadata from upload service response # { # 'size': 1024, # required # 'content_type': 'text/plain', # required # 'date_modified': '2014-11-07T20:24:15', # required # 'md5': 'd077f2', # } metadata = fields.DictionaryField() size = fields.IntegerField() content_type = fields.StringField() # Date file modified on third-party backend. Not displayed to user, since # this date may be earlier than the date of upload if the file already # exists on the backend date_modified = fields.DateTimeField() @property def location_hash(self): return self.location['object'] def is_duplicate(self, other): return self.location_hash == other.location_hash def update_metadata(self, metadata): self.metadata.update(metadata) self.content_type = self.metadata.get('contentType', None) try: self.size = self.metadata['size'] self.date_modified = parse_date(self.metadata['modified'], ignoretz=True) except KeyError as err: raise errors.MissingFieldError(str(err)) self.save()
class Conference(StoredObject): #: Determines the email address for submission and the OSF url # Example: If endpoint is spsp2014, then submission email will be # [email protected] or [email protected] and the OSF url will # be osf.io/view/spsp2014 endpoint = fields.StringField(primary=True, required=True, unique=True) #: Full name, e.g. "SPSP 2014" name = fields.StringField(required=True) info_url = fields.StringField(required=False, default=None) logo_url = fields.StringField(required=False, default=None) active = fields.BooleanField(required=True) admins = fields.ForeignField('user', list=True, required=False, default=None) #: Whether to make submitted projects public public_projects = fields.BooleanField(required=False, default=True) poster = fields.BooleanField(default=True) talk = fields.BooleanField(default=True) # field_names are used to customize the text on the conference page, the categories # of submissions, and the email adress to send material to. field_names = fields.DictionaryField( default=lambda: { 'submission1': 'poster', 'submission2': 'talk', 'submission1_plural': 'posters', 'submission2_plural': 'talks', 'meeting_title_type': 'Posters & Talks', 'add_submission': 'poster or talk', 'mail_subject': 'Presentation title', 'mail_message_body': 'Presentation abstract (if any)', 'mail_attachment': 'Your presentation file (e.g., PowerPoint, PDF, etc.)' }) # Cached number of submissions num_submissions = fields.IntegerField(default=0) @classmethod def get_by_endpoint(cls, endpoint, active=True): query = Q('endpoint', 'iexact', endpoint) if active: query &= Q('active', 'eq', True) try: return Conference.find_one(query) except ModularOdmException: raise ConferenceError('Endpoint {0} not found'.format(endpoint))
class TrashedFileNode(StoredObject): """The graveyard for all deleted FileNodes""" _id = fields.StringField(primary=True) last_touched = fields.DateTimeField() history = fields.DictionaryField(list=True) versions = fields.ForeignField('FileVersion', list=True) node = fields.ForeignField('node', required=True) parent = fields.AbstractForeignField(default=None) is_file = fields.BooleanField(default=True) provider = fields.StringField(required=True) name = fields.StringField(required=True) path = fields.StringField(required=True) materialized_path = fields.StringField(required=True) checkout = fields.AbstractForeignField('User') deleted_by = fields.AbstractForeignField('User') deleted_on = fields.DateTimeField(auto_now_add=True) @property def deep_url(self): """Allows deleted files to resolve to a view that will provide a nice error message and http.GONE """ return self.node.web_url_for('addon_deleted_file', trashed_id=self._id) def restore(self): """Recreate a StoredFileNode from the data in this object Will re-point all guids and finally remove itself :raises KeyExistsException: """ data = self.to_storage() data.pop('deleted_on') data.pop('deleted_by') restored = FileNode.resolve_class(self.provider, int(self.is_file))(**data) restored.save() TrashedFileNode.remove_one(self) return restored
class RepoMeta(StoredObject): _meta = {'optimistic': True} _id = fields.StringField(primary=True) is_public = fields.BooleanField(default=False) access_read = fields.ForeignField( 'KeyAuthContext', backref='read_repos', list=True, ) access_write = fields.ForeignField( 'KeyAuthContext', backref='write_repos', list=True, ) access_admin = fields.ForeignField( 'KeyAuthContext', backref='admin_repos', list=True, )
class QueuedMail(StoredObject): _id = fields.StringField(primary=True, default=lambda: str(bson.ObjectId())) user = fields.ForeignField('User', index=True, required=True) to_addr = fields.StringField() send_at = fields.DateTimeField(index=True, required=True) # string denoting the template, presend to be used. Has to be an index of queue_mail types email_type = fields.StringField(index=True, required=True) # dictionary with variables used to populate mako template and store information used in presends # Example: # self.data = { # 'nid' : 'ShIpTo', # 'fullname': 'Florence Welch', #} data = fields.DictionaryField() sent_at = fields.DateTimeField(index=True) def send_mail(self): """ Grabs the data from this email, checks for user subscription to help mails, constructs the mail object and checks presend. Then attempts to send the email through send_mail() :return: boolean based on whether email was sent. """ mail_struct = queue_mail_types[self.email_type] presend = mail_struct['presend'](self) mail = Mail( mail_struct['template'], subject=mail_struct['subject'], categories=mail_struct.get('categories', None) ) self.data['osf_url'] = settings.DOMAIN if presend and self.user.is_active and self.user.osf_mailing_lists.get(settings.OSF_HELP_LIST): send_mail(self.to_addr or self.user.username, mail, mimetype='html', **(self.data or {})) self.sent_at = datetime.utcnow() self.save() return True else: self.__class__.remove_one(self) return False def find_sent_of_same_type_and_user(self): """ Queries up for all emails of the same type as self, sent to the same user as self. Does not look for queue-up emails. :return: a list of those emails """ return self.__class__.find( Q('email_type', 'eq', self.email_type) & Q('user', 'eq', self.user) & Q('sent_at', 'ne', None) )
class Conference(StoredObject): #: Determines the email address for submission and the OSF url # Example: If endpoint is spsp2014, then submission email will be # [email protected] or [email protected] and the OSF url will # be osf.io/view/spsp2014 endpoint = fields.StringField(primary=True, required=True, unique=True) #: Full name, e.g. "SPSP 2014" name = fields.StringField(required=True) info_url = fields.StringField(required=False, default=None) logo_url = fields.StringField(required=False, default=None) active = fields.BooleanField(required=True) admins = fields.ForeignField('user', list=True, required=False, default=None) #: Whether to make submitted projects public public_projects = fields.BooleanField(required=False, default=True)
class OsfStorageFileVersion(StoredObject): _id = oid_primary_key creator = fields.ForeignField('user', required=True) date_created = fields.DateTimeField(auto_now_add=True) # Dictionary specifying all information needed to locate file on backend # { # 'service': 'buttfiles', # required # 'container': 'osf', # required # 'object': '20c53b', # required # 'worker_url': '127.0.0.1', # 'worker_host': 'upload-service-1', # } location = fields.DictionaryField(validate=validate_location) # Dictionary containing raw metadata from upload service response # { # 'size': 1024, # required # 'content_type': 'text/plain', # required # 'date_modified': '2014-11-07T20:24:15', # required # 'md5': 'd077f2', # } metadata = fields.DictionaryField() size = fields.IntegerField() content_type = fields.StringField() date_modified = fields.DateTimeField() @property def location_hash(self): return self.location['object'] def is_duplicate(self, other): return self.location_hash == other.location_hash def update_metadata(self, metadata): self.metadata.update(metadata) for key, parser in metadata_fields.iteritems(): try: value = metadata[key] except KeyError: raise errors.MissingFieldError setattr(self, key, parser(value)) self.save()
class WikiPage(StoredObject): _meta = {'optimistic': True} _id = fields.StringField(primary=True, index=True) versions = fields.ForeignField('WikiPageVersion', backref='page',list=True) title = fields.StringField(unique=True) @property def current(self): return self.versions[-1] @property def content(self): return self.current.content def update(self, text): new = WikiPageVersion(text=text) new.save() self.versions.append(new)
class GuidFile(GuidStoredObject): redirect_mode = 'proxy' _id = fields.StringField(primary=True) node = fields.ForeignField('node', required=True, index=True) _meta = { 'abstract': True, } @property def file_url(self): raise NotImplementedError @property def deep_url(self): if self.node is None: raise ValueError('Node field must be defined.') return os.path.join( self.node.deep_url, self.file_url, )
class AddonDataverseNodeSettings(StorageAddonBase, AddonOAuthNodeSettingsBase): oauth_provider = DataverseProvider serializer = serializer.DataverseSerializer dataverse_alias = fields.StringField() dataverse = fields.StringField() dataset_doi = fields.StringField() _dataset_id = fields.StringField() dataset = fields.StringField() # Legacy fields study_hdl = fields.StringField() # Now dataset_doi study = fields.StringField() # Now dataset foreign_user_settings = fields.ForeignField('addondataverseusersettings', backref='authorized') # Legacy settings objects won't have IDs @property def folder_name(self): return self.dataset @property def dataset_id(self): if self._dataset_id is None: connection = connect_from_settings_or_401(self.user_settings) dataverse = connection.get_dataverse(self.dataverse_alias) dataset = dataverse.get_dataset_by_doi(self.dataset_doi) self._dataset_id = dataset.id self.save() return self._dataset_id @dataset_id.setter def dataset_id(self, value): self._dataset_id = value @property def complete(self): return bool(self.has_auth and self.dataset_doi is not None) def _get_fileobj_child_metadata(self, filenode, user, cookie=None, version=None): kwargs = dict( provider=self.config.short_name, path=filenode.get('path', ''), node=self.owner, user=user, view_only=True, ) if cookie: kwargs['cookie'] = cookie if version: kwargs['version'] = version metadata_url = waterbutler_url_for('metadata', **kwargs) res = requests.get(metadata_url) if res.status_code != 200: # The Dataverse API returns a 404 if the dataset has no published files if res.status_code == http.NOT_FOUND and version == 'latest-published': return [] raise HTTPError(res.status_code, data={ 'error': res.json(), }) # TODO: better throttling? sleep(1.0 / 5.0) return res.json().get('data', []) def find_or_create_file_guid(self, path): file_id = path.strip('/') if path else '' return DataverseFile.get_or_create(node=self.owner, file_id=file_id) def delete(self, save=True): self.deauthorize(add_log=False) super(AddonDataverseNodeSettings, self).delete(save) def clear_settings(self): """Clear selected Dataverse and dataset""" self.dataverse_alias = None self.dataverse = None self.dataset_doi = None self.dataset_id = None self.dataset = None def deauthorize(self, auth=None, add_log=True): """Remove user authorization from this node and log the event.""" self.clear_settings() self.clear_auth() # Also performs a save # Log can't be added without auth if add_log and auth: node = self.owner self.owner.add_log( action='dataverse_node_deauthorized', params={ 'project': node.parent_id, 'node': node._id, }, auth=auth, ) def serialize_waterbutler_credentials(self): if not self.has_auth: raise exceptions.AddonError('Addon is not authorized') return {'token': self.external_account.oauth_secret} def serialize_waterbutler_settings(self): return { 'host': self.external_account.oauth_key, 'doi': self.dataset_doi, 'id': self.dataset_id, 'name': self.dataset, } def create_waterbutler_log(self, auth, action, metadata): url = self.owner.web_url_for('addon_view_or_download_file', path=metadata['path'], provider='dataverse') self.owner.add_log( 'dataverse_{0}'.format(action), auth=auth, params={ 'project': self.owner.parent_id, 'node': self.owner._id, 'dataset': self.dataset, 'filename': metadata['materialized'].strip('/'), 'urls': { 'view': url, 'download': url + '?action=download' }, }, ) ##### Callback overrides ##### # Note: Registering Dataverse content is disabled for now def before_register_message(self, node, user): """Return warning text to display if user auth will be copied to a registration. """ category = node.project_or_component if self.user_settings and self.user_settings.has_auth: return ( u'The contents of Dataverse add-ons cannot be registered at this time; ' u'the Dataverse dataset linked to this {category} will not be included ' u'as part of this registration.').format(**locals()) # backwards compatibility before_register = before_register_message def before_remove_contributor_message(self, node, removed): """Return warning text to display if removed contributor is the user who authorized the Dataverse addon """ if self.user_settings and self.user_settings.owner == removed: category = node.project_or_component name = removed.fullname return ( 'The Dataverse add-on for this {category} is authenticated by {name}. ' 'Removing this user will also remove write access to Dataverse ' 'unless another contributor re-authenticates the add-on.' ).format(**locals()) # backwards compatibility before_remove_contributor = before_remove_contributor_message # Note: Registering Dataverse content is disabled for now # def after_register(self, node, registration, user, save=True): # """After registering a node, copy the user settings and save the # chosen folder. # # :return: A tuple of the form (cloned_settings, message) # """ # clone, message = super(AddonDataverseNodeSettings, self).after_register( # node, registration, user, save=False # ) # # Copy user_settings and add registration data # if self.has_auth and self.folder is not None: # clone.user_settings = self.user_settings # clone.registration_data['folder'] = self.folder # if save: # clone.save() # return clone, message def after_fork(self, node, fork, user, save=True): """After forking, copy user settings if the user is the one who authorized the addon. :return: A tuple of the form (cloned_settings, message) """ clone, _ = super(AddonDataverseNodeSettings, self).after_fork(node=node, fork=fork, user=user, save=False) if self.user_settings and self.user_settings.owner == user: clone.user_settings = self.user_settings message = ( 'Dataverse authorization copied to forked {cat}.').format( cat=fork.project_or_component) else: message = ( 'Dataverse authorization not copied to forked {cat}. You may ' 'authorize this fork on the <u><a href="{url}">Settings</a></u> ' 'page.').format(url=fork.web_url_for('node_setting'), cat=fork.project_or_component) if save: clone.save() return clone, message def after_remove_contributor(self, node, removed, auth=None): """If the removed contributor was the user who authorized the Dataverse addon, remove the auth credentials from this node. Return the message text that will be displayed to the user. """ if self.user_settings and self.user_settings.owner == removed: self.user_settings = None self.save() message = ( u'Because the Dataverse add-on for {category} "{title}" was authenticated ' u'by {user}, authentication information has been deleted.' ).format(category=node.category_display, title=node.title, user=removed.fullname) if not auth or auth.user != removed: url = node.web_url_for('node_setting') message += ( u' You can re-authenticate on the <u><a href="{url}">Settings</a></u> page.' ).format(url=url) # return message def after_delete(self, node, user): self.deauthorize(Auth(user=user), add_log=True) self.save()
class AddonS3NodeSettings(StorageAddonBase, AddonNodeSettingsBase): registration_data = fields.DictionaryField() bucket = fields.StringField() user_settings = fields.ForeignField('addons3usersettings', backref='authorized') @property def folder_name(self): return self.bucket def find_or_create_file_guid(self, path): path = path.lstrip('/') return S3GuidFile.get_or_create(node=self.owner, path=path) @property def display_name(self): return u'{0}: {1}'.format(self.config.full_name, self.bucket) @property def complete(self): return self.has_auth and self.bucket is not None def authorize(self, user_settings, save=False): self.user_settings = user_settings self.owner.add_log( action='s3_node_authorized', params={ 'project': self.owner.parent_id, 'node': self.owner._id, }, auth=Auth(user_settings.owner), ) if save: self.save() def deauthorize(self, auth=None, log=True, save=False): self.registration_data = {} self.bucket = None self.user_settings = None if log: self.owner.add_log( action='s3_node_deauthorized', params={ 'project': self.owner.parent_id, 'node': self.owner._id, }, auth=auth, ) if save: self.save() def delete(self, save=True): self.deauthorize(log=False, save=False) super(AddonS3NodeSettings, self).delete(save=save) def serialize_waterbutler_credentials(self): if not self.has_auth: raise exceptions.AddonError( 'Cannot serialize credentials for S3 addon') return { 'access_key': self.user_settings.access_key, 'secret_key': self.user_settings.secret_key, } def serialize_waterbutler_settings(self): if not self.bucket: raise exceptions.AddonError( 'Cannot serialize settings for S3 addon') return {'bucket': self.bucket} def create_waterbutler_log(self, auth, action, metadata): url = self.owner.web_url_for('addon_view_or_download_file', path=metadata['path'], provider='s3') self.owner.add_log( 's3_{0}'.format(action), auth=auth, params={ 'project': self.owner.parent_id, 'node': self.owner._id, 'path': metadata['materialized'], 'bucket': self.bucket, 'urls': { 'view': url, 'download': url + '?action=download' } }, ) def to_json(self, user): ret = super(AddonS3NodeSettings, self).to_json(user) user_settings = user.get_addon('s3') ret.update({ 'bucket': self.bucket or '', 'has_bucket': self.bucket is not None, 'user_is_owner': (self.user_settings and self.user_settings.owner == user), 'user_has_auth': bool(user_settings) and user_settings.has_auth, 'node_has_auth': self.has_auth, 'owner': None, 'bucket_list': None, 'is_registration': self.owner.is_registration, 'valid_credentials': user_settings and user_settings.is_valid, }) if self.has_auth: ret['owner'] = self.user_settings.owner.fullname ret['owner_url'] = self.user_settings.owner.url ret['node_has_auth'] = True return ret @property def is_registration(self): return True if self.registration_data else False @property def has_auth(self): return bool(self.user_settings and self.user_settings.has_auth) #TODO Update callbacks def before_register(self, node, user): """ :param Node node: :param User user: :return str: Alert message """ category = node.project_or_component if self.user_settings and self.user_settings.has_auth: return ( u'The contents of S3 add-ons cannot be registered at this time; ' u'the S3 bucket linked to this {category} will not be included ' u'as part of this registration.').format(**locals()) def after_fork(self, node, fork, user, save=True): """ :param Node node: Original node :param Node fork: Forked node :param User user: User creating fork :param bool save: Save settings after callback :return tuple: Tuple of cloned settings and alert message """ clone, _ = super(AddonS3NodeSettings, self).after_fork(node, fork, user, save=False) # Copy authentication if authenticated by forking user if self.user_settings and self.user_settings.owner == user: clone.user_settings = self.user_settings clone.bucket = self.bucket message = ( 'Amazon Simple Storage authorization copied to forked {cat}.' ).format(cat=fork.project_or_component, ) else: message = ( 'Amazon Simple Storage authorization not copied to forked {cat}. You may ' 'authorize this fork on the <a href={url}>Settings</a> ' 'page.').format(cat=fork.project_or_component, url=fork.url + 'settings/') if save: clone.save() return clone, message def before_fork(self, node, user): """ :param Node node: :param User user: :return str: Alert message """ if self.user_settings and self.user_settings.owner == user: return ( 'Because you have authenticated the S3 add-on for this ' '{cat}, forking it will also transfer your authorization to ' 'the forked {cat}.').format(cat=node.project_or_component, ) return ( 'Because this S3 add-on has been authenticated by a different ' 'user, forking it will not transfer authentication to the forked ' '{cat}.').format(cat=node.project_or_component, ) def before_remove_contributor(self, node, removed): """ :param Node node: :param User removed: :return str: Alert message """ if self.user_settings and self.user_settings.owner == removed: return ( 'The Amazon Simple Storage add-on for this {category} is authenticated ' 'by {user}. Removing this user will also remove access ' 'to {bucket} unless another contributor re-authenticates.' ).format(category=node.project_or_component, user=removed.fullname, bucket=self.bucket) def after_remove_contributor(self, node, removed, auth=None): """ :param Node node: :param User removed: :return str: Alert message """ if self.user_settings and self.user_settings.owner == removed: self.user_settings = None self.bucket = None self.save() message = ( u'Because the Amazon Simple Storage add-on for {category} "{title}" was ' u'authenticated by {user}, authentication information has been deleted.' ).format(category=node.category_display, title=node.title, user=removed.fullname) if not auth or auth.user != removed: url = node.web_url_for('node_setting') message += ( u' You can re-authenticate on the <a href="{url}">Settings</a> page.' ).format(url=url) # return message def after_delete(self, node, user): self.deauthorize(Auth(user=user), log=True, save=True)
class StoredFileNode(StoredObject): """The storage backend for FileNode objects. This class should generally not be used or created manually as FileNode contains all the helpers required. A FileNode wraps a StoredFileNode to provider usable abstraction layer """ __indices__ = [{ 'unique': False, 'key_or_list': [('path', pymongo.ASCENDING), ('node', pymongo.ASCENDING), ('is_file', pymongo.ASCENDING), ('provider', pymongo.ASCENDING)] }, { 'unique': False, 'key_or_list': [('node', pymongo.ASCENDING), ('is_file', pymongo.ASCENDING), ('provider', pymongo.ASCENDING)] }] _id = fields.StringField(primary=True, default=lambda: str(bson.ObjectId())) # The last time the touch method was called on this FileNode last_touched = fields.DateTimeField() # A list of dictionaries sorted by the 'modified' key # The raw output of the metadata request deduped by etag # Add regardless it can be pinned to a version or not history = fields.DictionaryField(list=True) # A concrete version of a FileNode, must have an identifier versions = fields.ForeignField('FileVersion', list=True) node = fields.ForeignField('Node', required=True) parent = fields.ForeignField('StoredFileNode', default=None) is_file = fields.BooleanField(default=True) provider = fields.StringField(required=True) name = fields.StringField(required=True) path = fields.StringField(required=True) materialized_path = fields.StringField(required=True) # The User that has this file "checked out" # Should only be used for OsfStorage checkout = fields.AbstractForeignField('User') #Tags for a file, currently only used for osfStorage tags = fields.ForeignField('Tag', list=True) # For Django compatibility @property def pk(self): return self._id # For Django compatibility # TODO Find a better way @property def node_id(self): return self.node._id @property def deep_url(self): return self.wrapped().deep_url def wrapped(self): """Wrap self in a FileNode subclass """ return FileNode.resolve_class(self.provider, int(self.is_file))(self) def get_guid(self, create=False): """Attempt to find a Guid that points to this object. One will be created if requested. :rtype: Guid """ try: # Note sometimes multiple GUIDs can exist for # a single object. Just go with the first one return Guid.find(Q('referent', 'eq', self))[0] except IndexError: if not create: return None return Guid.generate(self)
class User(GuidStoredObject, AddonModelMixin): redirect_mode = 'proxy' # Node fields that trigger an update to the search engine on save SEARCH_UPDATE_FIELDS = { 'fullname', 'given_name', 'middle_names', 'family_name', 'suffix', 'merged_by', 'date_disabled', 'jobs', 'schools', 'social', } SOCIAL_FIELDS = { 'orcid': 'http://orcid.com/{}', 'github': 'http://github.com/{}', 'scholar': 'http://scholar.google.com/citation?user={}', 'twitter': 'http://twitter.com/{}', 'personal': '{}', 'linkedIn': 'https://www.linkedin.com/profile/view?id={}', 'impactStory': 'https://impactstory.org/{}', 'researcherId': 'http://researcherid.com/rid/{}', } _id = fields.StringField(primary=True) # NOTE: In the OSF, username is an email # May be None for unregistered contributors username = fields.StringField(required=False, unique=True, index=True) password = fields.StringField() fullname = fields.StringField(required=True, validate=string_required) is_registered = fields.BooleanField() # TODO: Migrate unclaimed users to the new style, then remove this attribute # Note: No new users should be created where is_claimed is False. # As of 9 Sep 2014, there were 331 legacy unclaimed users in the system. # When those users are migrated to the new style, this attribute should be # removed. is_claimed = fields.BooleanField() # Tags for internal use system_tags = fields.StringField(list=True) # Per-project unclaimed user data: # Format: { # <project_id>: { # 'name': <name that referrer provided>, # 'referrer_id': <user ID of referrer>, # 'token': <token used for verification urls>, # 'email': <email the referrer provided or None>, # 'last_sent': <timestamp of last email sent to referrer or None> # } # ... # } # TODO: add validation unclaimed_records = fields.DictionaryField(required=False) # The user who merged this account merged_by = fields.ForeignField('user', default=None, backref="merged") #: Verification key used for resetting password verification_key = fields.StringField() emails = fields.StringField(list=True) # Email verification tokens # Format: { # <token> : {'email': <email address>, # 'expiration': <datetime>} # } email_verifications = fields.DictionaryField() # Format: { # 'list1': True, # 'list2: False, # ... # } mailing_lists = fields.DictionaryField() aka = fields.StringField(list=True) date_registered = fields.DateTimeField(auto_now_add=dt.datetime.utcnow) # Watched nodes are stored via a list of WatchConfigs watched = fields.ForeignField("WatchConfig", list=True, backref="watched") # Recently added contributors stored via a list of users recently_added = fields.ForeignField("user", list=True, backref="recently_added") # CSL names given_name = fields.StringField() middle_names = fields.StringField() family_name = fields.StringField() suffix = fields.StringField() # Employment history # Format: { # 'title': <position or job title>, # 'institution': <institution or organization>, # 'department': <department>, # 'location': <location>, # 'startMonth': <start month>, # 'startYear': <start year>, # 'endMonth': <end month>, # 'endYear': <end year>, # 'ongoing: <boolean> # } jobs = fields.DictionaryField(list=True, validate=validate_history_item) # Educational history # Format: { # 'degree': <position or job title>, # 'institution': <institution or organization>, # 'department': <department>, # 'location': <location>, # 'startMonth': <start month>, # 'startYear': <start year>, # 'endMonth': <end month>, # 'endYear': <end year>, # 'ongoing: <boolean> # } schools = fields.DictionaryField(list=True, validate=validate_history_item) # Social links # Format: { # 'personal': <personal site>, # 'twitter': <twitter id>, # } social = fields.DictionaryField(validate=validate_social) api_keys = fields.ForeignField('apikey', list=True, backref='keyed') piwik_token = fields.StringField() date_last_login = fields.DateTimeField() date_confirmed = fields.DateTimeField() # When the user was disabled. date_disabled = fields.DateTimeField() # Format: { # 'node_id': 'timestamp' # } comments_viewed_timestamp = fields.DictionaryField() _meta = {'optimistic': True} def __repr__(self): return '<User({0!r}) with id {1!r}>'.format(self.username, self._id) @classmethod def create_unregistered(cls, fullname, email=None): """Creates a new unregistered user. :raises: DuplicateEmailError if a user with the given email address is already in the database. """ user = cls( username=email, fullname=fullname, ) user.update_guessed_names() if email: user.emails.append(email) user.is_registered = False return user @classmethod def create(cls, username, password, fullname): user = cls( username=username, fullname=fullname, ) user.update_guessed_names() user.set_password(password) return user @classmethod def create_unconfirmed(cls, username, password, fullname, do_confirm=True): """Create a new user who has begun registration but needs to verify their primary email address (username). """ user = cls.create(username, password, fullname) user.add_email_verification(username) user.is_registered = False return user @classmethod def create_confirmed(cls, username, password, fullname): user = cls.create(username, password, fullname) user.is_registered = True user.is_claimed = True user.date_confirmed = user.date_registered return user def update_guessed_names(self): """Updates the CSL name fields inferred from the the full name. """ parsed = utils.impute_names(self.fullname) self.given_name = parsed['given'] self.middle_names = parsed['middle'] self.family_name = parsed['family'] self.suffix = parsed['suffix'] def register(self, username, password=None): """Registers the user. """ self.username = username if password: self.set_password(password) if username not in self.emails: self.emails.append(username) self.is_registered = True self.is_claimed = True self.date_confirmed = dt.datetime.utcnow() self.update_search() self.update_search_nodes() # Emit signal that a user has confirmed signals.user_confirmed.send(self) return self def add_unclaimed_record(self, node, referrer, given_name, email=None): """Add a new project entry in the unclaimed records dictionary. :param Node node: Node this unclaimed user was added to. :param User referrer: User who referred this user. :param str given_name: The full name that the referrer gave for this user. :param str email: The given email address. :returns: The added record """ if not node.can_edit(user=referrer): raise PermissionsError( 'Referrer does not have permission to add a contributor ' 'to project {0}'.format(node._primary_key)) project_id = node._primary_key referrer_id = referrer._primary_key if email: clean_email = email.lower().strip() else: clean_email = None record = { 'name': given_name, 'referrer_id': referrer_id, 'token': generate_confirm_token(), 'email': clean_email } self.unclaimed_records[project_id] = record return record def display_full_name(self, node=None): """Return the full name , as it would display in a contributor list for a given node. NOTE: Unclaimed users may have a different name for different nodes. """ if node: unclaimed_data = self.unclaimed_records.get( node._primary_key, None) if unclaimed_data: return unclaimed_data['name'] return self.fullname @property def is_active(self): """Returns True if the user is active. The user must have activated their account, must not be deleted, suspended, etc. :return: bool """ return (self.is_registered and self.password is not None and not self.is_merged and not self.is_disabled and self.is_confirmed()) def get_unclaimed_record(self, project_id): """Get an unclaimed record for a given project_id. :raises: ValueError if there is no record for the given project. """ try: return self.unclaimed_records[project_id] except KeyError: # reraise as ValueError raise ValueError( 'No unclaimed record for user {self._id} on node {project_id}'. format(**locals())) def get_claim_url(self, project_id, external=False): """Return the URL that an unclaimed user should use to claim their account. Return ``None`` if there is no unclaimed_record for the given project ID. :param project_id: The project ID for the unclaimed record :raises: ValueError if a record doesn't exist for the given project ID :rtype: dict :returns: The unclaimed record for the project """ uid = self._primary_key base_url = settings.DOMAIN if external else '/' unclaimed_record = self.get_unclaimed_record(project_id) token = unclaimed_record['token'] return '{base_url}user/{uid}/{project_id}/claim/?token={token}'\ .format(**locals()) def set_password(self, raw_password): """Set the password for this user to the hash of ``raw_password``.""" self.password = generate_password_hash(raw_password) def check_password(self, raw_password): """Return a boolean of whether ``raw_password`` was correct.""" if not self.password or not raw_password: return False return check_password_hash(self.password, raw_password) def change_password(self, raw_old_password, raw_new_password, raw_confirm_password): """Change the password for this user to the hash of ``raw_new_password``.""" raw_old_password = (raw_old_password or '').strip() raw_new_password = (raw_new_password or '').strip() raw_confirm_password = (raw_confirm_password or '').strip() issues = [] if not self.check_password(raw_old_password): issues.append('Old password is invalid') elif raw_old_password == raw_new_password: issues.append('Password cannot be the same') if not raw_old_password or not raw_new_password or not raw_confirm_password: issues.append('Passwords cannot be blank') elif len(raw_new_password) < 6: issues.append('Password should be at least six characters') if raw_new_password != raw_confirm_password: issues.append('Password does not match the confirmation') if issues: raise ChangePasswordError(issues) self.set_password(raw_new_password) def _set_email_token_expiration(self, token, expiration=None): """Set the expiration date for given email token. :param str token: The email token to set the expiration for. :param datetime expiration: Datetime at which to expire the token. If ``None``, the token will expire after ``settings.EMAIL_TOKEN_EXPIRATION`` hours. This is only used for testing purposes. """ expiration = expiration or (dt.datetime.utcnow() + dt.timedelta( hours=settings.EMAIL_TOKEN_EXPIRATION)) self.email_verifications[token]['expiration'] = expiration return expiration def add_email_verification(self, email, expiration=None): """Add an email verification token for a given email.""" token = generate_confirm_token() self.email_verifications[token] = {'email': email.lower()} self._set_email_token_expiration(token, expiration=expiration) return token def get_confirmation_token(self, email, force=False): """Return the confirmation token for a given email. :param str email: Email to get the token for. :param bool force: If an expired token exists for the given email, generate a new token and return that token. :raises: ExpiredTokenError if trying to access a token that is expired and force=False. :raises: KeyError if there no token for the email. """ for token, info in self.email_verifications.items(): if info['email'].lower() == email.lower(): if info['expiration'] < dt.datetime.utcnow(): if not force: raise ExpiredTokenError( 'Token for email "{0}" is expired'.format(email)) else: new_token = self.add_email_verification(email) self.save() return new_token return token raise KeyError('No confirmation token for email "{0}"'.format(email)) def get_confirmation_url(self, email, external=True, force=False): """Return the confirmation url for a given email. :raises: ExpiredTokenError if trying to access a token that is expired. :raises: KeyError if there is no token for the email. """ base = settings.DOMAIN if external else '/' token = self.get_confirmation_token(email, force=force) return "{0}confirm/{1}/{2}/".format(base, self._primary_key, token) def verify_confirmation_token(self, token): """Return whether or not a confirmation token is valid for this user. :rtype: bool """ if token in self.email_verifications.keys(): return self.email_verifications.get( token)['expiration'] > dt.datetime.utcnow() return False def verify_claim_token(self, token, project_id): """Return whether or not a claim token is valid for this user for a given node which they were added as a unregistered contributor for. """ try: record = self.get_unclaimed_record(project_id) except ValueError: # No unclaimed record for given pid return False return record['token'] == token def confirm_email(self, token): if self.verify_confirmation_token(token): email = self.email_verifications[token]['email'] self.emails.append(email) # Complete registration if primary email if email.lower() == self.username.lower(): self.register(self.username) self.date_confirmed = dt.datetime.utcnow() # Revoke token del self.email_verifications[token] # Clear unclaimed records, so user's name shows up correctly on # all projects self.unclaimed_records = {} self.save() # Note: We must manually update search here because the fullname # field has not changed self.update_search() self.update_search_nodes() return True else: return False def update_search_nodes(self): """Call `update_search` on all nodes on which the user is a contributor. Needed to add self to contributor lists in search upon registration or claiming. """ for node in self.node__contributed: node.update_search() def is_confirmed(self): return bool(self.date_confirmed) @property def social_links(self): return { key: self.SOCIAL_FIELDS[key].format(val) for key, val in self.social.items() if val and self.SOCIAL_FIELDS.get(key) } @property def biblio_name(self): given_names = self.given_name + ' ' + self.middle_names surname = self.family_name if surname != given_names: initials = [ name[0].upper() + '.' for name in given_names.split(' ') if name and re.search(r'\w', name[0], re.I) ] return u'{0}, {1}'.format(surname, ' '.join(initials)) return surname @property def given_name_initial(self): """ The user's preferred initialization of their given name. Some users with common names may choose to distinguish themselves from their colleagues in this way. For instance, there could be two well-known researchers in a single field named "Robert Walker". "Walker, R" could then refer to either of them. "Walker, R.H." could provide easy disambiguation. NOTE: The internal representation for this should never end with a period. "R" and "R.H" would be correct in the prior case, but "R.H." would not. """ return self.given_name[0] @property def url(self): return '/{}/'.format(self._primary_key) @property def api_url(self): return '/api/v1/profile/{0}/'.format(self._primary_key) @property def absolute_url(self): return urlparse.urljoin(settings.DOMAIN, self.url) @property def display_absolute_url(self): url = self.absolute_url if url is not None: return re.sub(r'https?:', '', url).strip('/') @property def deep_url(self): return '/profile/{}/'.format(self._primary_key) @property def gravatar_url(self): return filters.gravatar(self, use_ssl=True, size=settings.GRAVATAR_SIZE_ADD_CONTRIBUTOR) def get_activity_points(self, db=None): db = db or framework.mongo.database return analytics.get_total_activity_count(self._primary_key, db=db) @property def is_disabled(self): """Whether or not this account has been disabled. Abstracts ``User.date_disabled``. :return: bool """ return self.date_disabled is not None @is_disabled.setter def is_disabled(self, val): """Set whether or not this account has been disabled.""" if val: self.date_disabled = dt.datetime.utcnow() else: self.date_disabled = None @property def is_merged(self): '''Whether or not this account has been merged into another account. ''' return self.merged_by is not None @property def profile_url(self): return '/{}/'.format(self._id) def get_summary(self, formatter='long'): return { 'user_fullname': self.fullname, 'user_profile_url': self.profile_url, 'user_display_name': name_formatters[formatter](self), 'user_is_claimed': self.is_claimed } def save(self, *args, **kwargs): self.username = self.username.lower().strip( ) if self.username else None ret = super(User, self).save(*args, **kwargs) if self.SEARCH_UPDATE_FIELDS.intersection(ret) and self.is_confirmed(): self.update_search() if settings.PIWIK_HOST and not self.piwik_token: try: piwik.create_user(self) except (piwik.PiwikException, ValueError): logger.error("Piwik user creation failed: " + self._id) return ret def update_search(self): from website import search try: search.search.update_user(self) except search.exceptions.SearchUnavailableError as e: logger.exception(e) log_exception() @classmethod def find_by_email(cls, email): try: user = cls.find_one(Q('emails', 'eq', email)) return [user] except: return [] def serialize(self, anonymous=False): return { 'id': utils.privacy_info_handle(self._primary_key, anonymous), 'fullname': utils.privacy_info_handle(self.fullname, anonymous, name=True), 'registered': self.is_registered, 'url': utils.privacy_info_handle(self.url, anonymous), 'api_url': utils.privacy_info_handle(self.api_url, anonymous), } ###### OSF-Specific methods ###### def watch(self, watch_config): """Watch a node by adding its WatchConfig to this user's ``watched`` list. Raises ``ValueError`` if the node is already watched. :param watch_config: The WatchConfig to add. :param save: Whether to save the user. """ watched_nodes = [each.node for each in self.watched] if watch_config.node in watched_nodes: raise ValueError('Node is already being watched.') watch_config.save() self.watched.append(watch_config) return None def unwatch(self, watch_config): """Unwatch a node by removing its WatchConfig from this user's ``watched`` list. Raises ``ValueError`` if the node is not already being watched. :param watch_config: The WatchConfig to remove. :param save: Whether to save the user. """ for each in self.watched: if watch_config.node._id == each.node._id: each.__class__.remove_one(each) return None raise ValueError('Node not being watched.') def is_watching(self, node): '''Return whether a not a user is watching a Node.''' watched_node_ids = set([config.node._id for config in self.watched]) return node._id in watched_node_ids def get_recent_log_ids(self, since=None): '''Return a generator of recent logs' ids. :param since: A datetime specifying the oldest time to retrieve logs from. If ``None``, defaults to 60 days before today. Must be a tz-aware datetime because PyMongo's generation times are tz-aware. :rtype: generator of log ids (strings) ''' log_ids = [] # Default since to 60 days before today if since is None # timezone aware utcnow utcnow = dt.datetime.utcnow().replace(tzinfo=pytz.utc) since_date = since or (utcnow - dt.timedelta(days=60)) for config in self.watched: # Extract the timestamps for each log from the log_id (fast!) # The first 4 bytes of Mongo's ObjectId encodes time # This prevents having to load each Log Object and access their # date fields node_log_ids = [ log_id for log_id in config.node.logs._to_primary_keys() if bson.ObjectId(log_id).generation_time > since_date and log_id not in log_ids ] # Log ids in reverse chronological order log_ids = _merge_into_reversed(log_ids, node_log_ids) return (l_id for l_id in log_ids) def get_daily_digest_log_ids(self): '''Return a generator of log ids generated in the past day (starting at UTC 00:00). ''' utcnow = dt.datetime.utcnow() midnight = dt.datetime(utcnow.year, utcnow.month, utcnow.day, 0, 0, 0, tzinfo=pytz.utc) return self.get_recent_log_ids(since=midnight) def merge_user(self, user, save=False): """Merge a registered user into this account. This user will be a contributor on any project :param user: A User object to be merged. """ # Inherit emails self.emails.extend(user.emails) # Inherit projects the user was a contributor for for node in user.node__contributed: node.add_contributor( contributor=self, permissions=node.get_permissions(user), visible=node.get_visible(user), log=False, ) try: node.remove_contributor( contributor=user, auth=Auth(user=self), log=False, ) except ValueError: logger.error('Contributor {0} not in list on node {1}'.format( user._id, node._id)) node.save() # Inherits projects the user created for node in user.node__created: node.creator = self node.save() user.merged_by = self user.save() if save: self.save() return None def get_projects_in_common(self, other_user, primary_keys=True): """Returns either a collection of "shared projects" (projects that both users are contributors for) or just their primary keys """ if primary_keys: projects_contributed_to = set( self.node__contributed._to_primary_keys()) return projects_contributed_to.intersection( other_user.node__contributed._to_primary_keys()) else: projects_contributed_to = set(self.node__contributed) return projects_contributed_to.intersection( other_user.node__contributed) def n_projects_in_common(self, other_user): """Returns number of "shared projects" (projects that both users are contributors for)""" return len(self.get_projects_in_common(other_user, primary_keys=True))
class FileVersion(StoredObject): """A version of an OsfStorageFileNode. contains information about where the file is located, hashes and datetimes """ _id = fields.StringField(primary=True, default=lambda: str(bson.ObjectId())) creator = fields.ForeignField('user') identifier = fields.StringField(required=True) # Date version record was created. This is the date displayed to the user. date_created = fields.DateTimeField(auto_now_add=True) # Dictionary specifying all information needed to locate file on backend # { # 'service': 'cloudfiles', # required # 'container': 'osf', # required # 'object': '20c53b', # required # 'worker_url': '127.0.0.1', # 'worker_host': 'upload-service-1', # } location = fields.DictionaryField(default=None, validate=utils.validate_location) # Dictionary containing raw metadata from upload service response # { # 'size': 1024, # required # 'content_type': 'text/plain', # required # 'date_modified': '2014-11-07T20:24:15', # required # 'md5': 'd077f2', # } metadata = fields.DictionaryField() size = fields.IntegerField() content_type = fields.StringField() # Date file modified on third-party backend. Not displayed to user, since # this date may be earlier than the date of upload if the file already # exists on the backend date_modified = fields.DateTimeField() @property def location_hash(self): return self.location['object'] @property def archive(self): return self.metadata.get('archive') def is_duplicate(self, other): return self.location_hash == other.location_hash def update_metadata(self, metadata, save=True): self.metadata.update(metadata) # metadata has no defined structure so only attempt to set attributes # If its are not in this callback it'll be in the next self.size = self.metadata.get('size', self.size) self.content_type = self.metadata.get('contentType', self.content_type) if self.metadata.get('modified') is not None: # TODO handle the timezone here the user that updates the file may see an # Incorrect version self.date_modified = parse_date(self.metadata['modified'], ignoretz=True) if save: self.save() def _find_matching_archive(self, save=True): """Find another version with the same sha256 as this file. If found copy its vault name and glacier id, no need to create additional backups. returns True if found otherwise false """ if 'sha256' not in self.metadata: return False # Dont bother searching for nothing if 'vault' in self.metadata and 'archive' in self.metadata: # Shouldn't ever happen, but we already have an archive return True # We've found ourself qs = self.__class__.find( Q('_id', 'ne', self._id) & Q('metadata.vault', 'ne', None) & Q('metadata.archive', 'ne', None) & Q('metadata.sha256', 'eq', self.metadata['sha256'])).limit(1) if qs.count() < 1: return False other = qs[0] try: self.metadata['vault'] = other.metadata['vault'] self.metadata['archive'] = other.metadata['archive'] except KeyError: return False if save: self.save() return True