Beispiel #1
0
class Recipe(db.Model, CRUDModel):
    __tablename__ = 'recipes'

    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String)
    rating = db.Column(db.Integer)
    date_added = db.Column(db.String)
    steps = db.relationship('Step', backref='recipe', lazy='dynamic')
    notes = db.relationship('Note', backref='recipe', lazy='dynamic')
    recipeingredients = db.relationship('RecipeIngredient',
                                        backref='recipe',
                                        lazy='dynamic')

    def __repr__(self):
        return '<Recipe {:d} {}>'.format(self.id, self.name)

    def __str__(self):
        return self.name

    def update_from(self, new_obj):
        if new_obj.name:
            self.name = new_obj.name
        if new_obj.rating:
            self.rating = new_obj.rating
        if new_obj.date_added:
            self.date_added = new_obj.date_added
        self.save()
Beispiel #2
0
class Cookbook(db.Model):
    __tablename__ = "cookbooks"

    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(30), nullable=False, unique=True)
    chef_id = db.Column(db.Integer, db.ForeignKey("chefs.id"))

    @classmethod
    def get_by_name(cls, name):
        return cls.query.filter_by(name=name).first()

    @classmethod
    def get_all(cls):
        return cls.query.all()

    @classmethod
    def add(cls, name, recipes=None):
        if recipes is None:
            recipes = []

        recipe_entities = [Recipe.get_by_name(recipe) for recipe in recipes]

        cookbook = cls(name=name, recipes=recipe_entities)
        db.session.add(cookbook)
        db.session.commit()

        return cookbook
Beispiel #3
0
class User(db.Model, UserMixin):

    # Create a table in the db
    __tablename__ = 'users'

    id = db.Column(db.Integer, primary_key=True)
    profile_image = db.Column(db.String(20),
                              nullable=False,
                              default='default_profile.png')
    email = db.Column(db.String(64), unique=True, index=True)
    username = db.Column(db.String(64), unique=True, index=True)
    password_hash = db.Column(db.String(128))
    # This connects BlogPosts to a User Author.
    posts = db.relationship('BlogPost', backref='author', lazy=True)

    def __init__(self, email, username, password):
        self.email = email
        self.username = username
        self.password_hash = generate_password_hash(password)

    def check_password(self, password):
        return check_password_hash(self.password_hash, password)

    def __str__(self):
        return "Username: %s" % self.username
Beispiel #4
0
class Product(db.Model):
    __tablename__ = "products"

    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(128), nullable=False, unique=True)
    shop_link = db.Column(db.String(256), nullable=False)

    recipes = db.relationship(
        "Recipe",
        secondary=recipe_product_association,
        backref=db.backref("products"),
    )

    @classmethod
    def get_by_name(cls, name):
        return cls.query.filter_by(name=name).first()

    @classmethod
    def get_all(cls):
        return cls.query.all()

    @classmethod
    def add(cls, name, shop_link=None):
        product = cls(name=name, shop_link=shop_link)
        db.session.add(product)
        db.session.commit()

        return product
Beispiel #5
0
class User(db.Model, UserMixin):
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(15), unique=True, nullable=False)
    email = db.Column(db.String(120), unique=True, nullable=False)
    password = db.Column(db.String(60), nullable=False)

    recipes = db.relationship('Recipe', backref='author', lazy=True)

    def __repr__(self):
        return f"User('{self.username}', '{self.email}')"
Beispiel #6
0
class Recipe(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    title = db.Column(db.String(100), nullable=False)
    cuisine = db.Column(db.String(100), nullable=False, default='')
    ingredients = db.Column(db.Text, nullable=False, default='')
    date = db.Column(db.DateTime, nullable=False, default=datetime.utcnow)
    description = db.Column(db.Text, nullable=False, default='')
    preparation = db.Column(db.Text, nullable=False, default='')
    picture = db.Column(db.String(300), nullable=False, default='https://dummyimage.com/200')
    requirement = db.Column(db.String(150), nullable=False, default='')

    user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False) # foreign key to the author of the recipe

    def __repr__(self):
        return f"Recipe('{self.title}', '{self.date}')"
