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
4
3
from logging import Formatter , FileHandler
5
4
from forms import *
6
5
import logging
@@ -26,21 +25,21 @@ def shutdown_server():
26
25
@app .route ('/shutdown' , methods = ['POST' ])
27
26
def shutdown ():
28
27
write ()
29
- s .clear ()
28
+ session .clear ()
30
29
shutdown_server ()
31
30
print len (cache_records ())
32
31
try :
33
32
if len (cache_records ()) >= 1 :
34
- hriks (
33
+ flash (
35
34
'Notification : Records Saved !'
36
35
)
37
36
except Exception :
38
- hriks (
37
+ flash (
39
38
'Notification : New file created with name "%s" ' % (
40
39
sys .argv [1 ]
41
40
)
42
41
)
43
- return render ('layouts/shutdown.html' )
42
+ return render_template ('layouts/shutdown.html' )
44
43
45
44
46
45
# Controllers.
@@ -50,7 +49,7 @@ def write():
50
49
for i in records :
51
50
print i
52
51
except Exception :
53
- hriks (
52
+ flash (
54
53
'New File created with name %s' % (
55
54
sys .argv [1 ]
56
55
)
@@ -59,19 +58,14 @@ def write():
59
58
fieldnames = [
60
59
'ids' , 'student_name' , 'academics' , 'sports' , 'social'
61
60
]
62
- writer = csv .DictWriter (
63
- csvfile , fieldnames = fieldnames
64
- )
61
+ writer = csv .DictWriter (csvfile , fieldnames = fieldnames )
65
62
66
63
writer .writeheader ()
67
64
try :
68
65
for record in records :
69
66
writer .writerow (record )
70
67
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 ]))
75
69
76
70
77
71
def read ():
@@ -83,22 +77,16 @@ def read():
83
77
84
78
85
79
def read_cache ():
86
- data = s ['data' ]
80
+ data = session ['data' ]
87
81
return data
88
82
89
83
90
84
def write_cache (student_name , academics , sports , social ):
91
- import pdb ; pdb .set_trace ()
92
85
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 )
102
90
new_dict = {}
103
91
new_dict ['ids' ] = ids_get ()
104
92
new_dict ['student_name' ] = student_name
@@ -122,12 +110,13 @@ def update_cache(ids, student_name, academics, sports, social):
122
110
123
111
def delete_cache (ids ):
124
112
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
131
120
132
121
133
122
def ids_get ():
@@ -146,80 +135,78 @@ def cache_records():
146
135
147
136
@app .route ('/' , methods = ['GET' , 'POST' ])
148
137
def home ():
149
- return render ('pages/placeholder.home.html' )
138
+ return render_template ('pages/placeholder.home.html' )
150
139
151
140
152
141
@app .route ('/delete' , methods = ['GET' , 'POST' ])
153
142
def delete ():
154
143
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' ]
158
147
data = delete_cache (ids )
159
- s ['data' ] = data
160
- hriks (
148
+ session ['data' ] = data
149
+ flash (
161
150
'Notification : Successfully deleted records with ID %s' % (ids )
162
151
)
163
- s ['ids' ] = None
164
- s ['student_name' ] = None
152
+ session ['ids' ] = None
153
+ session ['student_name' ] = None
165
154
return redirect (url_for ('home' ))
166
155
167
156
168
157
@app .route ('/edit' , methods = ['GET' , 'POST' ])
169
158
def edit ():
170
159
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' ]
175
164
if request .method == 'POST' :
176
165
return redirect (url_for ('update' ))
177
- return render (
166
+ return render_template (
178
167
'forms/update.html' , form = form , ids = ids , name = name
179
168
)
180
169
181
170
182
171
@app .route ('/update' , methods = ['GET' , 'POST' ])
183
172
def update ():
184
173
form = Update_student_info (request .form )
185
- ids = s ['ids' ]
186
- name = s ['student_name' ]
174
+ ids = session ['ids' ]
175
+ name = session ['student_name' ]
187
176
if request .method == 'POST' :
188
177
academics = form .academics .data
189
178
sports = form .sports .data
190
179
social = form .social .data
191
180
if academics is None or int (academics ) > 100 :
192
- hriks (
181
+ flash (
193
182
'Notification : %s is not a valid score.\
194
183
Please enter valid score for Academics' % (
195
184
academics ))
196
185
return redirect (url_for ('update' ))
197
186
elif sports is None or int (sports ) > 100 :
198
- hriks (
187
+ flash (
199
188
'Notification : %s is not a valid score.\
200
189
Please enter valid score for Sports' % (
201
190
sports ))
202
191
return redirect (url_for ('update' ))
203
192
elif social is None or int (social ) > 100 :
204
- hriks (
193
+ flash (
205
194
'Notification : %s is not a valid score.\
206
195
Please enter valid score for Social' % (
207
196
social ))
208
197
return redirect (url_for ('update' ))
209
198
else :
210
199
data = update_cache (
211
- s ['ids' ], s ['student_name' ],
200
+ session ['ids' ], session ['student_name' ],
212
201
academics , sports , social
213
202
)
214
- s ['data' ] = data
215
- hriks (
203
+ session ['data' ] = data
204
+ flash (
216
205
'Notification : Successfully updated record for %s \
217
206
with ID %s' % (name , ids )
218
207
)
219
- return redirect (
220
- url_for ('home' )
221
- )
222
- return render (
208
+ return redirect (url_for ('home' ))
209
+ return render_template (
223
210
'forms/update.html' , form = form , ids = ids , name = name
224
211
)
225
212
@@ -229,7 +216,7 @@ def addinfo():
229
216
try :
230
217
ids = ids_get ()
231
218
except Exception :
232
- hriks (
219
+ flash (
233
220
'Notification : File doesnot exits. Shutdown to create a \
234
221
file with name %s' % (sys .argv [1 ])
235
222
)
@@ -241,27 +228,27 @@ def addinfo():
241
228
sports = form .sports .data
242
229
social = form .social .data
243
230
if student_name is None :
244
- hriks (
231
+ flash (
245
232
'Notification : %s is not vaild. Please Enter valid \
246
233
name' % student_name
247
234
)
248
235
return redirect (url_for ('addinfo' ))
249
236
elif academics is None or int (academics ) > 100 :
250
- hriks (
237
+ flash (
251
238
'Notification : %s is not a valid score.\
252
239
Please enter valid score for Academics' % (
253
240
academics )
254
241
)
255
242
return redirect (url_for ('addinfo' ))
256
243
elif sports is None or int (sports ) > 100 :
257
- hriks (
244
+ flash (
258
245
'Notification : %s is not a valid score.\
259
246
Please enter valid score for Sports' % (
260
247
sports )
261
248
)
262
249
return redirect (url_for ('addinfo' ))
263
250
elif social is None or int (social ) > 100 :
264
- hriks (
251
+ flash (
265
252
'Notification : %s is not a valid score.\
266
253
Please enter valid score for Social' % (
267
254
social )
@@ -271,28 +258,28 @@ def addinfo():
271
258
data = write_cache (
272
259
student_name , academics , sports , social
273
260
)
274
- s ['data' ] = data
275
- hriks (
261
+ session ['data' ] = data
262
+ flash (
276
263
'Notification : %s successfully added to records \
277
264
with ID %s' % (
278
265
student_name , ids
279
266
)
280
267
)
281
268
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 )
283
270
284
271
285
272
@app .route ('/search' , methods = ['GET' , 'POST' ])
286
273
def search ():
287
274
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
288
275
inverse = [(value , key ) for key , value in count .items ()]
289
- s ['count' ] = max (inverse )[0 ]
276
+ session ['count' ] = max (inverse )[0 ]
290
277
if request .method == 'POST' :
291
278
search = request .form ['search' ]
292
279
try :
293
280
records = cache_records ()
294
281
except Exception :
295
- hriks (
282
+ flash (
296
283
'Notification : No such file present.\
297
284
Please provide a valid file or shutdown to create file'
298
285
)
@@ -305,24 +292,24 @@ def search():
305
292
if i ['ids' ] == int (search ):
306
293
count [int (search )] = count [int (search )] + 1
307
294
except Exception :
308
- hriks ('Notification : Invalid ID provided, Please provide ID' )
295
+ flash ('Notification : Invalid ID provided, Please provide ID' )
309
296
return redirect (url_for ('home' ))
310
- return render (
297
+ return render_template (
311
298
'pages/placeholder.search.html' , match = match , search = search
312
299
)
313
- return render ('pages/placeholder.search.html' , search = search )
300
+ return render_template ('pages/placeholder.search.html' , search = search )
314
301
315
302
316
303
# Error handlers.
317
304
318
305
@app .errorhandler (500 )
319
306
def internal_error (error ):
320
- return render ('errors/500.html' ), 500
307
+ return render_template ('errors/500.html' ), 500
321
308
322
309
323
310
@app .errorhandler (404 )
324
311
def not_found_error (error ):
325
- return render ('errors/404.html' ), 404
312
+ return render_template ('errors/404.html' ), 404
326
313
327
314
328
315
if not app .debug :
0 commit comments