Exemplo n.º 1
0
def content_item_delete_tag(user, org, content_item_id, tag_id):
    """
    Remove a tag from a content item.
    """
    c = ContentItem.query\
        .filter_by(id=content_item_id, org_id=org.id)\
        .first()

    if not c:
        raise NotFoundError(
            'An ContentItem with ID {} does not exist.'
            .format(content_item_id))

    tag = Tag.query\
        .filter_by(id=tag_id, org_id=org.id)\
        .first()

    if not tag:
        raise NotFoundError(
            'Tag with ID {} does not exist.'
            .format(tag_id))

    for tag in c.tags:
        if tag.id == tag_id:
            c.tags.remove(tag)

    db.session.add(c)
    db.session.commit()

    # return modified content item
    return jsonify(c)
Exemplo n.º 2
0
def event_add_tag(user, org, event_id, tag_id):
    """
    Add a tag to an event.
    """
    e = Event.query\
        .filter_by(id=event_id, org_id=org.id)\
        .first()
    if not e:
        raise NotFoundError(
            'An Event with ID {} does not exist.'.format(event_id))

    if not e.status == 'approved':
        raise RequestError(
            'You must first approve an Event before adding additional Tags.')

    tag = Tag.query\
        .filter_by(id=tag_id, org_id=org.id)\
        .first()
    if not tag:
        raise NotFoundError('Tag with ID {} does not exist.'.format(tag_id))

    if tag.type != 'impact':
        raise RequestError('Events can only be assigned Impact Tags.')

    if tag.id not in e.tag_ids:
        e.tags.append(tag)

    db.session.add(e)
    db.session.commit()

    # return modified event
    return jsonify(e)
Exemplo n.º 3
0
def merge_authors(user, org, from_author_id, to_author_id):
    """
    Remove an author to a content item.
    """
    from_a = Author.query\
        .filter_by(id=from_author_id, org_id=org.id)\
        .first()

    if not from_a:
        raise NotFoundError(
            'Author with ID "{}" does not exist."'.format(from_author_id))

    to_a = Author.query\
        .filter_by(id=to_author_id, org_id=org.id)\
        .first()

    if not to_a:
        raise NotFoundError(
            'Author with ID "{}" does not exist."'.format(to_author_id))

    # re associate content
    stmt = update(content_items_authors)\
        .where(content_items_authors.c.author_id == from_author_id)\
        .values(author_id=to_author_id)

    db.session.execute(stmt)

    # remove from author id
    db.session.delete(from_a)
    db.session.commit()

    return jsonify(to_a.to_dict(incl_content=True))
Exemplo n.º 4
0
def content_item_add_tag(user, org, content_item_id, tag_id):
    """
    Add a tag to an content_item.
    """
    c = ContentItem.query\
        .filter_by(id=content_item_id, org_id=org.id)\
        .first()

    if not c:
        raise NotFoundError(
            'An ContentItem with ID {} does not exist.'
            .format(content_item_id))

    tag = Tag.query\
        .filter_by(id=tag_id, org_id=org.id)\
        .first()

    if not tag:
        raise NotFoundError(
            'Tag with ID {} does not exist.'
            .format(tag_id))

    if tag.type != 'subject':
        raise RequestError(
            'Content Items can only be assigned Subject Tags.')

    if tag.id not in c.subject_tag_ids:
        c.tags.append(tag)

    db.session.add(c)
    db.session.commit()

    # return modified content item
    return jsonify(c)
Exemplo n.º 5
0
def update_setting(user, org, level, name_id):

    if level not in ['me', 'orgs']:
        raise NotFoundError(
            'You cannot store settings for \'{}\''.format(level))

    s = fetch_by_id_or_field(Setting,
                             'name',
                             name_id,
                             org_id=org.id,
                             user_id=user.id,
                             level=level)

    if not s:
        raise NotFoundError('Setting "{}" does not yet exist.'.format(
            name_id, org.name))

    # get the request data
    req_data = request_data()

    name = req_data.get('name')
    value = req_data.get('value')
    json_value = req_data.get('json_value')

    # if it's a json_value check whether we can parse it as such
    if json_value:
        if isinstance(value, basestring):
            try:
                obj_to_json(value)
            except:
                raise RequestError(
                    "Setting '{}' with value '{}' was declared as a "
                    "'json_value' but could not be parsed as such.".format(
                        name_id, value))

    # upsert / patch values.
    if name:
        s.name = name

    if json_value:
        if not isinstance(json_value, bool):
            if str(json_value).lower() in TRUE_VALUES:
                json_value = True
            else:
                json_value = False
        s.json_value = json_value
        s.value = obj_to_json(value)
    else:
        s.value = value
    db.session.add(s)
    db.session.commit()

    return jsonify(s)
