Added document indexes delete support, started merge with filesystem serving app

This commit is contained in:
Roberto Rosario
2011-05-18 14:34:55 -04:00
parent 1d20a10d49
commit 2a2fba9ac6
7 changed files with 130 additions and 93 deletions

View File

@@ -5,57 +5,55 @@ from django.utils.safestring import mark_safe
from metadata.classes import MetadataObject
#from filesystem_serving.conf.settings import MAX_RENAME_COUNT
from document_indexing.models import Index, IndexInstance
from document_indexing.conf.settings import AVAILABLE_INDEXING_FUNCTIONS
from document_indexing.conf.settings import FILESERVING_ENABLE
from document_indexing.conf.settings import FILESERVING_PATH
from document_indexing.conf.settings import SLUGIFY_PATHS
if SLUGIFY_PATHS == False:
# Do not slugify path or filenames and extensions
SLUGIFY_FUNCTION = lambda x: x
else:
SLUGIFY_FUNCTION = slugify
def evaluate_index(eval_dict, document, node, parent_index_instance=None):
warnings = []
if node.enabled:
try:
result = eval(node.expression, eval_dict, AVAILABLE_INDEXING_FUNCTIONS)
index_instance, created = IndexInstance.objects.get_or_create(index=node, value=result, parent=parent_index_instance)
if node.link_documents:
index_instance.documents.add(document)
for children in node.get_children():
children_warnings = evaluate_index(eval_dict, document, children, index_instance)
warnings.extend(children_warnings)
except NameError, exc:
warnings.append(_(u'Error in metadata indexing expression: %s') % exc)
#raise NameError()
#This should be a warning not an error
#pass
except Exception, exc:
warnings.append(_(u'Unable to create metadata indexing directory: %s') % exc)
return warnings
# External functions
def update_indexes(document):
print 'update_indexes'
"""
Update or create all the index instances related to a document
"""
warnings = []
eval_dict = {}
document_metadata_dict = dict([(metadata.metadata_type.name, metadata.value) for metadata in document.documentmetadata_set.all() if metadata.value])
eval_dict['document'] = document
metadata_dict = dict([(metadata.metadata_type.name, metadata.value) for metadata in document.documentmetadata_set.all() if metadata.value])
eval_dict['metadata'] = MetadataObject(metadata_dict)
eval_dict['metadata'] = MetadataObject(document_metadata_dict)
for root in Index.objects.filter(parent=None):
index_warnings = evaluate_index(eval_dict, document, root)
index_warnings = _evaluate_index(eval_dict, document, root)
warnings.extend(index_warnings)
return warnings
def delete_indexes(document):
print 'delete_indexes'
"""
Delete all the index instances related to a document
"""
warnings = []
for node in document.indexinstance_set.all():
_delete_document_from_node(document, node)
return warnings
def get_instance_link(index_instance=None, text=None, simple=False):
"""
Return an HTML anchor to an index instance
"""
if simple:
# Just display the instance's value or overrided text, no
# HTML anchor
@@ -74,6 +72,10 @@ def get_instance_link(index_instance=None, text=None, simple=False):
def get_breadcrumbs(index_instance, simple=False, single_link=False):
"""
Return a joined string of HTML anchors to every index instance's
parent from the root of the tree to the index instance
"""
result = []
if single_link:
# Return the entire breadcrumb as a single HTML anchor
@@ -90,3 +92,58 @@ def get_breadcrumbs(index_instance, simple=False, single_link=False):
return mark_safe(get_instance_link(index_instance=index_instance, text=(u' / '.join(result))))
else:
return mark_safe(u' / '.join(result))
# Internal functions
def _evaluate_index(eval_dict, document, node, parent_index_instance=None):
"""
Evaluate an index expression and update or create all the related
index instances also recursively calling itself to evaluate all the
index's children
"""
warnings = []
if node.enabled:
try:
result = eval(node.expression, eval_dict, AVAILABLE_INDEXING_FUNCTIONS)
index_instance, created = IndexInstance.objects.get_or_create(index=node, value=result, parent=parent_index_instance)
if node.link_documents:
index_instance.documents.add(document)
for children in node.get_children():
children_warnings = _evaluate_index(eval_dict, document, children, index_instance)
warnings.extend(children_warnings)
except (NameError, AttributeError), exc:
warnings.append(_(u'Error in document indexing update expression: %(expression)s; %(exception)s') % {
'expression': node.expression, 'exception': exc})
#raise NameError()
#This should be a warning not an error
#pass
except Exception, exc:
warnings.append(_(u'Error updating document index, expression: %(expression)s; %(exception)s') % {
'expression': node.expression, 'exception': exc})
return warnings
def _delete_document_from_node(document, node):
"""
Delete a documents reference from an index instance and call itself
recusively deleting documents and empty index instances up to the
root of the tree
"""
warnings = []
try:
node.documents.remove(document)
if node.documents.count() == 0 and node.get_children().count() == 0:
parent = node.parent
node.delete()
parent_warnings = _delete_document_from_node(document, parent)
warnings.extend(parent_warnings)
except Exception, exc:
warnings.append(_(u'Unable to delete document indexing node; %s') % exc)
return warnings