-
Notifications
You must be signed in to change notification settings - Fork 2
/
web.py
149 lines (112 loc) · 4.95 KB
/
web.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
from __future__ import absolute_import, division, unicode_literals
import urllib
import logging
import config
from flask import Flask, render_template, redirect, url_for, request, abort
import packages
import index
logging.info('Starting web application...')
app = Flask(__name__)
all_packages_dict = None
last_repodata_update = None
searchkit = None
@app.after_request
def cloudflare_caching(response):
if request.method == 'GET' and (not app.debug or config.CACHE_IN_DEBUG_MODE):
response.cache_control.max_age = config.CACHE_MAX_AGE
return response
@app.before_request
def check_repodata_freshness():
global last_repodata_update, all_packages_dict, searchkit
timestamp = packages.get_timestamp()
if timestamp != last_repodata_update:
logging.info('Loading package data and index...')
all_packages_dict = packages.get_all()
searchkit = index.searchkit_factory()
last_repodata_update = packages.get_timestamp()
logging.info('Done loading package data and reloading index.')
@app.context_processor
def inject_minor_os_release():
def _minor_os_release(version):
return packages.minor_os_release(all_packages_dict[version])
return dict(minor_os_release=_minor_os_release)
@app.context_processor
def inject_pretty_repo_names():
def _pretty_repo_name(repo):
return config.REPOSITORIES_PRETTY[repo]
return dict(pretty_repo_name=_pretty_repo_name)
@app.errorhandler(404)
def page_not_found(error):
return render_template('not_found.html', error=error), 404
@app.route('/')
def root():
return redirect(url_for('search', os_version='6'))
@app.route('/<os_version>/', methods=['GET', 'POST'])
def search(os_version):
if os_version not in config.OS_VERSIONS:
abort(404)
if request.method == 'POST':
search_query = request.form['search_query']
if all_packages_dict[os_version].get(search_query):
return redirect(url_for('package',
os_version=os_version,
package_name=search_query,
direct=True))
if search_query:
quoted_search_query = urllib.quote_plus(search_query)
return redirect(url_for('search_results',
os_version=os_version,
quoted_query=quoted_search_query))
return render_template('search.html', os_version=os_version)
def _package_versions_or_404(os_version, package_name):
try:
package_versions_list = all_packages_dict[os_version][package_name]
return package_versions_list
except KeyError:
return abort(404)
@app.route('/<os_version>/package/<package_name>/', methods=['GET'])
def package(os_version, package_name):
package_versions_list = _package_versions_or_404(os_version, package_name)
return render_template('package.html',
package=package_versions_list[-1],
download_url=packages.rpm_download_url(package_versions_list[-1],
os_version),
os_version=os_version,
direct_hit=request.args.get('direct'))
@app.route('/<os_version>/package/<package_name>/versions/', methods=['GET'])
def package_versions(os_version, package_name):
package_versions_list = _package_versions_or_404(os_version, package_name)
return render_template('versions.html',
versions=list(reversed(package_versions_list)),
os_version=os_version)
@app.route('/<os_version>/results/<quoted_query>/', methods=['GET'])
def search_results(os_version, quoted_query):
query = urllib.unquote_plus(quoted_query)
if not os_version in config.OS_VERSIONS:
return abort(404)
searcher = searchkit[os_version]['ix'].searcher()
parser = searchkit[os_version]['parser']
results = searcher.search(parser.parse(query), limit=config.LIMIT_RESULTS)
if len(results) >= config.LIMIT_RESULTS:
maximum_reached = True
else:
maximum_reached = False
package_versions_list = [all_packages_dict[os_version][result['name']][-1] for result in
results]
return render_template('results.html',
os_version=os_version,
results=package_versions_list,
query=query,
maximum_reached=maximum_reached)
@app.route('/<os_version>/all_packages/')
def all_packages(os_version):
newest_versions = packages.newest_versions_as_list(os_version, all_packages_dict)
return render_template('all_packages.html',
os_version=os_version,
newest_versions=newest_versions)
@app.route('/about/')
def about():
return render_template('about.html')
if __name__ == '__main__':
app.debug = True
app.run()