示例#1
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
示例#2
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
示例#3
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}')"
示例#4
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}')"
示例#5
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()
示例#6
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
示例#7
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()
示例#8
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)
示例#9
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()