-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
145 lines (98 loc) · 3.75 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
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.history 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 '{}, {}, {}'.format(name, description, site_url)
return redirect('/restaurants')
return render_template('create_restaurant.html')
@app.route('/restaurants')
def restaurant_list():
restaurants = Restaurants.query.all()
return render_template('restaurant.html', nav='restaurants', 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():
histories = Histories.query.order_by(desc(Histories.create_time)).limit(20)
return render_template('history.html', nav='history', histories=histories)
@app.route('/top')
def top():
restaurants = Restaurants.query.order_by('-draw').limit(5) # column前加-号是指倒序排序,最大到最小
return render_template('top.html', nav='top', restaurants=restaurants)
def meal_format(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, 19, 20, 21]:
return 'Dinner'
else:
return 'Supper'
def datetime_format(value):
return value.strftime('%Y-%m-%d %H:%M:%S')
app.jinja_env.filters['meal'] = meal_format
app.jinja_env.filters['datetime'] = datetime_format
if __name__ == '__main__':
app.jinja_environment.auto_reload = True
app.run(debug=True)