def _refresh_associated_items(self, original): """Refresh associated items before publishing Any further updates made to basic metadata done after item was associated will be carried on and used when validating those items. """ associations = original.get(ASSOCIATIONS) or {} for _, item in associations.items(): if type(item) == dict and item.get(config.ID_FIELD): keys = DEFAULT_SCHEMA.keys() if app.settings.get('COPY_METADATA_FROM_PARENT') and item.get( ITEM_TYPE) in MEDIA_TYPES: updates = original keys = FIELDS_TO_COPY_FOR_ASSOCIATED_ITEM else: updates = super().find_one( req=None, _id=item[config.ID_FIELD]) or {} try: is_db_item_bigger_ver = updates[ '_current_version'] > original['_current_version'] except KeyError: update_item_data(item, updates, keys) else: update_item_data(item, updates, keys, keep_existing=not is_db_item_bigger_ver)
def _set_updates_for_media_items(self, doc, updates): if doc.get("type") not in MEDIA_TYPES: return for key in DEFAULT_SCHEMA.keys(): if doc.get(key): updates[key] = doc[key]
def _refresh_associated_items(self, original): """Refreshes associated items with the latest version. Any further updates made to basic metadata done after item was associated will be carried on and used when validating those items. """ associations = original.get(ASSOCIATIONS) or {} for __, item in associations.items(): if type(item) == dict and item.get(config.ID_FIELD): keys = [ key for key in DEFAULT_SCHEMA.keys() if key not in PRESERVED_FIELDS ] if app.settings.get('COPY_METADATA_FROM_PARENT') and item.get( ITEM_TYPE) in MEDIA_TYPES: updates = original keys = FIELDS_TO_COPY_FOR_ASSOCIATED_ITEM else: updates = super().find_one( req=None, _id=item[config.ID_FIELD]) or {} try: is_db_item_bigger_ver = updates['_current_version'] > item[ '_current_version'] except KeyError: update_item_data(item, updates, keys) else: # if copying from parent the don't keep the existing # otherwise check the value is_db_item_bigger_ver keep_existing = not app.settings.get( 'COPY_METADATA_FROM_PARENT' ) and not is_db_item_bigger_ver update_item_data(item, updates, keys, keep_existing=keep_existing)
def _publish_associated_items(self, original, updates={}, publish=False): """Refresh and publish associated items before publishing. The publishing is done if the setting PUBLISH_ASSOCIATED_ITEMS was true. Any further updates made to basic metadata done after item was associated will be carried on and used when validating those items. """ publish_service = None if config.PUBLISH_ASSOCIATED_ITEMS and publish_services.get( self.publish_type): publish_service = get_resource_service( publish_services[self.publish_type]) associations = original.get(ASSOCIATIONS) or {} for associations_key, item in associations.items(): if type(item) == dict and item.get(config.ID_FIELD): keys = [ key for key in DEFAULT_SCHEMA.keys() if key not in PRESERVED_FIELDS ] if app.settings.get('COPY_METADATA_FROM_PARENT') and item.get( ITEM_TYPE) in MEDIA_TYPES: original_item = original keys = FIELDS_TO_COPY_FOR_ASSOCIATED_ITEM else: original_item = super().find_one( req=None, _id=item[config.ID_FIELD]) or {} update_item_data(item, original_item, keys) remove_unwanted(item) if publish_service and publish and item['type'] in MEDIA_TYPES: if item.get('task', {}).get('stage', None): del item['task']['stage'] if item['state'] not in PUBLISH_STATES: get_resource_service('archive_publish').patch( id=item.pop(config.ID_FIELD), updates=item) else: publish_service.patch(id=item.pop(config.ID_FIELD), updates=item) item['state'] = self.published_state original[ASSOCIATIONS][associations_key][ 'state'] = self.published_state original[ASSOCIATIONS][associations_key][ 'operation'] = self.publish_type if ASSOCIATIONS not in updates: updates[ASSOCIATIONS] = original[ASSOCIATIONS] updates[ASSOCIATIONS][associations_key] = original[ ASSOCIATIONS][associations_key] elif publish: # Publishing an item with an association set the state and operation of the embedded association original[ASSOCIATIONS][associations_key][ 'state'] = self.published_state original[ASSOCIATIONS][associations_key][ 'operation'] = self.publish_type
def update_item_data(item, data, keys=DEFAULT_SCHEMA.keys(), keep_existing=False): """Update main item data, so only keys from default schema. :param dict item: item to update :param dict data: update date :param list keys: keys of item to update :param bool keep_existing: if True, will only set non existing values """ for key in keys: if data.get(key): if keep_existing: item.setdefault(key, data[key]) else: item[key] = data[key]
def _refresh_associated_items(self, original): """Refresh associated items before publishing Any further updates made to basic metadata done after item was associated will be carried on and used when validating those items. """ associations = original.get(ASSOCIATIONS) or {} for _, item in associations.items(): if type(item) == dict and item.get(config.ID_FIELD): keys = DEFAULT_SCHEMA.keys() if app.settings.get('COPY_METADATA_FROM_PARENT') and item.get(ITEM_TYPE) in MEDIA_TYPES: updates = original keys = FIELDS_TO_COPY_FOR_ASSOCIATED_ITEM else: updates = super().find_one(req=None, _id=item[config.ID_FIELD]) or {} update_item_data(item, updates, keys)
def _publish_associated_items(self, original, updates={}, publish=False): """Refresh and publish associated items before publishing. The publishing is done if the setting PUBLISH_ASSOCIATED_ITEMS was true. Any further updates made to basic metadata done after item was associated will be carried on and used when validating those items. """ publish_service = None if config.PUBLISH_ASSOCIATED_ITEMS and publish_services.get(self.publish_type): publish_service = get_resource_service(publish_services[self.publish_type]) associations = original.get(ASSOCIATIONS) or {} for associations_key, item in associations.items(): if type(item) == dict and item.get(config.ID_FIELD): keys = [key for key in DEFAULT_SCHEMA.keys() if key not in PRESERVED_FIELDS] if app.settings.get('COPY_METADATA_FROM_PARENT') and item.get(ITEM_TYPE) in MEDIA_TYPES: original_item = original keys = FIELDS_TO_COPY_FOR_ASSOCIATED_ITEM else: original_item = super().find_one(req=None, _id=item[config.ID_FIELD]) or {} update_item_data(item, original_item, keys) remove_unwanted(item) if publish_service and publish and item['type'] in MEDIA_TYPES: if item.get('task', {}).get('stage', None): del item['task']['stage'] if item['state'] not in PUBLISH_STATES: get_resource_service('archive_publish').patch(id=item.pop(config.ID_FIELD), updates=item) else: publish_service.patch(id=item.pop(config.ID_FIELD), updates=item) item['state'] = self.published_state original[ASSOCIATIONS][associations_key]['state'] = self.published_state original[ASSOCIATIONS][associations_key]['operation'] = self.publish_type if ASSOCIATIONS not in updates: updates[ASSOCIATIONS] = original[ASSOCIATIONS] updates[ASSOCIATIONS][associations_key] = original[ASSOCIATIONS][associations_key] elif publish: # Publishing an item with an association set the state and operation of the embedded association original[ASSOCIATIONS][associations_key]['state'] = self.published_state original[ASSOCIATIONS][associations_key]['operation'] = self.publish_type
def update_item_data(item, data, keys=DEFAULT_SCHEMA.keys()): """Update main item data, so only keys from default schema. """ for key in keys: if data.get(key): item[key] = data[key]