from . import base
from . import ui_modules
+class APIv1IndexHandler(base.APIMixin, base.BaseHandler):
+ # Allow users to create builds
+ allow_users = True
+
+ @tornado.web.authenticated
+ async def get(self, distro_slug):
+ with self.db.transaction():
+ # Fetch distro
+ distro = self.backend.distros.get_by_slug(distro_slug)
+ if not distro:
+ raise tornado.web.HTTPError(404, "Could not find distro '%s'" % distro_slug)
+
+ # Fetch all repositories
+ try:
+ repos = self.current_user.repos[distro]
+ except KeyError:
+ repos = []
+
+ self.finish({
+ "repos" : [repo.to_json() for repo in repos],
+ })
+
+ @tornado.web.authenticated
+ async def post(self, distro_slug):
+ with self.db.transaction():
+ # Fetch distro
+ distro = self.backend.distros.get_by_slug(distro_slug)
+ if not distro:
+ raise tornado.web.HTTPError(404, "Could not find distro '%s'" % distro_slug)
+
+ # Fetch name
+ name = self.get_argument("name")
+
+ # Create a new repository
+ repo = await self.backend.repos.create(distro=distro,
+ name=name, owner=self.current_user)
+
+ # Set description
+ repo.description = self.get_argument("description", None)
+
+ # Return the result
+ self.finish(repo.to_json())
+
+
+class APIv1ShowHandler(base.APIMixin, base.BaseHandler):
+ def _get_repo(self, distro_slug, repo_slug):
+ # Fetch distro
+ self.distro = self.backend.distros.get_by_slug(distro_slug)
+ if not self.distro:
+ raise tornado.web.HTTPError(404, "Could not find distro '%s'" % distro_slug)
+
+ # Fetch repository
+ self.repo = self.current_users.get_repo(self.distro, repo_slug)
+ if not self.repo:
+ raise tornado.web.HTTPError(404, "Could not find repository '%s" % repo_slug)
+
+ @tornado.web.authenticated
+ async def get(self, distro_slug, name):
+ with self.db.transaction():
+ repo = self._get_repo(distro_slug, name)
+
+ self.finish(repo.to_json())
+
+ @tornado.web.authenticated
+ async def delete(self, distro_slug, name):
+ with self.db.transaction():
+ repo = self._get_repo(distro_slug, name)
+
+ # XXX check permissions
+
+ # Delete the repository
+ await self.repo.delete(self.current_user)
+
+
class BaseHandler(base.BaseHandler):
def _get_repo(self, distro_slug, repo_slug, user_slug=None):
user = None