Beispiel #7
0
class Chef(db.Model):
    __tablename__ = "chefs"

    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(30), nullable=False, unique=True)
    password_hash = db.Column(db.String(128))

    cookbooks = db.relationship("Cookbook", backref="chef")

    @classmethod
    def get_all(cls):
        return cls.query.all()

    @classmethod
    def get_by_name(cls, name):
        return cls.query.filter_by(name=name).first()
Beispiel #8
0
class Recipe(db.Model):
    __tablename__ = "recipes"

    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(128), nullable=False, unique=True)
    instruction = db.Column(db.String(2048), nullable=False)

    cookbooks = db.relationship(
        "Cookbook",
        secondary=cookbook_recipe_association,
        backref=db.backref("recipes"),
    )

    @classmethod
    def add(cls, name, instruction, categories=None, products=None):
        if categories is None:
            categories = []
        if products is None:
            products = []

        product_entities = [
            Product.get_by_name(product_name) for product_name in products
        ]
        category_entities = [
            Category.get_by_name(category_name) for category_name in categories
        ]

        recipe = cls(
            name=name,
            instruction=instruction,
            categories=category_entities,
            products=product_entities,
        )

        db.session.add(recipe)
        db.session.commit()

        return recipe

    @classmethod
    def get_by_name(cls, name):
        return cls.query.filter_by(name=name).first()

    @classmethod
    def get_all(cls):
        return cls.query.all()
Beispiel #9
0
class BlogPost(db.Model):
    # Setup the relationship to the User table
    users = db.relationship(User)

    # Model for the Blog Posts on Website
    id = db.Column(db.Integer, primary_key=True)
    user_id = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=False)
    date = db.Column(db.DateTime, nullable=False, default=datetime.utcnow)
    title = db.Column(db.String(140), nullable=False)
    text = db.Column(db.Text, nullable=False)

    def __init__(self, title, text, user_id):
        self.title = title
        self.text = text
        self.user_id = user_id

    def __str__(self):
        return "Recipe Id: %s --- Title: %d" % (self.id, self.title)
Beispiel #10
0
class Note(db.Model, CRUDModel):
    __tablename__ = 'notes'

    id = db.Column(db.Integer, primary_key=True)
    note = db.Column(db.String)
    recipe_id = db.Column(db.Integer, db.ForeignKey('recipes.id'))

    def __repr__(self):
        return '<Note {} {}>'.format(self.id, self.note)

    def __str__(self):
        return self.note

    def update_from(self, new_obj):
        self.note = new_obj.note
        self.recipe_id = new_obj.recipe_id

        self.save()
Beispiel #11
0
class Step(db.Model, CRUDModel):
    __tablename__ = 'steps'

    id = db.Column(db.Integer, primary_key=True)
    order = db.Column(db.Integer)
    step = db.Column(db.String)
    recipe_id = db.Column(db.Integer, db.ForeignKey('recipes.id'))

    def __repr__(self):
        return '<Step {} {}>'.format(self.id, self.step)

    def __str__(self):
        return self.step

    def update_from(self, new_obj):
        self.step = new_obj.step
        self.order = new_obj.order
        self.recipe_id = new_obj.recipe_id
        self.save()
Beispiel #12
0
class Unit(db.Model, CRUDModel):
    __tablename__ = 'units'

    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String, unique=True)
    recipeingredients = db.relationship('RecipeIngredient',
                                        backref='unit',
                                        lazy='dynamic')

    def __repr__(self):
        return '<Unit {} {}>'.format(self.id, self.name)

    def __str__(self):
        return self.name

    def update_from(self, new_obj):
        if new_obj.name:
            self.name = new_obj.name
        self.save()
