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 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 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)
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 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 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 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 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