class Role(RequiredFields): __tablename__ = 'auth_role' pk = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(15), unique=True) type_ = db.Column(db.String(15)) is_admin = db.Column(db.Boolean)
class UserSearchData(RequiredFields): __tablename__ = 'user_searchdata' pk = db.Column(db.Integer, primary_key=True) search = db.Column(db.JSON) user = db.Column(db.Integer, db.ForeignKey('auth_user.pk')) User = db.relationship("User", backref="user_searchdata")
class Source(RequiredFields): __tablename__ = 'recipe_source' pk = db.Column(db.Integer, primary_key=True) name = db.Column(db.String, unique=True) base_url = db.Column(db.String(30), unique=True) recipes = db.relationship('Recipe', backref='recipe_source', lazy='dynamic')
class FavouriteRecipe(RequiredFields): __tablename__ = 'user_favouriterecipe' pk = db.Column(db.Integer, primary_key=True) user = db.Column(db.Integer, db.ForeignKey('auth_user.pk')) recipe = db.Column(db.Integer, db.ForeignKey('recipe_recipe.pk')) Recipes = db.relationship("Recipe", backref="user_favouriterecipe") User = db.relationship("User", backref="user_favourite_recipe")
class User(RequiredFields): __tablename__ = 'auth_user' pk = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(64), unique=True) social_id = db.Column(db.String(64), nullable=True, unique=True) password = db.Column(db.Binary(64)) first_name = db.Column(db.String) last_name = db.Column(db.String) date_of_birth = db.Column(db.Date) role = db.Column(db.Integer, db.ForeignKey('auth_role.pk')) Role = db.relationship("Role", backref="auth_user")
class RecipeImage(RequiredFields): __tablename__ = 'recipe_recipe_image' pk = db.Column(db.Integer, primary_key=True) photo_id = db.Column(db.String(25), nullable=False) farm_id = db.Column(db.String(10), nullable=False) server_id = db.Column(db.String(10), nullable=False) secret = db.Column(db.String(20), nullable=False) title = db.Column(db.String(100), nullable=True) relevance = db.Column(db.Float, nullable=True) recipe = db.Column(db.Integer, db.ForeignKey('recipe_recipe.pk')) # photo sizes from flickr: https://www.flickr.com/services/api/misc.urls.html photo_sizes = { 'small-square': 'sq', 'large-swaure': 'q', 'thumbnail': 't', 'small_240': 'm', 'small_320': 'n', 'medium_500': '', 'medium_640': 'z', 'medium_800': 'c', 'larger': 'b' } img_types = ('jpg', 'gif', 'png') url_formatter = "https://farm{0}.staticflickr.com/{1}/{2}_{3}.{4}" url_formatter_size = "https://farm{0}.staticflickr.com/{1}/{2}_{3}_{4}.{5}" def get_flickr_url(self, size='medium', img_type='jpg'): if img_type not in self.img_types: img_type = 'jpg' size_letter = self.photo_sizes.get(size) if not size_letter: return self.url_formatter.format(self.farm_id, self.server_id, self.photo_id, self.secret, img_type) else: return self.url_formatter_size.format(self.farm_id, self.server_id, self.photo_id, self.secret, size_letter, img_type)
class IngredientRecipe(RequiredFields): __tablename = 'recipe_ingredientrecipe' pk = db.Column(db.Integer, primary_key=True) ingredient = db.Column(db.Integer, db.ForeignKey('recipe_ingredient.pk')) recipe = db.Column(db.Integer, db.ForeignKey('recipe_recipe.pk')) ingredient_amount = db.Column(db.Float) amount_units = db.Column(db.String(25)) percent_amount = db.Column(db.Float) ingredient_modifier = db.Column( db.Integer, db.ForeignKey('recipe_ingredientmodifier.pk'), nullable=True) Ingredient = db.relationship("Ingredient", backref="recipe_ingredientrecipe", lazy="subquery") IngredientModifier = db.relationship("IngredientModifier", backref="recipe_ingredientmodifier", lazy='joined')
class Recipe(RequiredFields): __tablename__ = 'recipe_recipe' pk = db.Column(db.Integer, primary_key=True) url = db.Column(db.String, unique=True) title = db.Column(db.String) average_rating = db.Column(db.Float) lowest_rating = db.Column(db.Float) highest_rating = db.Column(db.Float) count_rating = db.Column(db.Integer) raw_data = db.Column(db.Text) recipe_ingredients_text = db.Column(db.Text) recipe_ingredients_modifier_text = db.Column(db.Text) source = db.Column(db.Integer, db.ForeignKey('recipe_source.pk')) reviews = db.relationship('Review', backref='recipe_recipe', lazy='dynamic') ingredient_recipes = db.relationship("IngredientRecipe", backref="recipe_recipe", lazy="dynamic") recipe_images = db.relationship("RecipeImage", backref="recipe_recipe", lazy="joined") # __table_args__ = ( # db.Index( # 'idx_fulltext_recipe_title', # RequiredFields.create_tsvector(title), # postgresql_using="gin" # ), # db.Index( # 'idx_fulltext_ingredients', # RequiredFields.create_tsvector(recipe_ingredients_text), # postgresql_using="gin" # ), # db.Index( # 'idx_fulltext_ingredient_modifiers', # RequiredFields.create_tsvector(recipe_ingredients_modifier_text), # postgresql_using="gin" # ), # ) @property def thumbnail(self): if self.recipe_images: return self.recipe_images[0].get_flickr_url(size="thumbnail") return '' @property def medium_img(self): if self.recipe_images: return self.recipe_images[0].get_flickr_url() return ''
class IngredientModifier(RequiredFields): __tablename__ = 'recipe_ingredientmodifier' pk = db.Column(db.Integer, primary_key=True) name = db.Column(db.String)
class Ingredient(RequiredFields): __tablename__ = 'recipe_ingredient' pk = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(200), unique=True)
class Review(RequiredFields): __tablename__ = 'recipe_review' pk = db.Column(db.Integer, primary_key=True) review_text = db.Column(db.String) review_rating = db.Column(db.Float) recipe = db.Column(db.Integer, db.ForeignKey('recipe_recipe.pk'))