diff options
author | Jacob Mason <jacoblmason@gmail.com> | 2010-08-09 18:31:13 -0500 |
---|---|---|
committer | Jacob Mason <jacoblmason@gmail.com> | 2010-08-09 18:31:13 -0500 |
commit | 0f98c779c0813ffe3413f8678bb7c8cd88aabdab (patch) | |
tree | a18ca4493af5cebe6feefaf41a200b52b238dd41 /sphinx/websupport | |
parent | 18ef86f0452595e0d7a414ceddfc9615ca476549 (diff) | |
download | sphinx-git-0f98c779c0813ffe3413f8678bb7c8cd88aabdab.tar.gz |
added get_metadata
Diffstat (limited to 'sphinx/websupport')
-rw-r--r-- | sphinx/websupport/__init__.py | 15 | ||||
-rw-r--r-- | sphinx/websupport/storage/__init__.py | 9 | ||||
-rw-r--r-- | sphinx/websupport/storage/db.py | 5 | ||||
-rw-r--r-- | sphinx/websupport/storage/sqlalchemystorage.py | 14 |
4 files changed, 42 insertions, 1 deletions
diff --git a/sphinx/websupport/__init__.py b/sphinx/websupport/__init__.py index 38ebd2340..090fee1ad 100644 --- a/sphinx/websupport/__init__.py +++ b/sphinx/websupport/__init__.py @@ -160,7 +160,11 @@ class WebSupport(object): document = pickle.load(f) comment_opts = self._make_comment_options(username, moderator) - document['js'] = comment_opts + '\n' + document['js'] + comment_metadata = self.storage.get_metadata(docname, moderator) + + document['js'] = '\n'.join([comment_opts, + self._make_metadata(comment_metadata), + document['js']]) return document def get_search_results(self, q): @@ -390,3 +394,12 @@ class WebSupport(object): parts.append('</script>') return '\n'.join(parts) + def _make_metadata(self, data): + node_js = ', '.join(['%s: %s' % (node_id, comment_count) + for node_id, comment_count in data.iteritems()]) + js = """ +<script type="text/javascript"> + var COMMENT_METADATA = {%s}; +</script>""" % node_js + return js + 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() |