Skip to content

Commit 31c39b4

Browse files
authored
Merge pull request #33 from hriks/revert-32-file
Revert "modified session and render_template"
2 parents b04de1c + 67640ed commit 31c39b4

File tree

1 file changed

+59
-72
lines changed

1 file changed

+59
-72
lines changed

app.py

Lines changed: 59 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
from flask import Flask, render_template as render, request, redirect,\
2-
url_for, session as s
3-
from flask import flash as hriks
1+
from flask import Flask, render_template, request, redirect, url_for,\
2+
session, flash
43
from logging import Formatter, FileHandler
54
from forms import *
65
import logging
@@ -26,21 +25,21 @@ def shutdown_server():
2625
@app.route('/shutdown', methods=['POST'])
2726
def shutdown():
2827
write()
29-
s.clear()
28+
session.clear()
3029
shutdown_server()
3130
print len(cache_records())
3231
try:
3332
if len(cache_records()) >= 1:
34-
hriks(
33+
flash(
3534
'Notification : Records Saved !'
3635
)
3736
except Exception:
38-
hriks(
37+
flash(
3938
'Notification : New file created with name "%s" ' % (
4039
sys.argv[1]
4140
)
4241
)
43-
return render('layouts/shutdown.html')
42+
return render_template('layouts/shutdown.html')
4443

4544

4645
# Controllers.
@@ -50,7 +49,7 @@ def write():
5049
for i in records:
5150
print i
5251
except Exception:
53-
hriks(
52+
flash(
5453
'New File created with name %s' % (
5554
sys.argv[1]
5655
)
@@ -59,19 +58,14 @@ def write():
5958
fieldnames = [
6059
'ids', 'student_name', 'academics', 'sports', 'social'
6160
]
62-
writer = csv.DictWriter(
63-
csvfile, fieldnames=fieldnames
64-
)
61+
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
6562

6663
writer.writeheader()
6764
try:
6865
for record in records:
6966
writer.writerow(record)
7067
except Exception:
71-
hriks('New File created with name %s' % (
72-
sys.argv[1]
73-
)
74-
)
68+
flash('New File created with name %s' % (sys.argv[1]))
7569

7670

7771
def read():
@@ -83,22 +77,16 @@ def read():
8377

8478

8579
def read_cache():
86-
data = s['data']
80+
data = session['data']
8781
return data
8882

8983

9084
def write_cache(student_name, academics, sports, social):
91-
import pdb; pdb.set_trace()
9285
data = cache_records()
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()
86+
if len(data) > 20:
87+
count = session['count']
88+
print count
89+
data = delete_cache(count)
10290
new_dict = {}
10391
new_dict['ids'] = ids_get()
10492
new_dict['student_name'] = student_name
@@ -122,12 +110,13 @@ def update_cache(ids, student_name, academics, sports, social):
122110

123111
def delete_cache(ids):
124112
data = cache_records()
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
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
131120

132121

133122
def ids_get():
@@ -146,80 +135,78 @@ def cache_records():
146135

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

151140

152141
@app.route('/delete', methods=['GET', 'POST'])
153142
def delete():
154143
if request.method == 'POST':
155-
s['ids'] = request.form['submit']
156-
s['delete'] = request.form['delete']
157-
ids = s['ids']
144+
session['ids'] = request.form['submit']
145+
session['delete'] = request.form['delete']
146+
ids = session['ids']
158147
data = delete_cache(ids)
159-
s['data'] = data
160-
hriks(
148+
session['data'] = data
149+
flash(
161150
'Notification : Successfully deleted records with ID %s' % (ids)
162151
)
163-
s['ids'] = None
164-
s['student_name'] = None
152+
session['ids'] = None
153+
session['student_name'] = None
165154
return redirect(url_for('home'))
166155

167156

168157
@app.route('/edit', methods=['GET', 'POST'])
169158
def edit():
170159
form = Update_student_info(request.form)
171-
s['ids'] = request.form['submit']
172-
ids = s['ids']
173-
s['student_name'] = request.form['name']
174-
name = s['student_name']
160+
session['ids'] = request.form['submit']
161+
ids = session['ids']
162+
session['student_name'] = request.form['name']
163+
name = session['student_name']
175164
if request.method == 'POST':
176165
return redirect(url_for('update'))
177-
return render(
166+
return render_template(
178167
'forms/update.html', form=form, ids=ids, name=name
179168
)
180169

181170

182171
@app.route('/update', methods=['GET', 'POST'])
183172
def update():
184173
form = Update_student_info(request.form)
185-
ids = s['ids']
186-
name = s['student_name']
174+
ids = session['ids']
175+
name = session['student_name']
187176
if request.method == 'POST':
188177
academics = form.academics.data
189178
sports = form.sports.data
190179
social = form.social.data
191180
if academics is None or int(academics) > 100:
192-
hriks(
181+
flash(
193182
'Notification : %s is not a valid score.\
194183
Please enter valid score for Academics' % (
195184
academics))
196185
return redirect(url_for('update'))
197186
elif sports is None or int(sports) > 100:
198-
hriks(
187+
flash(
199188
'Notification : %s is not a valid score.\
200189
Please enter valid score for Sports' % (
201190
sports))
202191
return redirect(url_for('update'))
203192
elif social is None or int(social) > 100:
204-
hriks(
193+
flash(
205194
'Notification : %s is not a valid score.\
206195
Please enter valid score for Social' % (
207196
social))
208197
return redirect(url_for('update'))
209198
else:
210199
data = update_cache(
211-
s['ids'], s['student_name'],
200+
session['ids'], session['student_name'],
212201
academics, sports, social
213202
)
214-
s['data'] = data
215-
hriks(
203+
session['data'] = data
204+
flash(
216205
'Notification : Successfully updated record for %s \
217206
with ID %s' % (name, ids)
218207
)
219-
return redirect(
220-
url_for('home')
221-
)
222-
return render(
208+
return redirect(url_for('home'))
209+
return render_template(
223210
'forms/update.html', form=form, ids=ids, name=name
224211
)
225212

@@ -229,7 +216,7 @@ def addinfo():
229216
try:
230217
ids = ids_get()
231218
except Exception:
232-
hriks(
219+
flash(
233220
'Notification : File doesnot exits. Shutdown to create a \
234221
file with name %s' % (sys.argv[1])
235222
)
@@ -241,27 +228,27 @@ def addinfo():
241228
sports = form.sports.data
242229
social = form.social.data
243230
if student_name is None:
244-
hriks(
231+
flash(
245232
'Notification : %s is not vaild. Please Enter valid \
246233
name' % student_name
247234
)
248235
return redirect(url_for('addinfo'))
249236
elif academics is None or int(academics) > 100:
250-
hriks(
237+
flash(
251238
'Notification : %s is not a valid score.\
252239
Please enter valid score for Academics' % (
253240
academics)
254241
)
255242
return redirect(url_for('addinfo'))
256243
elif sports is None or int(sports) > 100:
257-
hriks(
244+
flash(
258245
'Notification : %s is not a valid score.\
259246
Please enter valid score for Sports' % (
260247
sports)
261248
)
262249
return redirect(url_for('addinfo'))
263250
elif social is None or int(social) > 100:
264-
hriks(
251+
flash(
265252
'Notification : %s is not a valid score.\
266253
Please enter valid score for Social' % (
267254
social)
@@ -271,28 +258,28 @@ def addinfo():
271258
data = write_cache(
272259
student_name, academics, sports, social
273260
)
274-
s['data'] = data
275-
hriks(
261+
session['data'] = data
262+
flash(
276263
'Notification : %s successfully added to records \
277264
with ID %s' % (
278265
student_name, ids
279266
)
280267
)
281268
return redirect(url_for('home'))
282-
return render('forms/register.html', form=form, ids=ids)
269+
return render_template('forms/register.html', form=form, ids=ids)
283270

284271

285272
@app.route('/search', methods=['GET', 'POST'])
286273
def search():
287274
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
288275
inverse = [(value, key) for key, value in count.items()]
289-
s['count'] = max(inverse)[0]
276+
session['count'] = max(inverse)[0]
290277
if request.method == 'POST':
291278
search = request.form['search']
292279
try:
293280
records = cache_records()
294281
except Exception:
295-
hriks(
282+
flash(
296283
'Notification : No such file present.\
297284
Please provide a valid file or shutdown to create file'
298285
)
@@ -305,24 +292,24 @@ def search():
305292
if i['ids'] == int(search):
306293
count[int(search)] = count[int(search)] + 1
307294
except Exception:
308-
hriks('Notification : Invalid ID provided, Please provide ID')
295+
flash('Notification : Invalid ID provided, Please provide ID')
309296
return redirect(url_for('home'))
310-
return render(
297+
return render_template(
311298
'pages/placeholder.search.html', match=match, search=search
312299
)
313-
return render('pages/placeholder.search.html', search=search)
300+
return render_template('pages/placeholder.search.html', search=search)
314301

315302

316303
# Error handlers.
317304

318305
@app.errorhandler(500)
319306
def internal_error(error):
320-
return render('errors/500.html'), 500
307+
return render_template('errors/500.html'), 500
321308

322309

323310
@app.errorhandler(404)
324311
def not_found_error(error):
325-
return render('errors/404.html'), 404
312+
return render_template('errors/404.html'), 404
326313

327314

328315
if not app.debug:

0 commit comments

Comments
 (0)