-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroutes.py
134 lines (119 loc) · 5.59 KB
/
routes.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
from flask import render_template, request, redirect, url_for, flash, abort
from flask_login import login_user, current_user, logout_user, login_required
from forms import TitleSearchForm, AuthorSearchForm, LoginForm, RegistrationForm, RequestResetForm, ResetPasswordForm
from models import User, Searches
from datetime import datetime
import email_validator, smtplib
from mail import send_reset_email
def init_routes(app, libgen, db, login_manager, bcrypt):
@login_manager.user_loader
def load_user(user_id):
return User.query.get(int(user_id))
@app.route('/')
def libhome():
return render_template('index.html')
@app.route('/authorsearch', methods=['GET', 'POST'])
@login_required
def authorsearch():
form = AuthorSearchForm()
if form.validate_on_submit():
filters = {}
if form.language.data:
filters['Language'] = form.language.data
if form.year.data:
filters['Year'] = form.year.data
if form.extension.data:
filters['Extension'] = form.extension.data
results = libgen.search_author_filtered(form.author.data, filters, exact_match=False)
new_search = Searches(item=form.author.data, user_id=current_user.id)
db.session.add(new_search)
db.session.commit()
return render_template('results.html', results=results)
return render_template('authorsearch.html', form=form)
@app.route('/titlesearch', methods=['GET', 'POST'])
@login_required
def titlesearch():
form = TitleSearchForm()
if form.validate_on_submit():
filters = {}
if form.language.data:
filters['Language'] = form.language.data
if form.year.data:
filters['Year'] = form.year.data
if form.extension.data:
filters['Extension'] = form.extension.data
results = libgen.search_title_filtered(form.title.data, filters, exact_match=False)
new_search = Searches(item=form.title.data, user_id=current_user.id)
db.session.add(new_search)
db.session.commit()
return render_template('results.html', results=results)
return render_template('titlesearch.html', form=form)
@app.route('/login', methods=['GET', 'POST'])
def login():
if current_user.is_authenticated:
return redirect(url_for('libhome'))
form = LoginForm()
if form.validate_on_submit():
user = User.query.filter_by(email=form.email.data).first()
if user and bcrypt.check_password_hash(user.password, form.password.data):
login_user(user, remember=form.remember.data)
next_page = request.args.get('next')
return redirect(next_page) if next_page else redirect(url_for('libhome'))
else:
flash('Login Unsuccessful. Please check email and password', 'danger')
return render_template('login.html', form=form)
@app.route('/logout')
def logout():
logout_user()
return redirect(url_for('libhome'))
@app.route('/reset_password', methods=['GET', 'POST'])
def reset_request():
if current_user.is_authenticated:
return redirect(url_for('libhome'))
form = RequestResetForm()
if form.validate_on_submit():
user = User.query.filter_by(email=form.email.data).first()
if user:
send_reset_email(user)
flash('An email has been sent with instructions to reset your password.', 'info')
return redirect(url_for('login'))
return render_template('reset_request.html', form=form)
@app.route('/reset_password/<token>', methods=['GET', 'POST'])
def reset_token(token):
if current_user.is_authenticated:
return redirect(url_for('libhome'))
user = User.verify_reset_token(token)
if user is None:
flash('That is an invalid or expired token', 'warning')
return redirect(url_for('reset_request'))
form = ResetPasswordForm()
if form.validate_on_submit():
hashed_password = bcrypt.generate_password_hash(form.password.data).decode('utf-8')
user.password = hashed_password
db.session.commit()
flash('Your password has been updated! You are now able to log in', 'success')
return redirect(url_for('login'))
return render_template('reset_token.html', form=form, token=token)
@app.route('/register', methods=['GET', 'POST'])
def register():
if current_user.is_authenticated:
return redirect(url_for('libhome'))
form = RegistrationForm()
if form.validate_on_submit():
hashed_password = bcrypt.generate_password_hash(form.password.data).decode('utf-8')
user = User(username=form.username.data, email=form.email.data, password=hashed_password)
db.session.add(user)
db.session.commit()
flash('Your account has been created! You are now able to log in', 'success')
return redirect(url_for('login'))
return render_template('register.html', form=form)
@app.route('/search-result')
@login_required
def results():
return render_template('results.html')
@app.route('/history')
@login_required
def history():
# Recuperar todas las búsquedas del usuario actual
searches = Searches.query.filter_by(user_id=current_user.id).all()
return render_template('history.html', searches=searches)