]> git.ipfire.org Git - pbs.git/blob - src/web/builds.py
builds: Implement filtering by user
[pbs.git] / src / web / builds.py
1 #!/usr/bin/python
2
3 import tornado.web
4
5 from ..errors import NoSuchDistroError
6
7 from . import base
8 from . import ui_modules
9
10 class APIv1IndexHandler(base.APIMixin, base.BaseHandler):
11 # Allow users to create builds
12 allow_users = True
13
14 @tornado.web.authenticated
15 async def post(self):
16 # Fetch the upload
17 upload = self.get_argument_upload("upload_id")
18 if not upload:
19 raise tornado.web.HTTPError(404, "Could not find upload")
20
21 # Check permissions of the upload
22 if not upload.has_perm(self.current_user):
23 raise tornado.web.HTTPError(403, "No permission for using upload %s" % upload)
24
25 # Fetch the repository
26 repo_name = self.get_argument("repo", None)
27
28 with self.db.transaction():
29 # Import the package
30 try:
31 package = await self.backend.packages.create(upload)
32
33 # If the distribution that is coded into the package could not be found,
34 # we will send that error to the user...
35 except NoSuchDistroError as e:
36 raise tornado.web.HTTPError(404, "Could not find distribution: %s" % e)
37
38 # Find the repository
39 repo = self.current_user.get_repo(package.distro, repo_name)
40 if not repo:
41 raise tornado.web.HTTPError(404, "Could not find repository")
42
43 # Create a new build
44 build = await self.backend.builds.create(repo, package, owner=self.current_user)
45
46 # Delete the upload
47 await upload.delete()
48
49 # Send some data about the build
50 self.finish({
51 "uuid" : build.uuid,
52 "name" : "%s" % build,
53 })
54
55 # Run dependency check on all jobs (in the background)
56 self.backend.run_task(self.backend.jobs.depcheck, build.jobs)
57
58
59 class IndexHandler(base.BaseHandler):
60 def get(self):
61 # Filters
62 user = self.get_argument_user("user", None)
63
64 # Fetch the most recent builds
65 if user:
66 builds = self.backend.builds.get_by_user(user, limit=25)
67 else:
68 builds = self.backend.builds.get_recent(limit=25)
69
70 self.render("builds/index.html", builds=builds, user=user)
71
72
73 class ShowHandler(base.BaseHandler):
74 async def get(self, uuid):
75 build = self.backend.builds.get_by_uuid(uuid)
76 if not build:
77 raise tornado.web.HTTPError(404, "Could not find build %s" % uuid)
78
79 # Fetch any fixed Bugs
80 bugs = await build.get_bugs()
81
82 self.render("builds/show.html", build=build, pkg=build.pkg,
83 distro=build.distro, bugs=bugs)
84
85
86 class DeleteHandler(base.BaseHandler):
87 @tornado.web.authenticated
88 def get(self, uuid):
89 build = self.backend.builds.get_by_uuid(uuid)
90 if not build:
91 raise tornado.web.HTTPError(404, "Could not find build %s" % uuid)
92
93 # Check permissions
94 if not build.can_be_deleted(self.current_user):
95 raise tornado.web.HTTPError(403, "%s cannot delete build %s" \
96 % (self.current_user, build))
97
98 self.render("builds/delete.html", build=build)
99
100 @tornado.web.authenticated
101 async def post(self, uuid):
102 build = self.backend.builds.get_by_uuid(uuid)
103 if not build:
104 raise tornado.web.HTTPError(404, "Could not find build %s" % uuid)
105
106 # Check permissions
107 if not build.can_be_deleted(self.current_user):
108 raise tornado.web.HTTPError(403, "%s cannot delete build %s" \
109 % (self.current_user, build))
110
111 # Perform Deletion
112 with self.db.transaction():
113 await build.delete(self.current_user)
114
115 self.redirect("/builds")
116
117
118 class WatchHandler(base.BaseHandler):
119 @tornado.web.authenticated
120 def post(self, uuid):
121 build = self.backend.builds.get_by_uuid(uuid)
122 if not build:
123 raise tornado.web.HTTPError(404, "Could not find build %s" % uuid)
124
125 with self.db.transaction():
126 build.add_watcher(self.current_user)
127
128 self.redirect("/builds/%s" % build.uuid)
129
130
131 class UnwatchHandler(base.BaseHandler):
132 @tornado.web.authenticated
133 def post(self, uuid):
134 build = self.backend.builds.get_by_uuid(uuid)
135 if not build:
136 raise tornado.web.HTTPError(404, "Could not find build %s" % uuid)
137
138 with self.db.transaction():
139 build.remove_watcher(self.current_user)
140
141 self.redirect("/builds/%s" % build.uuid)
142
143
144 class BuildDetailCommentHandler(base.BaseHandler):
145 @tornado.web.authenticated
146 def post(self, uuid):
147 build = self.backend.builds.get_by_uuid(uuid)
148
149 if not build:
150 raise tornado.web.HTTPError(404, "Build not found")
151
152 vote = self.get_argument("vote", "none")
153
154 if vote == "up":
155 vote = 1
156 elif vote == "down":
157 vote = -1
158 else:
159 vote = 0
160
161 text = self.get_argument("text", "")
162
163 # Add a new comment to the build.
164 if text or vote:
165 build.comment(self.current_user, text, vote)
166
167 # Redirect to the build detail page.
168 self.redirect("/build/%s" % build.uuid)
169
170
171 class ListModule(ui_modules.UIModule):
172 def render(self, builds):
173 return self.render_string("builds/modules/list.html", builds=builds)