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 _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 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 _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.")) update_schedule_settings(updated, PUBLISH_SCHEDULE, updated.get(PUBLISH_SCHEDULE)) validate_schedule(updated.get(SCHEDULE_SETTINGS, {}).get('utc_{}'.format(PUBLISH_SCHEDULE)), takes_package.get(SEQUENCE, 1) if takes_package else 1) 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'): # 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.!") 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, takes_package, 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 handle_duplicate(sender, item, original, operation): if operation == 'translate': # remove all belga-archive-360/video associations for k, v in deepcopy(item.get('associations', {})).items(): if v is not None and (is_belga_archive(v) or is_video(v)): del item.get('associations', {})[k] # keep schedule settings if still valid if original.get(ITEM_STATE) == CONTENT_STATE.SCHEDULED and original.get(PUBLISH_SCHEDULE): try: validate_schedule(original.get(SCHEDULE_SETTINGS, {}).get("utc_{}".format(PUBLISH_SCHEDULE))) item[SCHEDULE_SETTINGS] = original[SCHEDULE_SETTINGS] item[PUBLISH_SCHEDULE] = original[PUBLISH_SCHEDULE] except (SuperdeskApiError, KeyError): pass
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 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())) updates[ITEM_OPERATION] = ITEM_PUBLISH convert_task_attributes_to_objectId(updates)
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 test_validate_schedule(self): validate_schedule(utcnow() + timedelta(hours=2))
def test_validate_schedule_at_utc_zero_hours(self): validate_schedule((utcnow() + timedelta(days=1)).replace(hour=0, minute=0, second=0, microsecond=0))
def test_validate_schedule_at_utc_zero_hours(self): validate_schedule( (utcnow() + timedelta(days=1)).replace(hour=0, minute=0, second=0, microsecond=0))