class RecipeDietTypeSchema(ma.SQLAlchemyAutoSchema): recipe_id = ma.Int(dump_only=True) diet_type_id = ma.Int(dump_only=True) class Meta: model = RecipeDietType sqla_session = db.session load_instance = True
class RecipeIngredientSchema(ma.SQLAlchemyAutoSchema): id = ma.Int(dump_only=True) class Meta: model = RecipeIngredient sqla_session = db.session load_instance = True
class TagSchema(ma.SQLAlchemyAutoSchema): id = ma.Int(dump_only=True) class Meta: model = Tag sqla_session = db.session load_instance = True
class MealSchema(ma.SQLAlchemyAutoSchema): id = ma.Int(dump_only=True) time_created = fields.DateTime(dump_only=True) time_updated = fields.DateTime(dump_only=True) household_id = fields.Integer(required=True) recipes = fields.List( fields.Nested( RecipeSchema, only=( "id", "name", ), ) ) # recipes = fields.Pluck("self", "id", many=True) nutrition = fields.Raw(dump_only=True) date = fields.Date(required=True) def load(self, data, *args, **kwargs): # data = [ # {"recipes": super().load(item)} if isinstance() else item # for item in data # ] return super().load(data, *args, **kwargs) class Meta: model = Meal ssqla_session = db.session load_instance = True ordered = True
class RecipeTypeSchema(ma.SQLAlchemyAutoSchema): id = ma.Int(dump_only=True) class Meta: model = DishType sqla_session = db.session load_instance = True
class DishTypeSchema(ma.SQLAlchemyAutoSchema): id = ma.Int(dump_only=True) time_created = fields.DateTime(dump_only=True) time_updated = fields.DateTime(dump_only=True) name = fields.String() class Meta: model = DishType sqla_session = db.session load_instance = True
class IngredientSchema(ma.SQLAlchemyAutoSchema): id = ma.Int(dump_only=True) time_created = fields.DateTime(dump_only=True) time_updated = fields.DateTime(dump_only=True) class Meta: model = Ingredient sqla_session = db.session load_instance = True exclude = ("spoonacular_id", ) ordered = True
class HouseholdSchema(ma.SQLAlchemyAutoSchema): id = ma.Int(dump_only=True) recipes = fields.List( fields.Nested( RecipeSchema, only=("name", "image_url", "rating", "id", "time_created", "time_updated"), ) ) users = fields.List(fields.Nested(UserSchema, only=("id", "username", "active"))) class Meta: model = Household sqla_session = db.session load_instance = True exclude = ("meals",)
class CuisineSchema(ma.SQLAlchemyAutoSchema, RestXSchema): id = ma.Int(dump_only=True) class Meta: model = Cuisine sqla_session = db.session load_instance = True @staticmethod def get_restx_model() -> Model: return Model( "Cuisine Model", { "id": fields.Integer(readonly=True), "time_created": fields.DateTime(readonly=True), "time_updated": fields.DateTime(readonly=True), "name": fields.String(required=True), }, )
class SchemaWithIdMixin: id = ma.Int(dump_only=True)
class RecipeSchema(ma.SQLAlchemyAutoSchema, RestXSchema): id = ma.Int(dump_only=True) time_created = ma.auto_field(dump_only=True) time_updated = ma.auto_field(dump_only=True) name = ma.auto_field() recipe_url = ma.auto_field() thumbnail_url = ma.auto_field() image_url = ma.auto_field() author = ma.auto_field() rating = ma.auto_field() servings = ma.auto_field() prep_time = ma.auto_field() cook_time = ma.auto_field() source_name = ma.auto_field() diets = fields.Function(lambda recipe: [dt.name for dt in recipe.diets]) dish_types = fields.Function( lambda recipe: [dt.name for dt in recipe.dish_types]) cuisines = fields.Function( lambda recipe: [c.name for c in recipe.cuisines]) ingredients = fields.List( fields.Nested( RecipeIngredientSchema, only=( "amount", "unit", "us_unit_short", "us_unit_long", "us_amount", "metric_unit_short", "metric_unit_long", "metric_amount", "original_string", ), )) _links = ma.Hyperlinks({ "self": ma.URLFor("api_v1.get_recipe", values=dict(recipe_id="<id>")), # "collection": ma.URLFor("api_v1.list_recipes"), }) @staticmethod def get_restx_model() -> Model: return Model( "recipe_model", { "id": restxFields.Integer(readonly=True), "time_created": restxFields.DateTime(readonly=True), "time_updated": restxFields.DateTime(readonly=True), "name": restxFields.String(), "macros": restxFields.Raw(readonly=True), "directions": restxFields.List(restxFields.String), "image_url": restxFields.String(), "recipe_url": restxFields.String(), "thumbnail_url": restxFields.String(), "author": restxFields.String(), "servings": restxFields.Integer(), "rating": restxFields.Integer(), "prep_time": restxFields.Integer(), "cook_time": restxFields.Integer(), "source_name": restxFields.String(), "diets": restxFields.List(restxFields.String), "dish_types": restxFields.List(restxFields.String), "tags": restxFields.List(restxFields.String), "cuisines": restxFields.List(restxFields.String), "ingredients": restxFields.List(restxFields.Raw), }, ) class Meta: model = Recipe sqla_session = db.session load_instance = True ordered = True fields = ( "id", "time_created", "time_updated", "name", "macros", "directions", "image_url", "recipe_url", "thumbnail_url", "author", "servings", "rating", "prep_time", "cook_time", "source_name", "diets", "dish_types", "tags", "cuisines", "ingredients", )
class UserSchema(ma.SQLAlchemyAutoSchema, RestXSchema): id = ma.Int(dump_only=True) time_created = fields.DateTime(dump_only=True) time_updated = fields.DateTime(dump_only=True) username = ma.String(required=True) email = fields.Email(required=True) active = fields.Boolean() first_name = fields.String() last_name = fields.String() birthday = fields.Date() height_inches = fields.Float() weight_lbs = fields.Float() gender = fields.String(validate=validate.OneOf(["male", "female"])) password = ma.String(load_only=True, required=True) household = fields.Nested( "HouseholdSchema", only=( "id", "name", ), ) household_id = fields.Integer() roles = fields.List( fields.Nested( "RoleSchema", only=( "id", "name", ), ) ) @staticmethod def get_restx_model() -> Model: return Model( "User Model", { "id": restxFields.Integer(), "time_created": restxFields.DateTime(), "time_updated": restxFields.DateTime(), "username": restxFields.String(), "email": restxFields.String(), "active": restxFields.Boolean(), "first_name": restxFields.String(), "last_name": restxFields.String(), "birthday": restxFields.Date(), "height_inches": restxFields.Float(), "weight_lbs": restxFields.Float(), "gender": restxFields.String(), "household": restxFields.Raw(), "household_id": restxFields.Integer(), "roles": restxFields.List(restxFields.Raw()), }, ) class Meta: model = User sqla_session = db.session load_instance = True exclude = ("_password",) ordered = True