def on_created(self, docs): packages = [ doc for doc in docs if doc[ITEM_TYPE] == CONTENT_TYPE.COMPOSITE ] if packages: self.packageService.on_created(packages) profiles = set() for doc in docs: subject = get_subject(doc) if subject: msg = 'added new {{ type }} item about "{{ subject }}"' else: msg = 'added new {{ type }} item with empty header/title' add_activity(ACTIVITY_CREATE, msg, self.datasource, item=doc, type=doc[ITEM_TYPE], subject=subject) if doc.get('profile'): profiles.add(doc['profile']) get_resource_service('content_types').set_used(profiles) push_content_notification(docs)
def on_create(self, docs): ''' Create corresponding item on file upload ''' for doc in docs: file, content_type, metadata = self.get_file_from_document(doc) inserted = [doc['media']] file_type = content_type.split('/')[0] try: update_dates_for(doc) doc['guid'] = generate_guid(type=GUID_TAG) doc.setdefault('_id', doc['guid']) doc['type'] = self.type_av.get(file_type) doc['version'] = 1 doc['versioncreated'] = utcnow() rendition_spec = config.RENDITIONS['picture'] renditions = generate_renditions(file, doc['media'], inserted, file_type, content_type, rendition_spec, url_for_media) doc['renditions'] = renditions doc['mimetype'] = content_type doc['filemeta'] = metadata if not doc.get('_import', None): doc['creator'] = set_user(doc) add_activity('uploaded media {{ name }}', name=doc.get('headline', doc.get('mimetype')), renditions=doc.get('renditions')) except Exception as io: logger.exception(io) for file_id in inserted: delete_file_on_error(doc, file_id) abort(500) on_create_media_archive()
def send_translation_notifications(original): translated_items = get_resource_service("published").find( {"translation_id": original.get("guid"), "last_published_version": True} ) user_ids = set() for translated_item in translated_items: if translated_item.get("item_id") == original.get("_id"): changed_article = translated_item continue user_ids.add(translated_item.get("original_creator")) user_ids.add(translated_item.get("version_creator")) if len(user_ids) == 0 or changed_article is None: return add_activity("translated:changed", "", resource=None, item=changed_article, notify=user_ids) recipients = [] for user_id in user_ids: user = get_resource_service("users").find_one(req=None, _id=user_id) if user is not None and user.get("email", None) is not None: recipients.append(user.get("email")) if len(recipients) == 0: return username = g.user.get("display_name") or g.user.get("username") send_translation_changed(username, changed_article, recipients)
def on_replaced(self, document, original): get_component(ItemAutosave).clear(original['_id']) add_activity(ACTIVITY_UPDATE, 'replaced item {{ type }} about {{ subject }}', self.datasource, item=original, type=original['type'], subject=get_subject(original)) push_content_notification([document, original]) self.cropService.update_media_references(document, original)
def on_updated(self, updates, original): updated = copy(original) updated.update(updates) if self._stage_changed(updates, original): insert_into_versions(doc=updated) new_task = updates.get('task', {}) old_task = original.get('task', {}) if new_task.get('stage') != old_task.get('stage'): push_notification('task:stage', new_stage=str(new_task.get('stage', '')), old_stage=str(old_task.get('stage', '')), new_desk=str(new_task.get('desk', '')), old_desk=str(old_task.get('desk', ''))) else: push_notification(self.datasource, updated=1) if is_assigned_to_a_desk(updated): if self.__is_content_assigned_to_new_desk(original, updates) and \ not self._stage_changed(updates, original): insert_into_versions(doc=updated) add_activity(ACTIVITY_UPDATE, 'updated task {{ subject }} for item {{ type }}', self.datasource, item=updated, subject=get_subject(updated), type=updated['type'])
def _set_assignment_information(self, doc): if doc.get('planning') and doc['planning'].get('assigned_to'): planning = doc['planning'] if planning['assigned_to'].get('user') and not planning['assigned_to'].get('desk'): raise SuperdeskApiError.badRequestError(message="Assignment should have a desk.") # In case user was removed if not planning['assigned_to'].get('user'): planning['assigned_to']['user'] = None user = get_user() if user and user.get(config.ID_FIELD): planning['assigned_to']['assigned_by'] = user[config.ID_FIELD] planning['assigned_to']['assigned_date'] = utcnow() if planning['assigned_to'].get('user'): # Done to avoid fetching users data for every assignment # Because user assigned can also be a provider whose qcode # might be an invalid GUID, check if the user assigned is a valid user (GUID) # However, in a rare case where qcode of a provider is a valid GUID, # This will create activity records - inappropirate if ObjectId.is_valid(planning['assigned_to'].get('user')): add_activity(ACTIVITY_UPDATE, '{{assignor}} assigned a coverage to you', self.datasource, notify=[planning['assigned_to'].get('user')], assignor=user.get('username'))
def __send_notification(self, updates, user): user_id = user['_id'] if 'is_enabled' in updates and not updates['is_enabled']: push_notification('user_disabled', updated=1, user_id=str(user_id)) elif 'is_active' in updates and not updates['is_active']: push_notification('user_inactivated', updated=1, user_id=str(user_id)) elif 'role' in updates: push_notification('user_role_changed', updated=1, user_id=str(user_id)) elif 'privileges' in updates: added, removed, modified = compare_preferences(user.get('privileges', {}), updates['privileges']) if len(removed) > 0 or (1, 0) in modified.values(): push_notification('user_privileges_revoked', updated=1, user_id=str(user_id)) if len(added) > 0: add_activity(ACTIVITY_UPDATE, 'user {{user}} is granted new privileges: Please re-login.', self.datasource, notify=[user_id], user=user.get('display_name', user.get('username'))) elif 'user_type' in updates: if not is_admin(updates): push_notification('user_type_changed', updated=1, user_id=str(user_id)) else: add_activity(ACTIVITY_UPDATE, 'user {{user}} is updated to administrator: Please re-login.', self.datasource, notify=[user_id], user=user.get('display_name', user.get('username'))) else: push_notification('user', updated=1, user_id=str(user_id))
def on_created(self, docs): packages = [doc for doc in docs if doc[ITEM_TYPE] == CONTENT_TYPE.COMPOSITE] if packages: self.packageService.on_created(packages) profiles = set() for doc in docs: subject = get_subject(doc) if subject: msg = 'added new {{ type }} item about "{{ subject }}"' else: msg = 'added new {{ type }} item with empty header/title' add_activity(ACTIVITY_CREATE, msg, self.datasource, item=doc, type=doc[ITEM_TYPE], subject=subject) if doc.get('profile'): profiles.add(doc['profile']) self.cropService.update_media_references(doc, {}) if doc[ITEM_OPERATION] == ITEM_FETCH: app.on_archive_item_updated({'task': doc.get('task')}, doc, ITEM_FETCH) else: app.on_archive_item_updated({'task': doc.get('task')}, doc, ITEM_CREATE) get_resource_service('content_types').set_used(profiles) push_content_notification(docs)
def on_created(self, docs): for user_doc in docs: self.update_user_defaults(user_doc) add_activity(ACTIVITY_CREATE, 'created user {{user}}', user=user_doc.get('display_name', user_doc.get('username')))
def __send_notification(self, updates, desk): desk_id = desk[config.ID_FIELD] if 'members' in updates: added, removed = self.__compare_members(desk.get('members', {}), updates['members']) if len(removed) > 0: push_notification('desk_membership_revoked', updated=1, user_ids=[str(item) for item in removed], desk_id=str(desk_id)) for added_user in added: user = superdesk.get_resource_service('users').find_one( req=None, _id=added_user) add_activity( ACTIVITY_UPDATE, 'user {{user}} has been added to desk {{desk}}: Please re-login.', self.datasource, notify=added, user=user.get('username'), desk=desk.get('name')) else: push_notification(self.notification_key, updated=1, desk_id=str(desk.get(config.ID_FIELD)))
def notify_members(blog, origin, recipients): add_activity('notify', 'you have been added as a member', resource=None, item=blog, notify=recipients) send_email_to_added_members(blog, recipients, origin)
def notify_mentioned_users(docs, origin): for doc in docs: mentioned_users = doc.get('mentioned_users', {}).values() add_activity('notify', '', type='comment', item=doc, comment=doc.get('text'), comment_id=str(doc.get('_id')), notify=mentioned_users) send_email_to_mentioned_users(doc, mentioned_users, origin)
def on_updated(self, updates, original): get_component(ItemAutosave).clear(original['_id']) if original[ITEM_TYPE] == CONTENT_TYPE.COMPOSITE: self.packageService.on_updated(updates, original) updated = copy(original) updated.update(updates) if config.VERSION in updates: add_activity( ACTIVITY_UPDATE, 'created new version {{ version }} for item {{ type }} about "{{ subject }}"', self.datasource, item=updated, version=updates[config.VERSION], subject=get_subject(updates, original), type=updated[ITEM_TYPE]) push_content_notification([updated, original]) get_resource_service('archive_broadcast').reset_broadcast_status( updates, original) if updates.get('profile'): get_resource_service('content_types').set_used( [updates.get('profile')]) self.cropService.update_media_references(updates, original)
def on_created(self, docs): packages = [ doc for doc in docs if doc[ITEM_TYPE] == CONTENT_TYPE.COMPOSITE ] if packages: self.packageService.on_created(packages) profiles = set() for doc in docs: subject = get_subject(doc) if subject: msg = 'added new {{ type }} item about "{{ subject }}"' else: msg = 'added new {{ type }} item with empty header/title' add_activity(ACTIVITY_CREATE, msg, self.datasource, item=doc, type=doc[ITEM_TYPE], subject=subject) if doc.get('profile'): profiles.add(doc['profile']) self.cropService.update_media_references(doc, {}) if doc[ITEM_OPERATION] == ITEM_FETCH: app.on_archive_item_updated({'task': doc.get('task')}, doc, ITEM_FETCH) else: app.on_archive_item_updated({'task': doc.get('task')}, doc, ITEM_CREATE) get_resource_service('content_types').set_used(profiles) push_content_notification(docs)
def on_create(self, docs): """Create corresponding item on file upload.""" for doc in docs: if 'media' not in doc or doc['media'] is None: abort(400, description="No media found") file, content_type, metadata = self.get_file_from_document(doc) inserted = [doc['media']] file_type = content_type.split('/')[0] self._set_metadata(doc) try: doc[ITEM_TYPE] = self.type_av.get(file_type) rendition_spec = get_renditions_spec() renditions = generate_renditions(file, doc['media'], inserted, file_type, content_type, rendition_spec, url_for_media) doc['renditions'] = renditions doc['mimetype'] = content_type set_filemeta(doc, metadata) add_activity('upload', 'uploaded media {{ name }}', 'archive', item=doc, name=doc.get('headline', doc.get('mimetype')), renditions=doc.get('renditions')) except Exception as io: logger.exception(io) for file_id in inserted: delete_file_on_error(doc, file_id) abort(500)
def send_translation_notifications(original): translated_items = get_resource_service('published').find({ 'translation_id': original.get('guid'), 'last_published_version': True }) user_ids = set() for translated_item in translated_items: if translated_item.get('item_id') == original.get('_id'): changed_article = translated_item continue user_ids.add(translated_item.get('original_creator')) user_ids.add(translated_item.get('version_creator')) if len(user_ids) == 0 or changed_article is None: return add_activity('translated:changed', '', resource=None, item=changed_article, notify=user_ids) recipients = [] for user_id in user_ids: user = get_resource_service('users').find_one(req=None, _id=user_id) if user is not None and user.get('email', None) is not None: recipients.append(user.get('email')) if len(recipients) == 0: return username = g.user.get('display_name') or g.user.get('username') send_translation_changed(username, changed_article, recipients)
def on_updated(self, updates, original): get_component(ItemAutosave).clear(original['_id']) on_update_media_archive() if '_version' in updates: add_activity('created new version {{ version }} for item {{ type }} about {{ subject }}', version=updates['_version'], subject=get_subject(updates, original))
def on_deleted(self, doc): on_delete_media_archive() add_activity(ACTIVITY_DELETE, 'removed item {{ type }} about {{ subject }}', item=doc, type=doc['type'], subject=get_subject(doc))
def on_replaced(self, document, original): get_component(ItemAutosave).clear(original['_id']) add_activity(ACTIVITY_UPDATE, 'replaced item {{ type }} about {{ subject }}', self.datasource, item=original, type=original['type'], subject=get_subject(original)) user = get_user() push_notification('item:replaced', item=str(original['_id']), user=str(user.get('_id')))
def on_updated(self, updates, original): updated = copy(original) updated.update(updates) if self._stage_changed(updates, original): insert_into_versions(doc=updated) new_task = updates.get("task", {}) old_task = original.get("task", {}) if new_task.get("stage") != old_task.get("stage"): push_notification( "task:stage", new_stage=str(new_task.get("stage", "")), old_stage=str(old_task.get("stage", "")), new_desk=str(new_task.get("desk", "")), old_desk=str(old_task.get("desk", "")), ) else: push_notification(self.datasource, updated=1) if is_assigned_to_a_desk(updated): if self.__is_content_assigned_to_new_desk(original, updates) and not self._stage_changed(updates, original): insert_into_versions(doc=updated) add_activity( ACTIVITY_UPDATE, "updated task {{ subject }} for item {{ type }}", self.datasource, item=updated, subject=get_subject(updated), type=updated["type"], )
def on_created(self, docs): push_notification(self.datasource, created=1) for doc in docs: insert_into_versions(doc['_id']) if self.__is_assigned_to_a_desk(doc): add_activity(ACTIVITY_CREATE, 'added new task {{ subject }} of type {{ type }}', item=doc, subject=get_subject(doc), type=doc['type'])
def on_updated(self, updates, original): push_notification(self.datasource, updated=1) updated = copy(original) updated.update(updates) if updated.get('task') and updated['task'].get('desk'): add_activity(ACTIVITY_UPDATE, 'updated task {{ subject }} for item {{ type }}', item=updated, subject=get_subject(updated))
def __send_notification(self, updates, user): user_id = user['_id'] if 'is_enabled' in updates and not updates['is_enabled']: push_notification('user_disabled', updated=1, user_id=str(user_id)) elif 'is_active' in updates and not updates['is_active']: push_notification('user_inactivated', updated=1, user_id=str(user_id)) elif 'role' in updates: push_notification('user_role_changed', updated=1, user_id=str(user_id)) elif 'privileges' in updates: added, removed, modified = compare_preferences(user.get('privileges', {}), updates['privileges']) if len(removed) > 0 or (1, 0) in modified.values(): push_notification('user_privileges_revoked', updated=1, user_id=str(user_id)) if len(added) > 0: add_activity(ACTIVITY_UPDATE, 'user {{user}} has been granted new privileges: Please re-login.', self.datasource, notify=[user_id], user=user.get('display_name', user.get('username'))) elif 'user_type' in updates: if not is_admin(updates): push_notification('user_type_changed', updated=1, user_id=str(user_id)) else: add_activity(ACTIVITY_UPDATE, 'user {{user}} is updated to administrator: Please re-login.', self.datasource, notify=[user_id], user=user.get('display_name', user.get('username'))) else: push_notification('user', updated=1, user_id=str(user_id))
def notify_mentioned_users(docs, origin): for doc in docs: mentioned_users = doc.get('mentioned_users', {}).values() item = superdesk.get_resource_service('archive').find_one(req=None, _id=doc['item']) add_activity('notify', '', resource=None, type='comment', item=item, comment=doc.get('text'), comment_id=str(doc.get('_id')), notify=mentioned_users) send_email_to_mentioned_users(doc, mentioned_users, origin)
def notify_the_owner(doc, origin): if not get_user(): logger.info('there is no logged in user so no membership is allowed') else: blog = get_resource_service('blogs').find_one(req=None, _id=doc.get('blog')) owner = blog.get('original_creator') add_activity('notify', 'one user requested liveblog membership', resource=None, item=doc, notify=str(owner)) send_email_to_owner(doc, owner, origin)
def notify_mentioned_desks(docs): for doc in docs: mentioned_desks = doc.get('mentioned_desks', {}).values() if len(mentioned_desks) > 0: item = superdesk.get_resource_service('archive').find_one(req=None, _id=doc['item']) add_activity('desk:mention', '', resource=None, type='comment', item=item, comment=doc.get('text'), comment_id=str(doc.get('_id')), notify_desks=mentioned_desks)
def notify_members(blog, blog_url, recipients): add_activity('liveblog:add', 'you have been added as a member', resource=None, item=blog, item_slugline=blog.get('title'), notify=recipients) send_email_to_added_members(blog, recipients, blog_url)
def on_deleted(self, doc): if doc['type'] == 'composite': self.packageService.on_deleted(doc) add_activity(ACTIVITY_DELETE, 'removed item {{ type }} about {{ subject }}', self.datasource, item=doc, type=doc['type'], subject=get_subject(doc)) user = get_user() push_notification('item:deleted', item=str(doc['_id']), user=str(user.get('_id')))
def notify_mentioned_users(docs, origin): for doc in docs: mentioned_users = doc.get('mentioned_users', {}).values() item = superdesk.get_resource_service('archive').find_one(req=None, _id=doc['item']) add_activity('notify', '', type='comment', item=item, comment=doc.get('text'), comment_id=str(doc.get('_id')), notify=mentioned_users) send_email_to_mentioned_users(doc, mentioned_users, origin)
def on_created(self, docs): push_notification(self.datasource, created=1) for doc in docs: if doc.get('task') and doc['task'].get('desk'): add_activity(ACTIVITY_CREATE, 'added new task {{ subject }} of type {{ type }}', item=doc, subject=get_subject(doc), type=doc['type'])
def on_created(self, docs): on_create_media_archive() get_component(LegalArchiveProxy).create(docs) for doc in docs: add_activity(ACTIVITY_CREATE, 'added new item {{ type }} about {{ subject }}', item=doc, type=doc['type'], subject=get_subject(doc))
def on_created(self, docs): for user_doc in docs: self.__update_user_defaults(user_doc) add_activity(ACTIVITY_CREATE, 'created user {{user}}', self.datasource, user=user_doc.get('display_name', user_doc.get('username'))) self.update_stage_visibility_for_user(user_doc)
def on_created(self, docs): push_notification(self.datasource, created=1) push_notification('task:new') for doc in docs: insert_into_versions(doc['_id']) if is_assigned_to_a_desk(doc): add_activity(ACTIVITY_CREATE, 'added new task {{ subject }} of type {{ type }}', self.datasource, item=doc, subject=get_subject(doc), type=doc[ITEM_TYPE])
def on_deleted(self, doc): if doc[ITEM_TYPE] == CONTENT_TYPE.COMPOSITE: self.packageService.on_deleted(doc) remove_media_files(doc) add_activity(ACTIVITY_DELETE, 'removed item {{ type }} about {{ subject }}', self.datasource, item=doc, type=doc[ITEM_TYPE], subject=get_subject(doc)) push_content_notification([doc])
def on_created(self, docs): on_create_media_archive() get_component(LegalArchiveProxy).create(docs) for doc in docs: subject = get_subject(doc) if subject: msg = 'added new {{ type }} item about "{{ subject }}"' else: msg = 'added new {{ type }} item with empty header/title' add_activity(ACTIVITY_CREATE, msg, item=doc, type=doc['type'], subject=subject)
def on_updated(self, updates, original): get_component(ItemAutosave).clear(original['_id']) get_component(LegalArchiveProxy).update(original, updates) on_update_media_archive() if '_version' in updates: updated = copy(original) updated.update(updates) add_activity(ACTIVITY_UPDATE, 'created new version {{ version }} for item {{ type }} about {{ subject }}', item=updated, version=updates['_version'], subject=get_subject(updates, original))
def on_create(self, docs): """Create corresponding item on file upload.""" for doc in docs: if 'media' not in doc or doc['media'] is None: abort(400, description="No media found") # check content type of video by python-magic content_type = magic.from_buffer(doc['media'].read(1024), mime=True) doc['media'].seek(0) file_type = content_type.split('/')[0] if file_type == 'video' and app.config.get("VIDEO_SERVER_ENABLE"): if not self.videoEditor.check_video_server(): raise SuperdeskApiError( message="Cannot connect to videoserver", status_code=500) # upload media to video server res, renditions, metadata = self.upload_file_to_video_server( doc) # get thumbnails for timeline bar self.videoEditor.get_timeline_thumbnails(doc.get('media'), 40) else: file, content_type, metadata = self.get_file_from_document(doc) inserted = [doc['media']] # if no_custom_crops is set to False the custom crops are generated automatically on media upload # see (SDESK-4742) rendition_spec = get_renditions_spec( no_custom_crops=app.config.get("NO_CUSTOM_CROPS")) with timer('archive:renditions'): renditions = generate_renditions(file, doc['media'], inserted, file_type, content_type, rendition_spec, url_for_media) try: self._set_metadata(doc) doc[ITEM_TYPE] = self.type_av.get(file_type) doc[ITEM_STATE] = CONTENT_STATE.PROGRESS doc['renditions'] = renditions doc['mimetype'] = content_type set_filemeta(doc, metadata) add_activity('upload', 'uploaded media {{ name }}', 'archive', item=doc, name=doc.get('headline', doc.get('mimetype')), renditions=doc.get('renditions')) except Exception as io: logger.exception(io) for file_id in inserted: delete_file_on_error(doc, file_id) if res: self.videoEditor.delete(res.get('_id')) abort(500)
def on_updated(self, updates, original): get_component(ItemAutosave).clear(original['_id']) get_component(LegalArchiveProxy).update(original, updates) on_update_media_archive(item=original['_id']) if '_version' in updates: updated = copy(original) updated.update(updates) add_activity(ACTIVITY_UPDATE, 'created new version {{ version }} for item {{ type }} about "{{ subject }}"', item=updated, version=updates['_version'], subject=get_subject(updates, original), type=updated['type'])
def on_replaced(self, document, original): get_component(ItemAutosave).clear(original["_id"]) add_activity( ACTIVITY_UPDATE, "replaced item {{ type }} about {{ subject }}", self.datasource, item=original, type=original["type"], subject=get_subject(original), ) user = get_user() push_notification("item:replaced", item=str(original["_id"]), user=str(user.get("_id")))
def on_deleted(self, doc): get_component(ItemAutosave).clear(doc['_id']) if doc[ITEM_TYPE] == CONTENT_TYPE.COMPOSITE: self.packageService.on_deleted(doc) remove_media_files(doc) add_activity(ACTIVITY_DELETE, 'removed item {{ type }} about {{ subject }}', self.datasource, item=doc, type=doc[ITEM_TYPE], subject=get_subject(doc)) push_expired_notification([doc.get(config.ID_FIELD)]) app.on_archive_item_deleted(doc)
def on_deleted(self, doc): """Overriding to add to activity stream and handle user clean up. 1. Authenticated Sessions 2. Locked Articles 3. Reset Password Tokens """ add_activity(ACTIVITY_UPDATE, 'disabled user {{user}}', self.datasource, user=doc.get('display_name', doc.get('username'))) self.__clear_locked_items(str(doc['_id'])) self.__handle_status_changed(updates={'is_enabled': False, 'is_active': False}, user=doc)
def on_deleted(self, doc): if doc["type"] == "composite": self.packageService.on_deleted(doc) add_activity( ACTIVITY_DELETE, "removed item {{ type }} about {{ subject }}", self.datasource, item=doc, type=doc["type"], subject=get_subject(doc), ) user = get_user() push_notification("item:deleted", item=str(doc["_id"]), user=str(user.get("_id")))
def on_created(self, docs): packages = [doc for doc in docs if doc[ITEM_TYPE] == CONTENT_TYPE.COMPOSITE] if packages: self.packageService.on_created(packages) for doc in docs: subject = get_subject(doc) if subject: msg = 'added new {{ type }} item about "{{ subject }}"' else: msg = 'added new {{ type }} item with empty header/title' add_activity(ACTIVITY_CREATE, msg, self.datasource, item=doc, type=doc[ITEM_TYPE], subject=subject) push_content_notification(docs)
def on_created(self, docs): packages = [doc for doc in docs if doc["type"] == "composite"] if packages: self.packageService.on_created(packages) user = get_user() for doc in docs: subject = get_subject(doc) if subject: msg = 'added new {{ type }} item about "{{ subject }}"' else: msg = "added new {{ type }} item with empty header/title" add_activity(ACTIVITY_CREATE, msg, self.datasource, item=doc, type=doc["type"], subject=subject) push_notification("item:created", item=str(doc["_id"]), user=str(user.get("_id")))
def on_created(self, docs): push_notification(self.datasource, created=1) push_notification("task:new") for doc in docs: insert_into_versions(doc["_id"]) if is_assigned_to_a_desk(doc): add_activity( ACTIVITY_CREATE, "added new task {{ subject }} of type {{ type }}", self.datasource, item=doc, subject=get_subject(doc), type=doc[ITEM_TYPE], )
def on_created(self, docs): packages = [doc for doc in docs if doc['type'] == 'composite'] if packages: self.packageService.on_created(packages) get_component(LegalArchiveProxy).create(docs) user = get_user() for doc in docs: subject = get_subject(doc) if subject: msg = 'added new {{ type }} item about "{{ subject }}"' else: msg = 'added new {{ type }} item with empty header/title' add_activity(ACTIVITY_CREATE, msg, self.datasource, item=doc, type=doc['type'], subject=subject) push_notification('item:created', item=str(doc['_id']), user=str(user.get('_id')))
def __send_notification(self, updates, desk): desk_id = desk[config.ID_FIELD] if "members" in updates: added, removed = self.__compare_members(desk.get("members", {}), updates["members"]) if len(removed) > 0: push_notification( "desk_membership_revoked", updated=1, user_ids=[str(item) for item in removed], desk_id=str(desk_id) ) for added_user in added: user = superdesk.get_resource_service("users").find_one(req=None, _id=added_user) activity = add_activity( ACTIVITY_UPDATE, "user {{user}} has been added to desk {{desk}}: Please re-login.", self.datasource, notify=added, can_push_notification=False, user=user.get("username"), desk=desk.get("name"), ) push_notification("activity", _dest=activity["recipients"]) get_resource_service("users").update_stage_visibility_for_users() else: push_notification(self.notification_key, updated=1, desk_id=str(desk.get(config.ID_FIELD)))
def on_updated(self, updates, original): get_component(ItemAutosave).clear(original['_id']) get_component(LegalArchiveProxy).update(original, updates) if original['type'] == 'composite': self.packageService.on_updated(updates, original) user = get_user() if '_version' in updates: updated = copy(original) updated.update(updates) add_activity(ACTIVITY_UPDATE, 'created new version {{ version }} for item {{ type }} about "{{ subject }}"', self.datasource, item=updated, version=updates['_version'], subject=get_subject(updates, original), type=updated['type']) push_notification('item:updated', item=str(original['_id']), user=str(user.get('_id')))
def on_create(self, docs): """ Create corresponding item on file upload """ for doc in docs: if 'media' not in doc or doc['media'] is None: abort(400, description="No media found") file, content_type, metadata = self.get_file_from_document(doc) inserted = [doc['media']] file_type = content_type.split('/')[0] try: update_dates_for(doc) generate_unique_id_and_name(doc) doc['guid'] = generate_guid(type=GUID_TAG) doc.setdefault('_id', doc['guid']) doc['type'] = self.type_av.get(file_type) doc[config.VERSION] = 1 doc['versioncreated'] = utcnow() set_item_expiry({}, doc) rendition_spec = config.RENDITIONS['picture'] renditions = generate_renditions(file, doc['media'], inserted, file_type, content_type, rendition_spec, url_for_media) doc['renditions'] = renditions doc['mimetype'] = content_type doc['filemeta'] = metadata if not doc.get('_import', None): set_original_creator(doc) doc.setdefault(config.CONTENT_STATE, 'draft') if not doc.get('ingest_provider'): doc['source'] = DEFAULT_SOURCE_VALUE_FOR_MANUAL_ARTICLES add_activity('upload', 'uploaded media {{ name }}', 'archive', item=doc, name=doc.get('headline', doc.get('mimetype')), renditions=doc.get('renditions')) except Exception as io: logger.exception(io) for file_id in inserted: delete_file_on_error(doc, file_id) abort(500)
def on_updated(self, updates, original): new_stage = updates.get('task', {}).get('stage', '') old_stage = original.get('task', {}).get('stage', '') if new_stage != old_stage: push_notification('task:stage', new_stage=str(new_stage), old_stage=str(old_stage)) else: push_notification(self.datasource, updated=1) updated = copy(original) updated.update(updates) if self.__is_assigned_to_a_desk(updated): if self.__is_content_assigned_to_new_desk(original, updates): insert_into_versions(original['_id']) add_activity(ACTIVITY_UPDATE, 'updated task {{ subject }} for item {{ type }}', item=updated, subject=get_subject(updated), type=updated['type'])