Skip to content

Commit 3d86be3

Browse files
committed
modified session and render_template
1 parent 1093ab3 commit 3d86be3

1 file changed

Lines changed: 72 additions & 59 deletions

File tree

app.py

Lines changed: 72 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
from flask import Flask, render_template, request, redirect, url_for,\
2-
session, flash
1+
from flask import Flask, render_template as render, request, redirect,\
2+
url_for, session as s
3+
from flask import flash as hriks
34
from logging import Formatter, FileHandler
45
from forms import *
56
import logging
@@ -25,21 +26,21 @@ def shutdown_server():
2526
@app.route('/shutdown', methods=['POST'])
2627
def shutdown():
2728
write()
28-
session.clear()
29+
s.clear()
2930
shutdown_server()
3031
print len(cache_records())
3132
try:
3233
if len(cache_records()) >= 1:
33-
flash(
34+
hriks(
3435
'Notification : Records Saved !'
3536
)
3637
except Exception:
37-
flash(
38+
hriks(
3839
'Notification : New file created with name "%s" ' % (
3940
sys.argv[1]
4041
)
4142
)
42-
return render_template('layouts/shutdown.html')
43+
return render('layouts/shutdown.html')
4344

4445

4546
# Controllers.
@@ -49,7 +50,7 @@ def write():
4950
for i in records:
5051
print i
5152
except Exception:
52-
flash(
53+
hriks(
5354
'New File created with name %s' % (
5455
sys.argv[1]
5556
)
@@ -58,14 +59,19 @@ def write():
5859
fieldnames = [
5960
'ids', 'student_name', 'academics', 'sports', 'social'
6061
]
61-
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
62+
writer = csv.DictWriter(
63+
csvfile, fieldnames=fieldnames
64+
)
6265

6366
writer.writeheader()
6467
try:
6568
for record in records:
6669
writer.writerow(record)
6770
except Exception:
68-
flash('New File created with name %s' % (sys.argv[1]))
71+
hriks('New File created with name %s' % (
72+
sys.argv[1]
73+
)
74+
)
6975

7076

7177
def read():
@@ -77,16 +83,22 @@ def read():
7783

7884

7985
def read_cache():
80-
data = session['data']
86+
data = s['data']
8187
return data
8288

8389

8490
def write_cache(student_name, academics, sports, social):
91+
import pdb; pdb.set_trace()
8592
data = cache_records()
86-
if len(data) > 20:
87-
count = session['count']
88-
print count
89-
data = delete_cache(count)
93+
if len(data) == 20:
94+
if 'count' in s:
95+
count = s['count']
96+
print count
97+
data = delete_cache(count)
98+
else:
99+
data = cache_records()
100+
while len(data) == 20:
101+
data.pop()
90102
new_dict = {}
91103
new_dict['ids'] = ids_get()
92104
new_dict['student_name'] = student_name
@@ -110,13 +122,12 @@ def update_cache(ids, student_name, academics, sports, social):
110122

111123
def delete_cache(ids):
112124
data = cache_records()
113-
if ids:
114-
for delete in data:
115-
if int(delete['ids']) == int(ids):
116-
data.pop(data.index(delete))
117-
for ids in data:
118-
ids['ids'] = data.index(ids) + 1
119-
return data
125+
for delete in data:
126+
if int(delete['ids']) == int(ids):
127+
data.pop(data.index(delete))
128+
for ids in data:
129+
ids['ids'] = data.index(ids) + 1
130+
return data
120131

121132

122133
def ids_get():
@@ -135,78 +146,80 @@ def cache_records():
135146

136147
@app.route('/', methods=['GET', 'POST'])
137148
def home():
138-
return render_template('pages/placeholder.home.html')
149+
return render('pages/placeholder.home.html')
139150

140151

141152
@app.route('/delete', methods=['GET', 'POST'])
142153
def delete():
143154
if request.method == 'POST':
144-
session['ids'] = request.form['submit']
145-
session['delete'] = request.form['delete']
146-
ids = session['ids']
155+
s['ids'] = request.form['submit']
156+
s['delete'] = request.form['delete']
157+
ids = s['ids']
147158
data = delete_cache(ids)
148-
session['data'] = data
149-
flash(
159+
s['data'] = data
160+
hriks(
150161
'Notification : Successfully deleted records with ID %s' % (ids)
151162
)
152-
session['ids'] = None
153-
session['student_name'] = None
163+
s['ids'] = None
164+
s['student_name'] = None
154165
return redirect(url_for('home'))
155166

156167

157168
@app.route('/edit', methods=['GET', 'POST'])
158169
def edit():
159170
form = Update_student_info(request.form)
160-
session['ids'] = request.form['submit']
161-
ids = session['ids']
162-
session['student_name'] = request.form['name']
163-
name = session['student_name']
171+
s['ids'] = request.form['submit']
172+
ids = s['ids']
173+
s['student_name'] = request.form['name']
174+
name = s['student_name']
164175
if request.method == 'POST':
165176
return redirect(url_for('update'))
166-
return render_template(
177+
return render(
167178
'forms/update.html', form=form, ids=ids, name=name
168179
)
169180

170181

171182
@app.route('/update', methods=['GET', 'POST'])
172183
def update():
173184
form = Update_student_info(request.form)
174-
ids = session['ids']
175-
name = session['student_name']
185+
ids = s['ids']
186+
name = s['student_name']
176187
if request.method == 'POST':
177188
academics = form.academics.data
178189
sports = form.sports.data
179190
social = form.social.data
180191
if academics is None or int(academics) > 100:
181-
flash(
192+
hriks(
182193
'Notification : %s is not a valid score.\
183194
Please enter valid score for Academics' % (
184195
academics))
185196
return redirect(url_for('update'))
186197
elif sports is None or int(sports) > 100:
187-
flash(
198+
hriks(
188199
'Notification : %s is not a valid score.\
189200
Please enter valid score for Sports' % (
190201
sports))
191202
return redirect(url_for('update'))
192203
elif social is None or int(social) > 100:
193-
flash(
204+
hriks(
194205
'Notification : %s is not a valid score.\
195206
Please enter valid score for Social' % (
196207
social))
197208
return redirect(url_for('update'))
198209
else:
199210
data = update_cache(
200-
session['ids'], session['student_name'],
211+
s['ids'], s['student_name'],
201212
academics, sports, social
202213
)
203-
session['data'] = data
204-
flash(
214+
s['data'] = data
215+
hriks(
205216
'Notification : Successfully updated record for %s \
206217
with ID %s' % (name, ids)
207218
)
208-
return redirect(url_for('home'))
209-
return render_template(
219+
return redirect(
220+
url_for('home')
221+
)
222+
return render(
210223
'forms/update.html', form=form, ids=ids, name=name
211224
)
212225

@@ -216,7 +229,7 @@ def addinfo():
216229
try:
217230
ids = ids_get()
218231
except Exception:
219-
flash(
232+
hriks(
220233
'Notification : File doesnot exits. Shutdown to create a \
221234
file with name %s' % (sys.argv[1])
222235
)
@@ -228,27 +241,27 @@ def addinfo():
228241
sports = form.sports.data
229242
social = form.social.data
230243
if student_name is None:
231-
flash(
244+
hriks(
232245
'Notification : %s is not vaild. Please Enter valid \
233246
name' % student_name
234247
)
235248
return redirect(url_for('addinfo'))
236249
elif academics is None or int(academics) > 100:
237-
flash(
250+
hriks(
238251
'Notification : %s is not a valid score.\
239252
Please enter valid score for Academics' % (
240253
academics)
241254
)
242255
return redirect(url_for('addinfo'))
243256
elif sports is None or int(sports) > 100:
244-
flash(
257+
hriks(
245258
'Notification : %s is not a valid score.\
246259
Please enter valid score for Sports' % (
247260
sports)
248261
)
249262
return redirect(url_for('addinfo'))
250263
elif social is None or int(social) > 100:
251-
flash(
264+
hriks(
252265
'Notification : %s is not a valid score.\
253266
Please enter valid score for Social' % (
254267
social)
@@ -258,28 +271,28 @@ def addinfo():
258271
data = write_cache(
259272
student_name, academics, sports, social
260273
)
261-
session['data'] = data
262-
flash(
274+
s['data'] = data
275+
hriks(
263276
'Notification : %s successfully added to records \
264277
with ID %s' % (
265278
student_name, ids
266279
)
267280
)
268281
return redirect(url_for('home'))
269-
return render_template('forms/register.html', form=form, ids=ids)
282+
return render('forms/register.html', form=form, ids=ids)
270283

271284

272285
@app.route('/search', methods=['GET', 'POST'])
273286
def search():
274287
count = {1:0,2:0,3:0,4:0,5:0,6:0,7:0,8:0,9:0,10:0,11:0,12:0,13:0,14:0,15:0,16:0,17:0,18:0,19:0,20:0} # noqa
275288
inverse = [(value, key) for key, value in count.items()]
276-
session['count'] = max(inverse)[0]
289+
s['count'] = max(inverse)[0]
277290
if request.method == 'POST':
278291
search = request.form['search']
279292
try:
280293
records = cache_records()
281294
except Exception:
282-
flash(
295+
hriks(
283296
'Notification : No such file present.\
284297
Please provide a valid file or shutdown to create file'
285298
)
@@ -292,24 +305,24 @@ def search():
292305
if i['ids'] == int(search):
293306
count[int(search)] = count[int(search)] + 1
294307
except Exception:
295-
flash('Notification : Invalid ID provided, Please provide ID')
308+
hriks('Notification : Invalid ID provided, Please provide ID')
296309
return redirect(url_for('home'))
297-
return render_template(
310+
return render(
298311
'pages/placeholder.search.html', match=match, search=search
299312
)
300-
return render_template('pages/placeholder.search.html', search=search)
313+
return render('pages/placeholder.search.html', search=search)
301314

302315

303316
# Error handlers.
304317

305318
@app.errorhandler(500)
306319
def internal_error(error):
307-
return render_template('errors/500.html'), 500
320+
return render('errors/500.html'), 500
308321

309322

310323
@app.errorhandler(404)
311324
def not_found_error(error):
312-
return render_template('errors/404.html'), 404
325+
return render('errors/404.html'), 404
313326

314327

315328
if not app.debug:

0 commit comments

Comments
 (0)