Exemplo n.º 1
0
    def duplicate_item(self, original_doc, state=None, extra_fields=None, operation=None):
        """Duplicates an item.

        Duplicates the 'original_doc' including it's version history. If the article being duplicated is contained
        in a desk then the article state is changed to Submitted.

        :return: guid of the duplicated article
        """

        new_doc = original_doc.copy()
        self.remove_after_copy(new_doc, extra_fields)
        on_duplicate_item(new_doc, original_doc, operation)
        resolve_document_version(new_doc, SOURCE, 'PATCH', new_doc)

        if original_doc.get('task', {}).get('desk') is not None and new_doc.get(ITEM_STATE) != CONTENT_STATE.SUBMITTED:
            new_doc[ITEM_STATE] = CONTENT_STATE.SUBMITTED

        if state:
            new_doc[ITEM_STATE] = state

        convert_task_attributes_to_objectId(new_doc)
        get_model(ItemModel).create([new_doc])
        self._duplicate_versions(original_doc['_id'], new_doc)
        self._duplicate_history(original_doc['_id'], new_doc)
        app.on_archive_item_updated({'duplicate_id': new_doc['guid']}, original_doc, operation or ITEM_DUPLICATE)
        app.on_archive_item_updated({'duplicate_id': original_doc['_id']}, new_doc, operation or ITEM_DUPLICATED_FROM)

        return new_doc['guid']
Exemplo n.º 2
0
    def autosave(self, item_id, updates, user, etag):
        item_model = get_model(ItemModel)
        item = item_model.find_one({'_id': item_id})
        if item is None:
            raise SuperdeskError('Invalid item identifier', 404)
        if etag:
            item_model.validate_etag(item, etag)

        lock_user = item.get('lock_user', None)
        if lock_user and str(lock_user) != str(user['_id']):
            raise SuperdeskError(payload='The item was locked by another user')

        autosave_model = get_model(ItemAutosaveModel)
        item.update(updates)
        self.app.on_item_autosave(item)
        try:
            autosave_model.create(item)
        except:
            id = item['_id']
            del item['_id']
            autosave_model.update({'_id': item_id}, item)
            item['_id'] = id
        self.app.on_item_autosaved(item)
        updates.update(item)
        return updates
Exemplo n.º 3
0
    def autosave(self, item_id, updates, user, etag):
        item_model = get_model(ItemModel)
        item = item_model.find_one({'_id': item_id})
        if item is None:
            raise superdesk.SuperdeskError('Invalid item identifier', 404)
        if etag:
            item_model.validate_etag(item, etag)

        lock_user = item.get('lock_user', None)
        if lock_user and str(lock_user) != str(user['_id']):
            raise superdesk.SuperdeskError(payload='The item was locked by another user')

        autosave_model = get_model(ItemAutosaveModel)
        item.update(updates)
        self.app.on_item_autosave(item)
        try:
            autosave_model.create(item)
        except:
            id = item['_id']
            del item['_id']
            autosave_model.update({'_id': item_id}, item)
            item['_id'] = id
        self.app.on_item_autosaved(item)
        updates.update(item)
        return updates
Exemplo n.º 4
0
    def _duplicate_item(self, original_doc, state=None):
        """Duplicates an item.

        Duplicates the 'original_doc' including it's version history. If the article being duplicated is contained
        in a desk then the article state is changed to Submitted.

        :return: guid of the duplicated article
        """

        new_doc = original_doc.copy()
        self._remove_after_copy(new_doc)
        on_duplicate_item(new_doc, original_doc)
        resolve_document_version(new_doc, SOURCE, 'PATCH', new_doc)

        if original_doc.get('task', {}).get('desk') is not None and new_doc.get(ITEM_STATE) != CONTENT_STATE.SUBMITTED:
            new_doc[ITEM_STATE] = CONTENT_STATE.SUBMITTED

        if state:
            new_doc[ITEM_STATE] = state

        convert_task_attributes_to_objectId(new_doc)
        get_model(ItemModel).create([new_doc])
        self._duplicate_versions(original_doc['_id'], new_doc)
        self._duplicate_history(original_doc['_id'], new_doc)
        app.on_archive_item_updated({'duplicate_id': new_doc['guid']}, original_doc, ITEM_DUPLICATE)
        app.on_archive_item_updated({'duplicate_id': original_doc['_id']}, new_doc, ITEM_DUPLICATED_FROM)

        return new_doc['guid']
Exemplo n.º 5
0
 def restore(self, filter, user):
     item_model = get_model(ItemModel)
     item = item_model.find_one(filter)
     if item:
         updates = get_restore_updates(item)
         item_model.update(filter, updates)
         push_notification('item:restore', item=str(filter.get('_id')), user=str(user))
Exemplo n.º 6
0
    def lock(self, item_filter, user_id, session_id, etag):
        item_model = get_model(ItemModel)
        item = item_model.find_one(item_filter)

        if not item:
            raise SuperdeskApiError.notFoundError()

        can_user_lock, error_message = self.can_lock(item, user_id, session_id)

        if can_user_lock:
            self.app.on_item_lock(item, user_id)
            updates = {LOCK_USER: user_id, LOCK_SESSION: session_id, 'lock_time': utcnow()}
            item_model.update(item_filter, updates)

            if item.get(TASK):
                item[TASK]['user'] = user_id
            else:
                item[TASK] = {'user': user_id}

            superdesk.get_resource_service('tasks').assign_user(item[config.ID_FIELD], item[TASK])
            self.app.on_item_locked(item, user_id)
            push_notification('item:lock',
                              item=str(item.get(config.ID_FIELD)),
                              item_version=str(item.get(config.VERSION)),
                              user=str(user_id), lock_time=updates['lock_time'],
                              lock_session=str(session_id))
        else:
            raise SuperdeskApiError.forbiddenError(message=error_message)

        item = item_model.find_one(item_filter)
        return item
