Exemple #1
0
def test_list_paginate(db_conn, session, notices_table):
    """
    Expect to paginate lists of notices.
    """

    for i in range(0, 25):
        Notice.insert({
            'user_id': 'abcd1234',
            'kind': 'create_proposal',
            'data': {
                'user_name': '',
                'proposal_name': '',
                'entity_kind': '',
                'entity_name': '',
            }
        })

    request = {'cookies': {'session_id': session}, 'params': {}}
    code, response = routes.notice.list_notices_route(request)
    assert len(response['notices']) == 10
    request.update({'params': {'skip': 10}})
    code, response = routes.notice.list_notices_route(request)
    assert len(response['notices']) == 10
    request.update({'params': {'skip': 20}})
    code, response = routes.notice.list_notices_route(request)
    assert len(response['notices']) == 5
def test_list_paginate(db_conn, session, notices_table):
    """
    Expect to paginate lists of notices.
    """

    for i in range(0, 25):
        Notice.insert({
            'user_id': 'abcd1234',
            'kind': 'create_proposal',
            'data': {
                'user_name': '',
                'proposal_name': '',
                'entity_kind': '',
                'entity_name': '',
            }
        })

    request = {'cookies': {'session_id': session}, 'params': {}}
    code, response = routes.notice.list_notices_route(request)
    assert len(response['notices']) == 10
    request.update({'params': {'skip': 10}})
    code, response = routes.notice.list_notices_route(request)
    assert len(response['notices']) == 10
    request.update({'params': {'skip': 20}})
    code, response = routes.notice.list_notices_route(request)
    assert len(response['notices']) == 5
def test_list_paginate(db_conn, notices_table):
    """
    Expect to paginate lists of notices.
    """
    for i in range(0, 25):
        notices_table.insert({
            'id': i, 'user_id': 22, 'kind': 'create_proposal',
        }).run(db_conn)
    notices = Notice.list(user_id=22)
    assert len(notices) == 10
    notices = Notice.list(user_id=22, skip=20)
    assert len(notices) == 5
Exemple #4
0
def mark_notice_route(request, notice_id):
    """
    Mark notice as read or unread.
    Must be logged in as user, provide a valid ID, and own the notice.
    Return notice.
    """

    current_user = get_current_user(request)
    if not current_user:
        return abort(401)
    notice = Notice.get(id=notice_id)
    if not notice:
        return abort(404)
    if notice['user_id'] != current_user['id']:
        return abort(403)

    if 'read' not in request['params']:
        errors = [{
            'name': 'read',
            'message': 'You must specify read or unread.',
        }]
    elif request['params'].get('read') is True:
        notice, errors = notice.mark_as_read()
    elif request['params'].get('read') is False:
        notice, errors = notice.mark_as_unread()
    if len(errors):
        return 400, {
            'errors': errors,
            'ref': 'qR4CBtcfcYfWDTqK9JOXXLhO',
        }

    return 200, {'notice': notice.deliver(access='private')}
Exemple #5
0
def mark_notice_route(request, notice_id):
    """
    Mark notice as read or unread.
    Must be logged in as user, provide a valid ID, and own the notice.
    Return notice.
    """

    current_user = get_current_user(request)
    if not current_user:
        return abort(401)
    notice = Notice.get(id=notice_id)
    if not notice:
        return abort(404)
    if notice['user_id'] != current_user['id']:
        return abort(403)

    if 'read' not in request['params']:
        errors = [{
            'name': 'read',
            'message': 'You must specify read or unread.',
        }]
    elif request['params'].get('read') is True:
        notice, errors = notice.mark_as_read()
    elif request['params'].get('read') is False:
        notice, errors = notice.mark_as_unread()
    if len(errors):
        return 400, {
            'errors': errors,
            'ref': 'qR4CBtcfcYfWDTqK9JOXXLhO',
        }

    return 200, {'notice': notice.deliver(access='private')}
Exemple #6
0
def add_notice():
    # Todo 权限与安全
    form = request.form
    note = Notice.find(id=1)
    note.name = form.get('name', '')
    note.content = form.get('content', '')
    note.save()
    return redirect(url_for('.new_notice'))
