From 0ddc19292e30f390bb8558a9dca36be157e35654 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20P=C3=A9ters?= Date: Tue, 22 Nov 2011 12:34:50 +0100 Subject: [PATCH] add sorting options to documents/dossiers searches (#931) --- tabellio/searchform/form.py | 120 ++++++++++++++++++++++++++++++------ 1 file changed, 102 insertions(+), 18 deletions(-) diff --git a/tabellio/searchform/form.py b/tabellio/searchform/form.py index bfdfef5..87f48e1 100644 --- a/tabellio/searchform/form.py +++ b/tabellio/searchform/form.py @@ -25,6 +25,8 @@ from plone.formwidget.contenttree import ObjPathSourceBinder from tabellio.searchform.interfaces import MessageFactory as _ +import Missing + from Products.Five import BrowserView from Products.Five.browser.pagetemplatefile import ViewPageTemplateFile from zope.schema.interfaces import IContextSourceBinder @@ -188,6 +190,10 @@ class IFolderWithDocuments(interface.Interface): class IFolderWithPfbDocuments(interface.Interface): pass +def cmp_term(x, y): + from plone.i18n.normalizer.fr import normalizer + return cmp(normalizer.normalize(x.title), normalizer.normalize(y.title)) + @grok.provider(IContextSourceBinder) def possible_document_types(context): catalog = getToolByName(context, 'portal_catalog') @@ -199,6 +205,7 @@ def possible_document_types(context): doctype_id = doctype.encode('ascii', 'replace') doctype_str = MAPPING.get(doctype, doctype) terms.append(SimpleVocabulary.createTerm(doctype_id, doctype_id, doctype_str)) + terms.sort(cmp_term) return SimpleVocabulary(terms) def get_docdos_type_id(context, type_id): @@ -227,6 +234,7 @@ def possible_dossier_types(context): dostype_id = dostype.encode('ascii', 'replace') dostype_str = MAPPING.get(dostype, dostype) terms.append(SimpleVocabulary.createTerm(dostype_id, dostype_id, dostype_str)) + terms.sort(cmp_term) return SimpleVocabulary(terms) @grok.provider(IContextSourceBinder) @@ -240,6 +248,7 @@ def possible_question_types(context): questype_id = questype.encode('ascii', 'replace') questype_str = MAPPING.get(questype, questype) terms.append(SimpleVocabulary.createTerm(questype_id, questype_id, questype_str)) + terms.sort(cmp_term) return SimpleVocabulary(terms) @grok.provider(IContextSourceBinder) @@ -297,6 +306,10 @@ class IDocumentSearch(interface.Interface): start = schema.Date(title=_(u'Start'), required=False) end = schema.Date(title=_(u'End'), required=False) + sort_on = schema.Choice(title=_(u'Sort By'), required=True, + default=_(u'Session'), + values=[_(u'Session'), _(u'Type'), _(u'Number')]) + class DocumentSearchForm(form.Form): method = 'get' @@ -305,6 +318,7 @@ class DocumentSearchForm(form.Form): fields['authors'].widgetFactory = FieldAuthorsWidget fields['polgroups'].widgetFactory = FieldPolgroupsWidget fields['topics'].widgetFactory = FieldTopicsWidget + fields['sort_on'].widgetFactory = FieldRadioboxesWidget ignoreContext = True template = ViewPageTemplateFile('form_templates/view_effectivesearch.pt') @@ -340,6 +354,10 @@ class IDossierSearch(interface.Interface): start = schema.Date(title=_(u'Start'), required=False) end = schema.Date(title=_(u'End'), required=False) + sort_on = schema.Choice(title=_(u'Sort By'), required=True, + default=_(u'Session'), + values=[_(u'Session'), _(u'Type'), _(u'Number')]) + class DossierSearchForm(form.Form): prefix = 'dossier' @@ -349,6 +367,7 @@ class DossierSearchForm(form.Form): fields['participants'].widgetFactory = FieldAuthorsWidget fields['polgroups'].widgetFactory = FieldPolgroupsWidget fields['topics'].widgetFactory = FieldTopicsWidget + fields['sort_on'].widgetFactory = FieldRadioboxesWidget ignoreContext = True template = ViewPageTemplateFile('form_templates/view_dossier_search.pt') @@ -380,6 +399,10 @@ class IQuestionSearch(interface.Interface): start = schema.Date(title=_(u'Start'), required=False) end = schema.Date(title=_(u'End'), required=False) + sort_on = schema.Choice(title=_(u'Sort By'), required=True, + default=_(u'Session'), + values=[_(u'Session'), _(u'Type')]) + class QuestionSearchForm(form.Form): prefix = 'question' @@ -387,6 +410,7 @@ class QuestionSearchForm(form.Form): fields['authors'].widgetFactory = FieldAuthorsWidget fields['polgroups'].widgetFactory = FieldPolgroupsWidget fields['topics'].widgetFactory = FieldTopicsWidget + fields['sort_on'].widgetFactory = FieldRadioboxesWidget ignoreContext = True template = ViewPageTemplateFile('form_templates/view_question_search.pt') @@ -426,8 +450,8 @@ class IDocumentPfbSearch(interface.Interface): end = schema.Date(title=_(u'End'), required=False) sort_on = schema.Choice(title=_(u'Sort By'), required=True, - default=_(u'Type'), - values=[_(u'Type'), _(u'Number'), _(u'Session')]) + default=_(u'Session'), + values=[_(u'Session'), _(u'Type'), _(u'Number')]) class DocumentPfbSearchForm(form.Form): @@ -684,16 +708,6 @@ class SearchView(BrowserView): if data.get('decreted'): kw['decreted'] = data.get('decreted') - if data.get('sort_on') == 'Type': - kw['sort_on'] = 'doctype' - kw['sort_order'] = 'ascending' - elif data.get('sort_on') == 'Number': - kw['sort_on'] = 'no' - kw['sort_order'] = 'ascending' - elif data.get('sort_on') == 'Session': - kw['sort_on'] = 'session' - kw['sort_order'] = 'descending' - if not kw and not data.get('text'): return [] @@ -708,15 +722,85 @@ class SearchView(BrowserView): elif data.get('search_type_is_question'): kw['portal_type'] = 'tabellio.documents.question' - if not kw.get('sort_on'): - kw['sort_on'] = 'dateDoc' - kw['sort_order'] = 'descending' + from plone.i18n.normalizer.fr import normalizer + def cmp_doctype(x, y): + if not (x.doctype is None or y.doctype is None or + x.doctype is Missing.Value or y.doctype is Missing.Value): + type_x = normalizer.normalize(MAPPING.get(x.doctype, x.doctype)) + type_y = normalizer.normalize(MAPPING.get(y.doctype, y.doctype)) + t = cmp(type_x, type_y) + else: + t = -cmp(x.doctype, y.doctype) + if t == 0: + t = -cmp(x.session, y.session) + if t == 0: + t = -cmp(x.no, y.no) + if t == 0: + t = cmp(x.nodoc, y.nodoc) + return t - print 'kw:', kw - print '--' + def get_no_as_int(i): + if i: + return int(i.split('-')[0]) + return 0 + + def cmp_dostype(x, y): + if not (x.dostype is None or y.dostype is None or + x.dostype is Missing.Value or y.dostype is Missing.Value): + type_x = normalizer.normalize(MAPPING.get(x.dostype, x.dostype)) + type_y = normalizer.normalize(MAPPING.get(y.dostype, y.dostype)) + else: + t = -cmp(x.dostype, y.dostype) + t = cmp(type_x, type_y) + if t == 0: + t = -cmp(x.session, y.session) + if t == 0: + t = -cmp(x.no, y.no) + return t + + def cmp_questype(x, y): + if not (x.questype is None or y.questype is None or + x.questype is Missing.Value or y.questype is Missing.Value): + type_x = normalizer.normalize(MAPPING.get(x.questype, x.questype)) + type_y = normalizer.normalize(MAPPING.get(y.questype, y.questype)) + else: + t = -cmp(x.questype, y.questype) + t = cmp(type_x, type_y) + if t == 0: + t = -cmp(x.session, y.session) + if t == 0: + t = -cmp(x.dateDoc, y.dateDoc) + return t + + def cmp_number(x, y): + t = -cmp(get_no_as_int(x.no), get_no_as_int(y.no)) + if t == 0: + t = -cmp(x.session, y.session) + if t == 0: + t = cmp(x.nodoc, y.nodoc) + return t + + def cmp_session(x, y): + t = -cmp(x.session, y.session) + if t == 0: + t = -cmp(get_no_as_int(x.no), get_no_as_int(y.no)) + return t + + cmp_function = None + if data.get('sort_on') == 'Type': + if data.get('search_type_is_document'): + cmp_function = cmp_doctype + elif data.get('search_type_is_dossier'): + cmp_function = cmp_dostype + elif data.get('search_type_is_question'): + cmp_function = cmp_questype + elif data.get('sort_on') == 'Number': + cmp_function = cmp_number + elif data.get('sort_on') == 'Session': + cmp_function = cmp_session catalog = getToolByName(self.context, 'portal_catalog') - return catalog(**kw) + return sorted(catalog(**kw), cmp_function) def deputy_search_form(self): f = DeputySearchForm(self.context, self.request)