Exemplo n.º 7
0
    def unlock(self, item_filter, user_id, session_id, etag):
        item_model = get_model(ItemModel)
        item = item_model.find_one(item_filter)

        if not item:
            raise SuperdeskApiError.notFoundError()

        if not item.get(LOCK_USER):
            raise SuperdeskApiError.badRequestError(message="Item is not locked.")

        can_user_unlock, error_message = self.can_unlock(item, user_id)

        if can_user_unlock:
            self.app.on_item_unlock(item, user_id)

            # delete the item if nothing is saved so far
            # version 0 created on lock item
            if item.get(config.VERSION, 0) == 0 and item[ITEM_STATE] == CONTENT_STATE.DRAFT:
                superdesk.get_resource_service('archive').delete_action(lookup={'_id': item['_id']})
                push_content_notification([item])
            else:
                updates = {LOCK_USER: None, LOCK_SESSION: None, 'lock_time': None, 'force_unlock': True}
                item_model.update(item_filter, updates)
                self.app.on_item_unlocked(item, user_id)

            push_notification('item:unlock',
                              item=str(item_filter.get(config.ID_FIELD)),
                              item_version=str(item.get(config.VERSION)),
                              state=item.get(ITEM_STATE),
                              user=str(user_id), lock_session=str(session_id))
        else:
            raise SuperdeskApiError.forbiddenError(message=error_message)

        item = item_model.find_one(item_filter)
        return item
Exemplo n.º 8
0
    def spike(self, filter, user):
        item_model = get_model(ItemModel)
        item = item_model.find_one(filter)
        if item and can_lock(item, user):
            expiry_minutes = app.settings['SPIKE_EXPIRY_MINUTES']
            # check if item is in a desk
            if "task" in item and "desk" in item["task"]:
                # then use the desks spike_expiry
                desk = get_resource_service('desks').find_one(
                    _id=item["task"]["desk"], req=None)
                expiry_minutes = desk.get('spike_expiry', expiry_minutes)

            updates = {
                IS_SPIKED: True,
                EXPIRY: get_expiry_date(expiry_minutes)
            }
            item_model.update(filter, updates)
            push_notification('item:spike',
                              item=str(item.get('_id')),
                              user=str(user))
        else:
            raise SuperdeskError(
                "Item couldn't be spiked. It is locked by another user")
        item = item_model.find_one(filter)
        return item
Exemplo n.º 9
0
    def unlock(self, item_filter, user_id, session_id, etag):
        item_model = get_model(ItemModel)
        item = item_model.find_one(item_filter)

        if not item:
            raise SuperdeskApiError.notFoundError()

        if not item.get(LOCK_USER):
            raise SuperdeskApiError.badRequestError(
                message="Item is not locked.")

        can_user_unlock, error_message = self.can_unlock(item, user_id)

        if can_user_unlock:
            self.app.on_item_unlock(item, user_id)
            updates = {
                LOCK_USER: None,
                LOCK_SESSION: None,
                'lock_time': None,
                'force_unlock': True
            }
            item_model.update(item_filter, updates)
            self.app.on_item_unlocked(item, user_id)
            push_notification('item:unlock',
                              item=str(item_filter.get(config.ID_FIELD)),
                              user=str(user_id),
                              lock_session=str(session_id))
        else:
            raise SuperdeskApiError.forbiddenError(message=error_message)

        item = item_model.find_one(item_filter)
        return item
Exemplo n.º 10
0
    def unlock(self, item_filter, user_id, session_id, etag):
        item_model = get_model(ItemModel)
        item = item_model.find_one(item_filter)

        if not item:
            raise SuperdeskApiError.notFoundError()

        if not item.get(LOCK_USER):
            raise SuperdeskApiError.badRequestError(message="Item is not locked.")

        can_user_unlock, error_message = self.can_unlock(item, user_id)

        if can_user_unlock:
            self.app.on_item_unlock(item, user_id)

            # delete the item if nothing is saved so far
            # version 0 created on lock item
            if item[config.VERSION] == 0 and item["state"] == "draft":
                superdesk.get_resource_service("archive").delete(lookup={"_id": item["_id"]})
                return

            updates = {LOCK_USER: None, LOCK_SESSION: None, "lock_time": None, "force_unlock": True}
            item_model.update(item_filter, updates)
            self.app.on_item_unlocked(item, user_id)
            push_notification(
                "item:unlock",
                item=str(item_filter.get(config.ID_FIELD)),
                user=str(user_id),
                lock_session=str(session_id),
            )
        else:
            raise SuperdeskApiError.forbiddenError(message=error_message)

        item = item_model.find_one(item_filter)
        return item
