-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
168 lines (150 loc) · 4.78 KB
/
app.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
from flask import Flask, render_template, request, jsonify, session, redirect, escape, url_for
import MySQLdb
import lib.Users as Users
app = Flask(__name__)
class ServerError(Exception):pass
class DB:
conn = None
def connect(self):
config = {}
execfile("config.conf",config)
self.conn = MySQLdb.connect(
host=config['db_host'],
user=config['db_user'],
passwd=config['db_pass'],
db=config['db_data']
)
self.conn.autocommit(True)
self.conn.set_character_set('utf8')
def query(self, sql, args=None):
try:
cursor = self.conn.cursor()
cursor.execute(sql,args)
except (AttributeError, MySQLdb.OperationalError):
self.connect()
cursor = self.conn.cursor()
cursor.execute(sql,args)
return cursor
if __name__ == '__main__':
config = {}
execfile("config.conf",config)
app.secret_key = config['app_key']
db = DB()
notifications = None
#Routes
@app.route('/')
def index():
message = None
global notifications
if notifications:
message = notifications
notifications = None
if 'username' not in session:
message = {'message': 'Please log in', 'type': 'warning'}
return redirect(url_for('login'))
return render_template('index.html',session=session,message=message)
@app.route('/users')
def users():
message = None
global notifications
if notifications:
message = notifications
notifications = None
if 'username' not in session:
notifications = {'message': 'Please log in', 'type': 'warning'}
return redirect(url_for('login'))
if session['username'] != 'admin':
return redirect(url_for('index', message="Admin only page"))
users = Users.getUsers(db)
if not users:
notifications = {'message': 'Failed to retrieve users', 'type': 'error'}
return render_template('users.html', message=message)
return render_template('users.html', users=users, message=message)
@app.route('/users/edit/<username>')
def editUser(username):
return "ToDo"
@app.route('/users/delete/<username>')
def delUser(username):
global notifications
if 'username' not in session:
notifications = {'message': 'Please log in', 'type': 'warning'}
return redirect(url_for('login'))
if session['username'] != 'admin':
notifications = {'message': 'Admin only page', 'type': 'error'}
return redirect(url_for('index'))
result = Users.deleteUser(db,username)
if not result:
notifications = {'message': 'User deleted successfully', 'type': 'success'}
return redirect(url_for('users', message="User deleted successfully"))
notifications = {'message': 'Something went wrong: '+result, 'type': 'error'}
return redirect(url_for('users', message="Something went wrong: "+result))
@app.route('/login', methods=['GET', 'POST'])
def login():
message = None
global notifications
if notifications:
message = notifications
notifications = None
if 'username' in session:
return redirect(url_for('index'))
if request.method == 'POST':
result = Users.loginForm(db, request.form)
if not result:
notifications = {'message': 'Logged in', 'type': 'success'}
return redirect(url_for('index'))
else:
message = {'message': 'Failed to log in', 'type': 'error'}
return render_template('login.html', message=message)
return render_template('login.html',message=message)
@app.route('/logout')
def logout():
global notifications
if 'username' not in session:
return redirect(url_for('login'))
session.pop('username', None)
notifications = {'message': 'Logged out', 'type': 'success'}
return redirect(url_for('login'))
@app.route('/register', methods=['GET','POST'])
def register():
message = None
global notifications
if notifications:
message = notifications
notifications = None
if request.method == 'POST':
result = Users.registerUser(db, request.form, config['pw_rounds'])
if not result:
notifications = {'message': 'Registration successful', 'type': 'success'}
if session['username'] == 'admin':
return redirect(url_for('register'))
else:
return redirect(url_for('login'))
else:
message = {'message': 'Something went wrong: '+result, 'type': 'error'}
return render_template('register.html', message=message)
if 'username' in session and session['username'] == 'admin':
return render_template('register.html', message=message)
if config['registration_enabled']:
return render_template('register.html', message=message)
else:
notifications = {'message': 'User registration is disabled by the admin', 'type': 'warning'}
if 'username' in session:
return redirect(url_for('index'))
else:
return redirect(url_for('login'))
#Run app
if __name__ == '__main__':
if config['ssl']:
context = (config['ssl_crt'],config['ssl_key'])
app.run(
host=config['server_ip'],
port=config['server_port'],
ssl_context=context,
debug=config['debug']
)
else:
app.run(
host=config['server_ip'],
port=config['server_port'],
debug=config['debug']
)