Ejemplo n.º 1
0
def create_topic_route(request):
    """
  Create a new topic.
  """

    db_conn = request['db_conn']
    current_user = get_current_user(request)
    if not current_user:
        return abort(401, 'WJ50hh2STw-5ujy62wyXew')

    # ## STEP 1) Create topic
    topic_data = request['params']
    if not topic_data:
        return 400, {
            'errors': [{
                'name': 'topic',
                'message': 'Missing topic data.',
                'ref': 'PmocSz4OQUGa2T7x98yVlg',
            }],
        }
    topic_data['user_id'] = current_user['id']
    topic, errors = insert_topic(db_conn, topic_data)
    if errors:
        return 400, {'errors': errors, 'ref': 'UoyXf_vwSWee0tCWgxg4Zw'}

    # ## STEP 2) Add author as a follower
    insert_follow(
        db_conn, {
            'user_id': current_user['id'],
            'entity_id': topic['id'],
            'entity_kind': 'topic',
        })
    # TODO-2 also follow the entity automatically IF needed

    # ## STEP 3) Send out any needed notices
    send_notices(db_conn,
                 entity_id=topic['entity_id'],
                 entity_kind=topic['entity_kind'],
                 notice_kind='create_topic',
                 notice_data={
                     'user_name': current_user['name'],
                     'topic_name': topic['name'],
                     'entity_kind': topic['entity_kind'],
                     'entity_name': convert_uuid_to_slug(topic['entity_id']),
                 })

    # ## STEP 4) Return response
    return 200, {
        'topic': deliver_topic(topic),
    }
Ejemplo n.º 2
0
def test_entity(db_conn, cards_table, follows_table):
    """
    Expect a follow to require an entity kind and id.
    """

    create_card_a(db_conn, cards_table)
    follow_data = {
        'user_id': 'A',
    }
    follow, errors = insert_follow(follow_data, db_conn)
    assert len(errors) == 2
    follow_data['entity'] = {
        'id': 'A',
        'kind': 'card',
    }
    follow, errors = insert_follow(follow_data, db_conn)
    assert len(errors) == 0
Ejemplo n.º 3
0
def test_user_id(db_conn, cards_table, follows_table):
    """
    A follow should require a user_id.
    """

    create_card_a(db_conn, cards_table)
    follow_data = {
        'entity': {
            'id': 'A',
            'kind': 'card',
        }
    }
    follow, errors = insert_follow(follow_data, db_conn)
    assert len(errors) == 1
    follow_data['user_id'] = 'A'
    follow, errors = insert_follow(follow_data, db_conn)
    assert len(errors) == 0
Ejemplo n.º 4
0
def test_insert_follow(db_conn):
    create_test_follows(db_conn)
    data = {
        'user_id': user_b_uuid,
        'entity_id': test_unit_uuid,
        'entity_kind': 'unit',
    }
    follow, errors = insert_follow(db_conn, data)
    assert not follow
    assert errors
    data['entity_kind'] = 'subject'
    data['entity_id'] = test_unit_b_uuid
    follow, errors = insert_follow(db_conn, data)
    assert not follow
    assert errors
    data['entity_kind'] = 'unit'
    follow, errors = insert_follow(db_conn, data)
    assert not errors
    assert follow
Ejemplo n.º 5
0
def follow_route(request):
    """
  Follow a card, unit, or subject.
  """

    db_conn = request['db_conn']
    current_user = get_current_user(request)
    if not current_user:
        return abort(401, '0kW_gcpzQ7GomlCM28R8hw')
    follow_data = dict(**request['params'])
    follow_data['user_id'] = current_user['id']
    follow, errors = insert_follow(db_conn, follow_data)
    if errors:
        return 400, {'errors': errors, 'ref': 'R4AAxO7PT7udr2huRHIbnA'}
    return 200, {'follow': deliver_follow(follow, access='private')}
Ejemplo n.º 6
0
def follow_route(request):
    """
    Follow a card, unit, or set.
    """

    db_conn = request['db_conn']
    current_user = get_current_user(request)
    if not current_user:
        return abort(401)

    follow_data = dict(**request['params'])
    follow_data['user_id'] = current_user['id']

    follow, errors = insert_follow(follow_data, db_conn)
    if errors:
        return 400, {'errors': errors, 'ref': '4Qn9oWVWiGKvXSONQKHSy1T6'}

    return 200, {'follow': deliver_follow(follow, access='private')}