Exemplo n.º 11
0
    def unlock(self, item_filter, user_id, session_id, etag):
        item_model = get_model(ItemModel)
        item = item_model.find_one(item_filter)

        if not item:
            raise SuperdeskApiError.notFoundError()

        if not item.get(LOCK_USER):
            raise SuperdeskApiError.badRequestError(message="Item is not locked.")

        can_user_unlock, error_message = self.can_unlock(item, user_id)

        if can_user_unlock:
            self.app.on_item_unlock(item, user_id)

            # delete the item if nothing is saved so far
            if item['_version'] == 1 and item['state'] == 'draft':
                superdesk.get_resource_service('archive').delete(lookup={'_id': item['_id']})
                return

            updates = {LOCK_USER: None, LOCK_SESSION: None, 'lock_time': None, 'force_unlock': True}
            item_model.update(item_filter, updates)
            self.app.on_item_unlocked(item, user_id)
            push_notification('item:unlock', item=str(item_filter.get(config.ID_FIELD)), user=str(user_id),
                              lock_session=str(session_id))
        else:
            raise SuperdeskApiError.forbiddenError(message=error_message)

        item = item_model.find_one(item_filter)
        return item
Exemplo n.º 12
0
    def on_update(self, updates, original):
        if updates.get('content_expiry') == 0:
            updates['content_expiry'] = app.settings['CONTENT_EXPIRY_MINUTES']

        super().on_update(updates, original)

        if updates.get('content_expiry', None):
            docs = self.get_stage_documents(str(original[config.ID_FIELD]))
            for doc in docs:
                expiry = get_expiry_date(updates['content_expiry'], doc['versioncreated'])
                item_model = get_model(ItemModel)
                item_model.update({'_id': doc[config.ID_FIELD]}, {'expiry': expiry})

        if updates.get('working_stage', False):
            if not original.get('working_stage'):
                self.remove_old_default(original.get('desk'), 'working_stage')
                self.set_desk_ref(original, 'working_stage')
        else:
            if original.get('working_stage') and 'working_stage' in updates:
                raise SuperdeskApiError.forbiddenError(message='Must have one working stage in a desk')

        if updates.get('default_incoming', False):
            if not original.get('default_incoming'):
                self.remove_old_default(original.get('desk'), 'default_incoming')
                self.set_desk_ref(original, 'incoming_stage')
        else:
            if original.get('default_incoming') and 'default_incoming' in updates:
                raise SuperdeskApiError.forbiddenError(message='Must have one incoming stage in a desk')
Exemplo n.º 13
0
    def lock(self, item_filter, user_id, session_id, action):
        item_model = get_model(ItemModel)
        item = item_model.find_one(item_filter)

        # set the lock_id it per item
        lock_id = "item_lock {}".format(item.get(config.ID_FIELD))

        if not item:
            raise SuperdeskApiError.notFoundError()

        # get the lock it not raise forbidden exception
        if not lock(lock_id, expire=5):
            raise SuperdeskApiError.forbiddenError(
                message=_("Item is locked by another user."))

        try:
            can_user_lock, error_message = self.can_lock(
                item, user_id, session_id)

            if can_user_lock:
                self.app.on_item_lock(item, user_id)
                updates = {
                    LOCK_USER: user_id,
                    LOCK_SESSION: session_id,
                    LOCK_TIME: utcnow()
                }
                if action:
                    updates[LOCK_ACTION] = action

                updates[TASK] = item.get(TASK)
                if updates.get(TASK):
                    updates[TASK]["user"] = user_id
                else:
                    updates[TASK] = {"user": user_id}

                # tasks service will update the user
                superdesk.get_resource_service("tasks").assign_user(
                    item[config.ID_FIELD], updates)

                item = item_model.find_one(item_filter)
                self.app.on_item_locked(item, user_id)
                push_notification(
                    "item:lock",
                    item=str(item.get(config.ID_FIELD)),
                    item_version=str(item.get(config.VERSION)),
                    user=str(user_id),
                    lock_time=updates[LOCK_TIME],
                    lock_session=str(session_id),
                    _etag=item.get(config.ETAG),
                )
            else:
                raise SuperdeskApiError.forbiddenError(message=error_message)

            item = item_model.find_one(item_filter)
            return item
        finally:
            # unlock the lock :)
            unlock(lock_id, remove=True)
Exemplo n.º 14
0
 def update(self, original, updates):
     updated = copy(original)
     updated.update(updates)
     updated['_id_document'] = original['_id']
     del updated['_id']
     try:
         return get_model(LegalArchiveModel).create([updated])
     except Exception as e:
         get_component(Error).create(LegalArchiveModel.name(), [updated], str(e))
Exemplo n.º 15
0
 def unlock(self, filter, user, etag):
     item_model = get_model(ItemModel)
     item = item_model.find_one(filter)
     if item:
         self.app.on_item_unlock(item, user)
         updates = {LOCK_USER: None, 'lock_time': None}
         item_model.update(filter, updates)
         self.app.on_item_unlocked(item, user)
         push_notification('item:unlock', item=str(filter.get('_id')))