def test_list(db_conn, session, notices_table):
    """
    Expect to get a list of 10 notices by user ID.
    """
    for i in range(0, 10):
        Notice.insert({
            'user_id': 'abcd1234',
            'kind': 'create_proposal',
            'data': {
                'user_name': '',
                'proposal_name': '',
                'entity_kind': '',
                'entity_name': '',
            }
        })

    request = {'cookies': {'session_id': session}, 'params': {}}
    code, response = routes.notice.list_notices_route(request)
    assert code == 200
    assert len(response['notices']) == 10
    assert 'user_id' in response['notices'][0]
def test_list_user(db_conn, notices_table):
    """
    Expect to get a only notices of user.
    """
    notices_table.insert([
        {'id': 1, 'user_id': 22, 'kind': 'create_proposal'},
        {'id': 2, 'user_id': 22, 'kind': 'create_proposal'},
        {'id': 3, 'user_id': 24, 'kind': 'create_proposal'},
        {'id': 4, 'user_id': 25, 'kind': 'create_proposal'},
    ]).run(db_conn)
    notices = Notice.list(user_id=22)
    assert len(notices) == 2
def test_list(db_conn, notices_table):
    """
    Expect to get a list of 10 notices by user ID.
    """
    notices_table.insert([
        {'id': 1, 'user_id': 22, 'kind': 'create_proposal'},
        {'id': 2, 'user_id': 22, 'kind': 'create_proposal'},
        {'id': 3, 'user_id': 22, 'kind': 'create_proposal'},
        {'id': 4, 'user_id': 22, 'kind': 'create_proposal'},
    ]).run(db_conn)
    notices = Notice.list(user_id=22)
    assert len(notices) == 4
Exemple #10
0
def test_list(db_conn, session, notices_table):
    """
    Expect to get a list of 10 notices by user ID.
    """
    for i in range(0, 10):
        Notice.insert({
            'user_id': 'abcd1234',
            'kind': 'create_proposal',
            'data': {
                'user_name': '',
                'proposal_name': '',
                'entity_kind': '',
                'entity_name': '',
            }
        })

    request = {'cookies': {'session_id': session}, 'params': {}}
    code, response = routes.notice.list_notices_route(request)
    assert code == 200
    assert len(response['notices']) == 10
    assert 'user_id' in response['notices'][0]
def test_list_unread(db_conn, notices_table):
    """
    Expect to get a list of unread notices.
    """
    notices_table.insert([
        {'id': 1, 'user_id': 22, 'kind': 'create_proposal', 'read': True},
        {'id': 3, 'user_id': 22, 'kind': 'create_proposal', 'read': False},
        {'id': 4, 'user_id': 22, 'kind': 'create_proposal', 'read': False},
    ]).run(db_conn)
    notices = Notice.list(user_id=22, read=False)
    assert len(notices) == 2
    assert notices[0]['id'] in (3, 4)
    assert notices[1]['id'] in (3, 4)
Exemple #12
0
def list_notices_route(request):
    """
    List notices for current user.
    Take parameters `limit`, `skip`, `tag`, and `read`.
    """

    current_user = get_current_user(request)
    if not current_user:
        return abort(401)
    notices = Notice.list(user_id=current_user['id'], **request['params'])
    output = {'notices': [notice.deliver(access='private')
                          for notice in notices]}
    return 200, output
def test_create(db_conn, notices_table):
    """
    Expect to create a notice.
    """
    notice, errors = Notice.insert({
        'user_id': 'abcd1234',
        'kind': 'create_proposal',
        'tags': ['test']
    })
    record = notices_table.filter({'user_id': 'abcd1234'}).run(db_conn)
    record = list(record)[0]
    assert len(errors) == 0
    assert record['user_id'] == 'abcd1234'
    assert record['tags'] == ['test']
Exemple #14
0
def list_notices_route(request):
    """
    List notices for current user.
    Take parameters `limit`, `skip`, `tag`, and `read`.
    """

    current_user = get_current_user(request)
    if not current_user:
        return abort(401)
    notices = Notice.list(user_id=current_user['id'], **request['params'])
    output = {
        'notices': [notice.deliver(access='private') for notice in notices]
    }
    return 200, output
