only add node if it doesn't already exist

This commit is contained in:
Jacob Mason 2010-08-14 18:33:07 -05:00
parent a52fd809bf
commit 34379fedb7
4 changed files with 21 additions and 7 deletions

View File

@ -6,6 +6,8 @@ Web Support Quick Start
Building Documentation Data
~~~~~~~~~~~~~~~~~~~~~~~~~~~
New Test p
To make use of the web support package in your application you'll
need to build the data it uses. This data includes pickle files representing
documents, search indices, and node data that is used to track where

View File

@ -16,6 +16,13 @@ class StorageBackend(object):
"""
pass
def has_node(self, id):
"""Check to see if a node exists.
:param id: the id to check for.
"""
raise NotImplementedError()
def add_node(self, id, document, source):
"""Add a node to the StorageBackend.

View File

@ -33,11 +33,16 @@ class SQLAlchemyStorage(StorageBackend):
def pre_build(self):
self.build_session = Session()
def has_node(self, id):
session = Session()
node = session.query(Node).filter(Node.id == id).first()
session.close()
return True if node else False
def add_node(self, id, document, source):
node = Node(id, document, source)
self.build_session.add(node)
self.build_session.flush()
return node
def post_build(self):
self.build_session.commit()

View File

@ -27,19 +27,19 @@ class WebSupportTranslator(HTMLTranslator):
HTMLTranslator.dispatch_visit(self, node)
def handle_visit_commentable(self, node):
db_node = self.add_db_node(node)
# We will place the node in the HTML id attribute. If the node
# already has an id (for indexing purposes) put an empty
# span with the existing id directly before this node's HTML.
self.add_db_node(node)
if node.attributes['ids']:
self.body.append('<span id="%s"></span>'
% node.attributes['ids'][0])
node.attributes['ids'] = ['s%s' % db_node.id]
node.attributes['ids'] = ['s%s' % node.uid]
node.attributes['classes'].append(self.comment_class)
def add_db_node(self, node):
storage = self.builder.app.storage
db_node_id = storage.add_node(id=node.uid,
document=self.builder.cur_docname,
source=node.rawsource or node.astext())
return db_node_id
if not storage.has_node(node.uid):
storage.add_node(id=node.uid,
document=self.builder.cur_docname,
source=node.rawsource or node.astext())