class Farm(Base): __table_args__ = {'extend_existing': True} __tablename__ = 'farms' farm_id = db.Column(db.Integer, autoincrement=True, primary_key=True) name = db.Column(db.String(75)) ## NOT NULL email_address = db.Column(db.String(75), unique=True) phone_number = db.Column(db.String(20)) street_address = db.Column(db.Text) ## NOT NULL city = db.Column(db.String(40)) ## NOT NULL state = db.Column(db.String(2)) ## NOT NULL zip_code = zip_code = db.Column(db.Integer) created_at = db.Column(db.DateTime(timezone=True), nullable=False, default=get_now()) updated_at = db.Column(db.DateTime(timezone=True), default=get_now(), onupdate=get_now()) farmers = db.relationship('Farmer', backref='farms') def __init__(self, name, email_address, phone_number, street_address, city, state, zip_code): self.name = name self.email_address = email_address self.phone_number = phone_number self.street_address = street_address self.city = city self.state = state self.zip_code = zip_code def __repr__(self): return str('Farm: ' + str(self.farm_id) + ' ' + self.name)
class MarketDay(Base): __table_args__ = {'extend_existing': True} __tablename__ = 'market_days' market_day_id = db.Column(db.Integer, autoincrement=True, primary_key=True) date = db.Column(db.Date) ## NOT NULL day = db.Column(postgresql.ENUM(Day)) ## NOT NULL opens = db.Column(db.Time(timezone=True)) ## NOT NULL closes = db.Column(db.Time(timezone=True)) ## NOT NULL created_at = db.Column(db.DateTime(timezone=True), nullable=False, default=get_now()) updated_at = db.Column(db.DateTime(timezone=True), default=get_now(), onupdate=get_now()) market_id = db.Column(db.Integer, db.ForeignKey('markets.market_id')) def __init__(self, date, day, opens, closes): """ self.date = date self.day = day self.opens = opens self.closes = closes """ def __repr__(self): return str('MarketDay: ' + str(self.market_day_id) + ' ' + str(self.market_id))
class Category( Base ): __table_args__ = { 'extend_existing': True } __tablename__ = 'categories' category_id = db.Column( db.Integer, autoincrement = True, primary_key = True ) group = db.Column( db.String( 30 ), nullable = False ) category = db.Column( db.String( 30 ), nullable = False ) created_at = db.Column( db.DateTime( timezone = True ), nullable = False, default = get_now( ) ) updated_at = db.Column( db.DateTime( timezone = True ), default = get_now( ), onupdate = get_now( ) ) products = db.relationship( 'Product', backref = 'categories' ) def __init__( self, category ): self.category = category def __repr__( self ): return str( 'Category: ' + str( self.category_id ) + ' ' + self.group + ' ' + self.category )
class Product( Base ): __table_args__ = { 'extend_existing': True } __tablename__ = 'products' product_id = db.Column( db.Integer, autoincrement = True, primary_key = True ) name = db.Column( db.String( 40 ), nullable = False ) created_at = db.Column( db.DateTime( timezone = True ), nullable = False, default = get_now( ) ) updated_at = db.Column( db.DateTime( timezone = True ), default = get_now( ), onupdate = get_now( ) ) category_id = db.Column( db.Integer, db.ForeignKey( 'categories.category_id' ) ) markets = db.relationship( 'MarketDay', secondary = 'product_days', backref = 'products' ) ingredients = db.relationship( 'Ingredient', backref = 'products' ) farmers = db.relationship( 'FarmerProduct', backref = 'wares' ) def __init__( self, name ): self.name = name def __repr__( self ): return str( 'Product: ' + str( self.product_id ) + ' ' + self.name + ' ' + str( self.category_id ) )
class Recipe(Base): __table_args__ = {'extend_existing': True} __tablename__ = 'recipes' recipe_id = db.Column(db.Integer, autoincrement=True, primary_key=True) name = db.Column(db.String(50), nullable=False, unique=True) ## Refactoring into its own normalized, alternative table ''' recipe_directions = db.Column( db.Text ) ''' created_at = db.Column(db.DateTime(timezone=True), nullable=False, default=get_now()) updated_at = db.Column(db.DateTime(timezone=True), default=get_now(), onupdate=get_now()) ingredients = db.relationship('Ingredient', backref='recipes') directions = db.relationship('Direction', backref='recipes') def __init__(self, name, recipe_directions): self.name = name ## Placed into its own table for optimal normalization ''' self.recipe_directions = recipe_directions ''' def __repr__(self): return '<Recipe %r' % self.recipe_name
class Direction(Base): __table_args__ = {'extend_existing': True} __tablename__ = 'directions' direction_id = db.Column(db.Integer, autoincrement=True, primary_key=True) order = db.Column(db.SmallInteger) ## NOT NULL summary = db.Column(db.String(200)) instruction = db.Column(db.Text) ## NOT NULL created_at = db.Column(db.DateTime(timezone=True), nullable=False, default=get_now()) updated_at = db.Column(db.DateTime(timezone=True), default=get_now(), onupdate=get_now()) recipe_id = db.Column(db.Integer, db.ForeignKey('recipes.recipe_id')) def __init__(self, order, instruction): self.order = order self.instruction = instruction def __repr__(self): return '<Direction %r' % self.instruction
class FarmerProduct( Base ): __table_args__ = { 'extend_existing': True } __tablename__ = 'farmer_products' farmer_id = db.Column( db.Integer, db.ForeignKey( 'farmers.farmer_id' ), primary_key = True ) product_id = db.Column( db.Integer, db.ForeignKey( 'products.product_id' ), primary_key = True ) price = db.Column( postgresql.MONEY ) ## NOT NULL available = db.Column( db.Boolean ) ## NOT NULL created_at = db.Column( db.DateTime( timezone = True ), nullable = False, default = get_now( ) ) updated_at = db.Column( db.DateTime( timezone = True ), default = get_now( ), onupdate = get_now( ) ) farmers = db.relationship( 'Farmer', backref = 'wares' ) products = db.relationship( 'Product', backref = 'vendors' ) def __init__( self, farmer_id, product_id ): """ price, available """ self.farmer_id = farmer_id self.product_id = product_id """ self.price = price self.available = available """ def __repr__( self ): return str( 'FarmerProduct: ' + str( self.farmer_id ) + ' ' + str( self.product_id ) )
class Farmer(Base): __table_args__ = {'extend_existing': True} __tablename__ = 'farmers' farmer_id = db.Column(db.Integer, autoincrement=True, primary_key=True) first_name = db.Column(db.String(40), nullable=False) last_name = db.Column(db.String(40), nullable=False) ## Potential columns for login and registration authentication ''' gender = db.Column( db.String( 20 ) ) birthday = db.Column( db.Date ) email_address = db.Column( db.String( 75 ), unique = True ) password = db.Column( db.String( 50 ) ) phone_number = db.Column( db.String( 20 ) ) street_address = db.Column( db.Text ) city = db.Column( db.String( 40 ) ) state = db.Column( db.String( 2 ) ) zip_code = zip_code = db.Column( db.Integer( 5 ) ) ''' joined_at = db.Column(db.DateTime(timezone=True), nullable=False, default=get_now()) updated_at = db.Column(db.DateTime(timezone=True), default=get_now(), onupdate=get_now()) farm_id = db.Column(db.Integer, db.ForeignKey('farms.farm_id')) markets = db.relationship('MarketDay', secondary='farmer_days', backref='farmers') products = db.relationship('FarmerProduct', backref='vendors') def __init__(self, first_name, last_name): self.first_name = first_name self.last_name = last_name def __repr__(self): return str('Farmer: ' + str(self.farmer_id) + ' ' + self.first_name + ' ' + self.last_name)
class Ingredient(Base): __table_args__ = {'extend_existing': True} __tablename__ = 'ingredients' ingredient_id = db.Column(db.Integer, autoincrement=True, primary_key=True) ingredient_quantity = db.Column(db.String(255)) created_at = db.Column(db.DateTime(timezone=True), nullable=False, default=get_now()) updated_at = db.Column(db.DateTime(timezone=True), default=get_now(), onupdate=get_now()) recipe_id = db.Column(db.Integer, db.ForeignKey('recipes.recipe_id')) product_id = db.Column(db.Integer, db.ForeignKey('products.product_id')) def __init__(self, ingredient_quantity): ## Old names for previous database model backrefs ''' owner_recipe, owner_product ''' self.ingredient_quantity = ingredient_quantity ''' self.owner_recipe = owner_recipe self.owner_product = owner_product ''' def __repr__(self): return '<Ingredient %r' % self.ingredient_quantity
class Market(Base): __table_args__ = {'extend_existing': True} __tablename__ = 'markets' market_id = db.Column(db.Integer, autoincrement=True, primary_key=True) name = db.Column(db.String(75), nullable=False) url = db.Column(db.Text, nullable=False, unique=True) website = db.Column(db.Text, unique=True) email_address = db.Column(db.String(75), unique=True) phone_number = db.Column(db.String(20)) address_1 = db.Column(db.Text) ## NOT NULL address_2 = db.Column(db.Text) city = db.Column(db.String(40), nullable=False) state = db.Column(db.String(2), nullable=False) zip_code = db.Column(db.Integer, nullable=False) created_at = db.Column(db.DateTime(timezone=True), nullable=False, default=get_now()) updated_at = db.Column(db.DateTime(timezone=True), default=get_now(), onupdate=get_now()) days = db.relationship('MarketDay', backref='markets') def __init__(self, name, url, address_1, city, state, zip_code): ''' website, email_address, phone_number, address_2, ''' self.name = name self.url = url ''' self.website = website self.email_address = email_address self.phone_number = phone_number ''' self.address_1 = address_1 ''' self.address_2 = address_2 ''' self.city = city self.state = state self.zip_code = zip_code def __repr__(self): return str('Market: ' + self.market_id + ' ' + self.name + ' ' + self.url)
def __repr__( self ): return str( 'FarmerProduct: ' + str( self.farmer_id ) + ' ' + str( self.product_id ) ) class Category( Base ): __table_args__ = { 'extend_existing': True } __tablename__ = 'categories' category_id = db.Column( db.Integer, autoincrement = True, primary_key = True ) group = db.Column( db.String( 30 ), nullable = False ) category = db.Column( db.String( 30 ), nullable = False ) created_at = db.Column( db.DateTime( timezone = True ), nullable = False, default = get_now( ) ) updated_at = db.Column( db.DateTime( timezone = True ), default = get_now( ), onupdate = get_now( ) ) products = db.relationship( 'Product', backref = 'categories' ) def __init__( self, category ): self.category = category def __repr__( self ): return str( 'Category: ' + str( self.category_id ) + ' ' + self.group + ' ' + self.category ) ## A many-to-many table for which day products are sold product_days = db.Table( 'product_days', Base.metadata, db.Column( 'product_id', db.Integer, db.ForeignKey( 'products.product_id' ), primary_key = True ), db.Column( 'market_day_id', db.Integer, db.ForeignKey( 'market_days.market_day_id' ), primary_key = True ) )
default=get_now(), onupdate=get_now()) farmers = db.relationship('Farmer', backref='farms') def __init__(self, name, email_address, phone_number, street_address, city, state, zip_code): self.name = name self.email_address = email_address self.phone_number = phone_number self.street_address = street_address self.city = city self.state = state self.zip_code = zip_code def __repr__(self): return str('Farm: ' + str(self.farm_id) + ' ' + self.name) ## Farmers' days at market, a many-to-many db.relationship farmer_days = db.Table( 'farmer_days', Base.metadata, db.Column('farmer_id', db.Integer, db.ForeignKey('farmers.farmer_id'), primary_key=True), db.Column('market_day_id', db.Integer, db.ForeignKey('market_days.market_day_id'), primary_key=True))