Esempio n. 1
0
def webhooks_read_update_api(public_id):
    if request.method == 'GET':
        try:
            hook = g.db_session.query(Webhook).filter(
                Webhook.public_id == public_id,
                Webhook.namespace_id == g.namespace.id).one()
            return jsonify(hook)
        except NoResultFound:
            return err(404, "Couldn't find webhook with id {}"
                       .format(public_id))

    if request.method == 'PUT':
        data = request.get_json(force=True)
        # We only support updates to the 'active' flag.
        if data.keys() != ['active']:
            return err(400, 'Malformed webhook request')

        try:
            if data['active']:
                get_webhook_client().start_hook(public_id)
            else:
                get_webhook_client().stop_hook(public_id)
            return jsonify({"success": True})
        except zerorpc.RemoteError:
            return err(404, "Couldn't find webhook with id {}"
                       .format(public_id))
Esempio n. 2
0
def webhooks_read_update_api(public_id):
    if request.method == 'GET':
        try:
            hook = g.db_session.query(Webhook).filter(
                Webhook.public_id == public_id,
                Webhook.namespace_id == g.namespace.id).one()
            return jsonify(hook)
        except NoResultFound:
            return err(404,
                       "Couldn't find webhook with id {}".format(public_id))

    if request.method == 'PUT':
        data = request.get_json(force=True)
        # We only support updates to the 'active' flag.
        if data.keys() != ['active']:
            return err(400, 'Malformed webhook request')

        try:
            if data['active']:
                get_webhook_client().start_hook(public_id)
            else:
                get_webhook_client().stop_hook(public_id)
            return jsonify({"success": True})
        except zerorpc.RemoteError:
            return err(404,
                       "Couldn't find webhook with id {}".format(public_id))
Esempio n. 3
0
def contact_search_api():
    filter = request.args.get('filter', '')
    try:
        limit = int(request.args.get('limit', 10))
        offset = int(request.args.get('offset', 0))
    except ValueError:
        return err(400, 'limit and offset parameters must be integers')
    if limit < 0 or offset < 0:
        return err(
            400, 'limit and offset parameters must be nonnegative '
            'integers')
    if limit > 1000:
        return err(400, 'cannot request more than 1000 contacts at once.')
    order = request.args.get('order')
    if order == 'rank':
        results = contacts.search_util.search(g.db_session,
                                              g.namespace.account_id, filter,
                                              limit, offset)
    else:
        results = g.db_session.query(Contact). \
            filter(Contact.account_id == g.namespace.account_id,
                   Contact.source == 'local'). \
            order_by(asc(Contact.id)).limit(limit).offset(offset).all()

    return jsonify(results)
Esempio n. 4
0
def contact_read_api(public_id):
    # TODO auth with account object
    # Get all data for an existing contact.
    result = contacts.crud.read(g.namespace, g.db_session, public_id)
    if result is None:
        return err(404, "Couldn't find contact with id {0}".format(public_id))
    return jsonify(result)
Esempio n. 5
0
 def ns_all():
     """ Return all namespaces """
     # We do this outside the blueprint to support the case of an empty public_id.
     # However, this means the before_request isn't run, so we need to make our own session
     with session_scope() as db_session:
         namespaces = db_session.query(Namespace).all()
         result = jsonify(namespaces)
     return result
Esempio n. 6
0
 def ns_all():
     """ Return all namespaces """
     # We do this outside the blueprint to support the case of an empty public_id.
     # However, this means the before_request isn't run, so we need to make our own session
     with session_scope() as db_session:
         namespaces = db_session.query(Namespace).all()
         result = jsonify(namespaces)
     return result
Esempio n. 7
0
def contact_read_api(public_id):
    # TODO auth with account object
    # Get all data for an existing contact.
    result = contacts.crud.read(g.namespace, g.db_session, public_id)
    if result is None:
        return err(404, "Couldn't find contact with id {0}".
                   format(public_id))
    return jsonify(result)
Esempio n. 8
0
def message_api(public_id):
    if public_id == 'all':
        # TODO assert limit query parameter

        all_messages = g.db_session.query(Message).join(Thread).filter(
            Thread.namespace_id == g.namespace.id).limit(DEFAULT_LIMIT).all()
        return jsonify(all_messages)

    try:
        m = g.db_session.query(Message).filter(
            Message.public_id == public_id).one()
        assert int(m.namespace.id) == int(g.namespace.id)
        return jsonify(m)

    except NoResultFound:
        return err(
            404, "Couldn't find message with id {0} "
            "on namespace {1}".format(public_id, g.namespace_public_id))
