-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
296 lines (242 loc) · 9.91 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
from datetime import date, timedelta, datetime
from flask import render_template, redirect, request, url_for, flash, Response, session
from flask_login import login_user, login_required, logout_user, current_user
from werkzeug.utils import secure_filename
from digicert import app, db
from digicert.forms import LoginForm, RegistrationForm, ForgotPasswordForm, \
ContactUsForm, SubscribeForm, AddEventForm, \
AddCertificateForm
from digicert.models import User, Certificate, Event
from confirm_token import generate_confirmation_token, confirm_token
from send_mail import send_mail
@app.route('/', methods=['GET', 'POST'])
def index():
contact_form = ContactUsForm()
subscribe_form = SubscribeForm()
if contact_form.validate_on_submit():
pass
if subscribe_form.validate_on_submit():
pass
return render_template('index.html', contact_form=contact_form, subscribe_form=subscribe_form)
@app.route('/login', methods=['GET', 'POST'])
def login():
form = LoginForm()
if form.validate_on_submit():
user = User.query.filter_by(email=form.email.data).first()
if user is not None and user.check_password(form.password.data):
login_user(user, remember=form.remember_me.data)
flash('Logged in successfully.', 'success')
next_page = request.args.get('next')
if next_page is None or not next_page[0] == '/':
next_page = url_for('index')
return redirect(next_page)
else:
flash('Invalid username / password', 'error')
return render_template('login.html', form=form)
@app.route('/register', methods=['GET', 'POST'])
def register():
form = RegistrationForm()
if form.validate_on_submit():
username = str(form.email.data).split('@')[0]
check_exist = User.query.filter_by(email=form.email.data).first()
if check_exist:
flash('User already exist', 'error')
return redirect(url_for('login'))
user = User(email=form.email.data,
first_name=form.first_name.data,
last_name=form.last_name.data,
username=username,
password=form.password.data)
db.session.add(user)
db.session.commit()
token = generate_confirmation_token(email=form.email.data)
session['token'] = token
session['check'] = True
return redirect(url_for('confirm_email_view',
name=form.first_name.data,
email=form.email.data))
return render_template('register.html', form=form)
@app.route('/verify/<token>')
@login_required
def confirm_email(token):
email = None
try:
email = confirm_token(token)
except Exception as e:
flash('The confirmation link is invalid or expired.', 'error')
user = User.query.filter_by(email=email).first_or_404()
if user.confirmed:
flash('Account already confirmed', 'warning')
else:
user.confirmed = True
user.confirmed_on = datetime.now()
db.session.add(user)
db.session.commit()
flash('Account confirmed', 'success')
return redirect(url_for('index'))
@app.route('/confirm_email')
def confirm_email_view():
token = session['token']
name = request.args['name']
email = request.args['email']
try:
send_mail(name, email, token)
flash('Mail send successfully', 'success')
except Exception as e:
flash('Something went wrong! Please try again later', 'error')
User.query.filter_by(email=email).delete()
redirect(url_for('index'))
return render_template('confirm_email.html', email=email, name=name)
@app.route('/logout')
@login_required
def logout():
logout_user()
flash('You logged out!', 'success')
return redirect(url_for('index'))
@app.route('/forgot_password', methods=['GET', 'POST'])
def forgot_password():
form = ForgotPasswordForm()
if form.validate_on_submit():
if form.check_email(form.email):
email = form.email.data
redirect(url_for('index'))
return render_template('forgot-password.html', form=form)
@app.errorhandler(404)
def not_found(e):
print(e)
return render_template("404.html"), 404
# All events
@app.route('/events/<event_type>')
def events(event_type):
total_events = Event.query.count()
live_events = Event.query.filter(Event.end_date >= date.today(), Event.start_date <= date.today()).all()
upcoming_events = Event.query.filter(Event.start_date > date.today()).all()
ended_events = Event.query.filter(Event.end_date < date.today()).all()
if event_type == 'live':
all_events = live_events
elif event_type == 'upcoming':
all_events = upcoming_events
elif event_type == 'ended':
all_events = ended_events
else:
return 'Bad Request', 404
return render_template('events.html',
type=event_type.capitalize(),
events=all_events,
total_events=total_events,
total_live=len(live_events),
total_upcoming=len(upcoming_events),
total_ended=len(ended_events)
)
@app.route('/certificates/<string:cert_type>')
@login_required
def certificates(cert_type):
uploaded = Certificate.query.filter_by(user_id=current_user.id, cert_type='uploaded').all()
official = Certificate.query.filter_by(user_id=current_user.id, cert_type='official').all()
all_cert = Certificate.query.filter_by(user_id=current_user.id).all()
print(uploaded, official, all_cert)
if cert_type == 'uploaded':
all_certificates = uploaded
elif cert_type == 'official':
all_certificates = official
else:
return 'Bad Request', 404
total_cert = len(all_cert)
total_uploaded = len(uploaded)
total_official = len(official)
this_year = date.today().year
total_this_year = 0
for cert in all_cert:
if cert.obtained_date.year == this_year:
total_this_year += 1
return render_template('certificates.html',
certificates=all_certificates,
cert_type=cert_type.capitalize(),
total_cert=total_cert,
total_uploaded=total_uploaded,
total_official=total_official,
total_this_year=total_this_year,
this_year=this_year
)
@app.route('/add_certificate', methods=['GET', 'POST'])
@login_required
def add_certificate():
form = AddCertificateForm()
if form.validate_on_submit():
title = form.title.data
description = form.description.data
obtained_date = form.obtained_date.data
cert_image = form.certificate_image.data
filename = secure_filename(cert_image.filename)
mimetype = cert_image.mimetype
if not filename or not mimetype:
return 'Bad upload!', 400
new_cert = Certificate(title=title,
description=description,
obtained_date=obtained_date,
cert_img=cert_image.read(),
cert_img_mimetype=mimetype,
cert_type='uploaded',
user_id=current_user.id
)
db.session.add(new_cert)
db.session.commit()
flash('Certificate Uploaded', 'success')
return redirect(url_for('certificates', cert_type='uploaded'))
return render_template('add_certificate.html',
form=form,
today_date=date.today(),
start_date=date.today() - timedelta(days=36500)
)
@app.route('/add_event', methods=['GET', 'POST'])
@login_required
def add_event():
form = AddEventForm()
if form.validate_on_submit():
title = form.title.data
description = form.description.data
start_date = form.start_date.data
end_date = form.end_date.data
logo = form.logo.data
mode = form.mode.data
filename = secure_filename(logo.filename)
mimetype = logo.mimetype
if not filename or not mimetype:
return 'Bad upload!', 400
new_cert = Event(title=title,
description=description,
start_date=start_date,
end_date=end_date,
logo=logo.read(),
logo_mimetype=mimetype,
mode=mode
)
db.session.add(new_cert)
db.session.commit()
flash('New event added', 'success')
return redirect(url_for('events', event_type='upcoming'))
return render_template('add_event.html',
form=form,
today_date=date.today(),
max_date=date.today() + timedelta(days=365)
)
@app.route('/events/details/<string:slug>')
def event_details(slug):
details = Event.query.filter_by(slug=slug).first()
return render_template('event_details.html', details=details)
@app.route('/certificates/preview/<string:slug>')
@login_required
def view_certificate(slug):
cert = Certificate.query.filter_by(slug=slug, user_id=current_user.id).first()
if not cert:
return 'Img Not Found!', 404
return Response(cert.cert_img, mimetype=cert.cert_img_mimetype)
@app.route('/show_image/<int:event_id>')
def show_event_image(event_id):
img = Event.query.filter_by(id=event_id).first()
return Response(img.logo, mimetype=img.logo_mimetype)
@app.route('/resend_confirm_mail')
def resend_confirm_mail():
return render_template('confirm_email.html')
if __name__ == '__main__':
app.run()