-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
148 lines (100 loc) · 3.78 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
from flask import Flask, render_template, request, redirect
from database import db_session, init_db
from sqlalchemy import desc
from models.restaurants import Restaurants
from models.histories import Histories
import datetime
from random import choice
app = Flask(__name__)
@app.before_first_request
def init():
init_db()
@app.teardown_appcontext
def shutdown_session(exception=None):
db_session.remove()
@app.route('/')
def start():
now = datetime.datetime.now()
return render_template('start.html', nav='start', now=now)
@app.route('/draw')
def draw():
restaurants = Restaurants.query.all()
if not restaurants:
return redirect('/create-restaurant')
random_restaurant = choice(restaurants)
try:
restaurant = Restaurants.query.get(random_restaurant.id)
restaurant.draw += 1
history = Histories(restaurant_id=restaurant.id)
db_session.add(history)
db_session.commit()
except:
db_session.rollback()
return redirect('/')
now = datetime.datetime.now()
return render_template('draw.html', restaurant=restaurant, now=now)
@app.route('/create-restaurant', methods=['GET', 'POST'])
def create_restaurant():
if request.method == 'POST':
name = request.form.get('name')
description = request.form.get('description')
site_url = request.form.get('site_url')
restaurant = Restaurants(name=name, description=description, site_url=site_url)
db_session.add(restaurant)
db_session.commit()
return redirect('/restaurants')
return render_template('create_restaurant.html')
@app.route('/restaurants')
def restaurant_list():
restaurants = Restaurants.query.all()
return render_template('restaurants.html', nav='restaurant', restaurants=restaurants)
@app.route('/edit-restaurant', methods=['GET', 'POST'])
def edit_restaurant():
id = request.args.get('id')
restaurant = Restaurants.query.filter(Restaurants.id == id).first()
if request.method == 'POST':
name = request.form.get('name')
description = request.form.get('description')
site_url = request.form.get('site_url')
restaurant.name = name
restaurant.description = description
restaurant.site_url = site_url
restaurant.modified_time = datetime.datetime.now()
db_session.commit()
return redirect('/restaurants')
return render_template('edit_restaurant.html', restaurant=restaurant)
@app.route('/delete-restaurant')
def delete_restaurant():
id = request.args.get('id')
restaurant = Restaurants.query.filter(Restaurants.id == id).first()
if restaurant:
db_session.delete(restaurant)
db_session.commit()
return redirect('/restaurants')
@app.route('/history')
def history():
# 排序最後面到最前面 最多20筆資料
histories = Histories.query.order_by(desc(Histories.created_time)).limit(20)
return render_template('history.html', nav='history', histories=histories)
@app.route('/top')
def top():
# -draw是大到小 draw是小到大
restaurants = Restaurants.query.order_by('-draw').limit(5)
return render_template('top.html', nav='top', restaurants=restaurants)
def mealformat(value):
if value.hour in [4, 5, 6, 7, 8, 9]:
return 'Breakfast'
elif value.hour in [10, 11, 12, 13, 14, 15]:
return 'Lunch'
elif value.hour in [16, 17, 18, 20, 21]:
return 'Dinner'
else:
return 'Supper'
def datetimeformat(value):
return value.strftime('%Y-%m-%d %H:%M:%S')
app.jinja_env.filters['meal'] = mealformat
app.jinja_env.filters['datetime'] = datetimeformat
if __name__ == '__main__':
# 預設會快取,加上這行會讓他強制清空快取重新讀取
app.jinja_env.auto_reload = True
app.run(debug=True)