Esempio n. 9
0
def contact_create_api():
    # TODO(emfree) Detect attempts at duplicate insertions.
    data = request.get_json(force=True)
    name = data.get('name')
    email = data.get('email')
    if not any((name, email)):
        return err(400, 'Contact name and email cannot both be null.')
    new_contact = contacts.crud.create(g.namespace, g.db_session, name, email)
    return jsonify(new_contact)
Esempio n. 10
0
def message_api(public_id):
    if public_id == 'all':
        # TODO assert limit query parameter

        all_messages = g.db_session.query(Message).join(Thread).filter(
            Thread.namespace_id == g.namespace.id).limit(DEFAULT_LIMIT).all()
        return jsonify(all_messages)

    try:
        m = g.db_session.query(Message).filter(
            Message.public_id == public_id).one()
        assert int(m.namespace.id) == int(g.namespace.id)
        return jsonify(m)

    except NoResultFound:
        return err(404,
                   "Couldn't find message with id {0} "
                   "on namespace {1}".format(public_id, g.namespace_public_id))
Esempio n. 11
0
def files_api(public_id):
    if public_id == 'all':
        # TODO assert limit query parameter
        # TODO perhaps return just if content_disposition == 'attachment'
        all_files = g.db_session.query(Part) \
            .filter(Part.namespace_id == g.namespace.id) \
            .filter(Part.content_disposition is not None) \
            .limit(DEFAULT_LIMIT).all()
        return jsonify(all_files)

    try:
        f = g.db_session.query(Block).filter(
            Block.public_id == public_id).one()
        assert int(f.message.namespace.id) == int(g.namespace.id)
        return jsonify(f)

    except NoResultFound:
        return err(404, "Couldn't find file with id {0} "
                   "on namespace {1}".format(public_id, g.namespace_public_id))
Esempio n. 12
0
def contact_create_api():
    # TODO(emfree) Detect attempts at duplicate insertions.
    data = request.get_json(force=True)
    name = data.get('name')
    email = data.get('email')
    if not any((name, email)):
        return err(400, 'Contact name and email cannot both be null.')
    new_contact = contacts.crud.create(g.namespace, g.db_session,
                                       name, email)
    return jsonify(new_contact)
Esempio n. 13
0
def tag_create_api():
    data = request.get_json(force=True)
    if data.keys() != ['name']:
        return err(400, 'Malformed tag request')
    tag_name = data['name']
    if not UserTag.name_available(tag_name, g.namespace.id, g.db_session):
        return err(409, 'Tag name not available')

    tag = UserTag(name=tag_name, namespace=g.namespace)
    g.db_session.commit()
    return jsonify(tag)
Esempio n. 14
0
def tag_create_api():
    data = request.get_json(force=True)
    if data.keys() != ['name']:
        return err(400, 'Malformed tag request')
    tag_name = data['name']
    if not UserTag.name_available(tag_name, g.namespace.id, g.db_session):
        return err(409, 'Tag name not available')

    tag = UserTag(name=tag_name, namespace=g.namespace)
    g.db_session.commit()
    return jsonify(tag)
Esempio n. 15
0
def thread_api(public_id):
    public_id = public_id.lower()
    try:
        thread = g.db_session.query(Thread).filter(
            Thread.public_id == public_id,
            Thread.namespace_id == g.namespace.id).one()
        return jsonify(thread)

    except NoResultFound:
        return err(404, "Couldn't find thread with id `{0}` "
                   "on namespace {1}".format(public_id, g.namespace_public_id))
Esempio n. 16
0
def files_api(public_id):
    if public_id == 'all':
        # TODO assert limit query parameter
        # TODO perhaps return just if content_disposition == 'attachment'
        all_files = g.db_session.query(Part) \
            .filter(Part.namespace_id == g.namespace.id) \
            .filter(Part.content_disposition is not None) \
            .limit(DEFAULT_LIMIT).all()
        return jsonify(all_files)

    try:
        f = g.db_session.query(Block).filter(
            Block.public_id == public_id).one()
        assert int(f.message.namespace.id) == int(g.namespace.id)
        return jsonify(f)

    except NoResultFound:
        return err(
            404, "Couldn't find file with id {0} "
            "on namespace {1}".format(public_id, g.namespace_public_id))
Esempio n. 17
0
def thread_api(public_id):
    public_id = public_id.lower()
    try:
        thread = g.db_session.query(Thread).filter(
            Thread.public_id == public_id,
            Thread.namespace_id == g.namespace.id).one()
        return jsonify(thread)

    except NoResultFound:
        return err(
            404, "Couldn't find thread with id `{0}` "
            "on namespace {1}".format(public_id, g.namespace_public_id))