Exemplo n.º 6
0
def event_delete_tag(user, org, event_id, tag_id):
    """
    Remove a tag from an event.
    """
    e = Event.query\
        .filter_by(id=event_id, org_id=org.id)\
        .first()
    if not e:
        raise NotFoundError(
            'An Event with ID {} does not exist.'.format(event_id))

    if tag_id not in e.tag_ids:
        raise RequestError(
            'An Event with ID {} does not currently have an association '
            'with a Tag with ID {}.'.format(event_id, tag_id))

    # remove tag from event
    for tag in e.tags:
        if tag.id == tag_id:
            e.tags.remove(tag)

    # update metrics associated with these content item ids
    if len(e.content_item_ids):
        rollup_metric.content_summary_from_events(org, e.content_item_ids)

    db.session.add(e)
    db.session.commit()

    # return modified event
    return jsonify(e)
Exemplo n.º 7
0
def update_author(user, org, author_id):
    """
    Update an author.
    """

    a = Author.query\
        .filter_by(id=author_id, org_id=org.id)\
        .first()

    if not a:
        raise NotFoundError(
            'Author with ID "{}" does not exist."'.format(author_id))

    req_data = request_data()

    cols = get_table_columns(Author)
    for k in req_data.keys():
        if k not in cols or k in ['id', 'org_id']:
            req_data.pop(k, None)

    for k, v in req_data.items():
        setattr(a, k, v)

    db.session.add(a)
    db.session.commit()

    return jsonify(a)
Exemplo n.º 8
0
def author_add_content(user, org, author_id, content_item_id):
    """
    Add an author to a content item.
    """
    a = Author.query\
        .filter_by(id=author_id, org_id=org.id)\
        .first()

    if not a:
        raise NotFoundError(
            'Author with ID "{}" does not exist."'.format(author_id))

    c = ContentItem.query\
        .filter_by(id=content_item_id, org_id=org.id)\
        .first()
    if not c:
        raise RequestError(
            'ContentItem with ID {} does not exist.'.format(content_item_id))

    if a.id not in c.author_ids:
        c.authors.append(a)

    db.session.add(c)
    db.session.commit()

    # return modified event
    return jsonify(a.to_dict(incl_content=True))
Exemplo n.º 9
0
def author_remove_content(user, org, author_id, content_item_id):
    """
    Remove an author to a content item.
    """
    a = Author.query\
        .filter_by(id=author_id, org_id=org.id)\
        .first()

    if not a:
        raise NotFoundError(
            'Author with ID "{}" does not exist."'.format(author_id))

    c = ContentItem.query\
        .filter_by(id=content_item_id, org_id=org.id)\
        .first()

    if not c:
        raise RequestError(
            'ContentItem with ID {} does not exist.'.format(content_item_id))

    if a.id in c.author_ids:
        a.content_items.remove(c)

    db.session.add(a)
    db.session.commit()

    return delete_response()
Exemplo n.º 10
0
def get_comparison_items(level, level_id, org_id):
    """
    Get a comparison item for any level. We can add more functions to
    this as our needs grow.
    """
    fx = {
        'content': ContentMetricSummary.query
                        .filter_by(org_id=org_id)\
                        .filter(ContentMetricSummary.content_item_id.in_(level_id)),
        'orgs': OrgMetricSummary.query
                    .filter(OrgMetricSummary.org_id.in_(level_id)),
    }

    if not isinstance(level_id, list):
        level_id = [level_id]

    if len(level_id) == 1:
        o = fx[level].first()
    else:
        o = fx[level].all()
        if not len(o):
            o = None
    if not o:
        raise NotFoundError("No {} could be found with ids:\n{}".format(
            level, ",".join(level_id)))
    if len(level_id) == 1:
        yield o.to_dict()
    else:
        for oo in o:
            yield oo.to_dict()
Exemplo n.º 11
0
def parse_comparison_level(level):
    """
    Validate comparison levels.
    """
    if level not in ['content', 'orgs']:
        raise NotFoundError('URL Not Found')
    return level
Exemplo n.º 12
0
def update_author(user, org, author_id):
    """
    Update an author.
    """

    a = fetch_by_id_or_field(Author,
                             'name',
                             author_id,
                             org_id=org.id,
                             transform='upper')
    if not a:
        raise NotFoundError(
            'Author with ID/Name "{}" does not exist."'.format(author_id))

    req_data = request_data()

    cols = get_table_columns(Author)
    for k in req_data.keys():
        if k not in cols or k in ['id', 'org_id']:
            req_data.pop(k, None)

    for k, v in req_data.items():
        if k == 'name':
            if v:
                v = v.upper()
        setattr(a, k, v)

    db.session.add(a)
    db.session.commit()
    return jsonify(a)
