Merge with DasIch's branch

This commit is contained in:
Jacob Mason 2010-07-27 17:06:12 -05:00
commit 14b347e598
8 changed files with 55 additions and 25 deletions

View File

@ -25,7 +25,7 @@ class WebSupportBuilder(StandaloneHTMLBuilder):
def init_translator_class(self):
self.translator_class = WebSupportTranslator
def write_doc(self, docname, doctree):
# The translator needs the docname to generate ids.
self.cur_docname = docname

View File

@ -47,7 +47,7 @@ class WebSupport(object):
self._init_search(search)
self._init_storage(storage)
def _init_storage(self, storage):
if isinstance(storage, StorageBackend):
self.storage = storage
@ -61,7 +61,7 @@ class WebSupport(object):
ensuredir(path.dirname(db_path))
engine = create_engine('sqlite:///%s' % db_path)
self.storage = SQLAlchemyStorage(engine)
def _init_templating(self):
import sphinx
template_path = path.join(path.dirname(sphinx.__file__),
@ -74,7 +74,7 @@ class WebSupport(object):
self.search = search
else:
mod, cls = search_adapters[search]
search_class = getattr(__import__('sphinx.websupport.search.' + mod,
search_class = getattr(__import__('sphinx.websupport.search.' + mod,
None, None, [cls]), cls)
search_path = path.join(self.outdir, 'search')
self.search = search_class(search_path)
@ -208,14 +208,12 @@ class WebSupport(object):
"""
id = parent_id[1:]
is_node = parent_id[0] == 's'
node = self.storage.get_node(id) if is_node else None
parent = self.storage.get_comment(id) if not is_node else None
diff = get_diff_html(node.source, proposal) if proposal else None
return self.storage.add_comment(text, displayed, username, rating,
return self.storage.add_comment(text, displayed, username, rating,
time, proposal, diff, node, parent)
def process_vote(self, comment_id, user_id, value):
"""Process a user's vote. The web support package relies
on the API user to perform authentication. The API user will

View File

@ -1,3 +1,13 @@
# -*- coding: utf-8 -*-
"""
sphinx.websupport.comments
~~~~~~~~~~~~~~~~~~~~~~~~~~
Comments for the websupport package.
:copyright: Copyright 2007-2010 by the Sphinx team, see AUTHORS.
:license: BSD, see LICENSE for details.
"""
class StorageBackend(object):
def pre_build(self):

View File

@ -1,7 +1,19 @@
# -*- coding: utf-8 -*-
"""
sphinx.websupport.comments.db
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
SQLAlchemy table and mapper definitions used by the
:class:`sphinx.websupport.comments.SQLAlchemyStorage`.
:copyright: Copyright 2007-2010 by the Sphinx team, see AUTHORS.
:license: BSD, see LICENSE for details.
"""
from datetime import datetime
from sqlalchemy import Column, Integer, Text, String, Boolean, ForeignKey,\
DateTime
DateTime
from sqlalchemy.schema import UniqueConstraint
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import relation, sessionmaker
@ -15,13 +27,13 @@ db_prefix = 'sphinx_'
class Node(Base):
"""Data about a Node in a doctree."""
__tablename__ = db_prefix + 'nodes'
id = Column(Integer, primary_key=True)
document = Column(String(256), nullable=False)
line = Column(Integer)
source = Column(Text, nullable=False)
treeloc = Column(String(32), nullable=False)
def __init__(self, document, line, source, treeloc):
self.document = document
self.line = line
@ -89,7 +101,7 @@ class Comment(Base):
'node': self.node.id if self.node else None,
'parent': self.parent.id if self.parent else None,
'proposal_diff': self.proposal_diff,
'children': [child.serializable(user_id)
'children': [child.serializable(user_id)
for child in self.children]}
def pretty_delta(self, delta):
@ -104,7 +116,7 @@ class Comment(Base):
dt = (days, 'day')
return '%s %s ago' % dt if dt[0] == 1 else '%s %ss ago' % dt
class CommentVote(Base):
__tablename__ = db_prefix + 'commentvote'

View File

@ -1,7 +1,19 @@
# -*- coding: utf-8 -*-
"""
sphinx.websupport.comments.sqlalchemystorage
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
A SQLAlchemy storage backend.
:copyright: Copyright 2007-2010 by the Sphinx team, see AUTHORS.
:license: BSD, see LICENSE for details.
"""
from datetime import datetime
from sphinx.websupport.comments import StorageBackend
from sphinx.websupport.comments.db import Base, Node, Comment, CommentVote, Session
from sphinx.websupport.comments.db import Base, Node, Comment, CommentVote,\
Session
class SQLAlchemyStorage(StorageBackend):
def __init__(self, engine):
@ -29,28 +41,26 @@ class SQLAlchemyStorage(StorageBackend):
self.build_session.commit()
self.build_session.close()
def add_comment(self, text, displayed, username, rating, time,
def add_comment(self, text, displayed, username, rating, time,
proposal, proposal_diff, node=None, parent=None):
time = time or datetime.now()
session = Session()
comment = Comment(text, displayed, username, rating, time,
proposal, proposal_diff, node, parent)
session.add(comment)
session.commit()
comment = comment.serializable()
session.close()
return comment
def get_comment(self, comment_id):
session = Session()
comment = session.query(Comment) \
.filter(Comment.id == comment_id).first()
session.close()
return comment
return comment
def get_comments(self, parent_id, user_id):
parent_id = parent_id[1:]
@ -67,7 +77,7 @@ class SQLAlchemyStorage(StorageBackend):
vote = session.query(CommentVote).filter(
CommentVote.comment_id == comment_id).filter(
CommentVote.user_id == user_id).first()
comment = session.query(Comment).filter(
Comment.id == comment_id).first()

View File

@ -40,10 +40,10 @@ class WhooshSearch(BaseSearch):
def finish_indexing(self):
self.index_writer.commit()
def add_document(self, pagename, title, text):
self.index_writer.add_document(path=unicode(pagename),
title=title,
title=title,
text=text)
def handle_query(self, q):

View File

@ -37,7 +37,7 @@ class XapianSearch(BaseSearch):
def finish_indexing(self):
# Ensure the db lock is removed.
del self.database
def add_document(self, path, title, text):
self.database.begin_transaction()
# sphinx_page_path is used to easily retrieve documents by path.

View File

@ -25,7 +25,7 @@ class WebSupportTranslator(HTMLTranslator):
def init_support(self):
self.in_commentable = False
self.current_id = 0
def dispatch_visit(self, node):
if node.__class__.__name__ in self.commentable_nodes:
self.handle_visit_commentable(node)