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 brief_internal_routing(item: dict, **kwargs): guid = item.get('guid', 'unknown') logger.info('macro started item=%s', guid) try: assert str(item['profile']) == str( _get_profile_id(TEXT_PROFILE)), 'profile is not text' assert get_word_count(item['body_html']) < 301, 'body is too long' except AssertionError as err: logger.info('macro stop on assert item=%s error=%s', guid, err) raise StopDuplication() except KeyError as err: logger.error(err) raise StopDuplication() item.setdefault('subject', []) item['urgency'] = 2 item['profile'] = _get_profile_id(BRIEF_PROFILE) item['subject'] = _get_product_subject( _get_brief_subject(item.get('subject'))) item['status'] = CONTENT_STATE.SCHEDULED item['operation'] = 'publish' _fix_headline(item) _fix_body_html(item) # schedule +30m UTC_FIELD = 'utc_{}'.format(PUBLISH_SCHEDULE) try: published_at = item[SCHEDULE_SETTINGS][UTC_FIELD] except KeyError: published_at = utcnow() item[SCHEDULE_SETTINGS] = { 'time_zone': 'Europe/Brussels', } item[PUBLISH_SCHEDULE] = utc_to_local(item[SCHEDULE_SETTINGS]['time_zone'], published_at + timedelta(minutes=30)) update_schedule_settings(item, PUBLISH_SCHEDULE, item[PUBLISH_SCHEDULE]) item[PUBLISH_SCHEDULE] = item[PUBLISH_SCHEDULE].replace(tzinfo=None) # publish try: internal_destination_auto_publish(item) except StopDuplication: logger.info('macro done item=%s', guid) except DocumentError as err: logger.error('validation error when creating brief item=%s error=%s', guid, err) except Exception as err: logger.exception(err) # avoid another item to be created raise StopDuplication()
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 _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 brief_internal_routing(item: dict, **kwargs): guid = item.get('guid', 'unknown') logger.info('macro started item=%s', guid) try: assert str(item['profile']) == str( _get_profile_id(TEXT_PROFILE)), 'profile is not text' assert get_word_count(item['body_html']) < 301, 'body is too long' except AssertionError as err: logger.info('macro stop on assert item=%s error=%s', guid, err) raise StopDuplication() except KeyError as err: logger.error(err) raise StopDuplication() item.setdefault('subject', []) item['urgency'] = 2 item['profile'] = _get_profile_id(BRIEF_PROFILE) item['subject'] = _get_product_subject( _get_brief_subject(item.get('subject', []))) item['status'] = CONTENT_STATE.SCHEDULED item['operation'] = 'publish' _fix_headline(item) _fix_body_html(item) UTC_FIELD = 'utc_{}'.format(PUBLISH_SCHEDULE) try: published_at = item[SCHEDULE_SETTINGS][UTC_FIELD] except KeyError: published_at = utcnow() item[SCHEDULE_SETTINGS] = { 'time_zone': 'Europe/Brussels', } # Set item publish schedule to 7:30 am for autopublish between 4 to 7 am is_press_headline = item.get( 'headline') and 'press' in item['headline'].lower() current_datetime = utc_to_local(superdesk.app.config['DEFAULT_TIMEZONE'], utcnow()) if is_press_headline and time(4, 00) <= current_datetime.time() <= time( 7, 00): item[PUBLISH_SCHEDULE] = current_datetime.replace(hour=7, minute=30, second=00) logger.info( 'Set publish schedule to 7:30 am for autopublish between 4 to 7 am item=%s', item.get('guid', 'unknown')) else: # schedule +30m item[PUBLISH_SCHEDULE] = utc_to_local( item[SCHEDULE_SETTINGS]['time_zone'], published_at + timedelta(minutes=30)) update_schedule_settings(item, PUBLISH_SCHEDULE, item[PUBLISH_SCHEDULE]) item[PUBLISH_SCHEDULE] = item[PUBLISH_SCHEDULE].replace(tzinfo=None) # remove text in () brackets along with brackets if item.get("headline"): title = re.sub(r"\([^()]*\)", "", item['headline']) item['headline'] = " ".join(title.split()) # publish try: internal_destination_auto_publish(item) except StopDuplication: logger.info('macro done item=%s', guid) except DocumentError as err: logger.error('validation error when creating brief item=%s error=%s', guid, err) except Exception as err: logger.exception(err) # avoid another item to be created raise StopDuplication()