-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
143 lines (99 loc) · 2.91 KB
/
main.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
from flask import Flask, render_template, session, redirect, url_for, flash
from flask_login import LoginManager, login_required, login_user, current_user, logout_user
from firestore_service import get_user, set_appointment, get_appointment
from forms import LoginForm, PhoneForm, StatusForm
from models import UserModel, UserData
app = Flask(__name__)
app.config['SECRET_KEY'] = 'KEY'
login_manager = LoginManager()
login_manager.login_view = 'login'
login_manager.init_app(app)
@login_manager.user_loader
def load_user(username):
return UserModel.query(username)
@app.errorhandler(404)
def not_found(error):
return render_template('404.html')
@app.route('/')
def index():
return render_template('index.html')
@app.route('/appointment', methods=['GET', 'POST'])
def schedule():
form = PhoneForm()
context = {
'form': form,
}
if form.validate_on_submit():
brand = form.brand.data
model = form.model.data
problem = form.problem.data
problem_explanation = form.problem_explanation.data
email = form.email.data
set_appointment(brand, model, problem, problem_explanation, email)
return redirect('/success')
return render_template('schedule.html', **context)
@app.route('/dashboard')
@login_required
def dashboard():
username = current_user.id
context = {
'username': username,
}
return render_template('dashboard.html', **context)
@app.route('/device-status', methods=['GET', 'POST'])
def status():
form = StatusForm()
context = {
'form': form,
}
if form.validate_on_submit():
oid = form.order_id.data
appointment = get_appointment(oid)
apdict = appointment.to_dict()
context = {
'appointment': apdict,
}
if appointment.to_dict() is not None:
return render_template('checking.html', **context)
else:
flash('No existe la orden {}'.format(oid))
return render_template('status.html', **context)
@app.route('/login', methods=['GET', 'POST'])
def login():
login_form = LoginForm()
username = session.get('username')
context = {
'login_form': login_form,
'username': username
}
if current_user is True:
return redirect(url_for('dashboard'))
if login_form.validate_on_submit():
username = login_form.username.data
password = login_form.password.data
user_doc = get_user(username)
if user_doc.to_dict() is not None:
password_from_db = user_doc.to_dict()['password']
if password == password_from_db:
user_data = UserData(username, password)
user = UserModel(user_data)
login_user(user)
flash('Login success')
return redirect(url_for('dashboard'))
else:
flash('Information does not match')
else:
flash('User does not exist')
return render_template('login.html', **context)
@app.route('/logout')
@login_required
def logout():
logout_user()
return redirect(url_for('login'))
@app.route('/success')
def success():
email = session.get('email')
context = {
'email': email
}
return render_template('success.html', **context)