def save(self, *args, **kwargs): first_save = not bool(self.pk) saved_fields = self.get_dirty_fields() or [] old_subjects = kwargs.pop('old_subjects', []) if saved_fields: request, user_id = get_request_and_user_id() request_headers = {} if not isinstance(request, DummyRequest): request_headers = { k: v for k, v in get_headers_from_request(request).items() if isinstance(v, basestring) } user = OSFUser.load(user_id) if user: self.check_spam(user, saved_fields, request_headers) if not first_save and ('ever_public' in saved_fields and saved_fields['ever_public']): raise ValidationError('Cannot set "ever_public" to False') ret = super(Preprint, self).save(*args, **kwargs) if first_save: self._set_default_region() self.update_group_permissions() self._add_creator_as_contributor() if (not first_save and 'is_published' in saved_fields) or self.is_published: update_or_enqueue_on_preprint_updated(preprint_id=self._id, old_subjects=old_subjects, saved_fields=saved_fields) return ret
def save(self, *args, **kwargs): first_save = not bool(self.pk) saved_fields = self.get_dirty_fields() or [] old_subjects = kwargs.pop('old_subjects', []) if saved_fields: request, user_id = get_request_and_user_id() request_headers = {} if not isinstance(request, DummyRequest): request_headers = { k: v for k, v in get_headers_from_request(request).items() if isinstance(v, basestring) } user = OSFUser.load(user_id) if user: self.check_spam(user, saved_fields, request_headers) if not first_save and ('ever_public' in saved_fields and saved_fields['ever_public']): raise ValidationError('Cannot set "ever_public" to False') ret = super(PreprintService, self).save(*args, **kwargs) if (not first_save and 'is_published' in saved_fields) or self.is_published: update_or_enqueue_on_preprint_updated(preprint_id=self._id, old_subjects=old_subjects, saved_fields=saved_fields) return ret
def save(self, *args, **kwargs): first_save = not bool(self.pk) saved_fields = self.get_dirty_fields() or [] old_subjects = kwargs.pop('old_subjects', []) if saved_fields and (not settings.SPAM_CHECK_PUBLIC_ONLY or self.verified_publishable): request, user_id = get_request_and_user_id() request_headers = string_type_request_headers(request) user = OSFUser.load(user_id) if user: self.check_spam(user, saved_fields, request_headers) if not first_save and ('ever_public' in saved_fields and saved_fields['ever_public']): raise ValidationError('Cannot set "ever_public" to False') if self.has_submitted_preprint and not self.primary_file: raise ValidationError('Cannot save non-initial preprint without primary file.') ret = super(Preprint, self).save(*args, **kwargs) if first_save: self._set_default_region() self.update_group_permissions() self._add_creator_as_contributor() if (not first_save and 'is_published' in saved_fields) or self.is_published: update_or_enqueue_on_preprint_updated(preprint_id=self._id, old_subjects=old_subjects, saved_fields=saved_fields) return ret
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_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_preprint_license(self, license_detail, auth, save=False): license_record, license_changed = set_license(self, license_detail, auth, node_type='preprint') if license_changed: self.add_log( action=PreprintLog.CHANGED_LICENSE, params={ 'preprint': self._id, 'new_license': license_record.node_license.name }, auth=auth, save=False ) if save: self.save() update_or_enqueue_on_preprint_updated(preprint_id=self._id, saved_fields=['license'])
def set_preprint_license(self, license_detail, auth, save=False): license_record, license_changed = set_license(self, license_detail, auth, node_type='preprint') if license_changed: self.add_log( action=PreprintLog.CHANGED_LICENSE, params={ 'preprint': self._id, 'new_license': license_record.node_license.name }, auth=auth, save=False ) if save: self.save() update_or_enqueue_on_preprint_updated(preprint_id=self._id, saved_fields=['license'])
def test_update_or_enqueue_on_preprint_updated(self): first_subjects = [15] update_or_enqueue_on_preprint_updated( self.preprint._id, old_subjects=first_subjects, saved_fields={'contributors': True}) second_subjects = [16, 17] update_or_enqueue_on_preprint_updated(self.preprint._id, old_subjects=second_subjects, saved_fields={'title': 'Hello'}) updated_task = get_task_from_postcommit_queue( 'website.preprints.tasks.on_preprint_updated', predicate=lambda task: task.kwargs['preprint_id' ] == self.preprint._id) assert 'title' in updated_task.kwargs['saved_fields'] assert 'contributors' in updated_task.kwargs['saved_fields'] assert set(first_subjects + second_subjects).issubset( updated_task.kwargs['old_subjects'])
def test_update_or_enqueue_on_preprint_updated(self): first_subjects = [15] update_or_enqueue_on_preprint_updated( self.preprint._id, old_subjects=first_subjects, saved_fields={'contributors': True} ) second_subjects = [16, 17] update_or_enqueue_on_preprint_updated( self.preprint._id, old_subjects=second_subjects, saved_fields={'title': 'Hello'} ) updated_task = get_task_from_postcommit_queue( 'website.preprints.tasks.on_preprint_updated', predicate=lambda task: task.kwargs['preprint_id'] == self.preprint._id ) assert 'title' in updated_task.kwargs['saved_fields'] assert 'contributors' in updated_task.kwargs['saved_fields'] assert set(first_subjects + second_subjects).issubset(updated_task.kwargs['old_subjects'])
def remove_tag(self, tag, auth, save=True): if not tag: raise InvalidTagError elif not self.tags.filter(name=tag).exists(): raise TagNotFoundError else: tag_obj = Tag.objects.get(name=tag) self.tags.remove(tag_obj) self.add_log( action=PreprintLog.TAG_REMOVED, params={ 'preprint': self._id, 'tag': tag, }, auth=auth, save=False, ) if save: self.save() update_or_enqueue_on_preprint_updated(preprint_id=self._id, saved_fields=['tags']) return True
def remove_tag(self, tag, auth, save=True): if not tag: raise InvalidTagError elif not self.tags.filter(name=tag).exists(): raise TagNotFoundError else: tag_obj = Tag.objects.get(name=tag) self.tags.remove(tag_obj) self.add_log( action=PreprintLog.TAG_REMOVED, params={ 'preprint': self._id, 'tag': tag, }, auth=auth, save=False, ) if save: self.save() update_or_enqueue_on_preprint_updated(preprint_id=self._id, saved_fields=['tags']) return True
def on_tag_added(self, tag): update_or_enqueue_on_preprint_updated(preprint_id=self._id, saved_fields=['tags'])
def update_or_enqueue_on_resource_updated(self, user_id, first_save, saved_fields): # Needed for ContributorMixin return update_or_enqueue_on_preprint_updated(preprint_id=self._id, saved_fields=saved_fields)
def on_tag_added(self, tag): update_or_enqueue_on_preprint_updated(preprint_id=self._id, saved_fields=['tags'])
def update_or_enqueue_on_resource_updated(self, user_id, first_save, saved_fields): # Needed for ContributorMixin return update_or_enqueue_on_preprint_updated(preprint_id=self._id, saved_fields=saved_fields)