Exemplo n.º 16
0
    def unlock(self, item_filter, user_id, session_id, etag):
        item_model = get_model(ItemModel)
        item = item_model.find_one(item_filter)

        if not item:
            raise SuperdeskApiError.notFoundError()

        if not item.get(LOCK_USER):
            raise SuperdeskApiError.badRequestError(
                message=_("Item is not locked."))

        can_user_unlock, error_message = self.can_unlock(item, user_id)

        if can_user_unlock:
            self.app.on_item_unlock(item, user_id)
            updates = {}

            # delete the item if nothing is saved so far
            # version 0 created on lock item
            if item.get(config.VERSION,
                        0) == 0 and item[ITEM_STATE] == CONTENT_STATE.DRAFT:
                if item.get(ITEM_TYPE) == CONTENT_TYPE.COMPOSITE:
                    # if item is composite then update referenced items in package.
                    PackageService().update_groups({}, item)

                superdesk.get_resource_service("archive").delete_action(
                    lookup={"_id": item["_id"]})
                push_content_notification([item])
            else:
                updates = {}
                set_unlock_updates(updates)
                autosave = superdesk.get_resource_service(
                    "archive_autosave").find_one(req=None, _id=item["_id"])
                if autosave and item[ITEM_STATE] not in PUBLISH_STATES:
                    if not hasattr(
                            flask.g,
                            "user"):  # user is not set when session expires
                        flask.g.user = superdesk.get_resource_service(
                            "users").find_one(req=None, _id=user_id)
                    autosave.update(updates)
                    resolve_document_version(autosave, "archive", "PATCH",
                                             item)
                    superdesk.get_resource_service("archive").patch(
                        item["_id"], autosave)
                    item = superdesk.get_resource_service("archive").find_one(
                        req=None, _id=item["_id"])
                    insert_versioning_documents("archive", item)
                else:
                    item_model.update(item_filter, updates)
                    item = item_model.find_one(item_filter)
                self.app.on_item_unlocked(item, user_id)

            push_unlock_notification(item, user_id, session_id)
        else:
            raise SuperdeskApiError.forbiddenError(message=error_message)

        return item
Exemplo n.º 17
0
    def restore(self, filter, user_id):
        item_model = get_model(ItemModel)
        item = item_model.find_one(filter)
        if not item:
            raise SuperdeskApiError.notFoundError()

        updates = get_restore_updates(item)
        item_model.update(filter, updates)
        push_notification("item:restore", item=str(filter.get("_id")), user=str(user_id))
Exemplo n.º 18
0
 def unspike(self, filter, user):
     item_model = get_model(ItemModel)
     item = item_model.find_one(filter)
     if item:
         updates = get_unspike_updates(item)
         item_model.update(filter, updates)
         push_notification('item:unspike',
                           item=str(filter.get('_id')),
                           user=str(user))
Exemplo n.º 19
0
 def unlock(self, filter, user, etag):
     item_model = get_model(ItemModel)
     item = item_model.find_one(filter)
     if item:
         self.app.on_item_unlock(item, user)
         updates = {LOCK_USER: None, 'lock_time': None}
         item_model.update(filter, updates)
         self.app.on_item_unlocked(item, user)
         push_notification('item:unlock', item=str(filter.get('_id')))
Exemplo n.º 20
0
    def restore(self, filter, user_id):
        item_model = get_model(ItemModel)
        item = item_model.find_one(filter)
        if not item:
            raise SuperdeskApiError.notFoundError()

        updates = get_restore_updates(item)
        item_model.update(filter, updates)
        push_notification('item:restore', item=str(filter.get('_id')), user=str(user_id))
Exemplo n.º 21
0
 def create(self, items):
     citems = deepcopy(items)
     for citem in citems:
         citem['_id_document'] = citem['_id']
         del citem['_id']
     try:
         return get_model(LegalArchiveModel).create(citems)
     except Exception as e:
         get_component(Error).create(LegalArchiveModel.name(), items, str(e))
Exemplo n.º 22
0
 def on_update(self, updates, original):
     if updates.get('content_expiry') == 0:
         updates['content_expiry'] = app.settings['CONTENT_EXPIRY_MINUTES']
     super().on_update(updates, original)
     if updates.get('content_expiry', None):
         docs = self.get_stage_documents(str(original['_id']))
         for doc in docs:
             expiry = get_expiry_date(updates['content_expiry'], doc['versioncreated'])
             item_model = get_model(ItemModel)
             item_model.update({'_id': doc['_id']}, {'expiry': expiry})
Exemplo n.º 23
0
    def lock(self, item_filter, user_id, session_id, action):
        item_model = get_model(ItemModel)
        item = item_model.find_one(item_filter)

        # set the lock_id it per item
        lock_id = "item_lock {}".format(item.get(config.ID_FIELD))

        if not item:
            raise SuperdeskApiError.notFoundError()

        # get the lock it not raise forbidden exception
        if not lock(lock_id, expire=5):
            raise SuperdeskApiError.forbiddenError(
                message="Item is locked by another user.")

        try:
            can_user_lock, error_message = self.can_lock(
                item, user_id, session_id)

            if can_user_lock:
                self.app.on_item_lock(item, user_id)
                updates = {
                    LOCK_USER: user_id,
                    LOCK_SESSION: session_id,
                    'lock_time': utcnow()
                }
                if action:
                    updates['lock_action'] = action

                item_model.update(item_filter, updates)

                if item.get(TASK):
                    item[TASK]['user'] = user_id
                else:
                    item[TASK] = {'user': user_id}

                superdesk.get_resource_service('tasks').assign_user(
                    item[config.ID_FIELD], item[TASK])
                self.app.on_item_locked(item, user_id)
                item = item_model.find_one(item_filter)
                push_notification('item:lock',
                                  item=str(item.get(config.ID_FIELD)),
                                  item_version=str(item.get(config.VERSION)),
                                  user=str(user_id),
                                  lock_time=updates['lock_time'],
                                  lock_session=str(session_id),
                                  _etag=item.get(config.ETAG))
            else:
                raise SuperdeskApiError.forbiddenError(message=error_message)

            item = item_model.find_one(item_filter)
            return item
        finally:
            # unlock the lock :)
            unlock(lock_id, remove=True)
