# $Id$
# $URL$
logger = logging.getLogger('meresco.drilldown')
logger.debug("Starting logger")
drilldownConfig = infra.configRoot
serverConfig = infra.configRoot
spy = any.spy()
fieldFeatures = call.getFieldFeatures()
drilldownFields = call.getDrilldownFields('drilldownFields')
drilldownFieldsTop = call.getDrilldownFields('drilldownFields-top')
space = "\n "
def fieldAndTermSplit(fieldAndTerm):
return fieldAndTerm.split(':', 1)
def fieldAndTermJoin(field, term):
return '%s:%s' % (field, term)
def shrink(aString, maxLength=160):
if not isinstance(aString, unicode): aString = unicode(aString, 'utf-8')
if len(aString) > maxLength:
return aString[:maxLength] + '...'
return aString
def createDrilldownQuery(drilldownArgs, sortKeys):
newArguments = [];
if sortKeys: newArguments.append(('sortKeys', sortKeys))
for existing in drilldownArgs:
# make sure to convert the unicode into a native string before calling urlencode
newArguments.append(('drilldown', str(existing)))
# logger.debug(infra.prpt('newArguments',newArguments))
return urlencode(newArguments)
def displayLabel(label):
return '
%s
' % escapeHtml(label)
def displayTerm(term, fieldName, language ):
if term[0] == '"': term = term[1:-1]
maxLength = int(drilldownConfig.get('maxFacetItemLength', section='facets', default = "25"))
if fieldName == 'drilldown.mods.genre': term = term.replace('info:eu-repo/semantics/', '')
elif fieldName == 'drilldown.meta.repository.repositoryGroupId':
term = drilldownConfig.get(term, section='sourceName')
elif fieldName == 'drilldown.norm.date.decennium':
to = int(term) + 9
term = term + '-' + str(to)
elif fieldName == 'drilldown.norm.date.centennium':
to = int(term) + 99
term = term + '-' + str(to)
elif fieldName == 'drilldown.norm.language':
term = drilldownConfig.get(term, section = 'languages.translations', language=language)
return shrink(term, maxLength)
def displayItem(query, term, selected, field, language, count):
text = escapeHtml(displayTerm(term, field, language)) + ' (' + str(count) + ')'
if selected:
s = ''
else:
s = ''
return s
def showFacets(idType, text, label, showButton, language):
if idType == 'full':
if (showButton == True):
displayType = 'none'
else:
displayType = 'blocked'
buttonText = drilldownConfig.get('less', section='vocabulary', language=language)
else:
displayType = 'block'
buttonText = drilldownConfig.get('more', section='vocabulary', language=language)
s = space + ''
return s;
def displayItemGroup(label, termCounts, selectedFacets, fieldName, selectedFieldNames, arguments, language, sortKeys):
# logger.debug(infra.prpt('fieldFeatures', fieldFeatures))
if fieldName in fieldFeatures:
limit = int(fieldFeatures[fieldName][1])
else:
logger.error('Fieldname: "' + str(fieldName) + '" NOT declared in server.cfg[indices]drilldownFieldNames')
limit = int(drilldownConfig.get('defaultDefaultDisplaySize', section='facets', default='5'))
growingString = totalString = choppedString = ''
showButton = False
# logger.debug(infra.prpt('termCount', termCounts))
# logger.debug('fieldname: ' + fieldName + ' limit: ' + str(limit) + 'aantal: ' )
forceCollapse = 0
if limit < 0: forceCollapse = 1
number = 0
for term, count in termCounts:
if solrURL:
# term must be quoted, otherwise the search in an untokenized index field fails for complex terms.
term = '"%s"' %term
number += 1
# logger.debug('label: ' + label + 'term: ' + term + 'count: ' + str(count))
limit -= 1
if fieldName in selectedFieldNames and term in selectedFieldNames[fieldName]:
drilldownArgs = list(selectedFacets)
drilldownArgs.remove(fieldName + ":" + term)
query = createDrilldownQuery(drilldownArgs, sortKeys)
growingString += displayItem(query, term, True, fieldName, language, count)
else:
drilldownArgs = list(arguments.get('drilldown', []) + [fieldAndTermJoin(fieldName, term)])
query = createDrilldownQuery(drilldownArgs, sortKeys)
growingString += displayItem(query, term, False, fieldName, language, count)
if (limit == 0):
choppedString = growingString
showButton = True
if len(choppedString) and len (choppedString) < len (growingString):
showButton = True
elif len (choppedString) == len (growingString):
choppedString = ''
showButton = False
if forceCollapse:
choppedString = ' '
showButton = True
if (number > 0):
if choppedString:
totalString += showFacets( 'chopped', choppedString, label, showButton, language) ;
if growingString:
totalString += showFacets( 'full', growingString, label , showButton, language);
return totalString
def displayFilter(query, term , category, selection, **kwargs):
logger.debug(infra.prpt('******** Query:', query))
deselectImage = fixedPathForIE7(**kwargs) + 'files/' + drilldownConfig.get('deselect', section='images')
s = ''
return s
def displayDrilldownExtraLabel(levelField, selectedFieldNames,
language, selectedFacets, sortKeys, total ):
previousUpField = ""
s = ''
upField = ""
if levelField == "drilldown.norm.date.decennium" and "drilldown.norm.date.centennium" in selectedFieldNames:
upField = "drilldown.norm.date.centennium"
elif levelField == "drilldown.norm.date.year" and "drilldown.norm.date.centennium" in selectedFieldNames:
upField = "drilldown.norm.date.centennium"
elif levelField == "drilldown.norm.date.year" and "drilldown.norm.date.decennium" in selectedFieldNames:
upField = "drilldown.norm.date.decennium"
elif levelField == "drilldown.norm.name.oneinitial" and "drilldown.norm.name.lastname" in selectedFieldNames:
upField = "drilldown.norm.name.lastname"
if upField:
for term in selectedFieldNames[upField]:
if not (upField == previousUpField):
# label = drilldownFieldNames.get(upField, {'en':upField, 'nl':upField})[language]
label = drilldownConfig.get(upField, section='fieldNames', language=language)
s += displayLabel(label)
drilldownArgs = list(selectedFacets)
drilldownArgs.remove(upField + ":" + term)
query = createDrilldownQuery(drilldownArgs, sortKeys)
s += displayItem(query, term, True, upField, language, total)
previousUpField = upField
return s
def displayDrilldownFields(total, drilldownResults, multilevelResults, arguments, language):
chooseFromTitle = drilldownConfig.get('choosefrom', section='vocabulary', language=language)
selectedFacets = arguments.get('drilldown', [])
selectedFieldNames = {}
selectedAuthorFacet = ''
if selectedFacets:
for drilldownItem in selectedFacets:
fieldName, value = fieldAndTermSplit(drilldownItem)
#de aanname is dat het laatste geselecteerde facet als laatste verschijnt
if fieldName == 'drilldown.norm.name.lastname': selectedAuthorFacet = 'lastname'
elif fieldName == 'drilldown.norm.name.oneinitial': selectedAuthorFacet = 'oneinitial'
if not fieldName in selectedFieldNames: selectedFieldNames[fieldName] = []
selectedFieldNames[fieldName].append(value)
yield ''
yield '
' + escapeHtml(chooseFromTitle) +'
'
sortKeys = arguments.get('sortKeys', [''])[0]
for fieldName in multilevelResults:
result = multilevelResults[fieldName]
levelField = ''
if not 'level' in result:
logger.error("displayDrilldownFields skipping record: " + fieldName )
else: levelField = result['level']
int(drilldownConfig.get('maxFacetItemLength', section='facets', default="25"))
label = drilldownConfig.get(levelField, section='fieldNames',default='LEEG', language=language)
termCounts = result['termCounts']
yield displayItemGroup(label, termCounts, selectedFacets, levelField, selectedFieldNames,
arguments, language, sortKeys)
# spy(infra.prpt("fields",drilldownFields))
# spy(infra.prpt('------------------------drilldownResults' , drilldownResults))
drilldownResultsAsDict = drilldownResults
for fieldName, number, sorted in drilldownFields:
termCounts = []
if fieldName in drilldownResults:
termCounts = drilldownResults[fieldName]
else:
termCounts = drilldownResultsAsDict.get(fieldName,[])
label = drilldownConfig.get(fieldName, section='fieldNames', language=language)
if termCounts == []:
continue
yield displayItemGroup(label, termCounts, selectedFacets, fieldName, selectedFieldNames, arguments, language, sortKeys)
yield """
%(space)s
%(space)s """ % {'space': space}
def main(total, drilldownResults, multilevelResults, arguments, path='', *args, **kwargs):
### forcing english as language
# language = path.startswith('/en/') and 'en' or 'nl'
# drilldownConfig.setDefaults(section='labels', language=language)
language = getLanguage( kwargs['Headers'] )
yield space + ''
selectedFacets = arguments.get('drilldown', [])
selectedLabel= drilldownConfig.get('limitTo',default='Selected:',section='vocabulary', language=language)
sortKeys = arguments.get('sortKeys', [''])[0]
if selectedFacets:
yield space + '
%s
' % escapeHtml(selectedLabel)
for drilldownItem in reversed (selectedFacets):
fieldName, value = fieldAndTermSplit(drilldownItem)
drilldownArgs = set(selectedFacets)
drilldownArgs.remove(drilldownItem)
label = drilldownConfig.get(fieldName, section='fieldNames', language=language)
query = createDrilldownQuery(drilldownArgs, sortKeys)
term = displayTerm(value, fieldName, language)
if 'query' in kwargs: del kwargs['query']
yield displayFilter(query, value, label, term, **kwargs)
yield space + '
'
if total > 0:
yield displayDrilldownFields(total, drilldownResults, multilevelResults, arguments, language)
yield space + '
'