Ejemplo n.º 1
0
def update_action_list(items,
                       action_list,
                       force_insert=False,
                       item_type='items'):
    """
    Stores user id into array of action_list of an item
    :param items: items to be updated
    :param action_list: field name of the list
    :param force_insert: inserts into list regardless of the http method
    :param item_type: either items or agenda as the collection
    :return:
    """
    user_id = get_user_id()
    if user_id:
        db = app.data.get_mongo_collection(item_type)
        elastic = app.data._search_backend(item_type)
        if request.method == 'POST' or force_insert:
            updates = {'$addToSet': {action_list: user_id}}
        else:
            updates = {'$pull': {action_list: user_id}}
        for item_id in items:
            result = db.update_one({'_id': item_id}, updates)
            if result.modified_count:
                modified = db.find_one({'_id': item_id})
                elastic.update(item_type, item_id,
                               {action_list: modified[action_list]})
Ejemplo n.º 2
0
def watch_coverage():
    user_id = get_user_id()
    data = get_json_or_400()
    assert data.get('item_id')
    assert data.get('coverage_id')
    item = get_entity_or_404(data['item_id'], 'agenda')

    if item.get('watches') and user_id in item['watches']:
        return flask.jsonify({
            'error':
            gettext('Cannot edit coverage watch when watching a parent item.')
        }), 403

    try:
        coverage_index = [
            c['coverage_id'] for c in (item.get('coverages') or [])
        ].index(data['coverage_id'])
    except ValueError:
        return flask.jsonify({'error': gettext('Coverage not found.')}), 404

    updates = {'coverages': item['coverages']}
    if request.method == 'POST':
        updates['coverages'][coverage_index]['watches'] = list(
            set((updates['coverages'][coverage_index].get('watches') or []) +
                [user_id]))
    else:
        try:
            updates['coverages'][coverage_index]['watches'].remove(user_id)
        except Exception:
            return flask.jsonify({'error':
                                  gettext('Error removing watch.')}), 404

    get_resource_service('agenda').patch(data['item_id'], updates)
    return flask.jsonify(), 200
Ejemplo n.º 3
0
def bookmark():
    """Bookmark an item.

    Stores user id into item.bookmarks array.
    Uses mongodb to update the array and then pushes updated array to elastic.
    """
    data = get_json_or_400()
    assert data.get('items')
    update_action_list(data.get('items'), 'bookmarks', item_type='items')
    user_id = get_user_id()
    push_user_notification('saved_items', count=get_bookmarks_count(user_id, 'media_releases'))
    return flask.jsonify(), 200
Ejemplo n.º 4
0
def follow():
    data = get_json_or_400()
    assert data.get('items')
    for item_id in data.get('items'):
        user_id = get_user_id()
        item = get_entity_or_404(item_id, 'agenda')
        coverage_updates = {'coverages': item.get('coverages') or []}
        for c in coverage_updates['coverages']:
            if c.get('watches') and user_id in c['watches']:
                c['watches'].remove(user_id)

        if request.method == 'POST':
            updates = {
                'watches': list(set((item.get('watches') or []) + [user_id]))
            }
            if item.get('coverages'):
                updates.update(coverage_updates)

            get_resource_service('agenda').patch(item_id, updates)
        else:
            if request.args.get('bookmarks'):
                user_item_watches = [
                    u for u in (item.get('watches') or [])
                    if str(u) == str(user_id)
                ]
                if not user_item_watches:
                    # delete user watches of all coverages
                    get_resource_service('agenda').patch(
                        item_id, coverage_updates)
                    return flask.jsonify(), 200

            update_action_list(data.get('items'),
                               'watches',
                               item_type='agenda')

    push_user_notification(
        'saved_items',
        count=get_resource_service('agenda').get_saved_items_count())
    return flask.jsonify(), 200
Ejemplo n.º 5
0
def set_version_creator(doc):
    doc['version_creator'] = get_user_id()
Ejemplo n.º 6
0
def set_original_creator(doc):
    doc['original_creator'] = get_user_id()
Ejemplo n.º 7
0
 def on_updated(self, updates, original):
     # set session locale if updating locale for current user
     if updates.get('locale') and original['_id'] == get_user_id(
     ) and updates['locale'] != original.get('locale'):
         session['locale'] = updates['locale']
Ejemplo n.º 8
0
def push_user_notification(name, **kwargs):
    push_notification(':'.join(map(str, [name, get_user_id()])), **kwargs)