Commit 3327c176 authored by Willard's avatar Willard

Add dish type to forms and models

parent 25e5399e
import os.path import os.path
from canteeneo import app, db from canteeneo import app, db
from canteeneo.models import Stall, Dish from canteeneo.models import Stall, Dish, DishType
from canteeneo.forms import DishRegisterForm from canteeneo.forms import DishRegisterForm
from canteeneo.views import flash_form_errors, stall_validate, dish_validate, FormView from canteeneo.views import flash_form_errors, stall_validate, dish_validate, FormView
...@@ -26,7 +26,9 @@ class NewDishView(FormView): ...@@ -26,7 +26,9 @@ class NewDishView(FormView):
def render_get(self, stall_id): def render_get(self, stall_id):
return render_template('newdish.html', form=self.get_form(), stall=self.stall) return render_template('newdish.html', form=self.get_form(), stall=self.stall)
def get_form(self): def get_form(self):
return DishRegisterForm() form = DishRegisterForm()
form.dish_type.choices = [(dish_type.id, dish_type.name) for dish_type in DishType.query.all()]
return form
class DishView(View): class DishView(View):
decorators = [login_required, dish_validate] decorators = [login_required, dish_validate]
......
...@@ -68,6 +68,7 @@ class DishRegisterForm(FlaskForm): ...@@ -68,6 +68,7 @@ class DishRegisterForm(FlaskForm):
description = TextAreaField('Description', [DataRequired()]) description = TextAreaField('Description', [DataRequired()])
image = FileField('Image') image = FileField('Image')
price = DecimalField('Price', [DataRequired()]) price = DecimalField('Price', [DataRequired()])
dish_type = SelectField('Type', coerce=int)
def validate(self, editing=False): def validate(self, editing=False):
if not FlaskForm.validate(self): if not FlaskForm.validate(self):
......
...@@ -33,6 +33,7 @@ class Dish(db.Model): ...@@ -33,6 +33,7 @@ class Dish(db.Model):
description = db.Column(db.Text) description = db.Column(db.Text)
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'))
type_id = db.Column(db.Integer, db.ForeignKey('dish_type.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')) favorites = db.relationship('User', secondary=dish_favorites, backref=db.backref('dish', lazy='dynamic'))
reviews = db.relationship('DishReview', backref='dish', lazy='dynamic') reviews = db.relationship('DishReview', backref='dish', lazy='dynamic')
...@@ -43,6 +44,14 @@ class Dish(db.Model): ...@@ -43,6 +44,14 @@ class Dish(db.Model):
self.price = price self.price = price
self.stall_id = stall_id self.stall_id = stall_id
self.image_path = image_path self.image_path = image_path
class DishType(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(80))
dishes = db.relationship('Dish', backref='type', lazy='dynamic')
def __init__(self, name):
self.name = name
class Owner(db.Model): class Owner(db.Model):
id = db.Column(db.Integer, primary_key=True) id = db.Column(db.Integer, primary_key=True)
...@@ -80,6 +89,7 @@ class Owner(db.Model): ...@@ -80,6 +89,7 @@ class Owner(db.Model):
class Location(db.Model): class Location(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))
stalls = db.relationship('Stall', backref='location', lazy='dynamic')
def __init__(self, name): def __init__(self, name):
self.name = name self.name = name
......
...@@ -19,6 +19,9 @@ ...@@ -19,6 +19,9 @@
<div class="input-row"> <div class="input-row">
{{ form.price.label(class_="field-label") }} {{ form.price(class_="text-field", type="number", style="width: 5em;") }} {{ form.price.label(class_="field-label") }} {{ form.price(class_="text-field", type="number", style="width: 5em;") }}
</div> </div>
<div class="input-row">
{{ form.dish_type.label(class_="field-label") }} {{ form.dish_type(class_="text-field") }}
</div>
</div> </div>
<div class="dashboard-row"> <div class="dashboard-row">
<button type="submit">Apply</button> <button type="submit">Apply</button>
......
...@@ -19,6 +19,9 @@ ...@@ -19,6 +19,9 @@
<div class="input-row"> <div class="input-row">
{{ form.price.label(class_="field-label") }} {{ form.price(class_="text-field", type="number", style="width: 5em;") }} {{ form.price.label(class_="field-label") }} {{ form.price(class_="text-field", type="number", style="width: 5em;") }}
</div> </div>
<div class="input-row">
{{ form.dish_type.label(class_="field-label") }} {{ form.dish_type(class_="text-field") }}
</div>
</div> </div>
<div class="dashboard-row"> <div class="dashboard-row">
<button type="submit">Add</button> <button type="submit">Add</button>
......
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