def show(user_id): schema = UserSchema() user = User.get(id=user_id) if not user: abort(404) return schema.dumps(user)
def show(user_id): # This will serialize our data schema = UserSchema() # This gets a user by ID user = User.get(id=user_id) # If we can't find a user, send a 404 response if not user: abort(404) # otherwise, send back the user data as JSON return schema.dumps(user)
def update(user_id): schema = UserSchema() user = User.get(id=user_id) if not user: abort(404) try: data = schema.load(request.get_json()) user.set(**data) db.commit() except ValidationError as err: return jsonify({'message': 'Validation failed', 'errors': err.messages}), 422 return schema.dumps(user)
def create(): schema = UserSchema() try: data = schema.load(request.get_json()) user = User(**data) db.commit() except ValidationError as err: return jsonify({'message': 'Validation failed', 'errors': err.messages}), 422 return schema.dumps(user), 201
def edit_profile(): schema = UserSchema() data = request.get_json() if data.get('concession'): data['concession'] = data['concession'] == 'true' if data.get('keyword_ids'): print(g.current_user.keywords) previousKeywords = [] for keyword in g.current_user.keywords: previousKeywords.append(keyword) data['keywords'] = [ Keyword.get(id=keyword_id) for keyword_id in data['keyword_ids'] ] + previousKeywords del data['keyword_ids'] g.current_user.set(**data) db.commit() user_info = g.current_user return schema.dumps(user_info)
def create(): # This will deserialize the JSON from insomnia schema = UserSchema() try: # attempt to convert the JSON into a dict data = schema.load(request.get_json()) # Use that to create a user object user = User(**data) # store it in the database db.commit() except ValidationError as err: # if the validation fails, send back a 422 response return jsonify({ 'message': 'Validation failed', 'errors': err.messages }), 422 # otherwise, send back the user data as JSON return schema.dumps(user), 201
def index(): schema = UserSchema(many=True) users = User.select() print(users) return schema.dumps(users)
def user_index(): # This will serialize our data schema = UserSchema() #many=True user = User.get(id=g.current_user.id) return schema.dumps(user) # 'schema.dumps' converts the list to JSON
def profile(): schema = UserSchema() return schema.dumps(g.current_user)
def index(): # This will serialize our data # `many=True` because there are many users, ie we expect a list schema = UserSchema(many=True) users = User.select() # get all the users return schema.dumps(users) # `schema.dumps` converts the list to JSON