Exemple #15
0
def manager_notice_create():
    post_data = request.get_json()
    try:
        notice = Notice(title=post_data["title"],
                        content=post_data["content"],
                        is_important=post_data["isImportant"],
                        is_stick=post_data["isStick"])
        db.session.add(notice)
        db.session.commit()
    except TypeError:
        abort(400)
    return {
        'code': 0,
    }
def test_mark_as_read(db_conn, notices_table):
    """
    Expect to mark a notice as read.
    """
    notice, errors = Notice.insert({
        'user_id': 'abcd1234',
        'kind': 'create_proposal',
        'tags': ['test']
    })
    assert notice['read'] is False
    notice.mark_as_read()
    assert notice['read'] is True
    record = notices_table.filter({'user_id': 'abcd1234'}).run(db_conn)
    record = list(record)[0]
    assert record['read'] is True
Exemple #17
0
def send_notices(entity_id, entity_kind, notice_kind, notice_data):
    """
    When an event occurs, send notices outs.
    """

    user_ids = Follow.get_user_ids_by_entity(entity_id=entity_id,
                                             entity_kind=entity_kind)
    for user_id in user_ids:
        notice, errors = Notice.insert({
            'user_id': user_id,
            'kind': notice_kind,
            'data': notice_data,
        })
        if errors:
            raise Exception(errors)
def test_validations(db_conn, notices_table):
    """
    Expect to only create valid notices.
    - Fail if no user_id
    - Fail if no kind
    - Fail if read not boolean.
    - Fail if tags not list.
    """
    notice, errors = Notice.insert({
        'read': 1234,
        'tags': 'test'
    })
    assert len(errors) == 4
    assert get_error(errors, 'user_id')['message'] == 'Required.'
    assert get_error(errors, 'kind')['message'] == 'Required.'
    assert get_error(errors, 'read')['message'] == 'Must be true or false.'
    assert get_error(errors, 'tags')['message'] == 'Must be a list.'
def test_list_tag(db_conn, notices_table):
    """
    Expect to get a list of notices by tag.
    """
    notices_table.insert([
        {'id': 1, 'user_id': 22, 'kind': 'create_proposal',
            'tags': ['apple', 'banana']},
        {'id': 2, 'user_id': 22, 'kind': 'create_proposal',
            'tags': ['orange', 'banana']},
        {'id': 3, 'user_id': 23, 'kind': 'create_proposal',
            'tags': ['apple', 'grape']},
        {'id': 4, 'user_id': 22, 'kind': 'create_proposal',
            'tags': ['apple', 'peach']},
    ]).run(db_conn)
    notices = Notice.list(user_id=22, tag='apple')
    assert len(notices) == 2
    assert 'apple' in notices[0]['tags']
    assert 'apple' in notices[1]['tags']
def test_notices_kind(db_conn, notices_table):
    """
    Expect to filter notices by kind.
    """

    notices_table.insert([
        {'id': 1, 'user_id': 22, 'kind': 'create_proposal',
            'tags': ['apple', 'banana']},
        {'id': 2, 'user_id': 22, 'kind': 'accepted_proposal',
            'tags': ['orange', 'banana']},
        {'id': 3, 'user_id': 22, 'kind': 'create_proposal',
            'tags': ['apple', 'grape']},
        {'id': 4, 'user_id': 22, 'kind': 'new_topic',
            'tags': ['apple', 'peach']},
    ]).run(db_conn)
    notices = Notice.list(user_id=22, kind='create_proposal')
    assert len(notices) == 2
    assert notices[0]['kind'] == 'create_proposal'
    assert notices[1]['kind'] == 'create_proposal'
def test_mark_no_user(db_conn, notices_table):
    """
    Expect to error on not logged in when marking as read.
    """
    notice, errors = Notice.insert({
        'user_id': 'abcd1234',
        'kind': 'create_proposal',
        'data': {
            'user_name': '',
            'proposal_name': '',
            'entity_kind': '',
            'entity_name': '',
        }
    })
    nid = notice['id']

    request = {'params': {'read': True}}
    code, response = routes.notice.mark_notice_route(request, nid)
    assert code == 401
    record = notices_table.get(nid).run(db_conn)
    assert record['read'] is False
