-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
89 lines (69 loc) · 2.56 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
from flask import Flask, render_template
from src.forms import PageForm
from src.link import Link
from src.models import LinksLog, db, logSearch
from src.wiki import Wiki
from config.config import Config
app = Flask(__name__)
app.config.from_object(Config)
db.init_app(app)
@app.route('/',)
def index():
'''Homepage with search form.'''
form = PageForm()
return render_template('index.html', form=form)
@app.route('/results', methods=['GET', 'POST'], defaults={'page': 0,
'name': 'submit'})
@app.route('/results/<name>/<int:page>')
def results(name, page):
''' First ten results right after submitting the form
and thne for the next pages come without a form
but with GET parametrs'''
form = PageForm()
if form.validate_on_submit():
wp = Wiki(form.page.data)
start = 0
try:
num = len(wp.wikipage.references)
except KeyError:
logSearch(form.page.data, 0)
return render_template('errors/noresult.html',
message='Stránka nemá žádné reference.')
except AttributeError:
logSearch(form.page.data, -1)
return render_template('errors/noresult.html',
message='Tato stránka neexistuje.')
logSearch(form.page.data, num)
end = 10 if num > 10 else num
else:
wp = Wiki(name)
try:
num = len(wp.wikipage.references)
except AttributeError:
return render_template('errors/noresult.html',
message='Tato stránka neexistuje.')
start = Config.ROW_LIMIT * int(page)
end = start + Config.ROW_LIMIT
if num < end:
end = num
rows = []
for index in range(start, end):
link = Link(wp.wikipage.references[index])
result = link.isLive()
rows.append(result)
# log result to db
db.session.add(LinksLog(url=result.link,
status=result.status_code,
wal=result.sign))
db.session.commit()
return render_template('results.html',
desc=wp.getDescription(),
rows=rows,
page=int(page),
limit=Config.ROW_LIMIT,
max=len(wp.wikipage.references))
@app.errorhandler(404)
def page_not_found(e):
return render_template('errors/404.html'), 404
if __name__ == '__main__':
app.run()