async def user_change_status(request): user = await User.query.where(User.username == request.match_info['username']).gino.first_or_404() if user.status == request['data']['status']: raise web.HTTPBadRequest(reason='The user already has the status "{}"'.format(request['data']['status'])) await user.update(status=request['data']['status']).apply() return web.json_response(to_dict(UserSchema, user))
async def user_view(request): user = await User \ .query \ .where(and_(User.username == request.match_info['username'], User.status == User.STATUS_ACTIVE)) \ .gino \ .first_or_404() return web.json_response(to_dict(UserSchema, user))
async def tag_add(request): tag = await Tag.query.where(Tag.name == request['data']['name'] ).gino.first() if tag: raise web.HTTPBadRequest( reason='Tag "{}" is already exist'.format(request['data']['name'])) tag = await Tag.create(name=request['data']['name']) return web.json_response(to_dict(TagSchema, tag), status=201)
async def recipe_change_status(request): recipe = await Recipe.query.where(Recipe.id == request.match_info['id'] ).gino.first_or_404() if recipe.status == request['data']['status']: raise web.HTTPBadRequest(reason='The user already has the status "{}"'. format(request['data']['status'])) await recipe.update(status=request['data']['status']).apply() return web.json_response(to_dict(RecipeSchema, recipe))
async def user_add(request): if await User.query.where(User.username == request['data']['username'] ).gino.first(): raise web.HTTPBadRequest(reason='Username "{}" is already occupied'. format(request['data']['username'])) user = User(username=request['data']['username']) user.set_password(request['data']['password']) user = await user.create() return web.json_response(to_dict(UserSchema, user), status=201)
async def image_add(request): data = await request.post() image = data['image'] error = file.validate_extension(image.filename, ['.jpg', '.png']) if error: raise web.HTTPBadRequest(reason=error) filename = file.get_random_filename(image.filename) path = file.get_image_path(request.app, filename) await file.save_file(path, image.file) image = await Image.create(filename=filename) return web.json_response(to_dict(ImageSchema, image), status=201)
async def recipe_add(request): if 'tags' in request['data']: tag_names = [tag['name'] for tag in request['data']['tags']] tags = await Tag.query.where(Tag.name.in_(tag_names)).gino.all() if len(tags) != len(tag_names): raise web.HTTPBadRequest( reason='The sequence of tags is non-existent tags') else: tags = [] if 'image_id' in request['data']: if not await Image.query.where(Image.id == request['data']['image_id'] ).gino.first(): raise web.HTTPBadRequest(reason='Image with id={} does not exist'. format(request['data']['image_id'])) image_id = request['data']['image_id'] else: image_id = None recipe = await Recipe.create(name=request['data']['name'], description=request['data']['description'], type=request['data']['type'], image_id=image_id, user_id=request.user.id) recipe.user = request.user recipe_step_list = [{ 'number': recipe_step['number'], 'description': recipe_step['description'], 'recipe_id': recipe.id } for recipe_step in request['data']['recipe_steps']] recipe.recipe_steps = await RecipeStep.insert_by_list_data(recipe_step_list ) tag_list = [{'recipe_id': recipe.id, 'tag_id': tag.id} for tag in tags] await RecipeXTag.insert_by_list_data(tag_list) recipe.tags = tags count_recipe_user = await db.select([func.count( Recipe.id)]).where(Recipe.user_id == request.user.id).gino.scalar() await request.user.update(count_recipes=count_recipe_user).apply() return web.json_response(to_dict(RecipeSchema, recipe), status=201)
async def recipe_view(request): recipes = await Recipe \ .outerjoin(User) \ .outerjoin(RecipeStep) \ .outerjoin(RecipeXTag) \ .outerjoin(Tag) \ .select() \ .where(Recipe.id == int(request.match_info['id'])) \ .gino \ .load( Recipe.distinct(Recipe.id) .load(add_recipe_step=RecipeStep.distinct(RecipeStep.id)) .load(add_tag=Tag.distinct(Tag.id)) .load(user=Tag.distinct(User.id)) ) \ .all() if not recipes: raise web.HTTPNotFound() return web.json_response(to_dict(RecipeSchema, recipes[0]))
async def selected_recipe_add(request): if not await Recipe.query.where(Recipe.id == request['data']['recipe_id'] ).gino.first(): raise web.HTTPBadRequest(reason='Recipe with id={} does not exist'. format(request['data']['recipe_id'])) selected_recipe = await SelectedRecipe \ .query \ .where( and_(SelectedRecipe.recipe_id == request['data']['recipe_id'], SelectedRecipe.user_id == request.user.id) ) \ .gino \ .first() if selected_recipe: raise web.HTTPBadRequest( reason='SelectedRecipe for a recipe with id={} already exists'. format(request['data']['recipe_id'])) selected_recipe = await SelectedRecipe.create( recipe_id=request['data']['recipe_id'], user_id=request.user.id) return web.json_response(to_dict(SelectedRecipeSchema, selected_recipe), status=201)