Esempio n. 18
0
def file_upload_api():
    all_files = []
    for name, uploaded in request.files.iteritems():
        g.log.info("Processing upload '{0}'".format(name))
        f = Block()
        f.namespace = g.namespace
        f.content_type = uploaded.content_type
        f.filename = uploaded.filename
        f.data = uploaded.read()
        all_files.append(f)

    g.db_session.add_all(all_files)
    g.db_session.commit()  # to generate public_ids
    return jsonify(all_files)
Esempio n. 19
0
def file_upload_api():
    all_files = []
    for name, uploaded in request.files.iteritems():
        g.log.info("Processing upload '{0}'".format(name))
        f = Block()
        f.namespace = g.namespace
        f.content_type = uploaded.content_type
        f.filename = uploaded.filename
        f.data = uploaded.read()
        all_files.append(f)

    g.db_session.add_all(all_files)
    g.db_session.commit()  # to generate public_ids
    return jsonify(all_files)
Esempio n. 20
0
def draft_update_api(public_id):
    try:
        data = request.get_json(force=True)
    except ValueError:
        return err(400, 'Malformed request')

    to = data.get('to')
    cc = data.get('cc')
    bcc = data.get('bcc')
    subject = data.get('subject')
    body = data.get('body')
    files = data.get('files')

    draft = sendmail.update_draft(g.db_session, g.namespace.account, public_id,
                                  to, subject, body, files, cc, bcc)

    return jsonify(draft)
Esempio n. 21
0
def draft_update_api(public_id):
    try:
        data = request.get_json(force=True)
    except ValueError:
        return err(400, 'Malformed request')

    to = data.get('to')
    cc = data.get('cc')
    bcc = data.get('bcc')
    subject = data.get('subject')
    body = data.get('body')
    files = data.get('files')

    draft = sendmail.update_draft(g.db_session, g.namespace.account, public_id,
                                  to, subject, body, files, cc, bcc)

    return jsonify(draft)
Esempio n. 22
0
def draft_send_api():
    try:
        data = request.get_json(force=True)
    except ValueError:
        return err(400, 'Malformed request')

    draft_public_id = data.get('draft_id')

    to = data.get('to')
    cc = data.get('cc')
    bcc = data.get('bcc')
    subject = data.get('subject')
    body = data.get('body')
    files = data.get('files')

    if not draft_public_id or to:
        return err(400, 'Malformed request')

    result = sendmail.send_draft(g.db_session, g.namespace.account,
                                 draft_public_id, to, subject, body, files, cc,
                                 bcc)
    return jsonify(result)
Esempio n. 23
0
def draft_send_api():
    try:
        data = request.get_json(force=True)
    except ValueError:
        return err(400, 'Malformed request')

    draft_public_id = data.get('draft_id')

    to = data.get('to')
    cc = data.get('cc')
    bcc = data.get('bcc')
    subject = data.get('subject')
    body = data.get('body')
    files = data.get('files')

    if not draft_public_id or to:
        return err(400, 'Malformed request')

    result = sendmail.send_draft(g.db_session, g.namespace.account,
                                 draft_public_id, to, subject, body, files,
                                 cc, bcc)
    return jsonify(result)
Esempio n. 24
0
def thread_api_update(public_id):
    try:
        thread = g.db_session.query(Thread).filter(
            Thread.public_id == public_id,
            Thread.namespace_id == g.namespace.id).one()
    except NoResultFound:
        return err(
            404, "Couldn't find thread with id `{0}` "
            "on namespace {1}".format(public_id, g.namespace_public_id))
    data = request.get_json(force=True)
    if not set(data).issubset({'add_tags', 'remove_tags'}):
        return err(400, 'Can only add or remove tags from thread.')

    removals = data.get('remove_tags', [])

    # TODO(emfree) Currently trying to add/remove a read-only tag (i.e.,
    # anything but a UserTag) will give a "no tag found" error.

    for tag_name in removals:
        try:
            tag = g.db_session.query(UserTag).filter(
                UserTag.namespace_id == g.namespace.id,
                UserTag.name == tag_name).one()
            thread.usertags.discard(tag)
        except NoResultFound:
            return err(404, 'No tag found with name {}'.format(tag_name))

    additions = data.get('add_tags', [])
    for tag_name in additions:
        try:
            tag = g.db_session.query(UserTag).filter(
                UserTag.namespace_id == g.namespace.id,
                UserTag.name == tag_name).one()
            thread.usertags.add(tag)
        except NoResultFound:
            return err(404, 'No tag found with name {}'.format(tag_name))

    g.db_session.commit()
    return jsonify(thread)
