def update_post_route(request, topic_id, post_id): """ Update an existing post. Must be one's own post. For post: - Only the body field may be changed. For proposals: - Only the name, body, and status fields can be changed. - The status can only be changed to declined, and only when the current status is pending or blocked. For votes: - The only fields that can be updated are body and response. """ db_conn = request['db_conn'] current_user = get_current_user(request) if not current_user: return abort(401) # ## STEP 1) Find existing post instance ## # post_ = get_post_facade(db_conn, post_id) if not post_: return abort(404) if post_['user_id'] != current_user['id']: return abort(403) post_kind = post_['kind'] # ## STEP 2) Limit the scope of changes ## # post_data = request['params']['post'] if post_kind is 'post': post_data = pick(post_data, ('body',)) elif post_kind is 'proposal': post_data = pick(post_data, ('name', 'body', 'status',)) if (post_data.get('status') != 'declined' or post_data.get('status') not in ('pending', 'blocked',)): del post_data['status'] elif post_kind is 'vote': post_data = pick(post_data, ('body', 'response',)) # ## STEP 3) Validate and save post instance ## # post_, errors = post_.update(db_conn, post_data) if errors: errors = prefix_error_names('post.', errors) return 400, { 'errors': errors, 'ref': 'E4LFwRv2WEJZks7use7TCpww' } # ## STEP 4) 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 5) Return response ## # return 200, {'post': post_.deliver()}
def update_post_route(request, topic_id, post_id): """ Update an existing post. Must be one's own post. For post: - Only the body field may be changed. For proposals: - Only the name, body, and status fields can be changed. - The status can only be changed to declined, and only when the current status is pending or blocked. For votes: - The only fields that can be updated are body and response. """ db_conn = request['db_conn'] current_user = get_current_user(request) if not current_user: return abort(401) # ## STEP 1) Find existing post instance ## # post_ = get_post_facade(db_conn, post_id) if not post_: return abort(404) if post_['user_id'] != current_user['id']: return abort(403) post_kind = post_['kind'] # ## STEP 2) Limit the scope of changes ## # post_data = request['params']['post'] if post_kind is 'post': post_data = pick(post_data, ('body', )) elif post_kind is 'proposal': post_data = pick(post_data, ( 'name', 'body', 'status', )) if (post_data.get('status') != 'declined' or post_data.get('status') not in ( 'pending', 'blocked', )): del post_data['status'] elif post_kind is 'vote': post_data = pick(post_data, ( 'body', 'response', )) # ## STEP 3) Validate and save post instance ## # post_, errors = post_.update(db_conn, post_data) if errors: errors = prefix_error_names('post.', errors) return 400, {'errors': errors, 'ref': 'E4LFwRv2WEJZks7use7TCpww'} # ## STEP 4) 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 5) Return response ## # return 200, {'post': post_.deliver()}
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()}
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. """ current_user = get_current_user(request) if not current_user: return abort(401) topic = Topic.get(id=topic_id) 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()) if post_kind == 'proposal': errors = errors + prefix_error_names('entity.', entity.validate()) if len(errors): return 400, { 'errors': errors, 'ref': 'tux33ztgFj9ittSpS7WKIkq7' } # ## STEP 3) Save post (and entity) post_.save() if post_kind == 'proposal': entity.save() # ## STEP 4) Add author as a follower Follow.insert({ 'user_id': current_user['id'], 'entity': { 'id': topic['id'], 'kind': 'topic', } }) # TODO-2 also follow the entity # ## STEP 5) Make updates based on proposal / vote status if post_kind == 'proposal': update_entity_status(post_) if post_kind == 'vote': proposal = Proposal.get(id=post_['replies_to_id']) update_entity_status(proposal) # ## STEP 6) Return response return 200, {'post': post_.deliver()}