Exemple #22
0
def test_mark_not_owned(db_conn, session, notices_table):
    """
    Expect to error when not own notice when marking as read.
    """
    notice, errors = Notice.insert({
        'user_id': '1234abcd',
        'kind': 'create_proposal',
        'data': {
            'user_name': '',
            'proposal_name': '',
            'entity_kind': '',
            'entity_name': '',
        }
    })
    nid = notice['id']

    request = {'cookies': {'session_id': session}, 'params': {'read': True}}
    code, response = routes.notice.mark_notice_route(request, nid)
    assert code == 403
    record = notices_table.get(nid).run(db_conn)
    assert record['read'] is False
def test_mark_not_owned(db_conn, session, notices_table):
    """
    Expect to error when not own notice when marking as read.
    """
    notice, errors = Notice.insert({
        'user_id': '1234abcd',
        'kind': 'create_proposal',
        'data': {
            'user_name': '',
            'proposal_name': '',
            'entity_kind': '',
            'entity_name': '',
        }
    })
    nid = notice['id']

    request = {'cookies': {'session_id': session}, 'params': {'read': True}}
    code, response = routes.notice.mark_notice_route(request, nid)
    assert code == 403
    record = notices_table.get(nid).run(db_conn)
    assert record['read'] is False
Exemple #24
0
def test_mark_no_user(db_conn, notices_table):
    """
    Expect to error on not logged in when marking as read.
    """
    notice, errors = Notice.insert({
        'user_id': 'abcd1234',
        'kind': 'create_proposal',
        'data': {
            'user_name': '',
            'proposal_name': '',
            'entity_kind': '',
            'entity_name': '',
        }
    })
    nid = notice['id']

    request = {'params': {'read': True}}
    code, response = routes.notice.mark_notice_route(request, nid)
    assert code == 401
    record = notices_table.get(nid).run(db_conn)
    assert record['read'] is False
def test_mark(db_conn, session, notices_table):
    """
    Expect to mark a notice as read.
    """
    notice, errors = Notice.insert({
        'user_id': 'abcd1234',
        'kind': 'create_proposal',
        'data': {
            'user_name': '',
            'proposal_name': '',
            'entity_kind': '',
            'entity_name': '',
        }
    })
    nid = notice['id']

    request = {'cookies': {'session_id': session}, 'params': {'read': True}}
    code, response = routes.notice.mark_notice_route(request, nid)
    assert code == 200
    assert response['notice']['read'] is True
    record = notices_table.get(nid).run(db_conn)
    assert record['read'] is True
Exemple #26
0
def test_mark(db_conn, session, notices_table):
    """
    Expect to mark a notice as read.
    """
    notice, errors = Notice.insert({
        'user_id': 'abcd1234',
        'kind': 'create_proposal',
        'data': {
            'user_name': '',
            'proposal_name': '',
            'entity_kind': '',
            'entity_name': '',
        }
    })
    nid = notice['id']

    request = {'cookies': {'session_id': session}, 'params': {'read': True}}
    code, response = routes.notice.mark_notice_route(request, nid)
    assert code == 200
    assert response['notice']['read'] is True
    record = notices_table.get(nid).run(db_conn)
    assert record['read'] is True
def test_list_empty(db_conn, notices_table):
    """
    Expect to get an empty list when run out of notices.
    """
    notices = Notice.list(user_id=22)
    assert len(notices) == 0
Exemple #28
0
def new_notice():
    # Todo 权限与安全
    note = Notice.find(id=1)
    if note is None:
        note = Notice.new({})
    return render_template("notice/new.html", note=note)
Exemple #29
0
def notice():
    # return 一个最新公告内容json
    note = Notice.find(id=1)
    return json.dumps(note.content, ensure_ascii=False)
Exemple #30
0
def add():
    form = request.form
    Notice.new(form)
    return redirect(url_for('.index'))
Exemple #31
0
def index():
    comments = Notice.all()
    return render_template('notice_index.html', comments=comments)