def index(): schema = PoolSchema(many=True) if request.args: region = request.args.get('region') pool_type = request.args.get('type') pools = Pool.select(lambda pool: pool.region == region or pool.type == pool_type) else: pools = Pool.select() return schema.dumps(pools) # 'schema.dumps' converts the list to JSON
def star_pool(pool_id): schema = PoolSchema() pool = Pool.get(id=pool_id) pool.starred_by.add(g.current_user) db.commit() return schema.dumps(pool)
def delete_comment(pool_id, comment_id): schema = PoolSchema() pool = Pool.get(id=pool_id) comment = Comment.get(id=comment_id) comment.delete() db.commit() return schema.dumps(pool)
def delete(pool_id): pool = Pool.get(id=pool_id) if not pool: abort(404) pool.delete() db.commit() return '', 204
def show_comment(pool_id, comment_id): schema = PoolSchema() pool = Pool.get(id=pool_id) comment = Comment.get(id=comment_id) # If we can't find a pool, send a 404 response if not comment: abort(404) # otherwise, send back the pool data as JSON return schema.dumps(comment)
def show(pool_id): # This will serialize our data schema = PoolSchema() # This gets a pool by ID pool = Pool.get(id=pool_id) # If we can't find a pool, send a 404 response if not pool: abort(404) # otherwise, send back the pool data as JSON return schema.dumps(pool)
def update(pool_id): # This serializes our data, turns the object into a string schema = PoolSchema() # This gets the pool by its ID pool = Pool.get(id=pool_id) if not pool: abort(404) try: # attempt to turn the JSON string into a dict / object (de-serialization) data = schema.load(request.get_json()) pool.set(**data) db.commit() except ValidationError as err: return jsonify({'message': 'Validation failed', 'errors': err.messages}), 422 return schema.dumps(pool)
def create(): # This will deserialize the JSON from insomnia schema = PoolSchema() try: # attempt to convert the JSON into a dict data = schema.load(request.get_json()) # Use that to create a pool object data['user'] = g.current_user pool = Pool(**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 pool data as JSON return schema.dumps(pool), 201
def create_comment(pool_id): pool_schema = PoolSchema() comment_schema = CommentSchema() pool = Pool.get(id=pool_id) try: # attempt to convert the JSON into a dict data = comment_schema.load(request.get_json()) except ValidationError as err: # if the validation fails, send back a 422 response return jsonify({'message': 'Validation failed', 'errors': err.messages}), 422 # Use that to create a pool object Comment(**data, pool=pool, user=g.current_user) # Store it in the database db.commit() # otherwise, send back the pool data as JSON return pool_schema.dumps(pool), 201
george = User( username='******', email='*****@*****.**', password_hash=schema.generate_hash('pass'), image= 'https://scontent-lhr3-1.xx.fbcdn.net/v/t1.0-9/10644956_10152843839016159_5845249356064376143_n.jpg?_nc_cat=110&_nc_ht=scontent-lhr3-1.xx&oh=29baade9b102d375d0d3e29951771941&oe=5D5B4328' ) pool_one = Pool( name='Kenwood Ladies\' Bathing Pond', image= 'https://storify.com/services/proxy/2/Wzk4uTfDIg4TLKDVtWNSjw/https/d2kmm3vx031a1h.cloudfront.net/O7p7wh3vRkGK8G70Kg6y_20170719_101823.jpg', description= 'Hampstead has three different ponds for swimming: one for men, one for women and one mixed. Only swimmers over eight years of age are allowed; those between eight and 15 years old must be in the care of an adult. Winter swimming is sometimes available at the ponds, which remain popular with users. The Kenwood Ladies’ Pond is situated on Millfield Lane on the eastern edge of Hampstead Heath.', type='Pond', address='Hampstead Heath, Highgate, London NW3 1BP', lng='-0.160377', lat='51.566891', region='Greater London', heated=False, country='England', user=emma) pool_two = Pool( name='Serpentine Lido', image= 'https://secretldn.com/wp-content/uploads/2018/05/Serpentine-Lido.jpg', description= 'The lake is used all year around by the Serpentine Swimming Club and users of the Serpentine Lido. Regular triathlete sessions also take place.', type='Lido', address='Hyde Park, London, W2 2UH',