def check_in_or_out(self, user, checkout, save=False): """ Updates self.checkout with the requesting user or None, iff user has permission to check out file or folder. Adds log to self.target if target is a node. :param user: User making the request :param checkout: Either the same user or None, depending on in/out-checking :param save: Whether or not to save the user """ from osf.models import NodeLog # Avoid circular import target = self.target if isinstance(target, AbstractNode ) and self.is_checked_out and self.checkout != user: # Allow project admins to force check in if target.has_permission(user, permissions.ADMIN): # But don't allow force check in for prereg admin checked out files if self.checkout.has_perm( 'osf.view_prereg' ) and target.draft_registrations_active.filter( registration_schema__name='Prereg Challenge').exists(): raise exceptions.FileNodeCheckedOutError() else: raise exceptions.FileNodeCheckedOutError() if not target.has_permission(user, permissions.WRITE): raise exceptions.FileNodeCheckedOutError() action = NodeLog.CHECKED_OUT if checkout else NodeLog.CHECKED_IN if self.is_checked_out and action == NodeLog.CHECKED_IN or not self.is_checked_out and action == NodeLog.CHECKED_OUT: self.checkout = checkout if isinstance(target, Loggable): target.add_log( action=action, params={ 'kind': self.kind, 'project': target.parent_id, 'node': target._id, 'urls': { # web_url_for unavailable -- called from within the API, so no flask app 'download': '/project/{}/files/{}/{}/?action=download'.format( target._id, self.provider, self._id), 'view': '/project/{}/files/{}/{}'.format( target._id, self.provider, self._id) }, 'path': self.materialized_path }, auth=Auth(user), ) if save: self.save()
def move_under(self, destination_parent, name=None): if self.is_preprint_primary: if self.node != destination_parent.node or self.provider != destination_parent.provider: raise exceptions.FileNodeIsPrimaryFile() if self.is_checked_out: raise exceptions.FileNodeCheckedOutError() return super(OsfStorageFileNode, self).move_under(destination_parent, name)
def delete(self, user=None, parent=None): if self.node.preprint_file == self: self.node._is_preprint_orphan = True self.node.save() if self.is_checked_out: raise exceptions.FileNodeCheckedOutError() return super(OsfStorageFileNode, self).delete(user=user, parent=parent)
def move_under(self, destination_parent, name=None): if self.is_preprint_primary: if self.target != destination_parent.target or self.provider != destination_parent.provider: raise exceptions.FileNodeIsPrimaryFile() if self.is_checked_out: raise exceptions.FileNodeCheckedOutError() self.update_region_from_latest_version(destination_parent) return super(OsfStorageFileNode, self).move_under(destination_parent, name)
def delete(self, user=None, parent=None, **kwargs): if self.node.preprint_file and self.node.preprint_file.pk == self.pk: self.node._is_preprint_orphan = True self.node.save() if self.is_checked_out: raise exceptions.FileNodeCheckedOutError() self._path = self.path self._materialized_path = self.materialized_path return super(OsfStorageFileNode, self).delete(user=user, parent=parent, **kwargs)
def move_under(self, destination_parent, name=None): if self.is_preprint_primary: if self.target != destination_parent.target or self.provider != destination_parent.provider: raise exceptions.FileNodeIsPrimaryFile() if self.is_checked_out: raise exceptions.FileNodeCheckedOutError() most_recent_fileversion = self.versions.select_related( 'region').order_by('-created').first() if most_recent_fileversion and most_recent_fileversion.region != destination_parent.target.osfstorage_region: most_recent_fileversion.region = destination_parent.target.osfstorage_region most_recent_fileversion.save() return super(OsfStorageFileNode, self).move_under(destination_parent, name)
def check_in_or_out(self, user, checkout, save=False): """ Updates self.checkout with the requesting user or None, iff user has permission to check out file or folder. Adds log to self.node. :param user: User making the request :param checkout: Either the same user or None, depending on in/out-checking :param save: Whether or not to save the user """ from osf.models import NodeLog # Avoid circular import if ( self.is_checked_out and self.checkout != user and permissions.ADMIN not in self.node.get_permissions( user)) \ or permissions.WRITE not in self.node.get_permissions(user): raise exceptions.FileNodeCheckedOutError() action = NodeLog.CHECKED_OUT if checkout else NodeLog.CHECKED_IN if self.is_checked_out and action == NodeLog.CHECKED_IN or not self.is_checked_out and action == NodeLog.CHECKED_OUT: self.checkout = checkout self.node.add_log( action=action, params={ 'kind': self.kind, 'project': self.node.parent_id, 'node': self.node._id, 'urls': { # web_url_for unavailable -- called from within the API, so no flask app 'download': '/project/{}/files/{}/{}/?action=download'.format( self.node._id, self.provider, self._id), 'view': '/project/{}/files/{}/{}'.format( self.node._id, self.provider, self._id) }, 'path': self.materialized_path }, auth=Auth(user), ) if save: self.save()
def _check_delete_allowed(self): if self.is_preprint_primary: raise exceptions.FileNodeIsPrimaryFile() if self.is_checked_out: raise exceptions.FileNodeCheckedOutError() return True
def move_under(self, destination_parent, name=None): if self.is_checked_out: raise exceptions.FileNodeCheckedOutError() return super(OsfStorageFileNode, self).move_under(destination_parent, name)
def delete(self, user=None, parent=None): if self.is_checked_out: raise exceptions.FileNodeCheckedOutError() return super(OsfStorageFileNode, self).delete(user=user, parent=parent)