Exemplo n.º 24
0
 def hold(self, filter, user):
     item_model = get_model(ItemModel)
     item = item_model.find_one(filter)
     if item and can_lock(item, user):
         updates = {'state': 'on-hold'}
         item_model.update(filter, updates)
         push_notification('item:hold', item=str(item.get('_id')), user=str(user))
     else:
         raise SuperdeskError("Item couldn't be hold. It is locked by another user")
     item = item_model.find_one(filter)
     return item
Exemplo n.º 25
0
 def lock(self, filter, user, etag):
     item_model = get_model(ItemModel)
     item = item_model.find_one(filter)
     if item and self._can_lock(item, user):
         updates = {LOCK_USER: user, 'lock_time': utcnow()}
         item_model.update(filter, updates)
         item[LOCK_USER] = user
         push_notification('item:lock', item=str(item.get('_id')), user=str(user))
     else:
         raise SuperdeskError('Item locked by another user')
     return item
Exemplo n.º 26
0
 def unlock(self, filter, user, session, etag):
     item_model = get_model(ItemModel)
     item = item_model.find_one(filter)
     if item:
         self.app.on_item_unlock(item, user)
         updates = {LOCK_USER: None, LOCK_SESSION: None, 'lock_time': None, 'force_unlock': True}
         item_model.update(filter, updates)
         self.app.on_item_unlocked(item, user)
         push_notification('item:unlock', item=str(filter.get('_id')), user=str(user))
     item = item_model.find_one(filter)
     return item
Exemplo n.º 27
0
 def on_update(self, updates, original):
     if updates.get('content_expiry') == 0:
         updates['content_expiry'] = app.settings['CONTENT_EXPIRY_MINUTES']
     super().on_update(updates, original)
     if updates.get('content_expiry', None):
         docs = self.get_stage_documents(str(original['_id']))
         for doc in docs:
             expiry = get_expiry_date(updates['content_expiry'],
                                      doc['versioncreated'])
             item_model = get_model(ItemModel)
             item_model.update({'_id': doc['_id']}, {'expiry': expiry})
Exemplo n.º 28
0
    def autosave(self, item_id, updates, user, etag):
        item_model = get_model(ItemModel)
        item = item_model.find_one({'_id': item_id})
        if item is None:
            raise SuperdeskApiError.notFoundError('Invalid item identifier')

        lock_user = item.get('lock_user', None)
        if lock_user and str(lock_user) != str(user['_id']):
            raise SuperdeskApiError.forbiddenError('The item was locked by another user')

        autosave_model = get_model(ItemAutosaveModel)
        item.update(updates)
        self.app.on_item_autosave(item)
        autosave_item = autosave_model.find_one({'_id': item_id})
        if not autosave_item:
            autosave_model.create([item])
        else:
            autosave_model.update({'_id': item_id}, item, etag)
        self.app.on_item_autosaved(item)
        updates.update(item)
        return updates
Exemplo n.º 29
0
 def duplicate_item(self, original_doc):
     new_doc = original_doc.copy()
     del new_doc['_id']
     del new_doc['guid']
     generate_unique_id_and_name(new_doc)
     item_model = get_model(ItemModel)
     on_duplicate_item(new_doc)
     item_model.create([new_doc])
     self.duplicate_versions(original_doc['guid'], new_doc)
     if new_doc.get('state') != 'submitted':
         get_resource_service('tasks').patch(new_doc['_id'], {'state': 'submitted'})
     return new_doc['guid']
Exemplo n.º 30
0
    def _duplicate_item(self, original_doc):
        """
        Duplicates the 'original_doc' including it's version history. If the article being duplicated is contained
        in a desk then the article state is changed to Submitted.

        :return: guid of the duplicated article
        """

        new_doc = original_doc.copy()
        self._remove_after_copy(new_doc)
        on_duplicate_item(new_doc)
        resolve_document_version(new_doc, SOURCE, 'PATCH', new_doc)

        if original_doc.get('task', {}).get('desk') is not None and new_doc.get('state') != 'submitted':
            new_doc[ITEM_STATE] = CONTENT_STATE.SUBMITTED

        convert_task_attributes_to_objectId(new_doc)
        get_model(ItemModel).create([new_doc])
        self._duplicate_versions(original_doc['guid'], new_doc)

        return new_doc['guid']
Exemplo n.º 31
0
    def autosave(self, item_id, updates, user, etag):
        item_model = get_model(ItemModel)
        item = item_model.find_one({'_id': item_id})
        if item is None:
            raise SuperdeskApiError.notFoundError(_('Invalid item identifier'))

        lock_user = item.get('lock_user', None)
        if lock_user and str(lock_user) != str(user['_id']):
            raise SuperdeskApiError.forbiddenError(_('The item was locked by another user'))

        autosave_model = get_model(ItemAutosaveModel)
        item.update(updates)
        self.app.on_item_autosave(item)
        autosave_item = autosave_model.find_one({'_id': item_id})
        if not autosave_item:
            autosave_model.create([item])
        else:
            autosave_model.update({'_id': item_id}, item, etag)
        self.app.on_item_autosaved(item)
        updates.update(item)
        return updates
