-
Notifications
You must be signed in to change notification settings - Fork 0
/
site_main.py
162 lines (134 loc) · 5.62 KB
/
site_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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import tweepy
from flask import Flask, render_template, request, redirect, url_for, session, flash
import db
from CONFIG import SECRET_KEY
from bot.twitter_auth import authenticate
from make_table import get_table
from user_management import login_db
from user_management.page_restrictions import no_login, login_required, admin_required
from user_management.table import get_table as user_table
from forms import LoginForm, AddShowForm, AddUserForm, ManualTweetForm, ChangePasswordForm
app = Flask(__name__)
app.secret_key = SECRET_KEY
@app.route("/")
def index():
if "logged_in" in session:
return render_template("index.html", subheading="Home Page", page="home")
else:
return redirect(url_for("login"))
@app.route("/login/", methods=["GET", "POST"])
@no_login
def login():
error = None
loginForm = LoginForm()
if loginForm.validate_on_submit():
if login_db.verify(loginForm.username.data, loginForm.password.data):
session["logged_in"] = True
session["username"] = loginForm.username.data
session["type"] = login_db.get_type(loginForm.username.data)
flash("You have been logged in!")
return redirect(url_for("index"))
else:
error = "Invalid Credentials: Please try again."
return render_template("login.html", subheading="Log In", error=error, loginForm=loginForm, page="exempt")
@app.route("/logout/")
@login_required
def logout():
session.pop("logged_in", None)
session.pop("username", None)
session.pop("type", None)
flash("logged out")
return redirect(url_for("login"))
@app.route("/schedule/")
@login_required
def schedule():
return render_template("schedule.html", shows=get_table(), slots=db.read(), subheading="Schedule", page="schedule")
@app.route("/add/", methods=["GET", "POST"])
@login_required
def add_show():
showForm = AddShowForm()
if showForm.validate_on_submit():
show_info = [showForm.showName.data,
showForm.showDay.data,
showForm.showStart.data,
showForm.showEnd.data,
showForm.host1.data,
showForm.host2.data,
showForm.host3.data,
showForm.host4.data,
''
]
db.add(show_info)
return redirect(url_for("add_show"))
return render_template("add_show.html", alert=None, showForm=showForm, subheading="Adding New Show", page="add")
@app.route("/remove/<day>/<start>/<end>/")
@login_required
def remove_show(day, start, end):
try:
db.remove([day, start, end])
except IndexError:
pass
return redirect(url_for("schedule"))
@app.route("/user-management/", methods=["GET", "POST"])
@login_required
@admin_required
def users():
addUser = AddUserForm()
if addUser.validate_on_submit():
login_db.add([addUser.username.data, addUser.password.data, addUser.type.data])
return redirect(url_for("users"))
return render_template("user_management.html", subheading="User Management", addUser=addUser, table=user_table(),
accounts=login_db.read(), page="user-management")
@app.route("/user-management/user/<username>/")
@login_required
@admin_required
def user_profile(username):
for row in login_db.read():
if row[0] == username:
return render_template("user_profile.html", subheading="Viewing {}".format(username), username=username,
type=row[2], user_exists=True, page="user-profile")
else:
return render_template("user_profile.html", subheading="Not Found", username=username, type=None,
user_exists=False, page="user-profile")
@app.route("/user-management/user/delete/<username>/")
@login_required
@admin_required
def delete_user(username):
login_db.remove(username)
return redirect(url_for("users"))
@app.route("/manual-tweet/", methods=["GET", "POST"])
@login_required
def manual_tweet_page():
tweetForm = ManualTweetForm()
if tweetForm.validate_on_submit():
twit = authenticate()
tweet = tweetForm.tweet.data
try:
twit.update_status(tweet)
return redirect(url_for("manual_tweet_page"))
except tweepy.error.TweepError as e:
if session["type"] == "dev":
return render_template("manual_tweet.html", subheading="Send Tweet Manually", status="error", error=e,
tweetForm=tweetForm, page="manual-tweet")
else:
return render_template("manual_tweet.html", subheading="Send Tweet Manually", status="failed",
tweetForm=tweetForm, page="manual-tweet")
return render_template("manual_tweet.html", subheading="Send Tweet Manually", status=None, tweetForm=tweetForm,
page="manual-tweet")
@app.route("/settings/", methods=["GET", "POST"])
@login_required
def change_password():
changePass = ChangePasswordForm()
if changePass.validate_on_submit():
if login_db.verify(session["username"], changePass.oldPassword.data):
login_db.update(session["username"], changePass.newPassword.data)
flash("pass-updated")
else:
flash("bad-old")
return render_template("change_pass.html", subheading="Account Settings", message=None, changePass=changePass,
page="settings")
@app.errorhandler(404)
def page_not_found(e):
return render_template("error.html", subheading="", error=e, page="exempt"), 404
if __name__ == "__main__":
app.run(debug=True)