def set_primary_file(self, preprint_file, auth, save=False): if not self.root_folder: raise PreprintStateError('Preprint needs a root folder.') if not self.has_permission(auth.user, WRITE): raise PermissionsError('Must have admin or write permissions to change a preprint\'s primary file.') if preprint_file.target != self or preprint_file.provider != 'osfstorage': raise ValueError('This file is not a valid primary file for this preprint.') existing_file = self.primary_file self.primary_file = preprint_file self.primary_file.move_under(self.root_folder) self.primary_file.save() # only log if updating the preprint file, not adding for the first time if existing_file: self.add_log( action=PreprintLog.FILE_UPDATED, params={ 'preprint': self._id, 'file': self.primary_file._id }, auth=auth, save=False ) if save: self.save() update_or_enqueue_on_preprint_updated(preprint_id=self._id, saved_fields=['primary_file'])
def set_privacy(self, permissions, auth=None, log=True, save=True, check_addons=False, force=False): """Set the permissions for this preprint - mainly for spam purposes. :param permissions: A string, either 'public' or 'private' :param auth: All the auth information including user, API key. :param bool log: Whether to add a NodeLog for the privacy change. :param bool meeting_creation: Whether this was created due to a meetings email. :param bool check_addons: Check and collect messages for addons? """ if auth and not self.has_permission(auth.user, WRITE): raise PermissionsError('Must have admin or write permissions to change privacy settings.') if permissions == 'public' and not self.is_public: if (self.is_spam or (settings.SPAM_FLAGGED_MAKE_NODE_PRIVATE and self.is_spammy)) and not force: raise PreprintStateError( 'This preprint has been marked as spam. Please contact the help desk if you think this is in error.' ) self.is_public = True elif permissions == 'private' and self.is_public: self.is_public = False else: return False if log: action = PreprintLog.MADE_PUBLIC if permissions == 'public' else PreprintLog.MADE_PRIVATE self.add_log( action=action, params={ 'preprint': self._id, }, auth=auth, save=False, ) if save: self.save() return True
def update_prereg_links(self, auth: Auth, prereg_links: list, log: bool = True, save: bool = True): """ This method updates the field `prereg_links` that contains a list of validated URLS linking to prereg data and logs changes to it. :param auth: Auth object :param prereg_links: List list of validated urls with schemes to links to prereg data :param log: Boolean should this be logged? :param save: Boolean should this be saved immediately? :return: This method brought to you via a grant from the Alfred P Sloan Foundation. """ if prereg_links == self.prereg_links: return if not self.has_prereg_links == 'available' and prereg_links: raise PreprintStateError('You cannot edit this field while your prereg links' ' availability is set to false or is unanswered.') self.prereg_links = prereg_links if log: self.add_log( action=PreprintLog.UPDATE_PREREG_LINKS, params={ 'user': auth.user._id, }, auth=auth ) if save: self.save()
def update_prereg_link_info(self, auth: Auth, prereg_link_info: str, log: bool = True, save: bool = True): """ This method updates the field `prereg_link_info` that contains a one of a finite number of choice strings in contained in the list in the static member `PREREG_LINK_INFO_CHOICES` that describe the nature of the preprint's prereg links. :param auth: Auth object :param prereg_link_info: String a string describing the nature of the preprint's prereg links. :param log: Boolean should this be logged? :param save: Boolean should this be saved immediately? :return: This method brought to you via a grant from the Alfred P Sloan Foundation. """ if self.prereg_link_info == prereg_link_info: return if not self.has_prereg_links == 'available' and prereg_link_info: raise PreprintStateError('You cannot edit this field while your prereg links' ' availability is set to false or is unanswered.') self.prereg_link_info = prereg_link_info if log: self.add_log( action=PreprintLog.UPDATE_PREREG_LINKS_INFO, params={ 'user': auth.user._id, }, auth=auth ) if save: self.save()
def update_why_no_prereg(self, auth: Auth, why_no_prereg: str, log: bool = True, save: bool = True): """ This method updates the field `why_no_prereg` that contains a user provided explanation of prereg data unavailability and logs changes to it. :param auth: Auth object :param why_no_prereg: String explanation of prereg data unavailability :param log: Boolean should this be logged? :param save: Boolean should this be saved immediately? :return: This method brought to you via a grant from the Alfred P Sloan Foundation. """ if why_no_prereg == self.why_no_prereg: return if self.has_prereg_links == 'available' or self.has_prereg_links is None: raise PreprintStateError('You cannot edit this statement while your prereg links ' 'availability is set to true or is unanswered.') self.why_no_prereg = why_no_prereg if log: self.add_log( action=PreprintLog.UPDATE_WHY_NO_PREREG, params={ 'user': auth.user._id, }, auth=auth ) if save: self.save()
def update_why_no_data(self, auth: Auth, why_no_data: str, log: bool = True, save: bool = True): """ This method sets the field `why_no_data` a string that represents a user provided explanation for the unavailability of supplementary data for their preprint. :param auth: Auth object :param why_no_data: String a user provided explanation for the unavailability of data links for their preprint. :param log: Boolean should this be logged? :param save: Boolean should this be saved immediately? :return: This method brought to you via a grant from the Alfred P Sloan Foundation. """ if self.why_no_data == why_no_data: return if not self.has_data_links == 'no': raise PreprintStateError('You cannot edit this statement while your data links availability is set to true or' ' is unanswered.') else: self.why_no_data = why_no_data if log: self.add_log( action=PreprintLog.UPDATE_WHY_NO_DATA, params={ 'user': auth.user._id, }, auth=auth ) if save: self.save()
def update_data_links(self, auth: Auth, data_links: list, log: bool = True, save: bool = True): """ This method sets the field `data_links` which is a validated list of links to supplementary data for a preprint and logs that change. :param auth: Auth object :param data_links: List urls that should link to supplementary data for a preprint. :param log: Boolean should this be logged? :param save: Boolean should this be saved immediately? :return: This method brought to you via a grant from the Alfred P Sloan Foundation. """ if self.data_links == data_links: return if not self.has_data_links == 'available' and data_links: raise PreprintStateError('You cannot edit this statement while your data links availability is set to false' ' or is unanswered.') self.data_links = data_links if log: self.add_log( action=PreprintLog.UPDATE_DATA_LINKS, params={ 'user': auth.user._id, }, auth=auth ) if save: self.save()
def update_conflict_of_interest_statement(self, auth: Auth, coi_statement: str, log: bool = True, save: bool = True): """ This method sets the `conflict_of_interest_statement` field for this preprint and logs that change. :param auth: Auth object :param coi_statement: String represents a user's conflict of interest statement for their preprint. :param log: Boolean should this be logged? :param save: Boolean should this be saved immediately? :return: This method brought to you via a grant from the Alfred P Sloan Foundation. """ if self.conflict_of_interest_statement == coi_statement: return if not self.has_coi: raise PreprintStateError('You do not have the ability to edit a conflict of interest while the has_coi field is ' 'set to false or unanswered') self.conflict_of_interest_statement = coi_statement if log: self.add_log( action=PreprintLog.UPDATE_COI_STATEMENT, params={ 'user': auth.user._id, }, auth=auth, ) if save: self.save()
def set_permissions(self, user, permissions, validate=True, save=False): # Ensure that user's permissions cannot be lowered if they are the only admin if isinstance(user, PreprintContributor): user = user.user if validate and (self.has_permission(user, 'admin') and 'admin' not in permissions): if self.get_group('admin').user_set.count() <= 1: raise PreprintStateError('Must have at least one registered admin contributor') self.clear_permissions(user) self.add_permission(user, permissions) if save: self.save()