-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.py
312 lines (265 loc) · 8.53 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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
from flask import Flask, render_template as render,\
request, redirect, url_for,\
session as s
from flask import flash as hriks
from logging import Formatter, FileHandler
import models
from functools import wraps
import flask_login_auth
from forms import *
import logging
app = Flask(__name__)
app.secret_key = os.environ.get('Ssecret')
app.config.from_object('config')
# Decorator's
def login_requied(f):
@wraps(f)
def wrap(*args, **kwargs):
if 'name' in s:
return f(*args, **kwargs)
else:
hriks(' Sorry! You Need to Login to view this page')
return redirect(url_for('login'))
return wrap
def logout_requied(f):
@wraps(f)
def wrap(*args, **kwargs):
if 'name' in s:
hriks(
'Sorry! You are already logged in. Logout to perform your\
Action'
)
return redirect(url_for('home'))
else:
return f(*args, **kwargs)
return wrap
def auth_requied(f):
@wraps(f)
def wrap(*args, **kwargs):
if s['role'] == 'admin':
return f(*args, **kwargs)
else:
hriks(
' Sorry! You dont have any permission to perform such action')
return redirect(url_for('home'))
return wrap
# Controllers.
# user will be able to register new user when logged in
# as Admin or manager
@app.route('/register', methods=['GET', 'POST'])
@login_requied
@auth_requied
def register():
form = RegisterForm(request.form)
if request.method == 'POST':
user = models.create_user(
form.userid.data, form.name.data, form.role.data,
form.email.data, form.password.data
)
if user == 1:
hriks(
'ERROR! Please enter something or check yours\
username and password or user already exists'
)
return redirect(url_for('register'))
else:
return redirect(url_for('home'))
return render('forms/register.html', form=form)
# user will be able to register new user as normal
# user without login with only user role_authenticate
@app.route('/registration', methods=['GET', 'POST'])
def registration():
form = RegistrationForm(request.form)
if request.method == 'POST':
user = models.create_user(
form.userid.data,
form.name.data,
form.role.data,
form.email.data,
form.password.data
)
if user == 1:
hriks(
'ERROR! Please enter something or check yours\
username and password or user already exists'
)
return redirect(url_for('register'))
else:
return redirect(url_for('home'))
return render('forms/register.html', form=form)
# Open as you run the server
@app.route('/', methods=['GET', 'POST'])
def home():
if 'trigger_cache' in s:
messages = models.message_show()
s['messages'] = messages
s.pop('trigger_cache', None)
elif 'messages' in s:
messages = s['messages']
else:
messages = models.message_show()
s['messages'] = messages
return render('pages/placeholder.home.html', messages=messages, session=s) # noqa
@app.route('/about')
def about():
return render('pages/placeholder.about.html')
# Shows all user
@app.route('/users')
@login_requied
def users():
users = models.users()
return render('pages/placeholder.users.html', users=users)
# Logout user
@app.route('/logout')
@login_requied
def logout():
s.clear()
return redirect(url_for('home'))
# This implifies for login and authentication
# for user, role, blocked, and also count
# no if time user is logged in
@app.route('/login', methods=['GET', 'POST'])
@logout_requied
def login():
error = None
s.clear()
form = LoginForm(request.form)
if request.method == 'POST':
username = form.name.data
print username
password = form.password.data
print password
value = flask_login_auth.authenticate(username, password)
print value
if (value == 1):
role = flask_login_auth.role_authenticate(username, password)
blocked = flask_login_auth.blocked(username, password)
# Checks user is blocked
# and return value in true or
# False
if not blocked:
s['name'] = username
s['role'] = role
s['editid'] = None
# count will show no of time user logged in
count = models.count_show(username)
print count[0][0]
messages = models.message_show()
if count[0][0]:
s['count'] = count[0][0] + 1
new_count = s['count']
else:
new_count = 0
new_count = models.count_add(username, new_count)
models.count_show(username)
return render('pages/placeholder.home.html', session=s, messages=messages) # noqa
else:
error = 'Your Account is blocked !\
Please contact Admin'
return render(
'forms/login.html', form=form, error=error)
else:
error = 'Invalid username or password \
Please try again!'
return render('forms/login.html', form=form, error=error)
return render('forms/login.html', form=form)
# client will be able to search by username
@app.route('/search', methods=['GET', 'POST'])
@login_requied
def search():
search = request.form['search']
match = flask_login_auth.searchbox(search)
print match
return render(
'pages/placeholder.search.html', search=search,
match=match
)
# Post messages on Dashboard
# This can be done by any user
@app.route('/message', methods=['GET', 'POST'])
@login_requied
def message():
if request.method == 'POST':
models.post_messages(
s['name'], request.form['message'])
s['trigger_cache'] = 'post'
return redirect(url_for('home'))
# Admin will able to delete messages posted
# on Dashboard
@app.route('/delete', methods=['GET', 'POST'])
@login_requied
def delete():
if request.method == 'POST':
models.message_delete(
request.form['submit'])
s['trigger_cache'] = 'post'
return redirect(url_for('home'))
# Admin able to block the user ny using this method
@app.route('/block', methods=['GET', 'POST'])
@login_requied
def block():
if request.method == 'POST':
models.block(
request.form['submit'], request.form['userid'])
return redirect(url_for('users'))
# Able to edit own profile details
@app.route('/edit', methods=['GET', 'POST'])
@login_requied
def edit():
if request.method == 'POST':
s['editid'] = request.form['submit']
return redirect(url_for('update'))
# Able to edit user details
# this can be done only by user having
# role as 'Admin'
@app.route('/update', methods=['GET', 'POST'])
@login_requied
def update():
if 'editid' in s:
userid = s['editid']
else:
userid = s['name']
form = UpdateForm(request.form)
get_info = models.get_info(userid)
if request.method == 'POST':
user = models.update(
form.role.data, userid, form.password.data,
form.email.data, form.name.data
)
if user != 1:
hriks(
"ERROR! Already on this role, or you hadn't updated anything"
)
return redirect(url_for('update'))
else:
hriks(
'Successfully role changed for %s to %s' % (
userid, form.role.data)
)
s['editid'] = None
return redirect(url_for('users'))
return render(
'forms/update.html', form=form,
userid=userid, get_info=get_info
)
# Error handlers.
@app.errorhandler(500)
def internal_error(error):
# db_s.rollback()
return render('errors/500.html'), 500
@app.errorhandler(404)
def not_found_error(error):
return render('errors/404.html'), 404
if not app.debug:
file_handler = FileHandler('error.log')
file_handler.setFormatter(
Formatter('% (asctime)s % (levelname)s: % (message)s\
[in % (pathname)s: % (lineno)d]')
)
app.logger.setLevel(logging.INFO)
file_handler.setLevel(logging.INFO)
app.logger.addHandler(file_handler)
app.logger.info('errors')
# Default port:
if __name__ == '__main__':
app.run(debug=True)