added get_metadata

This commit is contained in:
Jacob Mason 2010-08-09 18:31:13 -05:00
parent 18ef86f045
commit 0f98c779c0
4 changed files with 42 additions and 1 deletions

View File

@ -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

View File

@ -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

View File

@ -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):

View File

@ -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()