add sort to pfb simple search (#1068)

This commit is contained in:
Frédéric Péters 2011-12-04 12:31:55 +01:00
parent 7fdd9f23c3
commit 76f1d33a82
4 changed files with 159 additions and 92 deletions

View File

@ -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

View File

@ -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

View File

@ -40,9 +40,17 @@
tal:attributes="value python: request.form.get('SearchableText')"/>
<input type="submit" class="submit-widget" value="Recherche"/>
</form>
<input type="hidden" id="SearchableText" tal:condition="python: request.form.get('SearchableText')"
tal:attributes="value python: request.form.get('SearchableText')"/>
<div id="deputies-results" tal:condition="python:len(deputy_results)">
<h2 id="b_deputy_start">Députés</h2>
<h2 id="b_deputy_start">Députés
<span class="sorton"><select id="deputies-sort">
<option disabled="disabled">Trier par</option>
<option value="name">Nom</option>
<option value="polgroup">Groupe politique</option>
</select></span>
</h2>
<div class="resultsinfobox">
<span>Nombre de résultats : <span tal:replace="python:len(deputy_results)"/></span>
</div>
@ -77,7 +85,14 @@
</div>
<div id="docs-results" tal:condition="python:len(doc_results)">
<h2 id="b_doc_start">Documents et dossiers</h2>
<h2 id="b_doc_start">Documents et dossiers
<span class="sorton"><select id="docs-sort">
<option disabled="disabled">Trier par</option>
<option value="session">Session</option>
<option value="type">Type</option>
<option value="number">Numéro</option>
</select></span>
</h2>
<div class="resultsinfobox">
<span>Nombre de résultats : <span tal:replace="python:len(doc_results)"/></span>
</div>
@ -153,6 +168,19 @@
</div>
<script metal:use-macro="view/js_macros/macros/livereload"></script>
<script>
(function($) {
$('.sorton select').change(function() {
var url = window.location.href.split('?')[0];
url = url + '?SearchableText=' + $('#SearchableText').val();
$('.sorton select').each(function() {
url = url + '&' + this.id + '=' + $(this).val();
});
url = url + '#' + $(this).parent().parent()[0].id;
window.location.href = url;
});
})(jQuery);
</script>
</tal:main-macro>
</metal:main>

View File

@ -14,6 +14,7 @@ from Products.Five import BrowserView
from Products.Five.browser.pagetemplatefile import ViewPageTemplateFile
import form
import cmpfunctions
class SimpleSearchView(BrowserView):
batch_macros = ViewPageTemplateFile('batch_macros.pt')
@ -82,10 +83,28 @@ class SimpleSearchView(BrowserView):
catalog = getToolByName(self.context, 'portal_catalog')
if not self.request.form.get('SearchableText'):
return []
return catalog(
sorton = self.request.form.get('deputies-sort')
c = catalog(
portal_type=['themis.datatypes.deputy'],
SearchableText=self.request.form.get('SearchableText'),
sort_on="sortable_title", sort_order='ascending')
if sorton == 'polgroup':
def cmp_by_polgroup(x, y):
xo = x.getObject()
yo = y.getObject()
if (xo.polgroup is None or xo.polgroup.to_object is None) and (
yo.polgroup is None or yo.polgroup.to_object is None):
return 0
if (xo.polgroup is None or xo.polgroup.to_object is None):
return -1
if (yo.polgroup is None or yo.polgroup.to_object is None):
return 1
return cmp(xo.polgroup.to_object.id, yo.polgroup.to_object.id)
c = list(c)
c.sort(cmp_by_polgroup)
return c
else:
return c
def page_results(self):
catalog = getToolByName(self.context, 'portal_catalog')
@ -100,12 +119,20 @@ class SimpleSearchView(BrowserView):
catalog = getToolByName(self.context, 'portal_catalog')
if not self.request.form.get('SearchableText'):
return []
return catalog(
sorton = self.request.form.get('docs-sort')
c = catalog(
portal_type=['tabellio.documents.dossier',
'tabellio.documents.document',
'tabellio.documents.question'],
SearchableText=self.request.form.get('SearchableText'),
sort_on='dateDoc', sort_order='descending')
SearchableText=self.request.form.get('SearchableText'))
sorton = 'session'
if sorton == 'session':
cmpf = cmpfunctions.Cmp().cmp_session
elif sorton == 'type':
cmpf = cmpfunctions.Cmp().cmp_doctype
elif sorton == 'number':
cmpf = cmpfunctions.Cmp().cmp_number
return sorted(c, cmpf);
def event_results(self):
catalog = getToolByName(self.context, 'portal_catalog')