Exemplo n.º 13
0
def event_add_thing(user, org, event_id, content_item_id):
    """
    Add a thing to an event.
    """
    e = Event.query.filter_by(id=event_id, org_id=org.id).first()
    if not e:
        raise NotFoundError(
            'An Event with ID {} does not exist.'.format(event_id))

    if not e.status == 'approved':
        raise RequestError(
            'You must first approve an Event before adding additional ContentItems.'
        )

    c = ContentItem.query\
        .filter_by(id=content_item_id, org_id=org.id)\
        .first()

    if not c:
        raise RequestError(
            'A ContentItem with ID {} does not exist.'.format(content_item_id))

    # add content item to event
    if c.id not in e.content_item_ids:
        e.content_items.append(c)

    db.session.add(e)
    db.session.commit()

    # return modified event
    return jsonify(e)
Exemplo n.º 14
0
def org_add_user(user, org_id_slug, user_email):

    if not user.admin:
        raise AuthError('You must be an admin to add a user to an Org.')

    # fetch org
    org = fetch_by_id_or_field(Org, 'slug', org_id_slug)

    if not org:
        raise NotFoundError('This Org does not exist.')

    # ensure the active user can edit this Org
    if user.id not in org.user_ids:
        raise ForbiddenError('You are not allowed to edit this Org.')

    # localize
    localize(org)

    # get this new user by id / email
    new_org_user = fetch_by_id_or_field(User, 'email', user_email)

    if not new_org_user:
        raise RequestError('User "{}" does not exist'.format(user_email))

    # ensure that user is not already a part of this Org.
    if new_org_user.id in org.user_ids:
        raise RequestError('User "{}" is already a part of Org "{}"'.format(
            new_org_user.email, org.name))

    org.users.append(new_org_user)
    db.session.commit()

    return jsonify(new_org_user)
Exemplo n.º 15
0
def delete_recipe(user, org, recipe_id):

    r = fetch_by_id_or_field(Recipe, 'slug', recipe_id, org_id=org.id)
    if not r:
        raise NotFoundError(
            'Recipe with id/slug {} does not exist.'.format(recipe_id))
    force = arg_bool('force', default=False)

    if force:
        db.session.delete(r)
    else:
        # just delete recipes with no approved events.
        event_cnt = r.events.filter_by(status='approved').count()
        if event_cnt > 0:
            r.status = 'inactive'
            db.session.add(r)
        else:
            db.session.delete(r)

    # set the status of associated pending events to 'deleted'
    cmd = """
    UPDATE events SET status='deleted' WHERE recipe_id = {} AND status='pending';
    """.format(r.id)
    db.session.execute(cmd)
    db.session.commit()
    return delete_response()
Exemplo n.º 16
0
def org_user(user, org_id_slug, user_email):

    # fetch org
    org = fetch_by_id_or_field(Org, 'slug', org_id_slug)

    if not org:
        raise NotFoundError('This Org does not exist.')

    # ensure the active user can edit this Org
    if user.id not in org.user_ids:
        raise ForbiddenError('You are not allowed to access this Org')

    # localize
    localize(org)

    # get this new user by id / email
    org_user = fetch_by_id_or_field(User, 'email', user_email)

    if not org_user:
        raise RequestError('This user does not yet exist')

    # check whether this user can view this other user:
    if not len(list(set(org_user.org_ids).intersection(set(user.org_ids)))):
        raise ForbiddenError('You are not allowed to view this user.'.format(
            user.email))

    return jsonify(org_user)
Exemplo n.º 17
0
def org_delete(user, org_id_slug):

    if not user.admin:
        raise AuthError('You must be an admin to delete an Org')

    # fetch org
    org = fetch_by_id_or_field(Org, 'slug', org_id_slug)

    # if it still doesn't exist, raise an error.
    if not org:
        raise NotFoundError('This Org does not exist.')

    # localize
    localize(org)

    # ensure the active user can edit this Org
    if user.id not in org.user_ids:
        raise ForbiddenError(
            'User "{}" is not allowed to access Org "{}".'.format(
                user.name, org.name))

    db.session.delete(org)
    db.session.commit()

    return delete_response()
Exemplo n.º 18
0
def event_delete_content_item(user, org, event_id, content_item_id):
    """
    Remove a thing from an event.
    """
    e = Event.query\
        .filter_by(id=event_id, org_id=org.id)\
        .first()
    if not e:
        raise NotFoundError(
            'An Event with ID {} does not exist.'.format(event_id))

    c = ContentItem.query\
        .filter_by(id=content_item_id, org_id=org.id)\
        .first()

    if not c:
        raise RequestError(
            'A ContentItem with ID {} does not exist.'.format(content_item_id))

    if content_item_id not in e.content_item_ids:
        raise RequestError(
            'An Event with ID {} does not currently have an association '
            'with a ContentItem with ID {}'.format(event_id, content_item_id))

    # remove the content item form the event.
    e.content_items.remove(c)

    db.session.add(e)
    db.session.commit()

    # return modified event
    return jsonify(e)
