def _validate(self, original, updates): self.raise_if_invalid_state_transition(original) updated = original.copy() updated.update(updates) self.raise_if_not_marked_for_publication(updated) if self.publish_type == 'publish': update_schedule_settings(updated, PUBLISH_SCHEDULE, updated.get(PUBLISH_SCHEDULE)) validate_schedule( updated.get(SCHEDULE_SETTINGS, {}).get('utc_{}'.format(PUBLISH_SCHEDULE))) if original[ITEM_TYPE] != CONTENT_TYPE.COMPOSITE and updates.get( EMBARGO): update_schedule_settings(updated, EMBARGO, updated.get(EMBARGO)) get_resource_service(ARCHIVE).validate_embargo(updated) if self.publish_type in [ITEM_CORRECT, ITEM_KILL]: if updates.get(EMBARGO) and not original.get(EMBARGO): raise SuperdeskApiError.badRequestError( "Embargo can't be set after publishing") if self.publish_type in [ITEM_CORRECT, ITEM_KILL]: if updates.get('dateline'): raise SuperdeskApiError.badRequestError( "Dateline can't be modified after publishing") if self.publish_type == ITEM_PUBLISH and updated.get('rewritten_by'): rewritten_by = get_resource_service(ARCHIVE).find_one( req=None, _id=updated.get('rewritten_by')) if rewritten_by and rewritten_by.get(ITEM_STATE) in PUBLISH_STATES: raise SuperdeskApiError.badRequestError( "Cannot publish the story after Update is published.!") publish_type = 'auto_publish' if updates.get( 'auto_publish') else self.publish_type validate_item = { 'act': publish_type, 'type': original['type'], 'validate': updated } validation_errors = get_resource_service('validate').post( [validate_item]) if validation_errors[0]: raise ValidationError(validation_errors) validation_errors = [] self._validate_associated_items(original, validation_errors) if original[ITEM_TYPE] == CONTENT_TYPE.COMPOSITE: self._validate_package(original, updates, validation_errors) if len(validation_errors) > 0: raise ValidationError(validation_errors)
def on_update(self, updates, original): self.raise_if_not_marked_for_publication(original) self.raise_if_invalid_state_transition(original) updated = original.copy() updated.update(updates) takes_package = self.takes_package_service.get_take_package(original) if self.publish_type == 'publish': # validate if take can be published if takes_package and not self.takes_package_service.can_publish_take( takes_package, updates.get(SEQUENCE, original.get(SEQUENCE, 1))): raise PublishQueueError.previous_take_not_published_error( Exception("Previous takes are not published.")) validate_schedule( updated.get('publish_schedule'), takes_package.get(SEQUENCE, 1) if takes_package else 1) if original[ITEM_TYPE] != CONTENT_TYPE.COMPOSITE and updates.get( EMBARGO): get_resource_service(ARCHIVE).validate_embargo(updated) if self.publish_type in ['correct', 'kill']: if updates.get(EMBARGO): raise SuperdeskApiError.badRequestError( "Embargo can't be set after publishing") if updates.get('dateline'): raise SuperdeskApiError.badRequestError( "Dateline can't be modified after publishing") validate_item = { 'act': self.publish_type, 'type': original['type'], 'validate': updated } validation_errors = get_resource_service('validate').post( [validate_item]) if validation_errors[0]: raise ValidationError(validation_errors) # validate the package if it is one package_validation_errors = [] self._validate_package_contents(original, takes_package, package_validation_errors) if len(package_validation_errors) > 0: raise ValidationError(package_validation_errors) self._set_updates(original, updates, updates.get(config.LAST_UPDATED, utcnow()))
def __init__(self, message=None, status_code=None, payload=None): """ :param message: a human readable error description :param status_code: response status code :param payload: a dict with request issues """ ValidationError.__init__(self, message) self.message = message if status_code: self.status_code = status_code if payload: self.payload = payload
def _raise_if_unpublished_related_items(self, original): if not request: return if (config.PUBLISH_ASSOCIATED_ITEMS or not original.get(ASSOCIATIONS) or self.publish_type not in [ITEM_PUBLISH, ITEM_CORRECT]): return archive_service = get_resource_service('archive') publishing_warnings_confirmed = strtobool( request.args.get('publishing_warnings_confirmed') or 'False') if not publishing_warnings_confirmed: for key, associated_item in original.get(ASSOCIATIONS).items(): if associated_item and is_related_content(key): item = archive_service.find_one( req=None, _id=associated_item.get('_id')) item = item if item else associated_item if item.get('state') not in PUBLISH_STATES: error_msg = json.dumps({ 'warnings': [ _('There are unpublished related ' + 'items that won\'t be sent out as ' + 'related items. Do you want to publish the article anyway?' ) ] }) raise ValidationError(error_msg)
def _validate(self, original, updates): self.raise_if_not_marked_for_publication(original) self.raise_if_invalid_state_transition(original) updated = original.copy() updated.update(updates) takes_package = self.takes_package_service.get_take_package(original) if self.publish_type == 'publish': # validate if take can be published if takes_package and not self.takes_package_service.can_publish_take( takes_package, updates.get(SEQUENCE, original.get(SEQUENCE, 1))): raise PublishQueueError.previous_take_not_published_error( Exception("Previous takes are not published.")) validate_schedule(updated.get(PUBLISH_SCHEDULE), takes_package.get(SEQUENCE, 1) if takes_package else 1) update_schedule_settings(updated, PUBLISH_SCHEDULE, updated.get(PUBLISH_SCHEDULE)) if original[ITEM_TYPE] != CONTENT_TYPE.COMPOSITE and updates.get(EMBARGO): get_resource_service(ARCHIVE).validate_embargo(updated) if self.publish_type in [ITEM_CORRECT, ITEM_KILL]: if updates.get(EMBARGO): raise SuperdeskApiError.badRequestError("Embargo can't be set after publishing") if updates.get('dateline'): raise SuperdeskApiError.badRequestError("Dateline can't be modified after publishing") if self.publish_type == ITEM_PUBLISH and updated.get('rewritten_by'): # if update is published then user cannot publish the takes rewritten_by = get_resource_service(ARCHIVE).find_one(req=None, _id=updated.get('rewritten_by')) if rewritten_by and rewritten_by.get(ITEM_STATE) in PUBLISH_STATES: raise SuperdeskApiError.badRequestError("Cannot publish the story after Update is published.!") validate_item = {'act': self.publish_type, 'type': original['type'], 'validate': updated} validation_errors = get_resource_service('validate').post([validate_item]) if validation_errors[0]: raise ValidationError(validation_errors) if original[ITEM_TYPE] == CONTENT_TYPE.COMPOSITE: package_validation_errors = [] self._validate_package_contents(original, takes_package, package_validation_errors) if len(package_validation_errors) > 0: raise ValidationError(package_validation_errors) self._validate_package(original, updates)
def validate_for_publish(item, **kwargs): doc = get_resource_service('archive').find_one(req=None, _id=item[config.ID_FIELD]) validate_item = {'act': ITEM_PUBLISH, 'type': doc['type'], 'validate': doc} validation_errors = get_resource_service('validate').post([validate_item]) if validation_errors[0]: raise ValidationError(validation_errors) return item
def update_user_prefs(self, updates, existing_user_preferences): user_prefs = updates.get(_user_preferences_key) if user_prefs is not None: # check if the input is validated against the default values for k in ((k for k, v in user_prefs.items() if k not in superdesk.default_user_preferences)): raise ValidationError('Invalid preference: %s' % k) existing_user_preferences.update(user_prefs) updates[_user_preferences_key] = existing_user_preferences
def update_session_prefs(self, updates, existing_session_preferences, session_id): session_prefs = updates.get(_session_preferences_key) if session_prefs is not None: for k in (k for k, v in session_prefs.items() if k not in superdesk.default_session_preferences): raise ValidationError('Invalid preference: %s' % k) existing = existing_session_preferences.get(session_id, {}) existing.update(session_prefs) existing_session_preferences[session_id] = existing updates[_session_preferences_key] = existing_session_preferences
def _validate(self, doc): """Validates the given story for publish action""" validate_item = { 'act': ITEM_PUBLISH, 'type': doc['type'], 'validate': doc } validation_errors = get_resource_service('validate').post( [validate_item]) if validation_errors[0]: raise ValidationError(validation_errors)
def check_for_circular_reference(self, package, item_id): if any(d for d in package.get(LINKED_IN_PACKAGES, []) if d['package'] == item_id): message = 'Trying to create a circular reference to: ' + item_id logger.error(message) raise ValidationError(message) else: # keep checking in the hierarchy for d in (d for d in package.get(LINKED_IN_PACKAGES, []) if 'package' in d): linked_package = get_resource_service(ARCHIVE).find_one(req=None, _id=d['package']) if linked_package: self.check_for_circular_reference(linked_package, item_id)
def partial_update(self, updates, existing_preferences, default_preferences, key): if updates.get(key) is not None: prefs = updates.get(key, {}) # check if the input is validated against the default values for k in ((k for k, v in prefs.items() if k not in default_preferences)): raise ValidationError('Invalid preference: %s' % k) if key == _user_preferences_key: for k in existing_preferences.keys(): updates[key][k] = dict( list(existing_preferences.get(k, {}).items()) + list(prefs.get(k, {}).items())) else: for k in existing_preferences.keys(): if prefs.get(k, []) == []: updates[key][k] = existing_preferences.get(k, [])
def check_for_circular_reference(self, package, item_id): if any(d for d in package.get(LINKED_IN_PACKAGES, []) if d['package'] == item_id): message = 'Trying to create a circular reference to: ' + item_id logger.error(message) raise ValidationError(message)
def check_parents(self, myid, parentid): parent = self.find_one(req=None, _id=parentid) if parent: if parent['_id'] == myid: raise ValidationError('Circular role inheritance') self.check_parents(myid, parent.get('extends'))
def on_update(self, updates, original): if updates.get('extends'): if updates.get('extends') == original.get('_id'): raise ValidationError('A role can not extend its self') self.check_parents(original.get('_id'), updates.get('extends'))