From: Michael Tremer Date: Sun, 23 Oct 2022 15:11:44 +0000 (+0000) Subject: distros: Refactor editing distributions X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=b5601528f351d44a1ae49cc05ff3ad5789e86aec;p=pbs.git distros: Refactor editing distributions Signed-off-by: Michael Tremer --- diff --git a/Makefile.am b/Makefile.am index c014540b..42cd14ae 100644 --- a/Makefile.am +++ b/Makefile.am @@ -157,7 +157,6 @@ webdir = $(buildservicedir)/web dist_templates_DATA = \ src/templates/base.html \ - src/templates/distro-edit.html \ src/templates/distro-source-commit-detail.html \ src/templates/distro-source-commit-reset.html \ src/templates/distro-source-commits.html \ @@ -208,6 +207,7 @@ dist_templates_builds_modules_DATA = \ templates_builds_modulesdir = $(templates_buildsdir)/modules dist_templates_distros_DATA = \ + src/templates/distros/edit.html \ src/templates/distros/index.html \ src/templates/distros/show.html diff --git a/src/buildservice/distribution.py b/src/buildservice/distribution.py index 16ecf04f..2996771d 100644 --- a/src/buildservice/distribution.py +++ b/src/buildservice/distribution.py @@ -105,22 +105,42 @@ class Distribution(base.DataObject): return 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) + + # Slug + @property def slug(self): return self.data.slug - @property - def slogan(self): + # Slogan + + def get_slogan(self): return self.data.slogan - @property - def description(self): + def set_slogan(self, slogan): + self._set_attribute("slogan", slogan or "") + + slogan = property(get_slogan, set_slogan) + + # 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) + # Arches def get_arches(self): @@ -131,10 +151,18 @@ class Distribution(base.DataObject): arches = property(get_arches, set_arches) - @property - def vendor(self): + # Vendor + + def get_vendor(self): return self.data.vendor + def set_vendor(self, vendor): + self._set_attribute("vendor", vendor) + + vendor = property(get_vendor, set_vendor) + + # Contact + def get_contact(self): return self.data.contact @@ -143,13 +171,11 @@ class Distribution(base.DataObject): contact = property(get_contact, set_contact) - def get_tag(self): - return self.data.tag - - def set_tag(self, tag): - self._set_attribute("tag", tag) + # Tag - tag = property(get_tag, set_tag) + @property + def tag(self): + return self.data.tag # Custom Configuration @@ -161,6 +187,16 @@ class Distribution(base.DataObject): custom_config = property(get_custom_config, set_custom_config) + # Permissions + + def has_perm(self, user): + # Anonymous users have no permissions + if not user: + return False + + # Must be admin + return user.is_admin() + @lazy_property def repos(self): repos = self.backend.repos._get_repositories(""" diff --git a/src/templates/distro-edit.html b/src/templates/distro-edit.html deleted file mode 100644 index 2f331337..00000000 --- a/src/templates/distro-edit.html +++ /dev/null @@ -1,99 +0,0 @@ -{% extends "base.html" %} - -{% block title %}{{ _("Edit distribution %s") % distro.name }}{% end block %} - -{% block body %} -

{{ _("Edit distribution %s") % distro.name }}

- -
- {% raw xsrf_form_html() %} - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
{{ _("Name") }} - - - {{ _("The fancy name of the distribution.") }} -
{{ _("Identifier") }} - {{ distro.sname }} - - {{ _("Cannot be changed.") }} -
{{ _("Tag") }} - - - {{ _("The tag is added to the package release.") }} -
{{ _("Vendor") }} - - - {{ _("From whom is the distribution from?") }} -
{{ _("Contact") }} - - - {{ _("The email address from the vendor.") }} -
{{ _("Slogan") }} - - - {{ _("A short sentence that characterizes the distribution.") }} -
{{ _("Architectures") }} - - - {{ _("For which architectures should the distribution be built?") }} -
{{ _("Sources") }} - - - {{ _("Which sources should be imported to the distribution?") }} -
- -
-
 