Ejemplo n.º 7
0
def create_topic_route(request):
    """
    Create a new topic.
    The first post (or proposal) must be provided.
    """

    db_conn = request['db_conn']

    current_user = get_current_user(request)
    if not current_user:
        return abort(401)

    # ## STEP 1) Create post and topic (and entity) instances
    topic_data = request['params'].get('topic')
    post_data = request['params'].get('post')
    if not topic_data:
        return 400, {
            'errors': [{
                'name': 'topic',
                'message': 'Missing topic data.'
            }],
            'ref': 'zknSd46f2hRNjSjVHCg6YLwN'
        }
    if not post_data:
        return 400, {
            'errors': [{
                'name': 'post',
                'message': 'Missing post data.'
            }],
            'ref': 'Qki4oWX4nTdNAjYI8z5iNawr'
        }
    topic_data = omit(topic_data, ('id', 'created', 'modified'))
    topic_data['user_id'] = current_user['id']
    topic_data, topic_errors = validate_topic(topic_data, db_conn)

    post_data = omit(post_data, (
        'id',
        'created',
        'modified',
    ))
    post_data['user_id'] = current_user['id']
    post_data['topic_id'] = topic_data['id']
    post_ = instance_post_facade(post_data)
    post_kind = post_['kind']
    if post_kind == 'proposal':
        entity = instance_new_entity(request['params'])
        entity_kind = get_kind(request['params'])
        post_['entity_version'] = {
            'id': entity['id'],
            'kind': entity_kind,
        }

    # ## STEP 2) Validate post and topic (and entity) instances
    errors = prefix_error_names('topic.', topic_errors)
    errors = errors + prefix_error_names('post.', post_.validate(db_conn))
    if post_kind == 'proposal':
        errors = (errors +
                  prefix_error_names('entity.', entity.validate(db_conn)))
    if len(errors):
        return 400, {'errors': errors, 'ref': 'TAY5pX3ghWBkSIVGTHzpQySa'}

    # ## STEP 3) Save post and topic (and entity)
    topic_data, topic_errors = insert_topic(topic_data, db_conn)
    post_['topic_id'] = topic_data['id']
    post_.save(db_conn)
    if post_kind == 'proposal':
        entity.save(db_conn)

    # ## STEP 4) Add author as a follower
    insert_follow(
        {
            'user_id': current_user['id'],
            'entity': {
                'id': topic_data['id'],
                'kind': 'topic',
            }
        }, db_conn)
    # TODO-2 also follow the entity automatically IF needed

    # ## STEP 5) Send out any needed notices
    send_notices(db_conn,
                 entity_id=topic_data['entity']['id'],
                 entity_kind=topic_data['entity']['kind'],
                 notice_kind='create_topic',
                 notice_data={
                     'user_name': current_user['name'],
                     'topic_name': topic_data['name'],
                     'entity_kind': topic_data['entity']['kind'],
                     'entity_name': topic_data['entity']['id'],
                 })

    if post_kind == 'proposal':
        send_notices(db_conn,
                     entity_id=topic_data['entity']['id'],
                     entity_kind=topic_data['entity']['kind'],
                     notice_kind='create_proposal',
                     notice_data={
                         'user_name': current_user['name'],
                         'topic_name': topic_data['name'],
                         'entity_kind': topic_data['entity']['kind'],
                         'entity_name': topic_data['entity']['id'],
                     })

    # ## STEP 5) Return response
    return 200, {'topic': deliver_topic(topic_data), 'post': post_.deliver()}
Ejemplo n.º 8
0
def create_post_route(request, topic_id):
    """
    Create a new post on a given topic.
    Proposal: must include entity (card, unit, or set) information.
    Vote: must refer to a valid proposal.
    """

    db_conn = request['db_conn']

    current_user = get_current_user(request)
    if not current_user:
        return abort(401)

    topic = get_topic({'id': topic_id}, db_conn)
    if not topic:
        return 404, {
            'errors': [{
                'name': 'topic_id',
                'message': c('no_topic'),
            }],
            'ref': 'PCSFCxsJtnlP0x9WzbPoKcwM',
        }

    # ## STEP 1) Create post (and entity) instances
    post_data = request['params'].get('post')
    if not post_data:
        return 400, {
            'errors': [{
                'name': 'post',
                'message': 'Missing post data.',
            }],
            'ref': 'ykQpZwJKq54MTCxgkx0p6baW'
        }
    post_data = omit(post_data, (
        'id',
        'created',
        'modified',
    ))
    post_data['user_id'] = current_user['id']
    post_data['topic_id'] = topic_id
    post_ = instance_post_facade(post_data)
    post_kind = post_['kind']
    if post_kind == 'proposal':
        entity = instance_new_entity(request['params'])
        entity_kind = get_kind(request['params'])
        post_['entity_version'] = {
            'id': entity['id'],
            'kind': entity_kind,
        }

    # ## STEP 2) Validate post (and entity) instances
    errors = prefix_error_names('post.', post_.validate(db_conn))
    if post_kind == 'proposal':
        errors = (errors +
                  prefix_error_names('entity.', entity.validate(db_conn)))
    if len(errors):
        return 400, {'errors': errors, 'ref': 'tux33ztgFj9ittSpS7WKIkq7'}

    # ## STEP 3) Save post (and entity)
    post_.save(db_conn)
    if post_kind == 'proposal':
        entity.save(db_conn)

    # ## STEP 4) Add author as a follower
    insert_follow(
        {
            'user_id': current_user['id'],
            'entity': {
                'id': topic['id'],
                'kind': 'topic',
            }
        }, db_conn)
    # TODO-2 also follow the entity

    # ## STEP 5) Make updates based on proposal / vote status
    if post_kind == 'proposal':
        update_entity_status(db_conn, post_)
    if post_kind == 'vote':
        proposal = Proposal.get(db_conn, id=post_['replies_to_id'])
        update_entity_status(db_conn, proposal)

    # ## STEP 6) Send notices
    if post_kind == 'proposal':
        send_notices(db_conn,
                     entity_id=topic['entity']['id'],
                     entity_kind=topic['entity']['kind'],
                     notice_kind='create_proposal',
                     notice_data={
                         'user_name': current_user['name'],
                         'topic_name': topic['name'],
                         'entity_kind': topic['entity']['kind'],
                         'entity_name': topic['entity']['id'],
                     })

    # ## STEP 7) Return response
    return 200, {'post': post_.deliver()}