-
Notifications
You must be signed in to change notification settings - Fork 19
/
runserver.py
295 lines (246 loc) · 9.76 KB
/
runserver.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
# encoding: utf-8
from flask import Flask, render_template, url_for, request, make_response, json, redirect
from sqlalchemy import create_engine, Column, desc, func
from sqlalchemy.types import String, Float, Integer, Date
from sqlalchemy.orm import sessionmaker
from sqlalchemy.ext.declarative import declarative_base
import sys
import datetime
reload(sys)
sys.setdefaultencoding("utf-8")
app = Flask(__name__)
Base = declarative_base()
class Shop(Base):
__tablename__ = 'shop'
id = Column(Integer, primary_key=True)
name = Column(String(40))
username = Column(String(20), unique=True)
password = Column(String(20))
def __init__(self, name, username, password):
self.name = name
self.username = username
self.password = password
def __repr__(self):
return '%d' % self.id
class Member(Base):
__tablename__ = 'member'
id = Column(Integer, primary_key=True)
name = Column(String(40))
phone = Column(String(20))
credit = Column(Integer, default=0)
balance = Column(Float)
shop = Column(Integer)
feature = Column(String(255), default='0')
def __init__(self, name, phone, credit, balance, shop, feature):
self.name = name
self.phone = phone
self.credit = credit
self.balance = balance
self.shop = shop
self.feature = feature
def __repr__(self):
return "%d" % self.id
class Product(Base):
__tablename__ = 'product'
id = Column(Integer, primary_key=True)
name = Column(String(40))
price = Column(Float)
shop = Column(String(10))
def __init__(self, name, price, shop):
self.name = name
self.price = price
self.shop = shop
def __repr__(self):
return "%d" % self.id
class History(Base):
__tablename__ = 'history'
id = Column(Integer, primary_key=True)
content = Column(String(255))
member = Column(String(10))
date = Column(Date)
sum = Column(Float)
def __init__(self, content, member, date, sum):
self.content = content
self.member = member
self.date = date
self.sum = sum
def __repr__(self):
return "%d" % self.id
# 初始化数据库连接:
engine = create_engine('mysql+mysqldb://root:lsq65602597@localhost:3306/crm?charset=utf8')
# 创建DBSession类型:
DBSession = sessionmaker(bind=engine)
@app.route('/', methods=['GET', 'POST'])
def login():
if (request.method == 'GET'):
return render_template('admin-login.html')
else:
username = request.form.get('username')
password = request.form.get('password')
session = DBSession()
current = session.query(Shop).filter(Shop.username == username).first()
session.close()
if (current == None):
return "0"
elif (password != current.password):
return "-1"
else:
return str(current.id)
@app.route('/member', methods=['GET', 'POST'])
def index():
username = request.cookies.get('username')
id = request.cookies.get('id')
session = DBSession()
currentName = session.query(Shop).filter(Shop.username == username).first().name
members = session.query(Member).filter(Member.shop == id).all()
session.close()
return render_template('admin-index.html', name=currentName, members=members)
@app.route('/add', methods=['GET', 'POST'])
def add():
if (request.method == 'GET'):
username = request.cookies.get('username')
session = DBSession()
currentName = session.query(Shop).filter(Shop.username == username).first().name
session.close()
return render_template('admin-add.html', name=currentName)
else:
username = request.cookies.get('username')
id = request.cookies.get('id')
name = request.form.get('name')
phone = request.form.get('phone')
balance = request.form.get('balance')
session = DBSession()
new_member = Member(name=name, phone=phone, credit=0, balance=balance, shop=id, feature='0')
session.add(new_member)
session.commit()
session.close()
return '1'
@app.route('/recognize', methods=['GET', 'POST'])
def recognize():
if (request.method == 'GET'):
username = request.cookies.get('username')
session = DBSession()
currentName = session.query(Shop).filter(Shop.username == username).first().name
session.close()
return render_template('admin-recognize.html', name=currentName)
@app.route('/search', methods=['GET', 'POST'])
def search():
username = request.cookies.get('username')
if (request.method == 'GET'):
session = DBSession()
currentName = session.query(Shop).filter(Shop.username == username).first().name
session.close()
return render_template('admin-search.html', name=currentName)
else:
phone = request.form.get('phone')
session = DBSession()
currentName = session.query(Shop).filter(Shop.username == username).first().name
print phone
member = session.query(Member).filter(Member.phone == phone).one()
session.close()
return render_template('admin-findone.html', name=currentName, member=member)
@app.route('/consume/<id>', methods=['GET', 'POST'])
def consume(id):
if (request.method == 'GET'):
username = request.cookies.get('username')
session = DBSession()
currentName = session.query(Shop).filter(Shop.username == username).first().name
shopId = session.query(Shop).filter(Shop.username == username).one().id
member = session.query(Member).filter(Member.id == id).one()
lists = session.query(Product).filter(Product.shop == shopId).all()
session.close()
return render_template('admin-consume.html', name=currentName, member=member, lists=lists)
@app.route('/content', methods=['GET', 'POST'])
def content():
member = request.form.get('member')
content = request.form.get('content')
sum = request.form.get('sum')
session = DBSession()
new_history = History(content=content, member=member, date=datetime.date.today(), sum=sum)
session.add(new_history)
session.commit()
session.close()
return '1'
@app.route('/recharge/<id>', methods=['GET', 'POST'])
def recharge(id):
if (request.method == 'GET'):
username = request.cookies.get('username')
session = DBSession()
currentName = session.query(Shop).filter(Shop.username == username).first().name
memberName = session.query(Member).filter(Member.id == id).first().name
session.close()
return render_template('admin-recharge.html', name=currentName, memberName=memberName, memberId=id)
else:
id = request.form.get('id')
balance = request.form.get('balance')
session = DBSession()
alter_member = session.query(Member).filter(Member.id == id).first()
alter_member.balance = float(balance) + alter_member.balance
session.add(alter_member)
session.commit()
session.close()
return '1'
@app.route('/history/<id>', methods=['GET'])
def history(id):
username = request.cookies.get('username')
session = DBSession()
currentName = session.query(Shop).filter(Shop.username == username).first().name
histories = session.query(History).filter(History.member == id).all()
memberName = session.query(Member).filter(Member.id == id).first().name
session.close()
return render_template('admin-history.html', name=currentName, memberName=memberName, histories=histories)
@app.route('/addproject', methods=['GET', 'POST'])
def addproject():
if (request.method == 'GET'):
username = request.cookies.get('username')
session = DBSession()
currentName = session.query(Shop).filter(Shop.username == username).first().name
session.close()
return render_template('admin-addproject.html', name=currentName)
else:
username = request.cookies.get('username')
id = request.cookies.get('id')
name = request.form.get('name')
price = request.form.get('price')
session = DBSession()
new_project = Product(name=name, price=price, shop=id)
session.add(new_project)
session.commit()
session.close()
return '1'
@app.route('/list', methods=['GET', 'POST'])
def list():
username = request.cookies.get('username')
id = request.cookies.get('id')
session = DBSession()
currentName = session.query(Shop).filter(Shop.username == username).first().name
lists = session.query(Product).filter(Product.shop == id).all()
session.close()
return render_template('admin-list.html', name=currentName, lists=lists)
@app.route('/alterProduct/<id>', methods=['GET', 'POST'])
def alterProduct(id):
if(request.method == 'GET'):
username = request.cookies.get('username')
session = DBSession()
currentName = session.query(Shop).filter(Shop.username == username).first().name
productName = session.query(Product).filter(Product.id == id).first().name
session.close()
return render_template('admin-alterproject.html', name=currentName, productName=productName, id=id)
else:
price = request.form.get('price')
session = DBSession()
alter_product = session.query(Product).filter(Product.id == id).first()
alter_product.price = float(price)
session.add(alter_product)
session.commit()
session.close()
return '1'
@app.route('/config', methods=['GET', 'POST'])
def config():
username = request.cookies.get('username')
session = DBSession()
currentName = session.query(Shop).filter(Shop.username == username).first().name
session.close()
return render_template('admin-config.html', name=currentName)
if __name__ == '__main__':
app.run(debug=True)