]> git.ipfire.org Git - pbs.git/commitdiff
builds: Refactor comments
authorMichael Tremer <michael.tremer@ipfire.org>
Thu, 13 Oct 2022 09:04:08 +0000 (09:04 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Thu, 13 Oct 2022 09:04:08 +0000 (09:04 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
po/pakfire-build-service.pot
src/buildservice/builds.py
src/database.sql
src/web/builds.py

index fba31489e7583b8eed36abd54386ec3cebe2e93c..f9ddd99ebd477d3dc8ac24b3166a278933a60879 100644 (file)
@@ -8,7 +8,7 @@ msgid ""
 msgstr ""
 "Project-Id-Version: PACKAGE VERSION\n"
 "Report-Msgid-Bugs-To: \n"
-"POT-Creation-Date: 2022-10-12 18:09+0000\n"
+"POT-Creation-Date: 2022-10-13 09:03+0000\n"
 "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
 "Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
 "Language-Team: LANGUAGE <LL@li.org>\n"
@@ -466,9 +466,6 @@ msgstr ""
 msgid "Running Jobs"
 msgstr ""
 
-msgid "Log"
-msgstr ""
-
 #, c-format
 msgid "Edit Builder %s"
 msgstr ""
@@ -938,6 +935,9 @@ msgstr ""
 msgid "The packages listed below were used to build %s."
 msgstr ""
 
+msgid "Log"
+msgstr ""
+
 msgid "Log In"
 msgstr ""
 
index 602451fda1b0ba8d38318113ad16e0fb29c67248..9e957ef7db9b7eb2a5fd4969704639de7c361b19 100644 (file)
@@ -5,7 +5,6 @@ import os
 import re
 
 from . import base
-from . import logs
 from . import users
 
 from .constants import *
@@ -222,33 +221,6 @@ class Builds(base.Object):
 
                return build
 
-       def get_comments(self, limit=10, offset=None, user=None):
-               query = "SELECT * FROM builds_comments \
-                       JOIN users ON builds_comments.user_id = users.id"
-               args = []
-
-               wheres = []
-               if user:
-                       wheres.append("users.id = %s")
-                       args.append(user.id)
-
-               if wheres:
-                       query += " WHERE %s" % " AND ".join(wheres)
-
-               # Sort everything.
-               query += " ORDER BY created_at DESC"
-
-               # Limits.
-               query += " LIMIT %s OFFSET %s"
-               args.extend([limit, offset])
-
-               comments = []
-               for comment in self.db.query(query, *args):
-                       comment = logs.CommentLogEntry(self.backend, comment)
-                       comments.append(comment)
-
-               return comments
-
 
 class Build(base.DataObject):
        table = "builds"
@@ -285,9 +257,6 @@ class Build(base.DataObject):
                # Deleted all associated bugs
                self.db.execute("DELETE FROM builds_bugs WHERE build_id = %s", self.id)
 
-               # Delete all comments
-               self.db.execute("DELETE FROM builds_comments WHERE build_id = %s", self.id)
-
                # Delete all watchers
                self.db.execute("DELETE FROM builds_watchers WHERE build_id = %s", self.id)
 
@@ -544,56 +513,41 @@ class Build(base.DataObject):
 
        ## Comment stuff
 
-       def get_comments(self, limit=10, offset=0):
-               query = "SELECT * FROM builds_comments \
-                       JOIN users ON builds_comments.user_id = users.id \
-                       WHERE build_id = %s     ORDER BY time_created ASC"
-
-               comments = []
-               for comment in self.db.query(query, self.id):
-                       comment = logs.CommentLogEntry(self.backend, comment)
-                       comments.append(comment)
-
-               return comments
-
-       def add_comment(self, user, text, score):
-               res = self.db.get("INSERT INTO builds_comments(build_id, user_id, \
-                       text, score) VALUES(%s, %s, %s, %s) RETURNING *",
-                       self.id, user.id, text, score)
-
-               comment = BuildComment(self.backend, res.id, data=res)
-
-               # Update the score cache
-               self.score += comment.score
+       def comment(self, *args, **kwargs):
+               """
+                       Submits a comment
+               """
+               # Create a new comment
+               comment = self.backend.builds.comments.create(build, *args, **kwargs)
 
-               # Send the new comment to all watchers and stuff
-               comment.send_message()
+               # Add to cache
+               self.comments.append(comment)
 
-               # Return the newly created comment
                return comment
 
        @lazy_property
-       def score(self):
-               res = self.db.get("SELECT SUM(score) AS score \
-                       FROM builds_comments WHERE build_id = %s", self.id)
-
-               return res.score or 0
-
-       def upvote(self, user, score=1):
-               # Creates an empty comment with a score
-               self.db.execute("INSERT INTO builds_comments(build_id, user_id, score) \
-                       VALUES(%s, %s, %s)", self.id, user.id, score)
-
-               # Update cache
-               self.score += score
+       def comments(self):
+               """
+                       Comments on this build
+               """
+               comments = self.backend.builds.comments._get_comments("""
+                       SELECT
+                               *
+                       FROM
+                               build_comments
+                       WHERE
+                               deleted IS FALSE
+                       WHERE
+                               build_id = %s
+                       ORDER BY created_at
+                       """, self.id,
+               )
 
-       def get_commenters(self):
-               users = self.db.query("SELECT DISTINCT users.id AS id FROM builds_comments \
-                       JOIN users ON builds_comments.user_id = users.id \
-                       WHERE builds_comments.build_id = %s AND NOT users.deleted = 'Y' \
-                       AND NOT users.activated = 'Y' ORDER BY users.id", self.id)
+               return list(comments)
 
-               return [users.User(self.backend, u.id) for u in users]
+       @property
+       def score(self):
+               return sum((c.score for c in self.comments))
 
        ## Logging stuff
 
@@ -642,24 +596,6 @@ class Build(base.DataObject):
 
                return self.backend.events.expand(res)
 
-       def get_log(self, comments=True, repo=True, limit=None):
-               entries = []
-
-               # Created entry.
-               created_entry = logs.CreatedLogEntry(self.backend, self)
-               entries.append(created_entry)
-
-               if comments:
-                       entries += self.get_comments(limit=limit)
-
-               # Sort all entries in chronological order.
-               entries.sort()
-
-               if limit:
-                       entries = entries[:limit]
-
-               return entries
-
        ## Watchers
 
        @lazy_property
@@ -794,8 +730,52 @@ class Build(base.DataObject):
                        self._update_bug(bug_id, status=status, resolution=resolution, comment=comment)
 
 
-class BuildComment(base.DataObject):
-       table = "builds_comments"
+class Comments(base.Object):
+       def _get_comments(self, query, *args):
+               res = self.db.query(query, *args)
+
+               for row in res:
+                       yield Comment(self.backend, row.id, data=row)
+
+       def _get_comment(self, query, *args):
+               res = self.db.get(query, *args)
+
+               if res:
+                       return Comment(self.backend, res.id, data=res)
+
+       def get_by_id(self, id):
+               return self._get_comment("""
+                       SELECT
+                               *
+                       FROM
+                               build_comments
+                       WHERE
+                               id = %s
+                       """, id,
+               )
+
+       def create(self, build, user, text=None, score=None):
+               comment = self._get_comment("""
+                       INSERT INTO
+                               build_comments(
+                                       build_id, user_id, text, score
+                               )
+                       VALUES(
+                               %s, %s, %s, %s
+                       )
+                       RETURNING
+                               *
+                       """, build, user, text or "", score or 0,
+               )
+
+               # Notify people about this new comment
+               comment.notify()
+
+               return comment
+
+
+class Comment(base.DataObject):
+       table = "build_comments"
 
        @lazy_property
        def build(self):
@@ -813,6 +793,6 @@ class BuildComment(base.DataObject):
        def score(self):
                return self.data.score
 
-       def send_message(self):
+       def notify(self):
                self.backend.messages.send_template_to_many(self.build.message_recipients, "builds/new-comment",
                        sender=self.user.envelope_from, build=self.build, user=self.user, text=self.text)
index f5a2a4e9cacdde6f8fe4623e12033ea8b53bcb7c..1c8bf93ef6a2b5ec6c21a123976ce7954e78c27a 100644 (file)
@@ -34,6 +34,21 @@ SET default_tablespace = '';
 
 SET default_table_access_method = heap;
 
+--
+-- Name: build_comments; Type: TABLE; Schema: public; Owner: -
+--
+
+CREATE TABLE public.build_comments (
+    id integer NOT NULL,
+    build_id integer NOT NULL,
+    user_id integer NOT NULL,
+    text text DEFAULT ''::text NOT NULL,
+    score integer NOT NULL,
+    created_at timestamp without time zone DEFAULT now() NOT NULL,
+    deleted boolean DEFAULT false NOT NULL
+);
+
+
 --
 -- Name: build_packages; Type: TABLE; Schema: public; Owner: -
 --
@@ -197,21 +212,6 @@ CREATE SEQUENCE public.builds_bugs_updates_id_seq
 ALTER SEQUENCE public.builds_bugs_updates_id_seq OWNED BY public.builds_bugs_updates.id;
 
 
---
--- Name: builds_comments; Type: TABLE; Schema: public; Owner: -
---
-
-CREATE TABLE public.builds_comments (
-    id integer NOT NULL,
-    build_id integer NOT NULL,
-    user_id integer NOT NULL,
-    text text,
-    score integer NOT NULL,
-    time_created timestamp without time zone DEFAULT now() NOT NULL,
-    time_updated timestamp without time zone
-);
-
-
 --
 -- Name: builds_comments_id_seq; Type: SEQUENCE; Schema: public; Owner: -
 --
@@ -228,7 +228,7 @@ CREATE SEQUENCE public.builds_comments_id_seq
 -- Name: builds_comments_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: -
 --
 
-ALTER SEQUENCE public.builds_comments_id_seq OWNED BY public.builds_comments.id;
+ALTER SEQUENCE public.builds_comments_id_seq OWNED BY public.build_comments.id;
 
 
 --
@@ -1104,6 +1104,13 @@ CREATE SEQUENCE public.users_id_seq
 ALTER SEQUENCE public.users_id_seq OWNED BY public.users.id;
 
 
+--
+-- Name: build_comments id; Type: DEFAULT; Schema: public; Owner: -
+--
+
+ALTER TABLE ONLY public.build_comments ALTER COLUMN id SET DEFAULT nextval('public.builds_comments_id_seq'::regclass);
+
+
 --
 -- Name: builders id; Type: DEFAULT; Schema: public; Owner: -
 --
@@ -1125,13 +1132,6 @@ ALTER TABLE ONLY public.builds ALTER COLUMN id SET DEFAULT nextval('public.build
 ALTER TABLE ONLY public.builds_bugs_updates ALTER COLUMN id SET DEFAULT nextval('public.builds_bugs_updates_id_seq'::regclass);
 
 
---
--- Name: builds_comments id; Type: DEFAULT; Schema: public; Owner: -
---
-
-ALTER TABLE ONLY public.builds_comments ALTER COLUMN id SET DEFAULT nextval('public.builds_comments_id_seq'::regclass);
-
-
 --
 -- Name: distributions id; Type: DEFAULT; Schema: public; Owner: -
 --
@@ -1265,6 +1265,14 @@ ALTER TABLE ONLY public.users ALTER COLUMN id SET DEFAULT nextval('public.users_
 ALTER TABLE ONLY public.users_emails ALTER COLUMN id SET DEFAULT nextval('public.users_emails_id_seq'::regclass);
 
 
+--
+-- Name: build_comments build_comments_pkey; Type: CONSTRAINT; Schema: public; Owner: -
+--
+
+ALTER TABLE ONLY public.build_comments
+    ADD CONSTRAINT build_comments_pkey PRIMARY KEY (id);
+
+
 --
 -- Name: builds builds_pkey; Type: CONSTRAINT; Schema: public; Owner: -
 --
@@ -1297,14 +1305,6 @@ ALTER TABLE ONLY public.builds_bugs_updates
     ADD CONSTRAINT idx_2198008_primary PRIMARY KEY (id);
 
 
---
--- Name: builds_comments idx_2198018_primary; Type: CONSTRAINT; Schema: public; Owner: -
---
-
-ALTER TABLE ONLY public.builds_comments
-    ADD CONSTRAINT idx_2198018_primary PRIMARY KEY (id);
-
-
 --
 -- Name: images_types idx_2198057_primary; Type: CONSTRAINT; Schema: public; Owner: -
 --
@@ -1473,6 +1473,27 @@ ALTER TABLE ONLY public.uploads
     ADD CONSTRAINT uploads_id PRIMARY KEY (id);
 
 
+--
+-- Name: build_comments_build_id; Type: INDEX; Schema: public; Owner: -
+--
+
+CREATE INDEX build_comments_build_id ON public.build_comments USING btree (build_id) WHERE (deleted IS FALSE);
+
+
+--
+-- Name: build_comments_created_at; Type: INDEX; Schema: public; Owner: -
+--
+
+CREATE INDEX build_comments_created_at ON public.build_comments USING btree (created_at) WHERE (deleted IS FALSE);
+
+
+--
+-- Name: build_comments_user_id; Type: INDEX; Schema: public; Owner: -
+--
+
+CREATE INDEX build_comments_user_id ON public.build_comments USING btree (user_id) WHERE (deleted IS FALSE);
+
+
 --
 -- Name: build_packages_build_id; Type: INDEX; Schema: public; Owner: -
 --
@@ -1538,20 +1559,6 @@ CREATE INDEX filelists_pkg_id ON public.filelists USING btree (pkg_id);
 ALTER TABLE public.filelists CLUSTER ON filelists_pkg_id;
 
 
---
--- Name: idx_2198018_build_id; Type: INDEX; Schema: public; Owner: -
---
-
-CREATE INDEX idx_2198018_build_id ON public.builds_comments USING btree (build_id);
-
-
---
--- Name: idx_2198018_user_id; Type: INDEX; Schema: public; Owner: -
---
-
-CREATE INDEX idx_2198018_user_id ON public.builds_comments USING btree (user_id);
-
-
 --
 -- Name: idx_2198147_name; Type: INDEX; Schema: public; Owner: -
 --
@@ -1823,18 +1830,18 @@ ALTER TABLE ONLY public.builds
 
 
 --
--- Name: builds_comments builds_comments_build_id; Type: FK CONSTRAINT; Schema: public; Owner: -
+-- Name: build_comments builds_comments_build_id; Type: FK CONSTRAINT; Schema: public; Owner: -
 --
 
-ALTER TABLE ONLY public.builds_comments
+ALTER TABLE ONLY public.build_comments
     ADD CONSTRAINT builds_comments_build_id FOREIGN KEY (build_id) REFERENCES public.builds(id);
 
 
 --
--- Name: builds_comments builds_comments_user_id; Type: FK CONSTRAINT; Schema: public; Owner: -
+-- Name: build_comments builds_comments_user_id; Type: FK CONSTRAINT; Schema: public; Owner: -
 --
 
-ALTER TABLE ONLY public.builds_comments
+ALTER TABLE ONLY public.build_comments
     ADD CONSTRAINT builds_comments_user_id FOREIGN KEY (user_id) REFERENCES public.users(id);
 
 
index ca988889adf92795bceb236836fb5c2124e8bcf8..9f0d6c2dff8a76864d1cacb5fac1c7d1152a778b 100644 (file)
@@ -118,7 +118,7 @@ class BuildDetailCommentHandler(base.BaseHandler):
 
                # Add a new comment to the build.
                if text or vote:
-                       build.add_comment(self.current_user, text, vote)
+                       build.comment(self.current_user, text, vote)
 
                # Redirect to the build detail page.
                self.redirect("/build/%s" % build.uuid)