def test_get_latest_accepted(db_conn, cards_table): """ Expect to pull the latest accepted version out of the database, given a kind and an entity_id. """ cards_table.insert([{ 'id': 'A1', 'entity_id': 'A', 'created': r.time(2004, 11, 3, 'Z'), 'status': 'accepted', }, { 'id': 'B2', 'entity_id': 'A', 'created': r.time(2005, 11, 3, 'Z'), 'status': 'accepted', }, { 'id': 'C3', 'entity_id': 'B', 'created': r.time(2006, 11, 3, 'Z'), 'status': 'accepted', }]).run(db_conn) e = entity.get_latest_accepted('card', 'A') assert isinstance(e, Card)
def test_get_latest_accepted(db_conn, cards_table): """ Expect to pull the latest accepted version out of the database, given a kind and an entity_id. """ cards_table.insert( [ {"id": "A1", "entity_id": "A", "created": r.time(2004, 11, 3, "Z"), "status": "accepted", "kind": "video"}, {"id": "B2", "entity_id": "A", "created": r.time(2005, 11, 3, "Z"), "status": "accepted", "kind": "video"}, {"id": "C3", "entity_id": "B", "created": r.time(2006, 11, 3, "Z"), "status": "accepted", "kind": "video"}, ] ).run(db_conn) e = entity.get_latest_accepted(db_conn, "card", "A") assert isinstance(e, Card)
def follow_route(request): """ Follow a card, unit, or set. """ # TODO-3 simplify this method. does some of this belong in the model? 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 = Follow(follow_data) errors = follow.validate() if errors: return 400, { 'errors': errors, 'ref': '4Qn9oWVWiGKvXSONQKHSy1T6' } # Ensure the entity exists TODO-3 should this be a model validation? if follow['entity']['kind'] == 'topic': entity = Topic.get(id=follow['entity']['id']) else: entity = get_latest_accepted(follow['entity']['kind'], follow['entity']['id']) if not entity: return abort(404) # Ensure we don't already follow TODO-3 should this be a model validation? prev = Follow.list(user_id=current_user['id'], entity_id=follow_data['entity']['id']) if prev: return abort(409) follow, errors = follow.save() if errors: return 400, { 'errors': errors, 'ref': 'gKU6wgTItxpKyDs0eAlonCmi', } return 200, {'follow': follow.deliver(access='private')}
def follow_route(request): """ Follow a card, unit, or set. """ # TODO-3 simplify this method. does some of this belong in the model? 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 = Follow(follow_data) errors = follow.validate() if errors: return 400, {'errors': errors, 'ref': '4Qn9oWVWiGKvXSONQKHSy1T6'} # Ensure the entity exists TODO-3 should this be a model validation? if follow['entity']['kind'] == 'topic': entity = Topic.get(id=follow['entity']['id']) else: entity = get_latest_accepted(follow['entity']['kind'], follow['entity']['id']) if not entity: return abort(404) # Ensure we don't already follow TODO-3 should this be a model validation? prev = Follow.list(user_id=current_user['id'], entity_id=follow_data['entity']['id']) if prev: return abort(409) follow, errors = follow.save() if errors: return 400, { 'errors': errors, 'ref': 'gKU6wgTItxpKyDs0eAlonCmi', } return 200, {'follow': follow.deliver(access='private')}
def get_posts_route(request, topic_id): """ Get a reverse chronological listing of posts for given topic. Includes topic meta data and posts (or proposals or votes). Paginates. """ db_conn = request['db_conn'] # Is the topic valid? topic = get_topic({'id': topic_id}, db_conn) if not topic: return 404, { 'errors': [{ 'name': 'topic_id', 'message': c('no_topic'), }], 'ref': 'pgnNbqSP1VUWkOYq8MVGPrSS', } # Pull the entity entity_kind = topic['entity']['kind'] entity = get_latest_accepted(db_conn, entity_kind, topic['entity']['id']) # Pull all kinds of posts posts = get_posts_facade(db_conn, limit=request['params'].get('limit') or 10, skip=request['params'].get('skip') or 0, topic_id=topic_id) # For proposals, pull up the proposal entity version # ...then pull up the previous version # ...make a diff between the previous and the proposal entity version diffs = {} entity_versions = {} for post_ in posts: if post_['kind'] == 'proposal': entity_version = entity_versions[post_['id']] = get_version( db_conn, post_['entity_version']['kind'], post_['entity_version']['id']) previous_version = get_version(db_conn, post_['entity_version']['kind'], entity_version['previous_id']) if previous_version: diffs[post_['id']] = object_diff(previous_version.deliver(), entity_version.deliver()) # TODO-2 SPLITUP create new endpoint for this instead users = {} for post_ in posts: user_id = post_['user_id'] if user_id not in users: user = get_user({'id': user_id}, db_conn) if user: users[user_id] = { 'name': user['name'], 'avatar': get_avatar(user['email'], 48), } # TODO-2 SPLITUP create new endpoints for these instead output = { 'topic': deliver_topic(topic), 'posts': [p.deliver() for p in posts], 'entity_versions': {p: ev.deliver('view') for p, ev in entity_versions.items()}, # 'diffs': diffs, TODO-2 this causes a circular dependency 'users': users, } if entity: output[entity_kind] = entity.deliver() return 200, output
def get_posts_route(request, topic_id): """ Get a reverse chronological listing of posts for given topic. Includes topic meta data and posts (or proposals or votes). Paginates. """ # Is the topic valid? topic = Topic.get(id=topic_id) if not topic: return 404, { 'errors': [{ 'name': 'topic_id', 'message': c('no_topic'), }], 'ref': 'pgnNbqSP1VUWkOYq8MVGPrSS', } # Pull the entity entity_kind = topic['entity']['kind'] entity = get_latest_accepted(entity_kind, topic['entity']['id']) # Pull all kinds of posts posts = get_posts_facade( limit=request['params'].get('limit') or 10, skip=request['params'].get('skip') or 0, topic_id=topic_id ) # For proposals, pull up the proposal entity version # ...then pull up the previous version # ...make a diff between the previous and the proposal entity version diffs = {} entity_versions = {} for post_ in posts: if post_['kind'] == 'proposal': entity_version = entity_versions[post_['id']] = get_version( post_['entity_version']['kind'], post_['entity_version']['id'] ) previous_version = get_version( post_['entity_version']['kind'], entity_version['previous_id'] ) if previous_version: diffs[post_['id']] = object_diff(previous_version.deliver(), entity_version.deliver()) # TODO-2 SPLITUP create new endpoint for this instead users = {} for post_ in posts: user_id = post_['user_id'] if user_id not in users: user = User.get(id=user_id) if user: users[user_id] = { 'name': user['name'], 'avatar': user.get_avatar(48) } # TODO-2 SPLITUP create new endpoints for these instead output = { 'topic': topic.deliver(), 'posts': [p.deliver() for p in posts], 'entity_versions': { p: ev.deliver() for p, ev in entity_versions.items() }, # 'diffs': diffs, TODO-2 this causes a circular dependency 'users': users, } if entity: output[entity_kind] = entity.deliver() return 200, output