Commit 8401a6ed authored by Willard's avatar Willard

Move API specific functions to api.py, fixe imports

parent f0fc44d0
...@@ -18,4 +18,5 @@ db = SQLAlchemy(app) ...@@ -18,4 +18,5 @@ db = SQLAlchemy(app)
migrate = Migrate(app, db) migrate = Migrate(app, db)
import models import models
import controllers import views
\ No newline at end of file import api
\ No newline at end of file
from canteeneo import app
from flask import jsonify, request
from models import Dish, Stall, Location
from datetime import datetime
@app.route('/api/all')
def all():
data = {'update_time': str(datetime.now()), 'dishes': [], 'stalls': [], 'locations': []}
for dish in Dish.query.all():
data['dishes'].append({
'id': dish.id,
'name': dish.name,
'price': dish.price,
'stall_id': dish.stall_id,
'image_path': dish.image_path
})
for stall in Stall.query.all():
data['stalls'].append({
'id': stall.id,
'name': stall.name
})
for loc in Location.query.all():
data['locations'].append({
'id': loc.id,
'name': loc.name
})
return jsonify(**data)
@app.route('/api/search/dish')
def search():
query = request.args.get('name')
result = Dish.query.filter(Dish.name.like("%" + query + "%")).all()
data = []
for dish in result:
data.append({
'id': dish.id,
'name': dish.name,
'price': dish.price,
'stall_name': dish.stall.name,
'image_path': dish.image_path
})
return jsonify(data)
\ No newline at end of file
...@@ -202,46 +202,6 @@ def unauthorized(): ...@@ -202,46 +202,6 @@ def unauthorized():
flash('You have to be logged in to view this page!') flash('You have to be logged in to view this page!')
return redirect(url_for('login')) return redirect(url_for('login'))
@app.route('/api/all')
def all():
data = {'update_time': str(datetime.now()), 'dishes': [], 'stalls': [], 'locations': []}
for dish in Dish.query.all():
data['dishes'].append({
'id': dish.id,
'name': dish.name,
'price': dish.price,
'stall_id': dish.stall_id,
'image_path': dish.image_path
})
for stall in Stall.query.all():
data['stalls'].append({
'id': stall.id,
'name': stall.name
})
for loc in Location.query.all():
data['locations'].append({
'id': loc.id,
'name': loc.name
})
return jsonify(**data)
@app.route('/api/search/dish')
def search():
query = request.args.get('name')
result = Dish.query.filter(Dish.name.like("%" + query + "%")).all()
data = []
for dish in result:
data.append({
'id': dish.id,
'name': dish.name,
'price': dish.price,
'stall_name': dish.stall.name,
'image_path': dish.image_path
})
return jsonify(data)
def flash_form_errors(form): def flash_form_errors(form):
for field, errors in form.errors.items(): for field, errors in form.errors.items():
for error in errors: for error in errors:
......
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