def test_entity(db_conn, topics_table):
    """
    Expect a topic to require an entity kind and id.
    """

    topic_data = {
        'user_id': 'Q',
        'name': 'A',
    }
    topic, errors = insert_topic(topic_data, db_conn)
    assert len(errors) == 2
    topic_data['entity'] = {
        'id': 'A',
        'kind': 'card',
    }
    topic, errors = insert_topic(topic_data, db_conn)
    assert len(errors) == 0
def test_user_id(db_conn, topics_table):
    """
    Expect a topic to require a user id.
    """

    topic_data = {
        'name': 'A',
        'entity': {
            'id': 'A',
            'kind': 'card',
        }
    }
    topic, errors = insert_topic(topic_data, db_conn)
    assert len(errors) == 1
    topic_data['user_id'] = 'Q'
    topic, errors = insert_topic(topic_data, db_conn)
    assert len(errors) == 0
Exemple #3
0
def test_insert_topic(db_conn):
    create_test_topics(db_conn)
    data = {
        'user_id': user_uuid,
        'entity_id': test_unit_uuid,
        'entity_kind': 'truck',
        'name': 'Lets talk even more about adding numbers',
    }
    topic, errors = insert_topic(db_conn, data)
    assert errors
    assert not topic
    data = {
        'user_id': user_uuid,
        'entity_id': test_unit_uuid,
        'entity_kind': 'unit',
        'name': 'Lets talk even more about adding numbers',
    }
    topic, errors = insert_topic(db_conn, data)
    assert not errors
    assert topic
Exemple #4
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),
    }
Exemple #5
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()}