Esempio n. 25
0
def thread_api_update(public_id):
    try:
        thread = g.db_session.query(Thread).filter(
            Thread.public_id == public_id,
            Thread.namespace_id == g.namespace.id).one()
    except NoResultFound:
        return err(404, "Couldn't find thread with id `{0}` "
                   "on namespace {1}".format(public_id, g.namespace_public_id))
    data = request.get_json(force=True)
    if not set(data).issubset({'add_tags', 'remove_tags'}):
        return err(400, 'Can only add or remove tags from thread.')

    removals = data.get('remove_tags', [])

    # TODO(emfree) Currently trying to add/remove a read-only tag (i.e.,
    # anything but a UserTag) will give a "no tag found" error.

    for tag_name in removals:
        try:
            tag = g.db_session.query(UserTag).filter(
                UserTag.namespace_id == g.namespace.id,
                UserTag.name == tag_name).one()
            thread.usertags.discard(tag)
        except NoResultFound:
            return err(404, 'No tag found with name {}'.  format(tag_name))

    additions = data.get('add_tags', [])
    for tag_name in additions:
        try:
            tag = g.db_session.query(UserTag).filter(
                UserTag.namespace_id == g.namespace.id,
                UserTag.name == tag_name).one()
            thread.usertags.add(tag)
        except NoResultFound:
            return err(404, 'No tag found with name {}'.  format(tag_name))

    g.db_session.commit()
    return jsonify(thread)
Esempio n. 26
0
def contact_search_api():
    filter = request.args.get('filter', '')
    try:
        limit = int(request.args.get('limit', 10))
        offset = int(request.args.get('offset', 0))
    except ValueError:
        return err(400, 'limit and offset parameters must be integers')
    if limit < 0 or offset < 0:
        return err(400, 'limit and offset parameters must be nonnegative '
                        'integers')
    if limit > 1000:
        return err(400, 'cannot request more than 1000 contacts at once.')
    order = request.args.get('order')
    if order == 'rank':
        results = contacts.search_util.search(g.db_session,
                                              g.namespace.account_id, filter,
                                              limit, offset)
    else:
        results = g.db_session.query(Contact). \
            filter(Contact.account_id == g.namespace.account_id,
                   Contact.source == 'local'). \
            order_by(asc(Contact.id)).limit(limit).offset(offset).all()

    return jsonify(results)
Esempio n. 27
0
def tag_query_api():
    results = list(g.namespace.usertags.union(g.namespace.account.folders))
    return jsonify(results)
Esempio n. 28
0
def webhooks_read_all_api():
    return jsonify(
        g.db_session.query(Webhook).filter(
            Webhook.namespace_id == g.namespace_id).all())
Esempio n. 29
0
def draft_get_api(public_id):
    draft = sendmail.get_draft(g.db_session, g.namespace.account, public_id)
    return jsonify(draft)
Esempio n. 30
0
def draft_delete_api(public_id):
    result = sendmail.delete_draft(g.db_session, g.namespace.account,
                                   public_id)
    return jsonify(result)
Esempio n. 31
0
def thread_query_api():
    return jsonify(g.lens.thread_query(g.db_session, limit=g.lens_limit,
                                       offset=g.lens_offset).all())
Esempio n. 32
0
def draft_get_all_api():
    drafts = sendmail.get_all_drafts(g.db_session, g.namespace.account)
    return jsonify(drafts)
Esempio n. 33
0
def webhooks_read_all_api():
    return jsonify(g.db_session.query(Webhook).
                   filter(Webhook.namespace_id == g.namespace_id).all())
Esempio n. 34
0
def message_query_api():
    return jsonify(
        g.lens.message_query(g.db_session,
                             limit=g.lens_limit,
                             offset=g.lens_offset).all())
Esempio n. 35
0
def thread_query_api():
    return jsonify(
        g.lens.thread_query(g.db_session,
                            limit=g.lens_limit,
                            offset=g.lens_offset).all())
Esempio n. 36
0
def draft_get_all_api():
    drafts = sendmail.get_all_drafts(g.db_session, g.namespace.account)
    return jsonify(drafts)
Esempio n. 37
0
def draft_get_api(public_id):
    draft = sendmail.get_draft(g.db_session, g.namespace.account, public_id)
    return jsonify(draft)
Esempio n. 38
0
def tag_query_api():
    results = list(g.namespace.usertags.union(g.namespace.account.folders))
    return jsonify(results)
Esempio n. 39
0
def draft_delete_api(public_id):
    result = sendmail.delete_draft(g.db_session, g.namespace.account,
                                   public_id)
    return jsonify(result)
Esempio n. 40
0
def message_query_api():
    return jsonify(g.lens.message_query(g.db_session, limit=g.lens_limit,
                                        offset=g.lens_offset).all())
Esempio n. 41
0
def index():
    return jsonify(g.namespace)
Esempio n. 42
0
def index():
    return jsonify(g.namespace)