Exemplo n.º 19
0
def delete_content_item(user, org, content_item_id):
    """
    Update an individual content-item.
    """
    c = ContentItem.query\
        .filter_by(id=content_item_id, org_id=org.id)\
        .first()

    if not c:
        raise NotFoundError(
            'An ContentItem with ID {} does not exist.'
            .format(content_item_id))

    # delete metrics
    cmd = """
    DELETE FROM content_metric_timeseries WHERE content_item_id = {0};
    DELETE FROM content_metric_summary WHERE content_item_id = {0};
    """.format(content_item_id)

    # delete content item + metrics
    db.session.execute(cmd)
    db.session.delete(c)
    db.session.commit()

    return delete_response()
Exemplo n.º 20
0
def list_settings(user, org, level):
    if level not in ['me', 'orgs']:
        raise NotFoundError(
            'You cannot store settings for \'{}\''.format(level))
    if level == 'orgs':
        return jsonify(org.settings)
    return jsonify(user.get_settings(org_id=org.id))
def create_content_item_timeseries(user, org, content_item_id):
    """
    Upsert content timseries metrics.
    """
    c = ContentItem.query\
        .filter_by(id=content_item_id)\
        .filter_by(org_id=org.id)\
        .first()

    if not c:
        raise NotFoundError(
            'A ContentItem with ID {} does not exist'.format(content_item_id))

    # insert content item id
    req_data = request_data()
    if not isinstance(req_data, list):
        req_data = [req_data]
    data = []
    for row in req_data:
        row.update({'content_item_id': c.id})
        data.append(row)

# load.
    ret = load.content_timeseries(
        data,
        org_id=org.id,
        metrics_lookup=org.content_timeseries_metrics,
        content_item_ids=[content_item_id],
        queue=False)

    return jsonify(ret)
Exemplo n.º 22
0
def event_delete_tag(user, org, event_id, tag_id):
    """
    Remove a tag from an event.
    """
    e = Event.query\
        .filter_by(id=event_id, org_id=org.id)\
        .first()
    if not e:
        raise NotFoundError(
            'An Event with ID {} does not exist.'.format(event_id))

    if tag_id not in e.tag_ids:
        raise RequestError(
            'An Event with ID {} does not currently have an association '
            'with a Tag with ID {}.'.format(event_id, tag_id))

    # remove tag from event
    for tag in e.tags:
        if tag.id == tag_id:
            e.tags.remove(tag)

    db.session.add(e)
    db.session.commit()

    # return modified event
    return jsonify(e)
Exemplo n.º 23
0
def get_content_timeseries(user, org, content_item_id):
    """
    Query an individual content timeseries.
    """
    c = ContentItem.query\
        .filter_by(id=content_item_id)\
        .filter_by(org_id=org.id)\
        .first()

    if not c:
        raise NotFoundError(
            'A ContentItem with ID {} does not exist'
            .format(content_item_id))

    # select / exclude
    select, exclude = arg_list('select', typ=str, exclusions=True, default=['*'])
    if '*' in select:
        exclude = []
        select = "*"

    kw = dict(
        unit=arg_str('unit', default='hour'),
        sparse=arg_bool('sparse', default=True),
        sig_digits=arg_int('sig_digits', default=2),
        select=select,
        exclude=exclude,
        rm_nulls=arg_bool('rm_nulls', default=False),
        time_since_start=arg_bool('time_since_start', default=False),
        transform=arg_str('transform', default=None),
        before=arg_date('before', default=None),
        after=arg_date('after', default=None)
    )

    q = QueryContentMetricTimeseries(org, [content_item_id], **kw)
    return jsonify(list(q.execute()))
Exemplo n.º 24
0
def author_remove_content(user, org, author_id, content_item_id):
    """
    Remove an author from a content item.
    """
    a = fetch_by_id_or_field(Author,
                             'name',
                             author_id,
                             org_id=org.id,
                             transform='upper')
    if not a:
        raise NotFoundError(
            'Author with ID/Name "{}" does not exist."'.format(author_id))

    c = ContentItem.query\
        .filter_by(id=content_item_id, org_id=org.id)\
        .first()

    if not c:
        raise RequestError(
            'ContentItem with ID {} does not exist.'.format(content_item_id))

    if a.id in c.author_ids:
        a.content_items.remove(c)

    db.session.add(a)
    db.session.commit()
    return delete_response()