import recipe.recipe.model as recipe_model list_param_recipe = recipe_model.Recipe().list_param class Factory(object): def clean_body(self, data): cleaned = self.remove_foreign_key(data) filled = self.fill_body_with_missing_key(cleaned) return filled @staticmethod def remove_foreign_key(data): clean_data = {} for i, j in data.items(): if i in list_param_recipe: clean_data[i] = j return clean_data @staticmethod def fill_body_with_missing_key(data): for key in list_param_recipe: if key not in data.keys(): if key in ["level", "resume", "cooking_time", "preparation_time", "nb_people", "note"]: data[key] = "" elif key in ["steps"]: data[key] = [] elif key in ["ingredients"]: data[key] = {} return data
from flask import abort import factory as factory import validator as validator import recipe.recipe.model as recipe server = factory.Server() validator = validator.Validator() recipe = recipe.Recipe() class Validator(object): def is_body_valid(self, data): self.is_title_valid(data) self.is_level_valid(data) self.is_resume_valid(data) self.is_cooking_time_valid(data) self.is_preparation_time_valid(data) self.is_nb_people_valid(data) self.is_note_valid(data) self.is_ingredients_valid(data) self.is_steps_valid(data) @staticmethod def is_title_valid(data): validator.is_mandatory("title", data) validator.is_string("title", data["title"]) validator.is_string_non_empty("title", data["title"]) return True
from flask import Blueprint, request import factory as factory import recipe.recipe.model as recipe_model import recipe.recipe.validator.GetRecipe as validator_GetRecipe import recipe.recipe.validator.PostRecipe as validator_PostRecipe import recipe.recipe.validator.PutRecipe as validator_PutRecipe import recipe.recipe.validator.DeleteRecipe as validator_DeleteRecipe import recipe.recipe.factory.PostRecipe as factory_PostRecipe import recipe.recipe.factory.PutRecipe as factory_PutRecipe recipe_api = Blueprint('recipe_api', __name__) recipe = recipe_model.Recipe() get_recipe_validator = validator_GetRecipe.Validator() post_recipe_validator = validator_PostRecipe.Validator() put_recipe_validator = validator_PutRecipe.Validator() delete_recipe_validator = validator_DeleteRecipe.Validator() post_recipe_factory = factory_PostRecipe.Factory() put_recipe_factory = factory_PutRecipe.Factory() """ recipe route - GET /recipe - GET /recipe/<_id> - POST /recipe - PUT /recipe - DELETE /recipe/<_id> """