-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck.py
107 lines (85 loc) · 2.96 KB
/
check.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
# coding=utf-8
from flask import Flask, request, render_template
from flask_sqlalchemy import SQLAlchemy
from datetime import datetime, timedelta
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///data.db'
app.config['JSON_AS_ASCII'] = False
db = SQLAlchemy(app)
class Record(db.Model):
id = db.Column(db.Integer, primary_key=True)
acct = db.Column(db.String(64))
it = db.Column(db.String(64))
tt = db.Column(db.DateTime)
def __init__(self, acct, it):
self.acct = acct
self.it = it
self.tt = datetime.now()
def __repr__(self):
return '[%s]%s(%s)' %(self.acct, self.it, self.tt)
@app.route('/打卡/')
def root():
return app.send_static_file('index.html')
@app.route('/打卡/api/send', methods=['POST'])
def send():
acct = request.form.get('acct')
it = request.form.get('it')
if(not (acct and it and len(acct) < 50 and len(it) < 50)):
return '格式不正确', 422
last = Record.query.filter_by(acct=acct,it=it).order_by(Record.tt.desc()).first()
if(last and datetime.now() - last.tt < timedelta(hours=6)):
return '间隔也太短了', 422
r = Record(acct, it)
db.session.add(r)
db.session.commit()
return str(Record.query.filter_by(acct=acct,it=it).count())
@app.route('/打卡/api/its')
def its():
query = db.session.query(Record.it.distinct().label('it'))
its = [e.it for e in query.all()]
return {'its': its}
@app.route('/打卡/api/top3/<acct>')
def getTop3(acct):
query = db.session.query(
Record.it, db.func.count(Record.id).label('it')
).group_by(Record.it).filter_by(acct=acct).order_by(db.desc('it')).limit(3)
its = [{'name': e[0], 'count': e[1]} for e in query.all()]
return {'top3': its}
@app.route('/打卡/web/all')
def allRecord():
query = Record.query
rs = [{
'acct': e.acct,
'it' : e.it,
'date': e.tt.strftime('%m-%d %I:%M:%S%p')
} for e in query.all()
]
return render_template('all.html', rs=rs[::-1])
@app.route('/打卡/web/acct/<acct>')
def acct_page(acct):
query = Record.query.filter_by(acct=acct)
rs = [{
'it' : e.it,
'date': e.tt.strftime('%m-%d %I:%M:%S%p')
} for e in query.all()
]
return render_template('acct.html', acct=acct,rs=rs[::-1])
@app.route('/打卡/web/it/<it>')
def it_page(it):
t1 = db.session.query(
Record.acct, db.func.count(Record.id).label('acct')
).group_by(Record.acct).filter_by(it=it).order_by(db.desc('acct')).first()
if(t1):
top = t1[0]
times = t1[1]
else:
top = times = '/'
query = Record.query.filter_by(it=it)
rs = [{
'acct' : e.acct,
'date': e.tt.strftime('%m-%d %I:%M:%S%p')
} for e in query.all()
]
return render_template('it.html', it=it,rs=rs[::-1],top=top,times=times)
if __name__ == '__main__':
app.run()