def _process_external_data(self, _data, update=False): data = _data.copy() if data.get("role"): role_name = data.pop("role") role = get_resource_service("roles").find_one( req=None, name=ignorecase_query(role_name)) if role: data["role"] = role["_id"] if not update and (data.get("desk") or app.config.get("USER_EXTERNAL_DESK")): desk_name = data.pop("desk", None) or app.config.get("USER_EXTERNAL_DESK") desk = get_resource_service("desks").find_one( req=None, name=ignorecase_query(desk_name)) if desk: data["desk"] = desk["_id"] data["needs_activation"] = False if update: data.pop("desk", None) data.pop("email", None) data.pop("username", None) elif data.get("username"): if app.config.get("USER_EXTERNAL_USERNAME_STRIP_DOMAIN"): data["username"] = data["username"].split("@")[0] data["username"] = data["username"].replace( "@", ".") # @ breaks mentioning validator = self._validator() if not validator.validate(data, update=update): raise ValidationError(validator.errors) return validator.normalized(data) if not update else data
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 _process_external_data(self, _data, update=False): data = _data.copy() if data.get('role'): role_name = data.pop('role') role = get_resource_service('roles').find_one( req=None, name=ignorecase_query(role_name)) if role: data['role'] = role['_id'] if not update and (data.get('desk') or app.config.get('USER_EXTERNAL_DESK')): desk_name = data.pop('desk', None) or app.config.get('USER_EXTERNAL_DESK') desk = get_resource_service('desks').find_one( req=None, name=ignorecase_query(desk_name)) if desk: data['desk'] = desk['_id'] data['needs_activation'] = False if update: data.pop('desk', None) data.pop('email', None) data.pop('username', None) elif data.get('username'): if app.config.get('USER_EXTERNAL_USERNAME_STRIP_DOMAIN'): data['username'] = data['username'].split('@')[0] data['username'] = data['username'].replace( '@', '.') # @ breaks mentioning validator = self._validator() if not validator.validate(data, update=update): raise ValidationError(validator.errors) return validator.normalized(data) if not update else data
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: {preference}').format(preference=k)) existing_user_preferences.update(user_prefs) updates[_user_preferences_key] = existing_user_preferences
def _validate(self, original, updates): self.raise_if_invalid_state_transition(original) self._raise_if_unpublished_related_items(original) updated = original.copy() updated.update(updates) self.raise_if_not_marked_for_publication(updated) if self.publish_type == "publish": # The publish schedule has not been cleared if ( updates.get(PUBLISH_SCHEDULE) or updated.get(SCHEDULE_SETTINGS, {}).get("utc_{}".format(PUBLISH_SCHEDULE)) or not original.get(PUBLISH_SCHEDULE) ): 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 == ITEM_KILL: if updates.get("dateline"): raise SuperdeskApiError.badRequestError(_("Dateline can't be modified on kill or take down")) 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.")) if self.publish_type == ITEM_PUBLISH and updated.get("rewrite_of"): rewrite_of = get_resource_service(ARCHIVE).find_one(req=None, _id=updated.get("rewrite_of")) if rewrite_of and rewrite_of.get(ITEM_STATE) not in PUBLISH_STATES: raise SuperdeskApiError.badRequestError(_("Can't publish update until original story 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], fields=True) for errors, fields in validation_errors: if errors: raise SuperdeskValidationError(errors, fields) validation_errors = [] self._validate_associated_items(original, updates, 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 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: {preference}').format(preference=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_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').create( [validate_item], fields=True) if validation_errors[0][0]: raise ValidationError(json.dumps(validation_errors[0][0])) return item
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").validate( validate_item) if validation_errors: raise ValidationError(json.dumps(validation_errors)) return item
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}").format(item_id=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 _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").validate( validate_item) if validation_errors: raise ValidationError(validation_errors)