Beispiel #13
0
class Department(db.Model, CRUDModel):
    __tablename__ = 'departments'

    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String)
    ingredients = db.relationship('Ingredient',
                                  backref='department',
                                  lazy='dynamic')

    def __repr__(self):
        return '<Department {} {}>'.format(self.id, self.name)

    def __str__(self):
        return self.name

    def update_from(self, new_obj):
        app.logger.debug('Updating {} from {}'.format(self, new_obj))
        self.name = new_obj.name
        self.save()
Beispiel #14
0
class Category(db.Model):
    __tablename__ = "categories"

    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(30), nullable=False, unique=True)

    recipes = db.relationship(
        "Recipe",
        secondary=recipe_category_association,
        backref=db.backref("categories"),
    )

    @classmethod
    def get_by_name(cls, name):
        return cls.query.filter_by(name=name).first()

    @classmethod
    def get_all(cls):
        return cls.query.all()
Beispiel #15
0
class Ingredient(db.Model, CRUDModel):
    __tablename__ = 'ingredients'

    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String, unique=True)
    department_id = db.Column(db.Integer, db.ForeignKey('departments.id'))
    recipeingredients = db.relationship('RecipeIngredient',
                                        backref='ingredient',
                                        lazy='dynamic')

    def __repr__(self):
        return '<Ingredient {} {}>'.format(self.id, self.name)

    def __str__(self):
        return self.name

    def update_from(self, new_obj):
        if new_obj.name:
            self.name = new_obj.name
        if new_obj.department:
            self.department = new_obj.department
        self.save()
Beispiel #16
0
class RecipeIngredient(db.Model, CRUDModel):
    __tablename__ = 'recipeingredients'

    id = db.Column(db.Integer, primary_key=True)
    recipe_id = db.Column(db.Integer, db.ForeignKey('recipes.id'))
    ingredient_id = db.Column(db.Integer, db.ForeignKey('ingredients.id'))
    qty = db.Column(db.Float)
    unit_id = db.Column(db.Integer, db.ForeignKey('units.id'))
    preparation = db.Column(db.String)

    # put a column for the ingredient heading here?
    # in this example, oil and flour would have ingredient grouping "roux" and
    # bell pepper... would have ingredient_grouping "gumbo"
    # if no groupings field would be blank
    # allow for links to subrecipes on our site?
    # Roux
    # 1 cup oil
    # 1 cup of flour
    # Gumbo
    # 1 bell pepper
    #

    # try to emulate Modernist Cuisine Style Recipes?
    # give each recipeingredient and step and group number
    # subrecipe wouldn't be important anymore
    #
    # 1 cup oil                                 Stir Together
    # 1 cup flour                           Cook until desired color
    # _____________________________________________________________
    # 1 Onion                                   Chop vegetables
    # 1 bell pepper

    def __repr__(self):
        return '<RecipeIngredient {} {} {}>'.format(self.qty, self.unit,
                                                    self.ingredient)

    def __str__(self):
        return '<RecipeIngredient {} {} {}>'.format(self.qty, self.unit,
                                                    self.ingredient)
Beispiel #17
0
    def add(cls, name, recipes=None):
        if recipes is None:
            recipes = []

        recipe_entities = [Recipe.get_by_name(recipe) for recipe in recipes]

        cookbook = cls(name=name, recipes=recipe_entities)
        db.session.add(cookbook)
        db.session.commit()

        return cookbook


cookbook_recipe_association = db.Table(
    "cookbook_recipe_association",
    db.Column("cookbook_id", db.Integer, db.ForeignKey("cookbooks.id")),
    db.Column("recipe_id", db.Integer, db.ForeignKey("recipes.id")),
)

recipe_product_association = db.Table(
    "recipe_product_association",
    db.Column("recipe_id", db.Integer, db.ForeignKey("recipes.id")),
    db.Column("product_id", db.Integer, db.ForeignKey("products.id")),
)

recipe_category_association = db.Table(
    "recipe_category_association",
    db.Column("recipe_id", db.Integer, db.ForeignKey("recipes.id")),
    db.Column("category_id", db.Integer, db.ForeignKey("categories.id")),
)