]> git.ipfire.org Git - people/jschlag/pbs.git/blob - web/handlers_mirrors.py
Drop dependency on textile
[people/jschlag/pbs.git] / web / handlers_mirrors.py
1 #!/usr/bin/python
2
3 import tornado.web
4
5 import backend
6
7 from handlers_base import BaseHandler
8
9 class MirrorListHandler(BaseHandler):
10 def get(self):
11 mirrors = self.pakfire.mirrors.get_all()
12 mirrors_nearby = self.pakfire.mirrors.get_for_location(self.remote_address)
13
14 mirrors_worldwide = []
15 for mirror in mirrors:
16 if mirror in mirrors_nearby:
17 continue
18
19 mirrors_worldwide.append(mirror)
20
21 kwargs = {
22 "mirrors" : mirrors,
23 "mirrors_nearby" : mirrors_nearby,
24 "mirrors_worldwide" : mirrors_worldwide,
25 }
26
27 # Get recent log messages.
28 kwargs["log"] = self.pakfire.mirrors.get_history(limit=5)
29
30 self.render("mirrors-list.html", **kwargs)
31
32
33 class MirrorDetailHandler(BaseHandler):
34 def get(self, hostname):
35 mirror = self.pakfire.mirrors.get_by_hostname(hostname)
36 if not mirror:
37 raise tornado.web.HTTPError(404, "Could not find mirror: %s" % hostname)
38
39 log = self.pakfire.mirrors.get_history(mirror=mirror, limit=10)
40
41 self.render("mirrors-detail.html", mirror=mirror, log=log)
42
43
44 class MirrorActionHandler(BaseHandler):
45 """
46 A handler that makes sure if the user has got sufficent rights to
47 do actions.
48 """
49 def prepare(self):
50 # Check if the user has sufficient rights to create a new mirror.
51 if not self.current_user.has_perm("manage_mirrors"):
52 raise tornado.web.HTTPError(403)
53
54
55 class MirrorNewHandler(MirrorActionHandler):
56 @tornado.web.authenticated
57 def get(self, hostname="", path="", hostname_missing=False, path_invalid=False):
58 self.render("mirrors-new.html", _hostname=hostname, path=path,
59 hostname_missing=hostname_missing, path_invalid=path_invalid)
60
61 @tornado.web.authenticated
62 def post(self):
63 errors = {}
64
65 hostname = self.get_argument("name", None)
66 if not hostname:
67 errors["hostname_missing"] = True
68
69 path = self.get_argument("path", "")
70 if path is None:
71 errors["path_invalid"] = True
72
73 if errors:
74 errors.update({
75 "hostname" : hostname,
76 "path" : path,
77 })
78 return self.get(**errors)
79
80 mirror = backend.mirrors.Mirror.create(self.pakfire, hostname, path,
81 user=self.current_user)
82 assert mirror
83
84 self.redirect("/mirror/%s" % mirror.hostname)
85
86
87 class MirrorEditHandler(MirrorActionHandler):
88 @tornado.web.authenticated
89 def get(self, hostname):
90 mirror = self.pakfire.mirrors.get_by_hostname(hostname)
91 if not mirror:
92 raise tornado.web.HTTPError(404, "Could not find mirror: %s" % hostname)
93
94 self.render("mirrors-edit.html", mirror=mirror)
95
96 @tornado.web.authenticated
97 def post(self, hostname):
98 mirror = self.pakfire.mirrors.get_by_hostname(hostname)
99 if not mirror:
100 raise tornado.web.HTTPError(404, "Could not find mirror: %s" % hostname)
101
102 hostname = self.get_argument("name")
103 path = self.get_argument("path", "")
104 owner = self.get_argument("owner", None)
105 contact = self.get_argument("contact", None)
106 enabled = self.get_argument("enabled", None)
107
108 if enabled:
109 mirror.set_status("enabled", user=self.current_user)
110 else:
111 mirror.set_status("disabled", user=self.current_user)
112
113 mirror.hostname = hostname
114 mirror.path = path
115 mirror.owner = owner
116 mirror.contact = contact
117
118 self.redirect("/mirror/%s" % mirror.hostname)
119
120
121 class MirrorDeleteHandler(MirrorActionHandler):
122 @tornado.web.authenticated
123 def get(self, hostname):
124 mirror = self.pakfire.mirrors.get_by_hostname(hostname)
125 if not mirror:
126 raise tornado.web.HTTPError(404, "Could not find mirror: %s" % hostname)
127
128 confirmed = self.get_argument("confirmed", None)
129 if confirmed:
130 mirror.set_status("deleted", user=self.current_user)
131
132 self.redirect("/mirrors")
133 return
134
135 self.render("mirrors-delete.html", mirror=mirror)