]> git.ipfire.org Git - pbs.git/blob - src/web/builds.py
builds: Refactor comments
[pbs.git] / src / web / builds.py
1 #!/usr/bin/python
2
3 import tornado.web
4
5 from . import base
6 from . import ui_modules
7
8 class BuildBaseHandler(base.BaseHandler):
9 def get_build(self, uuid):
10 build = self.backend.builds.get_by_uuid(uuid)
11 if not build:
12 raise tornado.web.HTTPError(404, "No such build: %s" % uuid)
13
14 return build
15
16
17 class IndexHandler(base.BaseHandler):
18 def get(self):
19 # Fetch the most recent builds
20 builds = self.backend.builds.get_recent(limit=25)
21
22 self.render("builds/index.html", builds=builds)
23
24
25 class ShowHandler(BuildBaseHandler):
26 async def get(self, uuid):
27 build = self.backend.builds.get_by_uuid(uuid)
28 if not build:
29 raise tornado.web.HTTPError(404, "Could not find build %s" % uuid)
30
31 # Fetch any fixed Bugs
32 bugs = await build.get_bugs()
33
34 self.render("builds/show.html", build=build, pkg=build.pkg,
35 distro=build.distro, bugs=bugs, repo=build.repo)
36
37
38 class BuildDeleteHandler(BuildBaseHandler):
39 @tornado.web.authenticated
40 def get(self, uuid):
41 build = self.get_build(uuid)
42
43 # Check if the user has got sufficient rights to do this modification.
44 if not build.has_perm(self.current_user):
45 raise tornado.web.HTTPError(403)
46
47 # Check if the user confirmed the action.
48 confirmed = self.get_argument("confirmed", None)
49 if confirmed:
50 # Save the name of the package to redirect the user
51 # to the other packages of that name.
52 package_name = build.pkg.name
53
54 # Delete the build and everything that comes with it.
55 with self.db.transaction():
56 build.delete()
57
58 return self.redirect("/package/%s" % package_name)
59
60 self.render("build-delete.html", build=build)
61
62
63 class BuildStateHandler(base.BaseHandler):
64 def get(self, uuid):
65 build = self.backend.builds.get_by_uuid(uuid)
66 if not build:
67 raise tornado.web.HTTPError(404, "No such build: %s" % uuid)
68
69 self.render("build-state.html", build=build)
70
71 @tornado.web.authenticated
72 def post(self, uuid):
73 build = self.backend.builds.get_by_uuid(uuid)
74 if not build:
75 raise tornado.web.HTTPError(404, "No such build: %s" % uuid)
76
77 # Check if user has the right to perform this action.
78 if not build.has_perm(self.current_user):
79 raise tornado.web.HTTPError(403, "User is not allowed to perform this action")
80
81 # Check if given state is valid.
82 state = self.get_argument("state", None)
83 if not state in ("broken", "unbreak", "obsolete"):
84 raise tornado.web.HTTPError(400, "Invalid argument given: %s" % state)
85
86 # XXX this is not quite accurate
87 if state == "unbreak":
88 state = "stable"
89
90 rem_from_repo = self.get_argument("rem_from_repo", False)
91 if rem_from_repo == "on":
92 rem_from_repo = True
93
94 # Perform the state change.
95 build.update_state(state, user=self.current_user, remove=rem_from_repo)
96
97 self.redirect("/build/%s" % build.uuid)
98
99
100 class BuildDetailCommentHandler(base.BaseHandler):
101 @tornado.web.authenticated
102 def post(self, uuid):
103 build = self.backend.builds.get_by_uuid(uuid)
104
105 if not build:
106 raise tornado.web.HTTPError(404, "Build not found")
107
108 vote = self.get_argument("vote", "none")
109
110 if vote == "up":
111 vote = 1
112 elif vote == "down":
113 vote = -1
114 else:
115 vote = 0
116
117 text = self.get_argument("text", "")
118
119 # Add a new comment to the build.
120 if text or vote:
121 build.comment(self.current_user, text, vote)
122
123 # Redirect to the build detail page.
124 self.redirect("/build/%s" % build.uuid)
125
126
127 class BuildManageHandler(base.BaseHandler):
128 @tornado.web.authenticated
129 def get(self, uuid):
130 build = self.backend.builds.get_by_uuid(uuid)
131 if not build:
132 raise tornado.web.HTTPError(404, "Build not found: %s" % uuid)
133
134 mode = "user"
135 if self.current_user.is_admin():
136 mode = self.get_argument("mode", "user")
137
138 # Get the next repo.
139 if build.repo:
140 next_repo = build.repo.__next__
141 else:
142 next_repo = build.distro.first_repo
143
144 self.render("build-manage.html", mode=mode, build=build,
145 distro=build.distro, repo=build.repo, next_repo=next_repo)
146
147 @tornado.web.authenticated
148 def post(self, uuid):
149 build = self.backend.builds.get_by_uuid(uuid)
150 if not build:
151 raise tornado.web.HTTPError(404, "Build not found: %s" % uuid)
152
153 # check for sufficient permissions
154 if not build.has_perm(self.current_user):
155 raise tornado.web.HTTPError(403)
156
157 action = self.get_argument("action")
158 assert action in ("push", "unpush")
159
160 current_repo = build.repo
161
162 if action == "unpush":
163 current_repo.rem_build(build, user=self.current_user)
164
165 elif action == "push":
166 repo_name = self.get_argument("repo")
167 next_repo = build.distro.get_repo(repo_name)
168
169 if not next_repo:
170 raise tornado.web.HTTPError(404, "No such repository: %s" % next_repo)
171
172 if not self.current_user.is_admin():
173 if not distro.repo.__next__ == next_repo:
174 raise tornado.web.HTTPError(403)
175
176 if current_repo:
177 current_repo.move_build(build, next_repo, user=self.current_user)
178 else:
179 next_repo.add_build(build, user=self.current_user)
180
181 self.redirect("/build/%s" % build.uuid)
182
183
184 class BuildWatchersHandler(base.BaseHandler):
185 def get(self, uuid):
186 build = self.backend.builds.get_by_uuid(uuid)
187
188 if not build:
189 raise tornado.web.HTTPError(404, "Build not found")
190
191 # Get a list of all watchers and sort them by their realname.
192 watchers = build.get_watchers()
193 watchers.sort(key=lambda watcher: watcher.realname)
194
195 self.render("builds-watchers-list.html", build=build, watchers=watchers)
196
197
198 class BuildWatchersAddHandler(base.BaseHandler):
199 @tornado.web.authenticated
200 def get(self, uuid, error_msg=None):
201 build = self.backend.builds.get_by_uuid(uuid)
202
203 if not build:
204 raise tornado.web.HTTPError(404, "Build not found")
205
206 # Get a list of all users that are currently watching this build.
207 watchers = build.get_watchers()
208
209 self.render("builds-watchers-add.html", error_msg=error_msg,
210 build=build, users=self.backend.users, watchers=watchers)
211
212 @tornado.web.authenticated
213 def post(self, uuid):
214 build = self.backend.builds.get_by_uuid(uuid)
215
216 if not build:
217 raise tornado.web.HTTPError(404, "Build not found")
218
219 # Get the user id of the new watcher.
220 user_id = self.current_user.id
221
222 if self.current_user.is_admin():
223 user_id = self.get_argument("user_id", self.current_user.id)
224 assert user_id
225
226 user = self.backend.users.get_by_id(user_id)
227 if not user:
228 _ = self.locale.translate
229 error_msg = _("User not found.")
230
231 return self.get(uuid, error_msg=error_msg)
232
233 # Actually add the user to the list of watchers.
234 build.add_watcher(user)
235
236 # Send user back to the build detail page.
237 self.redirect("/build/%s" % build.uuid)
238
239
240 class ListModule(ui_modules.UIModule):
241 def render(self, builds):
242 return self.render_string("builds/modules/list.html", builds=builds)