diff --git a/tabellio/searchform/cmpfunctions.py b/tabellio/searchform/cmpfunctions.py new file mode 100644 index 0000000..dcf6cab --- /dev/null +++ b/tabellio/searchform/cmpfunctions.py @@ -0,0 +1,91 @@ +import Missing +from tabellio.documents.typenames import MAPPING +from plone.i18n.normalizer.fr import normalizer +import tabellio.config.utils + +def get_no_as_int(i): + if i: + try: + return int(i.split('-')[0]) + except ValueError: + return 99999 + return 0 + +class Cmp: + + def cmp_doctype(self, 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 + + _ordered_sessions = None + def ordered_sessions(self): + if self._ordered_sessions: return self._ordered_sessions + self._ordered_sessions = tabellio.config.utils.get_ordered_sessions() + return self._ordered_sessions + ordered_sessions = property(ordered_sessions) + + def cmp_session_name(self, x, y): + if x is y or x == y: + return 0 + if x not in self.ordered_sessions: + return -1 + if y not in self.ordered_sessions: + return +1 + return cmp(self.ordered_sessions.index(x), self.ordered_sessions.index(y)) + + def cmp_dostype(self, 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 = -self.cmp_session_name(x.session, y.session) + if t == 0: + t = -cmp(x.no, y.no) + return t + + def cmp_questype(self, 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 = -self.cmp_session_name(x.session, y.session) + if t == 0: + t = -cmp(x.dateDoc, y.dateDoc) + return t + + def cmp_number(self, x, y): + t = -cmp(get_no_as_int(x.no), get_no_as_int(y.no)) + if t == 0: + t = -self.cmp_session_name(x.session, y.session) + if t == 0: + t = cmp(x.nodoc, y.nodoc) + return t + + def cmp_session(self, x, y): + t = -self.cmp_session_name(x.session, y.session) + if t == 0: + t = -cmp(get_no_as_int(x.no), get_no_as_int(y.no)) + if t == 0 and hasattr(x, 'nodoc'): + t = cmp(x.nodoc, y.nodoc) + return t + diff --git a/tabellio/searchform/form.py b/tabellio/searchform/form.py index 77dc1b8..4896df4 100644 --- a/tabellio/searchform/form.py +++ b/tabellio/searchform/form.py @@ -39,6 +39,7 @@ from tabellio.config.interfaces import ITabellioSettings import tabellio.config.utils from tabellio.documents.typenames import MAPPING +import cmpfunctions class ListAuthorsView(BrowserView): def get_folder_at_path(self, path): @@ -78,7 +79,6 @@ class ListAuthorsView(BrowserView): for object in self.deputies_folder.objectValues(): if object.portal_type != 'themis.datatypes.deputy': continue - re.split(r'[-\s]', 'hello-pol to') for name_part in re.split(r'[-\s]', normalizer.normalize(object.Title()).lower()): if name_part.startswith(q): s.append(object) @@ -786,99 +786,20 @@ class SearchView(BrowserView): elif data.get('search_type_is_question'): kw['portal_type'] = 'tabellio.documents.question' - 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 - - def get_no_as_int(i): - if i: - try: - return int(i.split('-')[0]) - except ValueError: - return 99999 - return 0 - - ordered_sessions = tabellio.config.utils.get_ordered_sessions() - def cmp_session_name(x, y): - if x is y or x == y: - return 0 - if x not in ordered_sessions: - return -1 - if y not in ordered_sessions: - return +1 - return cmp(ordered_sessions.index(x), ordered_sessions.index(y)) - - 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_session_name(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_session_name(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_session_name(x.session, y.session) - if t == 0: - t = cmp(x.nodoc, y.nodoc) - return t - - def cmp_session(x, y): - t = -cmp_session_name(x.session, y.session) - if t == 0: - t = -cmp(get_no_as_int(x.no), get_no_as_int(y.no)) - if t == 0 and hasattr(x, 'nodoc'): - t = cmp(x.nodoc, y.nodoc) - return t - cmp_function = None if data.get('sort_on') == 'Type': if data.get('search_type_is_document'): - cmp_function = cmp_doctype + cmp_function = cmpfunctions.Cmp().cmp_doctype elif data.get('search_type_is_adopteddocument'): - cmp_function = cmp_doctype + cmp_function = cmpfunctions.Cmp().cmp_doctype elif data.get('search_type_is_dossier'): - cmp_function = cmp_dostype + cmp_function = cmpfunctions.Cmp().cmp_dostype elif data.get('search_type_is_question'): - cmp_function = cmp_questype + cmp_function = cmpfunctions.Cmp().cmp_questype elif data.get('sort_on') == 'Number': - cmp_function = cmp_number + cmp_function = cmpfunctions.Cmp().cmp_number elif data.get('sort_on') == 'Session': - cmp_function = cmp_session + cmp_function = cmpfunctions.Cmp().cmp_session catalog = getToolByName(self.context, 'portal_catalog') print 'kw:', kw diff --git a/tabellio/searchform/simple-pfb.pt b/tabellio/searchform/simple-pfb.pt index 1f11df5..6860d84 100644 --- a/tabellio/searchform/simple-pfb.pt +++ b/tabellio/searchform/simple-pfb.pt @@ -40,9 +40,17 @@ tal:attributes="value python: request.form.get('SearchableText')"/> +