From: Michael Tremer Date: Sat, 25 Jun 2022 14:50:33 +0000 (+0000) Subject: repos: Make repositories editable X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=0bcebf530d4623008ee119f9a532736f5c2d9b19;p=pbs.git repos: Make repositories editable Signed-off-by: Michael Tremer --- diff --git a/Makefile.am b/Makefile.am index 76dd36ed..64d734a2 100644 --- a/Makefile.am +++ b/Makefile.am @@ -175,7 +175,6 @@ dist_templates_DATA = \ src/templates/package-detail-list.html \ src/templates/package-properties.html \ src/templates/queue.html \ - src/templates/repository-edit.html \ src/templates/search.html \ src/templates/source-list.html \ src/templates/updates-index.html \ @@ -311,6 +310,7 @@ templates_modules_selectdir = $(templates_modulesdir)/select dist_templates_repos_DATA = \ src/templates/repos/builds.html \ + src/templates/repos/edit.html \ src/templates/repos/show.html templates_reposdir = $(templatesdir)/repos diff --git a/src/buildservice/repository.py b/src/buildservice/repository.py index 4fded828..b72c0990 100644 --- a/src/buildservice/repository.py +++ b/src/buildservice/repository.py @@ -147,6 +147,21 @@ class Repository(base.DataObject): owner = property(get_owner, set_owner) + def has_perm(self, user): + """ + Returns True if the user has permission to manage this repository + """ + # Admins can edit anything + if user.is_admin(): + return True + + # If the user owns this repository, they may edit it + if self.owner == user: + return True + + # Nope + return False + # Slug @property @@ -214,10 +229,16 @@ class Repository(base.DataObject): return "\n".join(lines) - @property - def name(self): + # Name + + def get_name(self): return self.data.name + def set_name(self, name): + self._set_attribute("name", name) + + name = property(get_name, set_name) + @property def summary(self): lines = self.description.splitlines() @@ -227,10 +248,16 @@ class Repository(base.DataObject): return "N/A" - @property - def description(self): + # Description + + def get_description(self): return self.data.description + def set_description(self, description): + self._set_attribute("description", description or "") + + description = property(get_description, set_description) + @property def parent_id(self): return self.data.parent_id @@ -256,6 +283,16 @@ class Repository(base.DataObject): mirrored = property(get_mirrored, set_mirrored) + # Listed + + def get_listed(self): + return self.data.listed + + def set_listed(self, listed): + self._set_attribute("listed", listed) + + listed = property(get_listed, set_listed) + def _log_build(self, action, build, from_repo=None, to_repo=None, user=None): user_id = None if user: diff --git a/src/templates/repos/edit.html b/src/templates/repos/edit.html new file mode 100644 index 00000000..c09059ab --- /dev/null +++ b/src/templates/repos/edit.html @@ -0,0 +1,101 @@ +{% extends "../base.html" %} + +{% block title %}{{ _("Repositories") }} - {{ repo }} - {{ _("Edit") }}{% end block %} + +{% block container %} + + +
+
+
+
+ {% raw xsrf_form_html() %} + + + + + +

+ {{ _("Describe what this repository will contain") }} +

+ + + + {% if repo.owner %} + + + + +

+ {{ _("By unchecking this box, you can hide this repository from being listed") }} +

+ {% end %} + + {% if current_user.is_admin() %} + + + + +

+ {{ _("If enabled, this repository will be made available on mirror servers") }} +

+ {% end %} +
+ + + + + {{ _("Delete") }} + +
+
+
+{% end block %} diff --git a/src/templates/repos/show.html b/src/templates/repos/show.html index 312e462b..424f2c2a 100644 --- a/src/templates/repos/show.html +++ b/src/templates/repos/show.html @@ -42,10 +42,14 @@ {% end %} - + {{ _("Download Configuration") }} + + {{ _("Edit") }} + +
{{ _("Statistics") }}
diff --git a/src/templates/repository-edit.html b/src/templates/repository-edit.html deleted file mode 100644 index 33ce65aa..00000000 --- a/src/templates/repository-edit.html +++ /dev/null @@ -1,96 +0,0 @@ -{% extends "base.html" %} - -{% block title %}{{ _("Edit repository %s") % repo.name }}{% end block %} - -{% block body %} -

- {{ _("Edit repository %s") % repo.name }} - - {{ _("Distribution") }}: {{ distro.name }} -

- -
- {% raw xsrf_form_html() %} - - - - - - - - - - -
{{ _("Name") }} - - - {{ _("The name of the repository.") }} - {{ _("Must only contain of the lowercase characters.") }} -
{{ _("Description") }} - -
-
 
- -

{{ _("Score settings") }}

-

- {{ _("These settings configure the automatic score feature.") }} - {{ _("Builds that gained a certain score are moved to the next repository automatically and removed if the score is too bad.") }} -

- - - - - - - - - - - - - - - - -
{{ _("Needed score") }} - - - {{ _("The score that is needed for builds to automatically be moved into this repository.") }} -
{{ _("Minimum time") }} - - - {{ _("Every build must stay a minimum time in a repository.") }} - {{ _("This is to ensure that a package gets tested well.") }} - {{ _("Enter zero to disable the feature.") }} -
{{ _("Maximum time") }} - - - {{ _("If a build is more than a certain amount of time in a repository, it will automatically be removed.") }} - {{ _("This is to ensure that packages are not forgotten to be pushed.") }} - {{ _("Enter zero to disable the feature.") }} -
-
 
- -

{{ _("Build settings") }}

- - - - - - -
{{ _("Use package for builds?") }} - - - {{ _("Should the package be selected for builds by default?") }} - {{ _("Use with caution!") }} -
-
 
- - - - - -
- -
-
-{% end block %} diff --git a/src/web/__init__.py b/src/web/__init__.py index 89172944..e537b889 100644 --- a/src/web/__init__.py +++ b/src/web/__init__.py @@ -117,6 +117,8 @@ class Application(tornado.web.Application): repos.ConfigHandler), (r"/users/(?P\w+)/repos/(?P[A-Za-z0-9\-]+)/builds", repos.BuildsHandler), + (r"/users/(?P\w+)/repos/(?P[A-Za-z0-9\-]+)/edit", + repos.EditHandler), (r"/users/(?P\w+)/repos/(?P[A-Za-z0-9\-]+)/mirrorlist", repos.MirrorlistHandler), (r"/user/(\w+)/impersonate", users.UserImpersonateHandler), @@ -169,12 +171,11 @@ class Application(tornado.web.Application): repos.ConfigHandler), (r"/distros/(?P[A-Za-z0-9\-\.]+)/repos/(?P[A-Za-z0-9\-]+)/builds", repos.BuildsHandler), + (r"/distros/(?P[A-Za-z0-9\-\.]+)/repos/(?P[A-Za-z0-9\-]+)/edit", + repos.EditHandler), (r"/distros/(?P[A-Za-z0-9\-\.]+)/repos/(?P[A-Za-z0-9\-]+)/mirrorlist", repos.MirrorlistHandler), - (r"/distro/([A-Za-z0-9\-\.]+)/repo/([A-Za-z0-9\-]+)/edit", - RepositoryEditHandler), - (r"/distro/([A-Za-z0-9\-\.]+)/source/([A-Za-z0-9\-\.]+)", distributions.DistroSourceDetailHandler), (r"/distro/([A-Za-z0-9\-\.]+)/source/([A-Za-z0-9\-\.]+)/commits", diff --git a/src/web/base.py b/src/web/base.py index ae4992fb..59eaec2f 100644 --- a/src/web/base.py +++ b/src/web/base.py @@ -110,9 +110,18 @@ class BaseHandler(tornado.web.RequestHandler): # Typed Arguments + def get_argument_bool(self, name): + arg = self.get_argument(name, default=None) + + return arg == "on" + def get_argument_int(self, *args, **kwargs): arg = self.get_argument(*args, **kwargs) + # Return nothing + if not arg: + return None + try: return int(arg) except (TypeError, ValueError): diff --git a/src/web/handlers.py b/src/web/handlers.py index c7abfb1d..794ed5ed 100644 --- a/src/web/handlers.py +++ b/src/web/handlers.py @@ -33,22 +33,6 @@ class LogHandler(base.BaseHandler): self.render("log.html", log=self.backend.log) -class RepositoryEditHandler(base.BaseHandler): - @tornado.web.authenticated - def get(self, distro, repo): - distro = self.backend.distros.get_by_slug(distro) - if not distro: - raise tornado.web.HTTPError(404) - - repo = distro.get_repo(repo) - if not repo: - raise tornado.web.HTTPError(404) - - # XXX check if user has permissions to do this - - self.render("repository-edit.html", distro=distro, repo=repo) - - class RepoActionHandler(base.BaseHandler): @tornado.web.authenticated def post(self, type): diff --git a/src/web/repos.py b/src/web/repos.py index e77d3f6e..02edd765 100644 --- a/src/web/repos.py +++ b/src/web/repos.py @@ -80,6 +80,48 @@ class ConfigHandler(BaseHandler): self.finish(config) +class EditHandler(BaseHandler): + @tornado.web.authenticated + def get(self, **kwargs): + # Fetch the repository + repo = self._get_repo(**kwargs) + + # Check for permissions + if not repo.has_perm(self.current_user): + raise tornado.web.HTTPError(403, "%s cannot edit this repository" % self.current_user) + + self.render("repos/edit.html", repo=repo) + + @tornado.web.authenticated + def post(self, **kwargs): + # Fetch the repository + repo = self._get_repo(**kwargs) + + # Check for permissions + if not repo.has_perm(self.current_user): + raise tornado.web.HTTPError(403, "%s cannot edit this repository" % self.current_user) + + with self.db.transaction(): + repo.name = self.get_argument("name") + repo.description = self.get_argument("description", None) + repo.priority = self.get_argument_int("priority", None) + + # Attributes for custom repositories + if repo.owner: + repo.listed = self.get_argument_bool("listed") + + # Admin settings + if self.current_user.is_admin(): + repo.mirrored = self.get_argument_bool("mirrored") + + if repo.owner: + url = "/users/%s/repos/%s" % (repo.owner.name, repo.slug) + else: + url = "/distros/%s/repos/%s" % (repo.distro.slug, repo.slug) + + self.redirect(url) + + class MirrorlistHandler(BaseHandler): def get(self, **kwargs): # Fetch the repository