Exemplo n.º 32
0
 def duplicate_item(self, original_doc):
     new_doc = original_doc.copy()
     del new_doc["_id"]
     del new_doc["guid"]
     generate_unique_id_and_name(new_doc)
     item_model = get_model(ItemModel)
     on_duplicate_item(new_doc)
     item_model.create([new_doc])
     self.duplicate_versions(original_doc["guid"], new_doc)
     if new_doc.get("state") != "submitted":
         get_resource_service("tasks").patch(new_doc["_id"], {"state": "submitted"})
     return new_doc["guid"]
Exemplo n.º 33
0
    def unlock(self, item_filter, user_id, session_id, etag):
        item_model = get_model(ItemModel)
        item = item_model.find_one(item_filter)

        if not item:
            raise SuperdeskApiError.notFoundError()

        if not item.get(LOCK_USER):
            raise SuperdeskApiError.badRequestError(message="Item is not locked.")

        can_user_unlock, error_message = self.can_unlock(item, user_id)

        if can_user_unlock:
            self.app.on_item_unlock(item, user_id)
            updates = {}

            # delete the item if nothing is saved so far
            # version 0 created on lock item
            if item.get(config.VERSION, 0) == 0 and item[ITEM_STATE] == CONTENT_STATE.DRAFT:
                if item.get(ITEM_TYPE) == CONTENT_TYPE.COMPOSITE:
                    # if item is composite then update referenced items in package.
                    PackageService().update_groups({}, item)

                superdesk.get_resource_service('archive').delete_action(lookup={'_id': item['_id']})
                push_content_notification([item])
            else:
                updates = {LOCK_USER: None, LOCK_SESSION: None, 'lock_time': None,
                           'lock_action': None, 'force_unlock': True}
                autosave = superdesk.get_resource_service('archive_autosave').find_one(req=None, _id=item['_id'])
                if autosave and item[ITEM_STATE] not in PUBLISH_STATES:
                    if not hasattr(flask.g, 'user'):  # user is not set when session expires
                        flask.g.user = superdesk.get_resource_service('users').find_one(req=None, _id=user_id)
                    autosave.update(updates)
                    resolve_document_version(autosave, 'archive', 'PATCH', item)
                    superdesk.get_resource_service('archive').patch(item['_id'], autosave)
                    item = superdesk.get_resource_service('archive').find_one(req=None, _id=item['_id'])
                    insert_versioning_documents('archive', item)
                else:
                    item_model.update(item_filter, updates)
                    item = item_model.find_one(item_filter)
                self.app.on_item_unlocked(item, user_id)

            push_notification('item:unlock',
                              item=str(item_filter.get(config.ID_FIELD)),
                              item_version=str(item.get(config.VERSION)),
                              state=item.get(ITEM_STATE),
                              user=str(user_id), lock_session=str(session_id),
                              _etag=item.get(config.ETAG))
        else:
            raise SuperdeskApiError.forbiddenError(message=error_message)

        return item
Exemplo n.º 34
0
 def duplicate_item(self, original_doc):
     new_doc = original_doc.copy()
     del new_doc['_id']
     del new_doc['guid']
     generate_unique_id_and_name(new_doc)
     item_model = get_model(ItemModel)
     on_duplicate_item(new_doc)
     item_model.create([new_doc])
     self.duplicate_versions(original_doc['guid'], new_doc)
     if new_doc.get('state') != 'submitted':
         get_resource_service('tasks').patch(new_doc['_id'],
                                             {'state': 'submitted'})
     return new_doc['guid']
Exemplo n.º 35
0
 def lock(self, filter, user, etag):
     item_model = get_model(ItemModel)
     item = item_model.find_one(filter)
     if item and self._can_lock(item, user):
         self.app.on_item_lock(item, user)
         updates = {LOCK_USER: user, 'lock_time': utcnow()}
         item_model.update(filter, updates)
         superdesk.get_resource_service('tasks').assign_user(item['_id'], user)
         item[LOCK_USER] = user
         self.app.on_item_locked(item, user)
         push_notification('item:lock', item=str(item.get('_id')), user=str(user))
     else:
         raise SuperdeskError('Item locked by another user')
     return item
Exemplo n.º 36
0
    def autosave(self, item_id, updates, user, etag):
        updates.setdefault("_type", "archive")
        item_model = get_model(ItemModel)
        item = item_model.find_one({"_id": item_id})
        if item is None:
            raise SuperdeskApiError.notFoundError(_("Invalid item identifier"))

        lock_user = item.get("lock_user", None)
        if lock_user and str(lock_user) != str(user["_id"]):
            raise SuperdeskApiError.forbiddenError(
                _("The item was locked by another user"))

        autosave_model = get_model(ItemAutosaveModel)
        item.update(updates)
        self.app.on_item_autosave(item)
        autosave_item = autosave_model.find_one({"_id": item_id})
        if not autosave_item:
            autosave_model.create([item])
        else:
            autosave_model.update({"_id": item_id}, item, etag)
        self.app.on_item_autosaved(item)
        updates.update(item)
        return updates