-
-{% end block %} diff --git a/src/templates/distros/edit.html b/src/templates/distros/edit.html new file mode 100644 index 00000000..6a1c72c0 --- /dev/null +++ b/src/templates/distros/edit.html @@ -0,0 +1,110 @@ +{% extends "../base.html" %} + +{% block title %}{{ _("Distributions") }} - {{ distro }} - {{ _("Edit") }}{% end block %} + +{% block container %} + {% import pakfire %} + + + +
+
+
+
+ {% raw xsrf_form_html() %} + + {# Name #} +
+ +
+ +
+

+ {{ _("The name of this distributions") }} +

+
+ +
+ {# Vendor #} +
+ +
+ +
+

+ {{ _("The vendor of this distributions") }} +

+
+ + {# Contact #} +
+ +
+ +
+

+ {{ _("The contact of this distributions") }} +

+
+ + {# Slogan #} +
+ +
+ +
+

+ {{ _("The slogan of this distributions") }} +

+
+
+ +
+ + +
+
+ +
+
+
+ +
+
+ +
+ + +
+
+
+
+
+{% end block %} diff --git a/src/templates/distros/show.html b/src/templates/distros/show.html index a3415935..05d43b80 100644 --- a/src/templates/distros/show.html +++ b/src/templates/distros/show.html @@ -40,6 +40,14 @@ + +
+ {% if distro.has_perm(current_user) %} + + {{ _("Edit") }} + + {% end %} +
{% end %} diff --git a/src/web/__init__.py b/src/web/__init__.py index 8d4a41fb..33bad7be 100644 --- a/src/web/__init__.py +++ b/src/web/__init__.py @@ -145,6 +145,7 @@ class Application(tornado.web.Application): # Distributions (r"/distros", distributions.IndexHandler), (r"/distros/([A-Za-z0-9\-\.]+)", distributions.ShowHandler), + (r"/distros/([A-Za-z0-9\-\.]+)/edit", distributions.EditHandler), (r"/distros/(?P[A-Za-z0-9\-\.]+)/repos/(?P[A-Za-z0-9\-]+)", repos.ShowHandler), (r"/distros/(?P[A-Za-z0-9\-\.]+)/repos/(?P[A-Za-z0-9\-]+)\.repo", diff --git a/src/web/distributions.py b/src/web/distributions.py index 6b22f51a..3bd68f87 100644 --- a/src/web/distributions.py +++ b/src/web/distributions.py @@ -19,44 +19,33 @@ class ShowHandler(base.BaseHandler): self.render("distros/show.html", distro=distro) -class DistributionEditHandler(base.BaseHandler): - def prepare(self): - self.sources = self.backend.sources.get_all() - +class EditHandler(base.BaseHandler): @tornado.web.authenticated - def get(self, name): - distro = self.backend.distros.get_by_slug(name) + def get(self, slug): + distro = self.backend.distros.get_by_slug(slug) if not distro: - raise tornado.web.HTTPError(404, "Distro not found") + raise tornado.web.HTTPError(404, "Could not find distribution: %s" % slug) + + # Check for permissions + if not distro.has_perm(self.current_user): + raise tornado.web.HTTPError(403) - self.render("distro-edit.html", distro=distro, sources=self.sources) + self.render("distros/edit.html", distro=distro) @tornado.web.authenticated - def post(self, name): - distro = self.backend.distros.get_by_slug(name) + def post(self, slug): + distro = self.backend.distros.get_by_slug(slug) if not distro: - raise tornado.web.HTTPError(404, "Distro not found") - - name = self.get_argument("name", distro.name) - vendor = self.get_argument("vendor", distro.vendor) - contact = self.get_argument("contact", "") - slogan = self.get_argument("slogan", distro.slogan) - tag = self.get_argument("tag", "") - - distro.set("name", name) - distro.set("vendor", vendor) - distro.set("slogan", slogan) - - # Update the contact email address. - distro.contact = contact - - # Update the tag. - distro.tag = tag + raise tornado.web.HTTPError(404, "Could not find distribution: %s" % slug) - # Update architectures - distro.arches = self.get_arguments("arches", []) + with self.db.transaction(): + distro.name = self.get_argument("name") + distro.vendor = self.get_argument("vendor", None) + distro.contact = self.get_argument("contact", None) + distro.slogan = self.get_argument("slogan", None) + distro.arches = self.get_arguments("arches") - self.redirect("/distribution/%s" % distro.slug) + self.redirect("/distros/%s" % distro.slug) class DistroSourceDetailHandler(base.BaseHandler):