Commit 29f85a36 authored by Willard's avatar Willard

Add models for favorites and reviews

parent 11c5748b
...@@ -2,14 +2,25 @@ from canteeneo import db ...@@ -2,14 +2,25 @@ from canteeneo import db
from werkzeug.security import generate_password_hash, check_password_hash from werkzeug.security import generate_password_hash, check_password_hash
import hashlib import hashlib
dish_favorites = db.Table('dish_favorites',
db.Column('dish_id', db.Integer, db.ForeignKey('dish.id')),
db.Column('user_id', db.Integer, db.ForeignKey('user.id'))
)
stall_favorites = db.Table('stall_favorites',
db.Column('stall_id', db.Integer, db.ForeignKey('stall.id')),
db.Column('user_id', db.Integer, db.ForeignKey('user.id'))
)
class Stall(db.Model): class Stall(db.Model):
id = db.Column(db.Integer, primary_key=True) id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(80)) name = db.Column(db.String(80))
description = db.Column(db.Text) description = db.Column(db.Text)
owner_id = db.Column(db.Integer, db.ForeignKey('owner.id')) owner_id = db.Column(db.Integer, db.ForeignKey('owner.id'))
location_id = db.Column(db.Integer, db.ForeignKey('location.id')) location_id = db.Column(db.Integer, db.ForeignKey('location.id'))
dishes = db.relationship('Dish', backref='stall', cascade='all,delete', lazy='dynamic') dishes = db.relationship('Dish', backref='stall', cascade='all,delete', lazy='dynamic')
favorites = db.relationship('Stall', secondary=stall_favorites, backref=db.backref('stall', lazy='dynamic'))
reviews = db.relationship('StallReview', backref='stall', lazy='dynamic')
def __init__(self, name, description, owner_id, location_id): def __init__(self, name, description, owner_id, location_id):
self.name = name self.name = name
...@@ -24,6 +35,8 @@ class Dish(db.Model): ...@@ -24,6 +35,8 @@ class Dish(db.Model):
price = db.Column(db.Float) price = db.Column(db.Float)
stall_id = db.Column(db.Integer, db.ForeignKey('stall.id')) stall_id = db.Column(db.Integer, db.ForeignKey('stall.id'))
image_path = db.Column(db.String(160), unique=True) image_path = db.Column(db.String(160), unique=True)
favorites = db.relationship('User', secondary=dish_favorites, backref=db.backref('dish', lazy='dynamic'))
reviews = db.relationship('DishReview', backref='dish', lazy='dynamic')
def __init__(self, name, description, price, stall, image_path): def __init__(self, name, description, price, stall, image_path):
self.name = name self.name = name
...@@ -47,9 +60,6 @@ class Owner(db.Model): ...@@ -47,9 +60,6 @@ class Owner(db.Model):
self.email = email self.email = email
self.set_password(password) self.set_password(password)
def __repr__(self):
return self.username
def set_password(self, password): def set_password(self, password):
self.password = generate_password_hash(password) self.password = generate_password_hash(password)
...@@ -73,4 +83,55 @@ class Location(db.Model): ...@@ -73,4 +83,55 @@ class Location(db.Model):
name = db.Column(db.String(80)) name = db.Column(db.String(80))
def __init__(self, name): def __init__(self, name):
self.name = name self.name = name
\ No newline at end of file
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(80))
email = db.Column(db.String(80))
password = db.Column(db.String(160))
dish_favorites = db.relationship('Dish', secondary=dish_favorites, backref=db.backref('user', lazy='dynamic'))
stall_favorites = db.relationship('Stall', secondary=stall_favorites, backref=db.backref('user', lazy='dynamic'))
dish_reviews = db.relationship('DishReview', backref='user', lazy='dynamic')
stall_reviews = db.relationship('StallReview', backref='user', lazy='dynamic')
def __init__(self, name, email, password):
self.name = name
self.email = email
self.set_password(password)
def set_password(self, password):
self.password = generate_password_hash(password)
def check_password(self, password):
return check_password_hash(self.password, password)
class DishReview(db.Model):
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String(80))
body = db.Column(db.Text)
rating = db.Column(db.Integer)
user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
dish_id = db.Column(db.Integer, db.ForeignKey('dish.id'))
def __init__(self, title, body, rating, user_id, dish_id):
self.title = title
self.body = body
self.rating = rating
self.user_id = user_id
self.dish_id = dish_id
class StallReview(db.Model):
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String(80))
body = db.Column(db.Text)
rating = db.Column(db.Integer)
user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
stall_id = db.Column(db.Integer, db.ForeignKey('stall.id'))
def __init__(self, title, body, rating, user_id, stall_id):
self.title = title
self.body = body
self.rating = rating
self.user_id = user_id
self.stall_id = stall_id
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment