def edit_user(user_id): ValidationError.raise_assert(request.json, 'missing request json') body = request.json user = db.session.query(User).filter_by(id=user_id).first() QueryError.raise_assert(user is not None, 'user "{}" not found'.format(user_id)) # check that body user matches url user if 'user_id' in request.json: ValidationError.raise_assert(body['user_id']==user_id, \ 'payload user_id different from url') # check that token user == url user if g['current_user'].user_id != user_id: raise PermissionsError('not authorized to edit this user') if 'username' in body: user.username = body['username'] if 'display_name' in body: user.display_name = body['display_name'] output = { 'user_id': user.id, 'username': user.username, 'display_name': user.display_name, 'picture': user.picture, } return jsonify({'user': output})
def get_post(post_id=None): if not post_id is None: post = db.session.query(Post).filter_by(id=post_id).first() QueryError.raise_assert(post is not None, 'post "{}" not found'.format(post_id)) output = { 'post_id': post.id, 'created_date': post.created_date, 'modified_date': post.modified_date, 'title': post.title, 'body': decode_body(post.body), 'collaborators': [user.id for user in post.collaborators], 'upvotes': [user.id for user in post.upvotes], 'explicit_tags': [], 'implicit_tags': [], } for post_tag in post.post_tags: if post_tag.is_explicit: output['explicit_tags'].append(post_tag.tag.tag) else: output['implicit_tags'].append(post_tag.tag.tag) return jsonify({'post': output}) else: raise NotImplementedError('GET for multiple posts not implemented yet')
def get_user(user_id=None): # get single user if user_id is not None: user = db.session.query(User).filter_by(id=user_id).first() QueryError.raise_assert(user is not None, 'user "{}" not found'.format(user_id)) output = { 'user_id': user.id, 'username': user.username, 'display_name': user.display_name, 'picture': user.picture, } return jsonify({'user': output}) q = db.session.query(User) # TODO: allow url args output = [] for user in q.all(): output.append({ 'user_id': user.id, 'username': user.username, 'display_name': user.display_name, 'picture': user.picture, }) return jsonify({'users': output})
def edit_post(post_id=None): raise NotImplementedError() post = db.session.query(Post).filter_by(id=post_id).first() QueryError.raise_assert(post is not None, 'post "{}" not found'.format(post_id)) post.modified_date = datetime.datetime.now() db.session.add(post) db.session.commit()
def delete_post_upvote(post_id): post = db.session.query(Post).filter_by(id=post_id).first() QueryError.raise_assert(post is not None, 'post "{}" not found'.format(post_id)) if g.current_user in post.upvotes: post.upvotes.remove(g.current_user) db.session.add(post) db.session.commit() logger.debug('user "{}" delete upvote post "{}"'.format( g.current_user.id, post.id)) return jsonify({'code': 'success'})
def get_team(team_id=None): # get all teams if team_id is None: output = [] for team in db.session.query(Team).all(): output.append([{ 'team_id': team.id, 'team_name': team.team_name, }]) return jsonify({'teams': output}) # get single team else: team = db.session.query(Team).filter_by(id=team_id).first() QueryError.raise_assert(team is not None, 'team <{}> not found'.format(team_id)) return jsonify({'team_id': team.id, 'team_name': team.team_name})
def get_tag(tag_name=None): # single tag if tag_name is not None: tag = db.session.query(Tag).filter( db.func.lower(Tag.tag) == db.func.lower(tag_name)).first() QueryError.raise_assert(tag is not None, 'tag <{}> not found'.format(tag_name)) return jsonify({'tag': tag.tag}) # multiple tags q = db.session.query(Tag) # apply "startswith" filter if passed via request args if 'startswith' in request.args.keys(): QueryError.raise_assert( len(request.args.getlist('startswith')) <= 1, 'can only specify startswith arg once') tag_prefix = request.args.get('startswith') q = q.filter(db.func.lower(Tag.tag).startswith(tag_prefix)) logger.debug('filter tags starting with "{}"'.format(tag_prefix)) # handle "implicit" filter QueryError.raise_assert( len(request.args.getlist('implicit')) <= 1, 'can only specify implicit arg once') include_implicit = helpers.make_boolean( request.args.get('implicit', "false")) if not helpers.make_boolean(include_implicit): # filter out implicit tags q = q.filter(Tag.has_explicit == True) logger.debug('filter to only explicit tags') tags = q.all() output = [tag.tag for tag in tags] return jsonify({'tags': output})
def create_post(): payload = request.json logger.debug('validating request body') # validate payload assert payload is not None, 'missing json body' for required_field in ['title', 'body', 'collaborators', 'explicit_tags']: ValidationError.raise_assert( bool=required_field in payload, msg='"{}" required'.format(required_field)) logger.debug('create Post object') # init Post object post = Post(title=payload['title'], body=encode_body(payload['body'])) logger.debug('process tags') # get implicit tags explicit_tag_names = map(clean_whitespace, payload['explicit_tags']) explicit_tag_names = map(lambda x: (x, True), explicit_tag_names) # compute implicit tags from title implicit_tag_names = map(clean_whitespace, title_tokenizer(post.title)) implicit_tag_names = map(lambda x: (x, False), implicit_tag_names) known_tags = set() logger.debug('get/create tag objects') for (tag_name, is_explicit) in chain(explicit_tag_names, implicit_tag_names): tag_key = tag_name.strip().lower() if tag_key in known_tags: continue known_tags.add(tag_key) tag = db.session.query(Tag).filter( db.func.lower(Tag.tag) == db.func.lower(tag_name)).first() # allow on-the-fly tag creation if tag is None: tag = Tag(tag=tag_name, has_explicit=is_explicit) db.session.add(tag) else: if is_explicit and tag.has_explicit == False: tag.has_explicit = True db.session.add(tag) post_tag = PostTag(is_explicit=is_explicit) post_tag.tag = tag post.post_tags.append(post_tag) # add collaborators # TODO: allow mixed types (users & teams) logger.debug('get collaborators') for user_id in payload['collaborators']: user = db.session.query(User).filter_by(id=user_id).first() QueryError.raise_assert(user is not None, 'user "{}" not found'.format(user_id)) post.collaborators.append(user) if g.current_user not in post.collaborators: post.collaborators.append(g.current_user) logger.debug('persist Post object to db') db.session.add(post) db.session.commit() logger.debug('created Post(id={})'.format(post.id)) output = { 'post_id': post.id, 'title': post.title, 'body': decode_body(post.body), 'created_date': post.created_date, 'modified_date': post.modified_date, 'explicit_tags': [], 'implicit_tags': [], 'collaborators': [user.id for user in post.collaborators], 'upvotes': [user.id for user in post.upvotes], } for post_tag in post.post_tags: if post_tag.is_explicit: output['explicit_tags'].append(post_tag.tag.tag) else: output['implicit_tags'].append(post_tag.tag.tag) return jsonify({'post': output}), 201