Exemplo n.º 37
0
 def lock(self, filter, user, etag):
     item_model = get_model(ItemModel)
     item = item_model.find_one(filter)
     if item and self._can_lock(item, user):
         self.app.on_item_lock(item, user)
         updates = {LOCK_USER: user, 'lock_time': utcnow()}
         item_model.update(filter, updates)
         item[LOCK_USER] = user
         self.app.on_item_locked(item, user)
         push_notification('item:lock',
                           item=str(item.get('_id')),
                           user=str(user))
     else:
         raise SuperdeskError('Item locked by another user')
     return item
Exemplo n.º 38
0
    def lock(self, item_filter, user_id, session_id, action):
        item_model = get_model(ItemModel)
        item = item_model.find_one(item_filter)

        # set the lock_id it per item
        lock_id = "item_lock {}".format(item.get(config.ID_FIELD))

        if not item:
            raise SuperdeskApiError.notFoundError()

        # get the lock it not raise forbidden exception
        if not lock(lock_id, expire=5):
            raise SuperdeskApiError.forbiddenError(message="Item is locked by another user.")

        try:
            can_user_lock, error_message = self.can_lock(item, user_id, session_id)

            if can_user_lock:
                self.app.on_item_lock(item, user_id)
                updates = {LOCK_USER: user_id, LOCK_SESSION: session_id, 'lock_time': utcnow()}
                if action:
                    updates['lock_action'] = action

                item_model.update(item_filter, updates)

                if item.get(TASK):
                    item[TASK]['user'] = user_id
                else:
                    item[TASK] = {'user': user_id}

                superdesk.get_resource_service('tasks').assign_user(item[config.ID_FIELD], item[TASK])
                self.app.on_item_locked(item, user_id)
                item = item_model.find_one(item_filter)
                push_notification('item:lock',
                                  item=str(item.get(config.ID_FIELD)),
                                  item_version=str(item.get(config.VERSION)),
                                  user=str(user_id), lock_time=updates['lock_time'],
                                  lock_session=str(session_id),
                                  _etag=item.get(config.ETAG))
            else:
                raise SuperdeskApiError.forbiddenError(message=error_message)

            item = item_model.find_one(item_filter)
            return item
        finally:
            # unlock the lock :)
            unlock(lock_id, remove=True)
Exemplo n.º 39
0
    def hold(self, filter, user_id, session):
        item_model = get_model(ItemModel)
        item = item_model.find_one(filter)
        if not item:
            raise SuperdeskApiError.notFoundError()

        can_user_lock, error_message = get_component(ItemLock).can_lock(item, user_id, session)

        if can_user_lock:
            updates = {'state': 'on-hold'}
            item_model.update(filter, updates)
            push_notification('item:hold', item=str(item.get('_id')), user=str(user_id))
        else:
            raise SuperdeskApiError.forbiddenError(message=error_message)

        item = item_model.find_one(filter)
        return item
Exemplo n.º 40
0
    def hold(self, filter, user_id, session):
        item_model = get_model(ItemModel)
        item = item_model.find_one(filter)
        if not item:
            raise SuperdeskApiError.notFoundError()

        can_user_lock, error_message = get_component(ItemLock).can_lock(item, user_id, session)

        if can_user_lock:
            updates = {"state": "on-hold"}
            item_model.update(filter, updates)
            push_notification("item:hold", item=str(item.get("_id")), user=str(user_id))
        else:
            raise SuperdeskApiError.forbiddenError(message=error_message)

        item = item_model.find_one(filter)
        return item
Exemplo n.º 41
0
    def spike(self, filter, user):
        item_model = get_model(ItemModel)
        item = item_model.find_one(filter)
        if item and can_lock(item, user):
            expiry_minutes = app.settings['SPIKE_EXPIRY_MINUTES']
            # check if item is in a desk
            if "task" in item and "desk" in item["task"]:
                    # then use the desks spike_expiry
                    desk = get_resource_service('desks').find_one(_id=item["task"]["desk"], req=None)
                    expiry_minutes = desk.get('spike_expiry', expiry_minutes)

            updates = {IS_SPIKED: True, EXPIRY: get_expiry_date(expiry_minutes)}
            item_model.update(filter, updates)
            push_notification('item:spike', item=str(item.get('_id')), user=str(user))
        else:
            raise SuperdeskError("Item couldn't be spiked. It is locked by another user")
        item = item_model.find_one(filter)
        return item
Exemplo n.º 42
0
 def unlock(self, filter, user, session, etag):
     item_model = get_model(ItemModel)
     item = item_model.find_one(filter)
     if item:
         self.app.on_item_unlock(item, user)
         updates = {
             LOCK_USER: None,
             LOCK_SESSION: None,
             'lock_time': None,
             'force_unlock': True
         }
         item_model.update(filter, updates)
         self.app.on_item_unlocked(item, user)
         push_notification('item:unlock',
                           item=str(filter.get('_id')),
                           user=str(user))
     item = item_model.find_one(filter)
     return item
