import re
from . import base
-from . import logs
from . import users
from .constants import *
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"
# 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)
## 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
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
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):
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)
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: -
--
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: -
--
-- 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;
--
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: -
--
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: -
--
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: -
--
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: -
--
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: -
--
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: -
--
--
--- 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);