Ejemplo n.º 1
0
class Rating(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    rater = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
    recipe = db.Column(db.Integer, db.ForeignKey('recipe.id'), nullable=False)
    rating = db.Column(db.Integer, nullable=False)

    def __repr__(self):
        return '<Rating %r>' % self.id

    def to_json(self):
        return {'id':self.id, 'rater':self.rater,
                'recipe':self.recipe, 'rating':self.rating}
Ejemplo n.º 2
0
class Vote(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    voter = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
    comment = db.Column(db.Integer, db.ForeignKey('comment.id'), nullable=False)
    good = db.Column(db.Boolean, nullable=False)

    def __repr__(self):
        return '<Vote %r>' % self.id

    def to_json(self):
        return {'id':self.id, 'voter':self.voter,
                'comment':self.comment, 'good':self.good}
Ejemplo n.º 3
0
class Category(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(64), unique=True, nullable=False)

    category_recipes = db.relationship("CategoryRecipe",
                                       backref="Category",
                                       cascade="all, delete-orphan",
                                       lazy="noload")

    def __repr__(self):
        return '<Category %r>' % self.name

    def to_json(self):
        return {'id': self.id, 'name': self.name}
Ejemplo n.º 4
0
class Role(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(64), unique=True)
    description = db.Column(db.String(128))

    def __repr__(self):
        return '<Role %r>' % self.name

    def to_json(self):
        return {
            'id': self.id,
            'name': self.name,
            'description': self.description
        }
Ejemplo n.º 5
0
class CategoryRecipe(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    recipe = db.Column(db.Integer, db.ForeignKey('recipe.id'))
    category = db.Column(db.Integer, db.ForeignKey('category.id'))

    def __repr__(self):
        return '<CategoryRecipe %r>' % self.id

    def to_json(self):
        return {
            'id': self.id,
            'recipe': self.recipe,
            'category': self.category
        }
Ejemplo n.º 6
0
class Comment(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    author = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
    recipe = db.Column(db.Integer, db.ForeignKey('recipe.id'), nullable=False)
    text = db.Column(db.String(512), nullable=False)
    popularity = db.Column(db.Integer)
    voters = db.Column(db.Integer)
    published = db.Column(db.DateTime())

    votes = db.relationship("Vote", backref="Comment", 
                            cascade="all, delete-orphan", lazy="noload")

    def __repr__(self):
        return '<Comment %r>' % self.id

    def to_json(self):
        return {'id':self.id, 'author':self.author,
                'recipe':self.recipe, 'text':self.text,
                'popularity':self.popularity, 'voters':self.voters,
                'published':self.published.strftime('%Y-%m-%d %H:%M:%S')}

    def gets_vote(self, vote_object):
        self.votes.append(vote_object)
        self.voters += 1
        self.popularity += 1 if vote_object.good else -1

    def vote_changes(self, vote_object, new_vote):
        if vote_object.good != new_vote:
            self.popularity += 2 if new_vote else -2
        vote_object.good = new_vote

    def recalculate_popularity(self):
        """Worst case method to fall back on if needed"""
        self.voters = 0
        for x in self.votes:
            self.voters += 1
            if x.good:
                self.popularity += 1
            else:
                self.popularity -= 1
Ejemplo n.º 7
0
class Roles_Users(db.Model):
    id = db.Column(db.Integer, db.ForeignKey())
Ejemplo n.º 8
0
from casualchef.dbapp import app as app
from casualchef.dbapp import db as db

roles_users = db.Table(
    'roles_users', db.Column('user_id', db.Integer, db.ForeignKey('user.id')),
    db.Column('role_id', db.Integer, db.ForeignKey('role.id')))

from casualchef.models.role import Role as Role
from casualchef.models.user import User as User
from casualchef.models.recipe import Recipe as Recipe
from casualchef.models.category_recipe import CategoryRecipe as CategoryRecipe
from casualchef.models.category import Category as Category
from casualchef.models.rating import Rating as Rating
from casualchef.models.vote import Vote as Vote
from casualchef.models.comment import Comment as Comment
Ejemplo n.º 9
0
class Recipe(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    author = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
    name = db.Column(db.String(64), nullable=False)
    description = db.Column(db.String(256), nullable=False)
    ingredients = db.Column(db.String(256), nullable=False)
    guide = db.Column(db.String(2048), nullable=False)
    picture_url = db.Column(db.String(256), nullable=False)
    rating = db.Column(db.Integer)
    raters = db.Column(db.Integer)
    published = db.Column(db.DateTime())

    categories = db.relationship("CategoryRecipe",
                                 backref="Recipe",
                                 cascade="all, delete-orphan",
                                 lazy="joined")
    ratings = db.relationship("Rating",
                              backref="Recipe",
                              cascade="all, delete-orphan",
                              lazy="noload")
    comments = db.relationship("Comment",
                               backref="Recipe",
                               cascade="all, delete-orphan",
                               lazy="noload")

    def gets_rating(self, rating_object):
        self.ratings.append(rating_object)
        self.rating += rating_object.rating
        self.raters += 1

    def rating_changes(self, rating_object, new_rating):
        self.rating -= rating_object.rating
        rating_object.rating = new_rating
        rating_object.modified = True
        self.rating += new_rating

    def get_average_rating(self):
        if self.raters == 0:
            return self.raters
        return self.rating / self.raters

    def recalculate_rating(self):
        """Worst case method to fall back on if needed"""
        self.rating = sum([x.rating for x in self.ratings])

    def __repr__(self):
        return '<Recipe %r>' % self.name

    def to_json(self):
        return {
            'id': self.id,
            'author': self.author,
            'name': self.name,
            'description': self.description,
            'ingredients': self.ingredients,
            'guide': self.guide,
            'picture_url': self.picture_url,
            'rating': self.rating,
            'raters': self.raters,
            'published': self.published.strftime('%Y-%m-%d %H:%M:%S'),
            'categories': [c.category for c in self.categories]
        }