Exemplo n.º 43
0
    def lock(self, filter, user, session, etag):
        item_model = get_model(ItemModel)
        item = item_model.find_one(filter)
        if item and can_lock(item, user):
            self.app.on_item_lock(item, user)
            updates = {LOCK_USER: user, LOCK_SESSION: session, 'lock_time': utcnow()}
            item_model.update(filter, updates)

            if 'task' in item and 'user' in item['task']:
                item['task']['user'] = user
            else:
                item['task'] = {'user': user}

            superdesk.get_resource_service('tasks').assign_user(item['_id'], item['task'])
            self.app.on_item_locked(item, user)
            push_notification('item:lock', item=str(item.get('_id')), user=str(user))
        else:
            raise SuperdeskError('Item locked by another user')
        item = item_model.find_one(filter)
        return item
Exemplo n.º 44
0
 def lock(self, filter, user, session, etag):
     item_model = get_model(ItemModel)
     item = item_model.find_one(filter)
     if item and can_lock(item, user):
         self.app.on_item_lock(item, user)
         updates = {
             LOCK_USER: user,
             LOCK_SESSION: session,
             'lock_time': utcnow()
         }
         item_model.update(filter, updates)
         superdesk.get_resource_service('tasks').assign_user(
             item['_id'], user)
         self.app.on_item_locked(item, user)
         push_notification('item:lock',
                           item=str(item.get('_id')),
                           user=str(user))
     else:
         raise SuperdeskError('Item locked by another user')
     item = item_model.find_one(filter)
     return item
Exemplo n.º 45
0
    def on_update(self, updates, original):
        if updates.get('content_expiry') == 0:
            updates['content_expiry'] = app.settings['CONTENT_EXPIRY_MINUTES']
        super().on_update(updates, original)
        if updates.get('content_expiry', None):
            docs = self.get_stage_documents(str(original['_id']))
            for doc in docs:
                expiry = get_expiry_date(updates['content_expiry'],
                                         doc['versioncreated'])
                item_model = get_model(ItemModel)
                item_model.update({'_id': doc['_id']}, {'expiry': expiry})

        if updates.get('default_incoming', False):
            if not original.get('default_incoming'):
                self.remove_old_default(original.get('desk'),
                                        'default_incoming')
                self.set_desk_ref(original, 'incoming_stage')
        else:
            if original.get(
                    'default_incoming') and 'default_incoming' in updates:
                raise SuperdeskApiError.forbiddenError(
                    message='Must have one incoming stage in a desk')
Exemplo n.º 46
0
    def _duplicate_item(self, original_doc):
        """
        Duplicates the 'original_doc' including it's version history. If the article being duplicated is contained
        in a desk then the article state is changed to Submitted.

        :return: guid of the duplicated article
        """

        new_doc = original_doc.copy()
        del new_doc[config.ID_FIELD]
        del new_doc['guid']

        new_doc[ITEM_OPERATION] = ITEM_DUPLICATE
        item_model = get_model(ItemModel)

        on_duplicate_item(new_doc)
        resolve_document_version(new_doc, SOURCE, 'PATCH', new_doc)
        if original_doc.get('task', {}).get('desk') is not None and new_doc.get('state') != 'submitted':
            new_doc[ITEM_STATE] = CONTENT_STATE.SUBMITTED
        item_model.create([new_doc])
        self._duplicate_versions(original_doc['guid'], new_doc)

        return new_doc['guid']
Exemplo n.º 47
0
    def unlock(self, item_filter, user_id, session_id, etag):
        item_model = get_model(ItemModel)
        item = item_model.find_one(item_filter)

        if not item:
            raise SuperdeskApiError.notFoundError()

        if not item.get(LOCK_USER):
            raise SuperdeskApiError.badRequestError(message="Item is not locked.")

        can_user_unlock, error_message = self.can_unlock(item, user_id)

        if can_user_unlock:
            self.app.on_item_unlock(item, user_id)
            updates = {LOCK_USER: None, LOCK_SESSION: None, 'lock_time': None, 'force_unlock': True}
            item_model.update(item_filter, updates)
            self.app.on_item_unlocked(item, user_id)
            push_notification('item:unlock', item=str(item_filter.get(config.ID_FIELD)), user=str(user_id),
                              lock_session=str(session_id))
        else:
            raise SuperdeskApiError.forbiddenError(message=error_message)

        item = item_model.find_one(item_filter)
        return item
Exemplo n.º 48
0
 def clear(self, item_id):
     autosave_model = get_model(ItemAutosaveModel)
     return autosave_model.delete({"_id": item_id})
Exemplo n.º 49
0
 def get(self, req, lookup):
     return get_model(LegalArchiveModel).find(lookup)
Exemplo n.º 50
0
 def clear(self, item_id):
     autosave_model = get_model(ItemAutosaveModel)
     return autosave_model.delete({'_id': item_id})
Exemplo n.º 51
0
 def create(self, resource, docs, error):
     error = {'resource': resource, 'docs': docs, 'error': error}
     get_model(ErrorsModel).create(error)
Exemplo n.º 52
0
    def unlock_session(self, user_id, session_id):
        item_model = get_model(ItemModel)
        items = item_model.find({LOCK_SESSION: str(session_id)})

        for item in items:
            self.unlock({"_id": item["_id"]}, user_id, session_id, None)
Exemplo n.º 53
0
    def unlock_session(self, user_id, session_id):
        item_model = get_model(ItemModel)
        items = item_model.find({'lock_session': session_id})

        for item in items:
            self.unlock({'_id': item['_id']}, user_id, session_id, None)