/
server.py
173 lines (122 loc) · 4.77 KB
/
server.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
from flask import Flask, flash, redirect, render_template, request, session
from flask_debugtoolbar import DebugToolbarExtension
from jinja2 import StrictUndefined
from model import connect_to_db, db
from model import *
from datetime import datetime, date
from twilio.rest import TwilioRestClient
from secrets import TW_ACCOUNT_SID, TW_AUTH_TOKEN, TWILIO_NUMBER
app = Flask(__name__)
# Required to use Flask sessions and the debug toolbar
app.secret_key = "\xf0\x89\r\xbd/m\x9f<\xfd\xab8\xf00\xf8\x07t\x02\xec9\xd1\xa5&`B"
app.jinja_env.undefined = StrictUndefined
print TW_ACCOUNT_SID, TW_AUTH_TOKEN, TWILIO_NUMBER
@app.route('/')
def splash_login():
"""Splash Page"""
return render_template("splash.html")
@app.route('/login')
def sign_in():
"""Login Page"""
return render_template("login.html")
@app.route('/process_login', methods=['POST'])
def login_user():
"""Log in user based on db info"""
email = request.form["email"]
password = request.form["password"]
user = Staff.query.filter_by(email=email).first()
if user.password == password:
session["staff_id"] = user.staff_id
session["username"] = user.fname
return redirect('/schedule')
@app.route('/logout')
def logout():
# remove the username from the session if it's there
session.pop('staff_id', None)
return redirect("/")
# set the secret key. keep this really secret:
app.secret_key = '\xf0\x89\r\xbd/m\x9f<\xfd\xab8\xf00\xf8\x07t\x02\xec9\xd1\xa5&`B'
# @app.route('/logout', methods=['POST'])
# def logout():
# """Log out."""
# del session["staff_id"]
# del session["username"]
# flash("You have logged out successfully.")
# return redirect("/")
# @app.route('/logout', methods=['POST'])
# def logout_user():
# """Logout of session"""
# session["staff_id"] = None
# session["username"] = None
# # print session["staff_id"]
# # print session["username"]
# flash('You were logged out')
# return render_template('/login.html')
@app.route('/schedule')
def choose_schedule():
"""Main Scheduling Page"""
teams = Team.query.all()
units = Unit.query.all()
staff = Staff.query.all()
trainings = Training.query.all()
building = Building.query.all()
room = Room.query.all()
return render_template("schedule.html", teams=teams, trainings=trainings,
units=units, staff=staff)
@app.route('/submit_schedule', methods=['POST'])
def submit_schedule():
"Submit/Post webform to db"
team = request.form['team_name']
training = request.form['training_name']
staff_id = request.form['staff_id']
start_date = request.form['start_date']
start_date = datetime.strptime(start_date, '%Y-%m-%d')
start_time = request.form['start_time']
start_time = datetime.strptime(start_time, '%H:%M')
end_time = request.form['end_time']
end_time = datetime.strptime(end_time, '%H:%M')
# Creates new user in DB
new_training = TrainingAssignment(
team_id=team,
training_id=training,
staff_id=staff_id,
start_date=start_date,
start_time=start_time,
end_time=end_time)
db.session.add(new_training)
db.session.commit()
staff_poc = Staff.query.filter_by(staff_id=staff_id).first()
poc_phone = staff_poc.work_phone
client = TwilioRestClient(TW_ACCOUNT_SID, TW_AUTH_TOKEN)
message = client.messages.create(to="+1{}".format(poc_phone),
from_=TWILIO_NUMBER,
body= "You have been scheduled for {} training on {} starting at {}.".format (training, start_date, start_time))
flash("Schedule Request has been created for {}".format(team))
return redirect('/dashboard')
"Show schedule posted to database"
@app.route('/dashboard')
def process_request():
"""Show result of schedule choices on dashboard."""
if session["staff_id"] == "ADMIN":
assignments = TrainingAssignment.query.all()
else:
assignments = TrainingAssignment.query.filter_by(staff_id = session["staff_id"]).all()
# print assignments
# print session["staff_id"]
return render_template("dashboard.html", assignments=assignments)
"Reformat datetime on dashboard"
@app.template_filter('date')
def datetimeformat(value, format='%b-%d-%y'):
return value.strftime(format)
@app.template_filter('time')
def timeformat(value, format='%I:%M %p'):
return value.strftime(format)
################################################################################
if __name__ == "__main__":
# We have to set debug=True here, since it has to be True at the point
# that we invoke the DebugToolbarExtension
# app.debug = True
connect_to_db(app)
# Use the DebugToolbar
DebugToolbarExtension(app)
app.run()