summaryrefslogtreecommitdiff
path: root/sphinx/websupport/storage
diff options
context:
space:
mode:
Diffstat (limited to 'sphinx/websupport/storage')
-rw-r--r--sphinx/websupport/storage/__init__.py9
-rw-r--r--sphinx/websupport/storage/db.py5
-rw-r--r--sphinx/websupport/storage/sqlalchemystorage.py14
3 files changed, 28 insertions, 0 deletions
diff --git a/sphinx/websupport/storage/__init__.py b/sphinx/websupport/storage/__init__.py
index 70b23150d..24d4ade55 100644
--- a/sphinx/websupport/storage/__init__.py
+++ b/sphinx/websupport/storage/__init__.py
@@ -61,6 +61,15 @@ class StorageBackend(object):
"""
raise NotImplementedError()
+ def get_metadata(self, docname, moderator):
+ """Get metadata for a document. This is currently just a dict
+ of node_id's with associated comment counts.
+
+ :param docname: the name of the document to get metadata for.
+ :param moderator: whether the requester is a moderator.
+ """
+ raise NotImplementedError()
+
def get_data(self, node_id, username, moderator):
"""Called to retrieve all data for a node. This should return a
dict with two keys, *source* and *comments* as described by
diff --git a/sphinx/websupport/storage/db.py b/sphinx/websupport/storage/db.py
index 64f7f3e25..4a84cd086 100644
--- a/sphinx/websupport/storage/db.py
+++ b/sphinx/websupport/storage/db.py
@@ -112,6 +112,9 @@ class Comment(Base):
proposal_diff = Column(Text)
path = Column(String(256), index=True)
+ node_id = Column(Integer, ForeignKey(db_prefix + 'nodes.id'))
+ node = relation(Node, backref="comments")
+
def __init__(self, text, displayed, username, rating, time,
proposal, proposal_diff):
self.text = text
@@ -127,12 +130,14 @@ class Comment(Base):
# This exists because the path can't be set until the session has
# been flushed and this Comment has an id.
if node_id:
+ self.node_id = node_id
self.path = '%s.%s' % (node_id, self.id)
else:
session = Session()
parent_path = session.query(Comment.path).\
filter(Comment.id == parent_id).one().path
session.close()
+ self.node_id = parent_path.split('.')[0]
self.path = '%s.%s' % (parent_path, self.id)
def serializable(self, vote=0):
diff --git a/sphinx/websupport/storage/sqlalchemystorage.py b/sphinx/websupport/storage/sqlalchemystorage.py
index 94318a965..1aaa84738 100644
--- a/sphinx/websupport/storage/sqlalchemystorage.py
+++ b/sphinx/websupport/storage/sqlalchemystorage.py
@@ -12,6 +12,7 @@
from datetime import datetime
from sqlalchemy.orm import aliased
+from sqlalchemy.sql import func
from sphinx.websupport.errors import *
from sphinx.websupport.storage import StorageBackend
@@ -84,6 +85,19 @@ class SQLAlchemyStorage(StorageBackend):
session.close()
raise UserNotAuthorizedError()
+ def get_metadata(self, docname, moderator):
+ session = Session()
+ subquery = session.query(
+ Comment.id, Comment.node_id,
+ func.count('*').label('comment_count')).group_by(
+ Comment.node_id).subquery()
+ nodes = session.query(Node.id, subquery.c.comment_count).outerjoin(
+ (subquery, Node.id==subquery.c.node_id)).filter(
+ Node.document==docname)
+ session.close()
+ session.commit()
+ return dict([(k, v or 0) for k, v in nodes])
+
def get_data(self, node_id, username, moderator):
session = Session()
node = session.query(Node).filter(Node.id == node_id).one()