-
Notifications
You must be signed in to change notification settings - Fork 0
/
pydoor.py
41 lines (31 loc) · 1.08 KB
/
pydoor.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
import logging
import sys
from datetime import datetime
from doorapp import get_door_app_environ
from authentication import get_authenticator_environ
from flask import Flask, redirect, request
logger = logging.getLogger(__name__)
app = Flask(__name__)
door_app = get_door_app_environ(start=True)
authenticator = get_authenticator_environ()
@app.route('/operate', methods=['GET', 'POST'])
def operate():
if request.method == 'GET':
return redirect('/')
uid = request.form.get('uid', '')
password = request.form.get('password', '')
if not authenticator.check_credentials(uid, password):
now = datetime.utcnow()
print(f'{now}: Authentication failed (user: {uid})', file=sys.stderr)
return redirect("/unauthorized.html")
action = request.form.get('type', '').lower()
if action == 'open':
door_app.door_driver.unlock(uid)
return redirect('/opened.html')
elif action == 'close':
door_app.door_driver.lock(uid)
return redirect('/closed.html')
def main():
app.run()
if __name__ == '__main__':
main()