-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
364 lines (281 loc) · 12.1 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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
from flask import Flask, render_template, flash, url_for, redirect, request, session, send_file, send_from_directory, jsonify
from content_management import *
from passlib.hash import sha256_crypt
from wtforms import Form, BooleanField, PasswordField, validators, StringField, TextAreaField, DateField
from wtforms.fields.html5 import EmailField
from wtforms.fields import SelectField, IntegerField
from pymysql import escape_string as thwart
from connection import *
import gc
from flask_mail import Mail, Message
import os
from get18_back import *
from wrappers import *
import models
app = Flask(__name__, instance_path="/home/sriteja/PycharmProjects/Blood Bank/protected")
app.secret_key = 'sriteja_charan'
app.config.update(
DEBUG=True,
MAIL_SERVER='students.iiit.ac.in',
MAIL_PORT=587,
MAIL_USE_TLS=True,
MAIL_USERNAME='[email protected]',
MAIL_PASSWORD='Mail Password'
)
mail = Mail(app)
USER_DICT = content()
bloodgroups_lis = bloodgroups()
states = state_list()
gender_lis = gender_list()
cities_dict = cities_list()
#Required Classes
class RequestForm(Form):
bloodgroup = SelectField('Blood Group', [validators.InputRequired()], coerce=str, choices=bloodgroups_lis,
render_kw={"placeholder": "Select blood group"})
state = SelectField('State or Union Teritorry', [validators.InputRequired()], coerce=str, choices=states)
class RegistrationForm(Form):
username = StringField('Username', [validators.Length(min=4, max=20)], render_kw={"placeholder": "Unique username"})
firstname = StringField('First Name', [validators.InputRequired()])
lastname = StringField('Last Name')
email = StringField('Email Address', [validators.Email()], render_kw={"placeholder": "Eg: [email protected]"})
phonenumber = StringField('Phone Number', [validators.InputRequired()], render_kw={"placeholder": "Indian mobile number"})
date = DateField('Date Of Birth', [validators.InputRequired()], render_kw={"placeholder": "Format: YYYY-MM-DD"})
# age = IntegerField('Age', [validators.InputRequired()], render_kw={"placeholder":">16 to donate blood"})
weight = IntegerField('Weight in kgs', [validators.InputRequired()], render_kw={"placeholder": "50<yourWeight<150"})
bloodgroup = SelectField('Blood Group', [validators.InputRequired()], coerce=str, choices=bloodgroups_lis, render_kw={"placeholder": "Select blood group"})
gender = SelectField('Gender', [validators.InputRequired()], coerce=str, choices=gender_lis)
pdonations = IntegerField('Previous Donations', default=0)
address = TextAreaField('Address', [validators.InputRequired()])
# city = SelectField('City',[validators.Length(max=30),validators.InputRequired()])
# state = StringField('State', [validators.Length(max=30),validators.InputRequired()], render_kw={"placeholder":"Please select presently statying State or Union Territory"})
state = SelectField('State or Union Teritorry', [validators.InputRequired()], coerce=str, choices=states)
password = PasswordField('Password', [validators.Length(min=7), validators.InputRequired(), validators.EqualTo('confirm', message="Passwords must match.")], render_kw={"placeholder": "Min-7, 1 Upper, 1 lower, 1 spec char"})
confirm = PasswordField('Repeat Password', render_kw={"placeholder": "Must be equal to previoius field"})
accept_tos = BooleanField('I accept the <a href="/tos">Terms of Service</a> and <a href="/privacy/">Privacy Notice</a> (Last Updated Apr 6 2018)', [validators.InputRequired()], id='accept_tos',render_kw={"required":"", "onclick": "check_selected()"})
def get_name(uid):
c,conn = connection()
c.execute("select firstname, lastname from user where uid = '{0}'".format(uid))
name = c.fetchone()
name = name[0]+ ' ' + name[1]
c.close()
return name
# Error Handlers
@app.errorhandler(404)
def page_not_found(e):
return render_template("404.html")
@app.errorhandler(405)
def method_not_found(e):
try:
return render_template("405.html")
except Exception as e:
return render_template("500.html")
@app.errorhandler(500)
def internal_server():
return render_template("500.html")
# @app.route('/<path:url_path>/')
# @app.route('/')
# def home_page(url_path='/'):
# return render_template('main.html')
@app.route('/')
def index():
return render_template("index.html")
@app.route('/contact/')
def contact():
return render_template("contact.html")
@app.route('/contributions/')
def contributions():
bd_dict = models.get_bd_dict()
return render_template('about-us.html', bd_dict=bd_dict)
@app.route('/reg/')
def reg():
return render_template('registeration_form.html')
@app.route('/faq/')
def faq():
return render_template("faq.html")
@app.route('/precautions/')
def precautions():
return render_template("precautions.html")
@app.route('/temp/')
@logout_required
def heck():
print(request.path)
return request.path
@app.route('/dashboard/', methods=['GET', 'POST'])
@login_required
def dashboard():
form = RequestForm(request.form)
exit_status, data_list, city, state, blood_group, notifications, names = models.make_dashboard(form, request, flash, session)
if exit_status is 0:
return render_template("find_donors.html", user_list=data_list, city=city, state=state, blood_group=blood_group)
elif exit_status is 1:
return render_template("dashboard.html", USER_DICT=USER_DICT, cities_dict=cities_dict, states=states,
bloodgroups_lis=bloodgroups_lis, form=form, noti_names=zip(notifications, names))
@app.route('/make_a_request/', methods=['GET', 'POST'])
def make_request():
models.make_blood_request(request, flash, session)
return redirect(url_for('dashboard'))
@app.route('/return-file/')
def return_file():
return send_file('/home/sriteja/PycharmProjects/Blood Bank/logo.png', attachment_filename='logo.png')
@app.route('/filedown/')
def file_downloads():
return render_template('downloads.html')
@app.route('/slashboard/')
def slashboard():
try: pass
# return render_template("dashboard.html", USER_DICT=HFS)
except Exception as e:
return render_template("500.html", error=e)
@app.route('/verify', methods=['GET'])
@app.route('/verify/<path:urlpath>', methods=['GET'])
def verify_email(urlpath=0):
try:
temp = models.check_verify_link(urlpath)
if temp == "verification_success.html":
return render_template("verification_success.html")
else:
return "temp"
except Exception as e:
return str(e)
@app.route('/sendmail/')
def send_mail():
try:
send_verification_mail('teja', '[email protected]', "https://www.google.com")
# msg = Message("Success",
# sender=("Blood Bank", "[email protected]"),
# recipients=['[email protected]', '[email protected]'])
# msg.html = render_template("Welcome to Republic.html", username = "teja", link = "google.com")
# mail.send(msg)
return render_template('Welcome to Blood Bank.html', username='teja', link='https://www.google.com')
except Exception as e:
return str(e)
def gen_verify_link(username, email):
link = '127.0.0.1:5000/verify/' + sha256_crypt.encrypt(str(username)) + sha256_crypt.encrypt(str(email))
return link
def send_verification_mail(username, email, link):
try:
msg = Message("Account verification",
sender=("Blood Bank", "[email protected]"),
recipients=[email, '[email protected]'])
msg.body = "Hello " + username +\
",\n Thanks for registering as a blood donor. To avoid spam," \
"please verify your email account by clicking on this link" + link
msg.html = render_template("Welcome to Blood Bank.html", username=username, link=link)
mail.send(msg)
return 'Mail sent'
except Exception as e:
return str(e)
def send_mail_change(username, email, link):
try:
print("came_here")
msg = Message("Email Changed",
sender=("Blood Bank", "[email protected]"),
recipients=['[email protected]', email])
msg.body = "Hello" + username +\
",\n As you updated your email id, you need to your email," \
"please verify it by clicking on this link" + link
msg.html = render_template("email_changed.html", username=username, link=link)
mail.send(msg)
return 'Mail sent'
except Exception as e:
return str(e)
@app.route('/sve/')
def sev():
try:
send_verification_mail('teja', '[email protected]',
gen_verify_link('teja', '[email protected]'))
return "mail_sent"
except Exception as e:
return str(e)
@app.route('/include/')
def include_example():
try:
replies = {'sri': 'cool',
'teja': 'good',
'chinni': 'nice',
'charan': 'super'}
return render_template('include.html', replies=replies)
except Exception as e:
return str(e)
def special_requirement(f):
@wraps(f)
def wrap(*args, **kwargs):
try:
if 'sriteja' == session['username']:
return f(*args, **kwargs)
else:
return redirect(url_for('dashboard'))
except Exception as e:
return redirect(url_for('dashboard'))
return wrap
@app.route('/secret/<path:filename>')
@special_requirement
def protected(filename):
try:
return send_from_directory(os.path.join(app.instance_path, ''), filename)
except Exception as e:
return redirect(url_for('dashboard'))
@app.route('/interactive/')
def interactive():
try:
return render_template("interactive.html")
except Exception as e:
return str(e)
@app.route('/jinjaman/')
def jinja_tem():
try:
gc.collect()
data = [15, '15', 'Python is gd', 'python, java, c, c++', '<p><strong>hello</strong></p>']
return render_template("jinja_tem.html", data=data)
except Exception as e:
return str(e)
@app.route('/converters/')
@app.route('/converters/<path:page>')
@app.route('/converters/<page>')
def convert_url(page=1):
try:
gc.collect()
return render_template("convert_url.html", page=page)
except Exception as e:
return str(e)
@app.route('/logout/')
@login_required
def logout():
session.clear()
flash("You have been logged out")
gc.collect()
return redirect(url_for('index'))
@app.route('/update/', methods=['GET', 'POST'])
@login_required
def update_details():
exit_status, error, form, min_date, max_date, data_dict = models.update_user_details(request, session, flash, send_mail_change)
return render_template('update.html', cities_dict=cities_dict, states=states, form=form, min_date=min_date, max_date=max_date, data_dict=data_dict, error=error)
@app.route('/login/', methods=['GET', 'POST'])
@logout_required
def login_page():
error = ''
try:
exit_status, error = models.check_login(request, session, flash)
print(exit_status, error)
if not exit_status:
return redirect(url_for('dashboard'))
gc.collect()
return render_template("login.html", error=error)
except Exception as e:
flash(e)
return render_template("login.html", error=error)
@app.route('/state/')
def state_select():
return render_template('cs.html', states=state_list(), cities_dict=cities_list())
@app.route('/check/')
def check():
return render_template('temp2.html')
@app.route('/register/', methods=["GET", "POST"])
@logout_required
def register_page():
exit_status, form, max_date, min_date = models.registration(request, RegistrationForm, flash, session, send_verification_mail)
if not exit_status:
return redirect(url_for('dashboard'))
return render_template('register.html', form=form, max_date=max_date, min_date=min_date, cities_dict=cities_dict)
if __name__ == '__main__':
app.run(debug=True)