# $Id$ # $URL$ logger = logging.getLogger('meresco.drilldown') logger.debug("Starting logger") drilldownConfig = configs['drilldown'] def getDrilldownFields(drilldownConfig,drilldownFieldsName): # no access to drilldownConfig, why? logger.debug("drilldownConfig:" + str(drilldownConfig)) drilldownList = drilldownConfig.get(drilldownFieldsName, section='drilldownFields') logger.debug (infra.prpt("list",drilldownList)) list = [] for line in drilldownList.rsplit("\n"): if line == "": continue (name,number,boolean) = line.rsplit(',') if boolean == 'True': boolean = True else: boolean = False number = int (number) tupe = (name.strip(), number, boolean) list.append(tupe) return list drilldownFields = getDrilldownFields(drilldownConfig,'drilldownFields') drilldownFieldsTop = getDrilldownFields(drilldownConfig,'drilldownFields-top') drilldownFieldsNoMultiLevel = [] drilldownFieldsNoMultiLevel.extend(drilldownFieldsTop) drilldownFieldsNoMultiLevel.extend(drilldownFields) space = "\n " def fieldAndTermSplit(fieldAndTerm): return fieldAndTerm.split(':', 1) def fieldAndTermJoin(field, term): return '%s:%s' % (field, term) def shrink(aString, maxLength=160): if len(aString) > maxLength: return aString[:maxLength] + '...' return aString def createDrilldownQuery(drilldownArgs, sortKeys): newArguments = []; if sortKeys: newArguments.append(('sortKeys', sortKeys)) for existing in drilldownArgs: newArguments.append(('drilldown', existing)) return urlencode(newArguments) def displayLabel(label): return '
%s
' % label def displayTerm(term, fieldName, language, maxLength=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) return shrink(term, maxLength) def displayItem_checkbox(query, term, selected, field, language, count): s = space + ' (' + str(count) + ')' if selected: # s = '' + text + '' s = '' else: s = '
' s += text s += '
' return s def displayItemGroup(label, termCounts, selectedFacets, fieldName, selectedFieldNames, arguments, language, sortKeys): s = '' t = '' for term, count in termCounts: if fieldName in selectedFieldNames and term in selectedFieldNames[fieldName]: drilldownArgs = set(selectedFacets) drilldownArgs.remove(fieldName + ":" + term) query = createDrilldownQuery(drilldownArgs, sortKeys) s += displayItem(query, term, True, fieldName, language, count) else: drilldownArgs = set(arguments.get('drilldown', []) + [fieldAndTermJoin(fieldName, term)]) query = createDrilldownQuery(drilldownArgs, sortKeys) s += displayItem(query, term, False, fieldName, language, count) if (s): t = space + '
%s' % displayLabel(label) + s + '
' return t def displayFilter(query, term , category, selection): # logger.debug('Query:' + query) deselectImage = '/files/' + drilldownConfig.get('deselect', section='images') s = '
' s += 'deselect' + term + '' + escapeHtml(selection) 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') s += displayLabel(label) drilldownArgs = set(selectedFacets) drilldownArgs.remove(upField + ":" + term) query = createDrilldownQuery(drilldownArgs, sortKeys) s += displayItem(query, term, True, upField, language, total) previousUpField = upField return s def displayDrilldownFields(total, docset, drilldownResults, multilevelResults, arguments, language): chooseFromTitle = drilldownConfig.get('choosefrom') selectedFacets = arguments.get('drilldown', []) selectedFieldNames = {} if selectedFacets: for drilldownItem in selectedFacets: fieldName, value = fieldAndTermSplit(drilldownItem) if not fieldName in selectedFieldNames: selectedFieldNames[fieldName] = [] selectedFieldNames[fieldName].append(value) yield '
' yield '
' + chooseFromTitle +'
' sortKeys = arguments.get('sortKeys', [''])[0] if multilevelResults or total < 5000000: # TP: result set is not too large, multilevel results will be calculated # or this is already done (multilevelResults) if not multilevelResults: multilevelResults = any.multiLevelDrilldown(docset, ['date', 'author']) for (fieldName, levelField), termCounts in multilevelResults: if ( levelField == None ): logger.error("displayDrilldownFields skipping record: " + fieldName ) levelField='' yield displayDrilldownExtraLabel(levelField, selectedFieldNames, language, selectedFacets, sortKeys, total) label = drilldownConfig.get(levelField, section = 'fieldNames',default='LEEG') yield displayItemGroup(label, termCounts, selectedFacets, levelField, selectedFieldNames, arguments, language, sortKeys) else: for fieldName in multilevelResults: result = multilevelResults[fieldName] levelField = '' if not 'level' in result: logger.error("displayDrilldownFields skipping record: " + fieldName ) else: levelField = result['level'] yield displayDrilldownExtraLabel(levelField, selectedFieldNames, language, selectedFacets, sortKeys, total) label = drilldownConfig.get(levelField, section = 'fieldNames',default='LEEG') termCounts = result['termCounts'] yield displayItemGroup(label, termCounts, selectedFacets, levelField, selectedFieldNames, arguments, language, sortKeys) fields = drilldownFields # fields = sortedDrilldownFields else: # TP: result set is too large, only one level of the multilevel facests will be calculated. fields = drilldownFieldsNoMultiLevel if not drilldownResults: drilldownResults = any.drilldown(docset, fields) drilldownResultsAsDict = dict(drilldownResults) else: drilldownResultsAsDict = drilldownResults for fieldName, number, sorted in fields: termCounts = [] if fieldName in drilldownResults: termCounts = drilldownResults[fieldName] else: termCounts = drilldownResultsAsDict.get(fieldName,[]) label = drilldownConfig.get(fieldName, section = 'fieldNames') if termCounts == []: continue yield displayItemGroup(label, termCounts, selectedFacets, fieldName, selectedFieldNames, arguments, language, sortKeys) yield """ %(space)s
%(space)s """ % {'space': space} def main(total, docset, drilldownResults, multilevelResults, arguments, path='', *args, **kwargs): ### forcing english as language language = 'en' # language = path.startswith('/en/') and 'en' or 'nl' drilldownConfig.setDefaults(section='labels', language=language) yield space + '
' selectedFacets = arguments.get('drilldown', []) selectedLabel= drilldownConfig.get('limitTo',default='Selected:') sortKeys = arguments.get('sortKeys', [''])[0] if selectedFacets: yield space + '
%s
' % selectedLabel for drilldownItem in selectedFacets: fieldName, value = fieldAndTermSplit(drilldownItem) drilldownArgs = set(selectedFacets) drilldownArgs.remove(drilldownItem) label = drilldownConfig.get(fieldName, section = 'fieldNames') query = createDrilldownQuery(drilldownArgs, sortKeys) term = displayTerm(value, fieldName, language) yield displayFilter(query, value, label, term) yield space + '
' if total > 0: yield displayDrilldownFields(total, docset, drilldownResults, multilevelResults, arguments, language) yield space + '
'