]> git.ipfire.org Git - people/jschlag/pbs.git/blob - src/web/handlers.py
e83a82eaeac4a9ab1eac7aff2f0d64a8c7f7922e
[people/jschlag/pbs.git] / src / web / handlers.py
1 #!/usr/bin/python
2
3 import random
4 import tornado.web
5
6 from .handlers_auth import *
7 from .handlers_base import *
8 from .handlers_builds import *
9 from .handlers_builders import *
10 from .handlers_distro import *
11 from .handlers_jobs import *
12 from .handlers_keys import *
13 from .handlers_mirrors import *
14 from .handlers_packages import *
15 from .handlers_search import *
16 from .handlers_updates import *
17 from .handlers_users import *
18
19 class IndexHandler(BaseHandler):
20 def get(self):
21 jobs = self.pakfire.jobs.get_active()
22 jobs += self.pakfire.jobs.get_latest(age="24 hours", limit=5)
23
24 # Updates
25 updates = []
26 active = True
27 for type in ("stable", "unstable", "testing"):
28 u = self.pakfire.updates.get_latest(type=type)
29 if u:
30 updates.append((type, u, active))
31 active = False
32
33 self.render("index.html", jobs=jobs, updates=updates)
34
35
36 class Error404Handler(BaseHandler):
37 def get(self):
38 raise tornado.web.HTTPError(404)
39
40
41 class StatisticsMainHandler(BaseHandler):
42 def get(self):
43 args = {}
44
45 # Build statistics.
46 args.update({
47 "builds_count" : self.pakfire.builds.count(),
48 })
49
50 # Job statistics.
51 args.update({
52 "jobs_count_all" : self.pakfire.jobs.count(),
53 "jobs_avg_build_time" : self.pakfire.jobs.get_average_build_time(),
54 })
55
56 self.render("statistics/index.html", **args)
57
58
59 class UploadsHandler(BaseHandler):
60 @tornado.web.authenticated
61 def get(self):
62 if not self.current_user.is_admin():
63 raise tornado.web.HTTPError(403)
64
65 uploads = self.pakfire.uploads.get_all()
66
67 self.render("uploads-list.html", uploads=uploads)
68
69
70 class DocsIndexHandler(BaseHandler):
71 def get(self):
72 self.render("docs-index.html")
73
74
75 class DocsBuildsHandler(BaseHandler):
76 def get(self):
77 self.render("docs-build.html")
78
79
80 class DocsUsersHandler(BaseHandler):
81 def get(self):
82 self.render("docs-users.html")
83
84
85 class DocsWhatsthisHandler(BaseHandler):
86 def get(self):
87 self.render("docs-whatsthis.html")
88
89
90 class FileDetailHandler(BaseHandler):
91 def get(self, uuid):
92 pkg, file = self.pakfire.packages.get_with_file_by_uuid(uuid)
93
94 if not file:
95 raise tornado.web.HTTPError(404, "File not found")
96
97 self.render("file-detail.html", pkg=pkg, file=file)
98
99
100 class LogHandler(BaseHandler):
101 def get(self):
102 self.render("log.html", log=self.pakfire.log)
103
104
105 class SessionsHandler(BaseHandler):
106 def prepare(self):
107 # This is only accessible for administrators.
108 if not self.current_user.is_admin():
109 raise tornado.web.HTTPError(403)
110
111 @tornado.web.authenticated
112 def get(self):
113 # Sort the sessions by user.
114 users = {}
115
116 for s in self.backend.sessions:
117 try:
118 users[s.user].append(s)
119 except KeyError:
120 users[s.user] = [s]
121
122 sessions = sorted(users.items())
123
124 self.render("sessions/index.html", sessions=sessions)
125
126
127 class RepositoryDetailHandler(BaseHandler):
128 def get(self, distro, repo):
129 distro = self.pakfire.distros.get_by_name(distro)
130 if not distro:
131 raise tornado.web.HTTPError(404)
132
133 repo = distro.get_repo(repo)
134 if not repo:
135 raise tornado.web.HTTPError(404)
136
137 limit = self.get_argument("limit", 50)
138 try:
139 limit = int(limit)
140 except ValueError:
141 limit = None
142
143 offset = self.get_argument("offset", 0)
144 try:
145 offset = int(offset)
146 except ValueError:
147 offset = None
148
149 builds = repo.get_builds(limit=limit, offset=offset)
150 unpushed_builds = repo.get_unpushed_builds()
151 obsolete_builds = repo.get_obsolete_builds()
152
153 # Get the build times of this repository.
154 build_times = repo.get_build_times()
155
156 self.render("repository-detail.html", distro=distro, repo=repo,
157 builds=builds, unpushed_builds=unpushed_builds,
158 obsolete_builds=obsolete_builds, build_times=build_times)
159
160
161 class RepositoryEditHandler(BaseHandler):
162 @tornado.web.authenticated
163 def get(self, distro, repo):
164 distro = self.pakfire.distros.get_by_name(distro)
165 if not distro:
166 raise tornado.web.HTTPError(404)
167
168 repo = distro.get_repo(repo)
169 if not repo:
170 raise tornado.web.HTTPError(404)
171
172 # XXX check if user has permissions to do this
173
174 self.render("repository-edit.html", distro=distro, repo=repo)
175
176
177 class RepositoryConfHandler(BaseHandler):
178 def get(self, distro, repo):
179 distro = self.pakfire.distros.get_by_name(distro)
180 if not distro:
181 raise tornado.web.HTTPError(404)
182
183 repo = distro.get_repo(repo)
184 if not repo:
185 raise tornado.web.HTTPError(404)
186
187 # This is a plaintext file.
188 self.set_header("Content-Type", "text/plain")
189
190 # Write the header.
191 self.write("# Downloaded from the pakfire build service on %s.\n\n" \
192 % datetime.datetime.utcnow())
193 self.write(repo.get_conf())
194 self.finish()
195
196
197 class RepositoryMirrorlistHandler(BaseHandler):
198 def get(self, distro, repo):
199 distro = self.pakfire.distros.get_by_name(distro)
200 if not distro:
201 raise tornado.web.HTTPError(404)
202
203 repo = distro.get_repo(repo)
204 if not repo:
205 raise tornado.web.HTTPError(404)
206
207 # This is a plaintext file.
208 self.set_header("Content-Type", "text/plain")
209
210 arch = self.get_argument("arch", None)
211 if not arch or not self.backend.arches.exists(arch):
212 raise tornado.web.HTTPError(400, "You must specify a valid architecture")
213
214 ret = {
215 "type" : "mirrorlist",
216 "version" : 1,
217 }
218
219 # A list with mirrors that are sent to the user.
220 mirrors = []
221
222 # Only search for mirrors on repositories that are supposed to be found
223 # on mirror servers.
224
225 if repo.mirrored:
226 # Select a list of preferred mirrors
227 for mirror in self.mirrors.get_for_location(self.current_address):
228 mirrors.append({
229 "url" : "/".join((mirror.url, distro.identifier, repo.identifier, arch)),
230 "location" : mirror.country_code,
231 "preferred" : 1,
232 })
233
234 # Add all other mirrors at the end in a random order
235 remaining_mirrors = [m for m in self.backend.mirrors if not m in mirrors]
236 random.shuffle(remaining_mirrors)
237
238 for mirror in remaining_mirrors:
239 mirrors.append({
240 "url" : "/".join((mirror.url, distro.identifier, repo.identifier, arch)),
241 "location" : mirror.country_code,
242 "preferred" : 0,
243 })
244
245 else:
246 repo_baseurl = self.pakfire.settings.get("repository_baseurl")
247 if repo_baseurl.endswith("/"):
248 repo_baseurl = repo_baseurl[:-1]
249
250 for mirror in self.mirrors.get_all():
251 print mirror.url, repo_baseurl
252 if not mirror.url == repo_baseurl:
253 continue
254
255 mirror = {
256 "url" : "/".join((mirror.url, distro.identifier, repo.identifier, arch)),
257 "location" : mirror.country_code,
258 "preferred" : 0,
259 }
260
261 mirrors.append(mirror)
262 break
263
264 ret["mirrors"] = mirrors
265 self.write(ret)
266
267
268
269 class RepoActionHandler(BaseHandler):
270 @tornado.web.authenticated
271 def post(self, type):
272 assert type in ("run", "remove")
273
274 action_id = self.get_argument("id")
275
276 action = self.pakfire.repos.get_action_by_id(action_id)
277 if not action:
278 raise tornado.web.HTTPError(400)
279
280 if type == "run":
281 action.run(self.current_user)
282
283 elif type == "remove":
284 action.delete(self.current_user)