This repository has been archived on 2023-02-21. You can view files and clone it, but cannot push or open issues or pull requests.
tabellio.searchform/tabellio/searchform/form.py

1036 lines
40 KiB
Python
Raw Normal View History

# -*- coding: utf-8 -*-
import re
import time
2011-11-21 13:57:09 +01:00
import datetime
2011-10-21 19:33:36 +02:00
from five import grok
from plone.memoize import instance, ram
from zope import interface, schema, component
2011-09-08 08:56:35 +02:00
from z3c.form import form, field, button
from plone.z3cform.layout import wrap_form
from Products.CMFCore.utils import getToolByName
from zope.interface import implements
2011-09-08 08:56:35 +02:00
from z3c.form.ptcompat import ViewPageTemplateFile
2011-11-05 12:16:21 +01:00
import z3c.form.interfaces
from z3c.form.browser import text
from z3c.form import widget
from z3c.form.interfaces import ITextWidget
from z3c.form.browser.checkbox import CheckBoxWidget
from z3c.form.browser.radio import RadioWidget
2011-09-08 08:56:35 +02:00
from z3c.relationfield.schema import RelationChoice, RelationList
from plone.formwidget.contenttree import ObjPathSourceBinder
2011-11-23 10:51:13 +01:00
import plone.directives.form
2011-09-08 08:56:35 +02:00
from tabellio.searchform.interfaces import MessageFactory as _
import Missing
from Products.Five import BrowserView
from Products.Five.browser.pagetemplatefile import ViewPageTemplateFile
2011-10-21 19:33:36 +02:00
from zope.schema.interfaces import IContextSourceBinder
from zope.schema.vocabulary import SimpleVocabulary, SimpleTerm
from plone.registry.interfaces import IRegistry
from tabellio.config.interfaces import ITabellioSettings
2011-11-06 16:13:56 +01:00
import tabellio.config.utils
2011-11-13 16:40:37 +01:00
from tabellio.documents.typenames import MAPPING
2011-11-06 16:13:56 +01:00
class ListAuthorsView(BrowserView):
def get_folder_at_path(self, path):
current = self.portal
for part in path.split('/'):
if not part:
continue
current = getattr(current, part)
return current
_deputies_folder = None
def deputies_folder(self):
if self._deputies_folder:
return self._deputies_folder
path = self.settings.deputiesPath
self._deputies_folder = self.get_folder_at_path(path)
return self._deputies_folder
deputies_folder = property(deputies_folder)
def __call__(self):
from plone.i18n.normalizer.fr import normalizer
self.portal = getToolByName(self.context, 'portal_url').getPortalObject()
self.settings = component.getUtility(IRegistry).forInterface(ITabellioSettings, False)
self.request.response.setHeader('Content-type', 'text/plain')
2011-11-14 15:55:55 +01:00
id = self.request.form.get('id')
if id:
try:
t = getattr(self.deputies_folder, id)
except AttributeError:
return 'XXX'
return t.Title()
s = []
q = normalizer.normalize(unicode(self.request.form.get('q'), 'utf-8')).lower()
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)
def cmp_deputy(x, y):
return cmp(normalizer.normalize(x.Title()).lower(),
normalizer.normalize(y.Title()).lower())
s.sort(cmp_deputy)
return '\n'.join(['%s|%s' % (x.Title(), x.id) for x in s])
2011-11-11 14:30:06 +01:00
class ListPolgroupsView(BrowserView):
def get_folder_at_path(self, path):
current = self.portal
for part in path.split('/'):
if not part:
continue
current = getattr(current, part)
return current
_polgroups_folder = None
def polgroups_folder(self):
if self._polgroups_folder:
return self._polgroups_folder
path = self.settings.polgroupsPath
self._polgroups_folder = self.get_folder_at_path(path)
return self._polgroups_folder
polgroups_folder = property(polgroups_folder)
def __call__(self):
self.portal = getToolByName(self.context, 'portal_url').getPortalObject()
self.settings = component.getUtility(IRegistry).forInterface(ITabellioSettings, False)
self.request.response.setHeader('Content-type', 'text/plain')
2011-11-14 15:55:55 +01:00
id = self.request.form.get('id')
if id:
try:
t = getattr(self.polgroups_folder, id)
except AttributeError:
return 'XXX'
return t.Title()
2011-11-11 14:30:06 +01:00
s = []
q = unicode(self.request.form.get('q'), 'utf-8').lower()
2011-11-11 14:30:06 +01:00
for object in self.polgroups_folder.objectValues():
if object.portal_type != 'themis.datatypes.polgroup':
continue
if q in object.id or q in object.Title().lower():
s.append(object)
return '\n'.join(['%s|%s' % (x.Title(), x.id) for x in s])
class ListTopicsView(BrowserView):
def __call__(self):
from plone.i18n.normalizer.fr import normalizer
topics = tabellio.config.utils.get_topics_dict()
self.request.response.setHeader('Content-type', 'text/plain')
2011-11-14 15:55:55 +01:00
id = self.request.form.get('id')
if id:
try:
return '%s' % topics.get(id, id)[0]
except KeyError:
return 'XXX'
query_terms = [normalizer.normalize(x).lower() for x in unicode(self.request.form.get('q'), 'utf-8').split()]
print 'query terms:', query_terms
sorted_topics = topics.items()
sorted_topics.sort(lambda x,y: cmp(x[1][1].lower(), y[1][1].lower()))
2011-11-14 15:55:55 +01:00
2011-11-11 14:30:06 +01:00
r = []
for key, value in sorted_topics:
2011-11-11 14:30:06 +01:00
for term in query_terms:
if not term in value[1].lower():
2011-11-11 14:30:06 +01:00
break
else:
r.append('%s|%s' % (value[0], key))
if len(r) > 30:
break
return '\n'.join(r)
2011-11-11 14:30:06 +01:00
class IAuthorsWidget(ITextWidget):
pass
class AuthorsWidget(text.TextWidget):
implements(IAuthorsWidget)
klass = u'authors'
def FieldAuthorsWidget(field, request):
return widget.FieldWidget(field, AuthorsWidget(request))
2011-11-11 14:30:06 +01:00
class IPolgroupsWidget(ITextWidget):
pass
class PolgroupsWidget(text.TextWidget):
implements(IPolgroupsWidget)
klass = u'polgroups'
def FieldPolgroupsWidget(field, request):
return widget.FieldWidget(field, PolgroupsWidget(request))
class ITopicsWidget(ITextWidget):
pass
class TopicsWidget(text.TextWidget):
implements(ITopicsWidget)
klass = u'topics'
def FieldTopicsWidget(field, request):
return widget.FieldWidget(field, TopicsWidget(request))
def FieldDroppedCheckboxWidget(field, request):
return widget.FieldWidget(field, CheckBoxWidget(request))
def FieldRadioboxesWidget(field, request):
return widget.FieldWidget(field, RadioWidget(request))
class IFolderWithDocuments(interface.Interface):
pass
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))
2011-10-21 19:33:36 +02:00
@grok.provider(IContextSourceBinder)
def possible_document_types(context):
catalog = getToolByName(context, 'portal_catalog')
possible_doctypes = catalog.uniqueValuesFor('doctype')
terms = []
for doctype in possible_doctypes:
2011-11-05 13:37:14 +01:00
if not doctype:
continue
if doctype == u'Réponse à question écrite':
continue
2011-10-21 19:33:36 +02:00
doctype_id = doctype.encode('ascii', 'replace')
2011-11-13 16:40:37 +01:00
doctype_str = MAPPING.get(doctype, doctype)
terms.append(SimpleVocabulary.createTerm(doctype_id, doctype_id, doctype_str))
terms.sort(cmp_term)
2011-10-21 19:33:36 +02:00
return SimpleVocabulary(terms)
def get_docdos_type_id(context, type_id):
if type_id in MAPPING.keys():
return type_id
catalog = getToolByName(context, 'portal_catalog')
possible_doctypes = catalog.uniqueValuesFor('doctype')
possible_dostypes = catalog.uniqueValuesFor('dostype')
for iter_type_id in (possible_dostypes + possible_doctypes):
if iter_type_id is None:
continue
rep_id = iter_type_id.encode('ascii', 'replace')
if type_id in (iter_type_id, rep_id):
return iter_type_id
return None
2011-11-05 12:16:21 +01:00
@grok.provider(IContextSourceBinder)
def possible_dossier_types(context):
catalog = getToolByName(context, 'portal_catalog')
possible_dostypes = catalog.uniqueValuesFor('dostype')
terms = []
for dostype in possible_dostypes:
2011-11-05 13:37:14 +01:00
if not dostype:
continue
2011-11-05 12:16:21 +01:00
dostype_id = dostype.encode('ascii', 'replace')
2011-11-13 16:40:37 +01:00
dostype_str = MAPPING.get(dostype, dostype)
terms.append(SimpleVocabulary.createTerm(dostype_id, dostype_id, dostype_str))
terms.sort(cmp_term)
2011-11-05 12:16:21 +01:00
return SimpleVocabulary(terms)
2011-11-05 18:55:40 +01:00
@grok.provider(IContextSourceBinder)
def possible_question_types(context):
catalog = getToolByName(context, 'portal_catalog')
possible_questypes = catalog.uniqueValuesFor('questype')
terms = []
for questype in possible_questypes:
if not questype:
continue
questype_id = questype.encode('ascii', 'replace')
2011-11-13 16:40:37 +01:00
questype_str = MAPPING.get(questype, questype)
terms.append(SimpleVocabulary.createTerm(questype_id, questype_id, questype_str))
terms.sort(cmp_term)
2011-11-05 18:55:40 +01:00
return SimpleVocabulary(terms)
2011-11-05 12:16:21 +01:00
2011-11-06 16:13:56 +01:00
@grok.provider(IContextSourceBinder)
def possible_sessions(context):
terms = []
for term in tabellio.config.utils.get_legisl_and_sessions():
term_id = term.encode('ascii', 'replace')
terms.append(SimpleVocabulary.createTerm(term_id, term_id, term))
return SimpleVocabulary(terms)
@grok.provider(IContextSourceBinder)
def possible_polgroups(context):
terms = []
current = getToolByName(context, 'portal_url').getPortalObject()
settings = component.getUtility(IRegistry).forInterface(ITabellioSettings, False)
for part in settings.polgroupsPath.split('/'):
if not part:
continue
current = getattr(current, part)
for object in current.objectValues():
if object.portal_type != 'themis.datatypes.polgroup':
continue
polgroup_id = object.id
polgroup_str = object.Title()
terms.append(SimpleVocabulary.createTerm(polgroup_id, polgroup_id, polgroup_str))
return SimpleVocabulary(terms)
2011-11-23 10:51:13 +01:00
@grok.provider(IContextSourceBinder)
def possible_active_polgroups(context):
terms = []
current = getToolByName(context, 'portal_url').getPortalObject()
settings = component.getUtility(IRegistry).forInterface(ITabellioSettings, False)
for part in settings.polgroupsPath.split('/'):
if not part:
continue
current = getattr(current, part)
for object in current.objectValues():
if object.portal_type != 'themis.datatypes.polgroup':
continue
if not object.active:
continue
polgroup_id = object.id
polgroup_str = object.Title()
terms.append(SimpleVocabulary.createTerm(polgroup_id, polgroup_id, polgroup_str))
return SimpleVocabulary(terms)
@grok.provider(IContextSourceBinder)
def possible_topics(context):
catalog = getToolByName(context, 'portal_catalog')
topics = tabellio.config.utils.get_topics_dict()
terms = []
2011-11-20 21:07:28 +01:00
for key, value in sorted(topics.items()):
topic_id = key
topic_str = value[0]
terms.append(SimpleVocabulary.createTerm(topic_id, topic_id, topic_str))
return SimpleVocabulary(terms)
2011-11-06 16:13:56 +01:00
2011-10-03 14:21:24 +02:00
class IDocumentSearch(interface.Interface):
2011-11-05 18:55:40 +01:00
search_type_is_document = schema.TextLine(title=u'Search Type', default=u'1', required=False)
2011-09-08 08:56:35 +02:00
nodoc = schema.TextLine(title=_(u'Document Number'), required=False)
nosuite = schema.TextLine(title=_(u'Suite Number'), required=False)
doctype = schema.Choice(title=_(u'Document Type'), required=False,
2011-11-05 12:16:21 +01:00
source=possible_document_types)
2011-09-08 08:56:35 +02:00
ttitle = schema.TextLine(title=_(u'Title'), required=False)
text = schema.TextLine(title=_(u'Text'), required=False)
authors = schema.TextLine(title=_(u'Authors'), required=False)
2011-11-11 14:30:06 +01:00
polgroups = schema.TextLine(title=_(u'Political Groups'), required=False)
topics = schema.TextLine(title=_(u'Topics'), required=False)
2011-11-06 16:13:56 +01:00
session = schema.Choice(title=_(u'Legislature / Session'), required=False,
source=possible_sessions)
2011-09-08 08:56:35 +02:00
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')])
2011-09-08 08:56:35 +02:00
2011-10-03 14:21:24 +02:00
class DocumentSearchForm(form.Form):
2011-11-05 12:16:21 +01:00
method = 'get'
prefix = 'document'
2011-10-03 14:21:24 +02:00
fields = field.Fields(IDocumentSearch)
fields['authors'].widgetFactory = FieldAuthorsWidget
2011-11-11 14:30:06 +01:00
fields['polgroups'].widgetFactory = FieldPolgroupsWidget
fields['topics'].widgetFactory = FieldTopicsWidget
2011-09-08 08:56:35 +02:00
ignoreContext = True
2011-11-23 10:51:13 +01:00
template = ViewPageTemplateFile('form_templates/view_form.pt')
2011-09-08 08:56:35 +02:00
2011-11-05 12:16:21 +01:00
def updateWidgets(self):
super(DocumentSearchForm, self).updateWidgets()
2011-11-05 18:55:40 +01:00
self.widgets['search_type_is_document'].mode = z3c.form.interfaces.HIDDEN_MODE
2011-11-05 12:16:21 +01:00
2011-10-21 20:40:31 +02:00
@button.buttonAndHandler(_(u'Search'))
2011-09-08 08:56:35 +02:00
def handleApply(self, action):
pass
2011-09-08 08:56:35 +02:00
2011-10-03 14:21:24 +02:00
class IDossierSearch(interface.Interface):
2011-11-05 18:55:40 +01:00
search_type_is_dossier = schema.TextLine(title=u'Search Type', default=u'1', required=False)
2011-10-03 14:21:24 +02:00
nodos = schema.TextLine(title=_(u'Dossier Number'), required=False)
dostype = schema.Choice(title=_(u'Dossier Type'), required=False,
2011-11-05 12:16:21 +01:00
source=possible_dossier_types)
2011-10-03 14:21:24 +02:00
ttitle = schema.TextLine(title=_(u'Title'), required=False)
authors = schema.TextLine(title=_(u'Authors'), required=False)
2011-11-11 14:30:06 +01:00
polgroups = schema.TextLine(title=_(u'Political Groups'), required=False)
participants = schema.TextLine(title=_(u'Participants'), required=False)
2011-11-11 14:30:06 +01:00
topics = schema.TextLine(title=_(u'Topics'), required=False)
2011-11-06 16:13:56 +01:00
session = schema.Choice(title=_(u'Legislature / Session'), required=False,
source=possible_sessions)
adopted = schema.Bool(title=_(u'Limit to adopted'), required=False, default=False)
decreted = schema.Bool(title=_(u'Limit to decreted'), required=False, default=False)
2011-10-03 14:21:24 +02:00
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')])
2011-10-03 14:21:24 +02:00
class DossierSearchForm(form.Form):
prefix = 'dossier'
2011-11-05 12:16:21 +01:00
method = 'get'
2011-10-03 14:21:24 +02:00
fields = field.Fields(IDossierSearch)
fields['authors'].widgetFactory = FieldAuthorsWidget
fields['participants'].widgetFactory = FieldAuthorsWidget
2011-11-11 14:30:06 +01:00
fields['polgroups'].widgetFactory = FieldPolgroupsWidget
fields['topics'].widgetFactory = FieldTopicsWidget
2011-10-03 14:21:24 +02:00
ignoreContext = True
2011-11-23 10:51:13 +01:00
template = ViewPageTemplateFile('form_templates/view_form.pt')
2011-10-03 14:21:24 +02:00
2011-11-05 18:55:40 +01:00
def updateWidgets(self):
super(DossierSearchForm, self).updateWidgets()
self.widgets['search_type_is_dossier'].mode = z3c.form.interfaces.HIDDEN_MODE
2011-10-21 20:40:31 +02:00
@button.buttonAndHandler(_(u'Search'))
2011-10-03 14:21:24 +02:00
def handleApply(self, action):
pass
2011-10-03 14:21:24 +02:00
class IQuestionSearch(interface.Interface):
2011-11-05 18:55:40 +01:00
search_type_is_question = schema.TextLine(title=u'Search Type', default=u'1', required=False)
questype = schema.Choice(title=_(u'Question Type'), required=False,
2011-11-05 18:55:40 +01:00
source=possible_question_types)
2011-10-03 14:21:24 +02:00
ttitle = schema.TextLine(title=_(u'Title'), required=False)
authors = schema.TextLine(title=_(u'Authors'), required=False)
2011-11-11 14:30:06 +01:00
polgroups = schema.TextLine(title=_(u'Political Groups'), required=False)
topics = schema.TextLine(title=_(u'Topics'), required=False)
2011-11-06 16:13:56 +01:00
session = schema.Choice(title=_(u'Legislature / Session'), required=False,
source=possible_sessions)
2011-10-03 14:21:24 +02:00
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')])
2011-10-03 14:21:24 +02:00
class QuestionSearchForm(form.Form):
prefix = 'question'
2011-10-03 14:21:24 +02:00
fields = field.Fields(IQuestionSearch)
fields['authors'].widgetFactory = FieldAuthorsWidget
2011-11-11 14:30:06 +01:00
fields['polgroups'].widgetFactory = FieldPolgroupsWidget
fields['topics'].widgetFactory = FieldTopicsWidget
2011-10-03 14:21:24 +02:00
ignoreContext = True
2011-11-23 10:51:13 +01:00
template = ViewPageTemplateFile('form_templates/view_form.pt')
2011-10-03 14:21:24 +02:00
2011-11-05 18:55:40 +01:00
def updateWidgets(self):
super(QuestionSearchForm, self).updateWidgets()
self.widgets['search_type_is_question'].mode = z3c.form.interfaces.HIDDEN_MODE
2011-10-21 20:40:31 +02:00
@button.buttonAndHandler(_(u'Search'))
2011-10-03 14:21:24 +02:00
def handleApply(self, action):
pass
2011-10-03 14:21:24 +02:00
class QuestionPfbSearchForm(QuestionSearchForm):
fields = field.Fields(IQuestionSearch)
2011-11-30 00:04:37 +01:00
fields['authors'].title = _(u'Author(s)')
fields['sort_on'].widgetFactory = FieldRadioboxesWidget
2011-11-30 00:04:37 +01:00
@button.buttonAndHandler(_(u'Submit'))
def handleApply(self, action):
pass
class IDocumentPfbSearch(interface.Interface):
2011-11-05 18:55:40 +01:00
search_type_is_document = schema.TextLine(title=u'Search Type', default=u'1')
2011-11-30 00:04:37 +01:00
l_doctypes = schema.List(title=_(u'Type'), required=False,
value_type=schema.Choice(title=_(u'Type'), required=False,
source=possible_document_types))
2011-11-30 00:04:37 +01:00
authors = schema.TextLine(title=_(u'Author(s)'), required=False)
2011-11-29 23:25:48 +01:00
#l_polgroups = schema.List(title=_(u'Political Groups'), required=False,
# value_type=schema.Choice(title=_(u'Political Group'),
# required=False, source=possible_polgroups))
session = schema.Choice(title=_(u'Legislature / Session'), required=False,
source=possible_sessions)
ttitle = schema.TextLine(title=_(u'Title'), required=False,
description=_(u'Description for the title field'))
l_topics = schema.List(title=_(u'Topics'), required=False,
value_type=schema.Choice(required=False,
source=possible_topics));
2011-11-29 23:25:48 +01:00
text = schema.TextLine(title=_(u'Text'), required=False)
nodoc = schema.TextLine(title=_(u'Document Number'), required=False)
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 DocumentPfbSearchForm(form.Form):
2011-11-05 12:16:21 +01:00
method = 'get'
2011-11-11 14:30:06 +01:00
prefix = 'document'
fields = field.Fields(IDocumentPfbSearch)
fields['authors'].widgetFactory = FieldAuthorsWidget
fields['l_doctypes'].widgetFactory = FieldDroppedCheckboxWidget
2011-11-29 23:38:24 +01:00
#fields['l_polgroups'].widgetFactory = FieldDroppedCheckboxWidget
fields['l_topics'].widgetFactory = FieldDroppedCheckboxWidget
fields['sort_on'].widgetFactory = FieldRadioboxesWidget
ignoreContext = True
2011-11-23 10:51:13 +01:00
template = ViewPageTemplateFile('form_templates/view_form.pt')
2011-11-05 18:55:40 +01:00
def updateWidgets(self):
super(DocumentPfbSearchForm, self).updateWidgets()
self.widgets['search_type_is_document'].mode = z3c.form.interfaces.HIDDEN_MODE
2011-11-30 00:04:37 +01:00
@button.buttonAndHandler(_(u'Submit'))
def handleApply(self, action):
pass
class IAdoptedDocumentPfbSearch(interface.Interface):
search_type_is_adopteddocument = schema.TextLine(title=u'Search Type', default=u'1')
2011-11-30 00:04:37 +01:00
l_doctypes = schema.List(title=_(u'Type'), required=False,
value_type=schema.Choice(title=_(u'Type'), required=False,
values=[_(u'Projets'), _(u'Propositions')]))
2011-11-30 00:04:37 +01:00
authors = schema.TextLine(title=_(u'Author(s)'), required=False)
2011-11-29 23:25:48 +01:00
#l_polgroups = schema.List(title=_(u'Political Groups'), required=False,
# value_type=schema.Choice(title=_(u'Political Group'),
# required=False, source=possible_polgroups))
session = schema.Choice(title=_(u'Legislature / Session'), required=False,
source=possible_sessions)
ttitle = schema.TextLine(title=_(u'Title'), required=False)
l_topics = schema.List(title=_(u'Topics'), required=False,
value_type=schema.Choice(required=False,
source=possible_topics));
2011-11-29 23:25:48 +01:00
text = schema.TextLine(title=_(u'Text'), required=False)
nodoc = schema.TextLine(title=_(u'Document Number'), required=False)
start = schema.Date(title=_(u'Start'), required=False)
end = schema.Date(title=_(u'End'), required=False)
adopted = schema.Bool(title=_(u'Limit to adopted'), required=False, default=True)
sort_on = schema.Choice(title=_(u'Sort By'), required=True,
default=_(u'Session'),
values=[_(u'Session'), _(u'Type'), _(u'Number')])
class AdoptedDocumentPfbSearchForm(form.Form):
prefix = 'adopteddocument'
fields = field.Fields(IAdoptedDocumentPfbSearch)
fields['authors'].widgetFactory = FieldAuthorsWidget
fields['l_doctypes'].widgetFactory = FieldDroppedCheckboxWidget
2011-11-29 23:38:24 +01:00
#fields['l_polgroups'].widgetFactory = FieldDroppedCheckboxWidget
fields['l_topics'].widgetFactory = FieldDroppedCheckboxWidget
fields['sort_on'].widgetFactory = FieldRadioboxesWidget
ignoreContext = True
template = ViewPageTemplateFile('form_templates/view_form.pt')
def updateWidgets(self):
super(AdoptedDocumentPfbSearchForm, self).updateWidgets()
self.widgets['adopted'].mode = z3c.form.interfaces.HIDDEN_MODE
self.widgets['search_type_is_adopteddocument'].mode = z3c.form.interfaces.HIDDEN_MODE
2011-11-30 00:04:37 +01:00
@button.buttonAndHandler(_(u'Submit'))
def handleApply(self, action):
pass
class IDeputySearch(interface.Interface):
search_type_is_deputy = schema.TextLine(title=u'Search Type', default=u'1', required=False)
name = schema.TextLine(title=_(u'Name'), required=False)
polgroup = schema.Choice(title=_(u'Political Group'),
2011-11-23 10:51:13 +01:00
required=False, source=possible_active_polgroups)
class DeputySearchForm(form.Form):
prefix = 'deputy'
fields = field.Fields(IDeputySearch)
ignoreContext = True
2011-11-23 10:51:13 +01:00
template = ViewPageTemplateFile('form_templates/view_form.pt')
def updateWidgets(self):
super(DeputySearchForm, self).updateWidgets()
self.widgets['search_type_is_deputy'].mode = z3c.form.interfaces.HIDDEN_MODE
2011-11-30 18:49:54 +01:00
@button.buttonAndHandler(_(u'Submit'))
def handleApply(self, action):
pass
2011-11-20 20:18:21 +01:00
class IEventSearch(interface.Interface):
search_type_is_event = schema.TextLine(title=u'Search Type', default=u'1', required=False)
start = schema.Date(title=_(u'Start'), required=False)
end = schema.Date(title=_(u'End'), required=False)
class EventSearchForm(form.Form):
prefix = 'event'
fields = field.Fields(IEventSearch)
ignoreContext = True
template = ViewPageTemplateFile('form_templates/view_form.pt')
def updateWidgets(self):
super(EventSearchForm, self).updateWidgets()
self.widgets['search_type_is_event'].mode = z3c.form.interfaces.HIDDEN_MODE
2011-11-30 18:49:54 +01:00
@button.buttonAndHandler(_(u'Submit'))
2011-11-20 20:18:21 +01:00
def handleApply(self, action):
pass
2011-11-05 12:16:21 +01:00
class IGlobalSearchForm(interface.Interface):
2011-11-05 18:55:40 +01:00
search_type_is_document = schema.TextLine(title=u'Search Type', required=False)
search_type_is_adopteddocument = schema.TextLine(title=u'Search Type', required=False)
2011-11-05 18:55:40 +01:00
search_type_is_dossier = schema.TextLine(title=u'Search Type', required=False)
search_type_is_question = schema.TextLine(title=u'Search Type', required=False)
2011-11-05 12:16:21 +01:00
nodoc = schema.TextLine(title=_(u'Document Number'), required=False)
nosuite = schema.TextLine(title=_(u'Suite Number'), required=False)
doctype = schema.Choice(title=_(u'Type'), required=False,
source=possible_document_types)
nodos = schema.TextLine(title=_(u'Dossier Number'), required=False)
dostype = schema.Choice(title=_(u'Type'), required=False,
source=possible_dossier_types)
2011-11-05 18:55:40 +01:00
questype = schema.Choice(title=_(u'Type'), required=False,
source=possible_question_types)
2011-11-05 12:16:21 +01:00
ttitle = schema.TextLine(title=_(u'Title'), required=False)
text = schema.TextLine(title=_(u'Text'), required=False)
authors = schema.TextLine(title=_(u'Authors'), required=False)
participants = schema.TextLine(title=_(u'Participants'), required=False)
2011-11-20 16:09:57 +01:00
polgroups = schema.TextLine(title=_(u'Political Groups'), required=False)
2011-11-11 14:30:06 +01:00
topics = schema.TextLine(title=_(u'Topics'), required=False)
2011-11-06 16:13:56 +01:00
session = schema.Choice(title=_(u'Legislature / Session'), required=False,
source=possible_sessions)
2011-11-05 12:16:21 +01:00
start = schema.Date(title=_(u'Start'), required=False)
end = schema.Date(title=_(u'End'), required=False)
adopted = schema.Bool(title=_(u'Limit to adopted'), required=False, default=False)
decreted = schema.Bool(title=_(u'Limit to decreted'), required=False, default=False)
2011-11-05 12:16:21 +01:00
l_doctypes = schema.List(title=_(u'Types'), required=False,
value_type=schema.Choice(title=_(u'Type'), required=False,
source=possible_document_types))
l_polgroups = schema.List(title=_(u'Political Groups'), required=False,
value_type=schema.Choice(title=_(u'Political Group'),
required=False, source=possible_polgroups))
l_topics = schema.List(title=_(u'Topics'), required=False,
value_type=schema.Choice(required=False,
source=possible_topics));
2011-11-21 18:28:28 +01:00
sort_on = schema.Choice(title=_(u'Sort By'), required=False,
default=_(u'Type'),
values=[_(u'Type'), _(u'Number'), _(u'Session')])
2011-11-05 12:16:21 +01:00
class GlobalSearchForm(form.Form):
fields = field.Fields(IGlobalSearchForm)
ignoreContext = True
class SearchView(BrowserView):
batch_macros = ViewPageTemplateFile('batch_macros.pt')
2011-11-23 10:51:13 +01:00
js_macros = ViewPageTemplateFile('js_macros.pt')
def portal_url(self):
return getToolByName(self.context, 'portal_url').getPortalPath()
def db_connection(self):
portal = getToolByName(self.context, 'portal_url').getPortalObject()
return portal.db._wrapper.connection
db_connection = property(db_connection)
def _get_ids_cache_key(method, self, text):
return (text, )
@ram.cache(_get_ids_cache_key)
def get_ids_from_postgres(self, text):
try:
cursor = self.db_connection.cursor()
except AttributeError:
return []
# looks like there's no way to quote things properly for to_tsquery,
from plone.i18n.normalizer.fr import normalizer
text = re.sub(r'[^\w\s]', ' ', normalizer.normalize(text))
cursor.execute("""SELECT t_document.id
FROM t_document JOIN t_file
ON (t_document.text1id = t_file.fileid OR
t_document.textdefid = t_file.fileid)
WHERE object_fts @@ to_tsquery('default_french', '''%s''')""" % text)
ids = [x[0] for x in cursor.fetchall()]
cursor.close()
return ids
def get_batchlinkparams(self):
d = dict()
for key in self.request.form:
d[key] = self.request.form[key]
if type(d[key]) is str:
d[key] = unicode(d[key], 'utf-8').encode('utf-8')
elif type(d[key]) is unicode:
d[key] = d[key].encode('utf-8')
return d
2011-11-05 18:55:40 +01:00
def search_results(self, search_type):
print 'search type:', search_type
if self.request.form.get('document.widgets.search_type_is_document'):
GlobalSearchForm.prefix = 'document'
elif self.request.form.get('dossier.widgets.search_type_is_dossier'):
GlobalSearchForm.prefix = 'dossier'
elif self.request.form.get('question.widgets.search_type_is_question'):
GlobalSearchForm.prefix = 'question'
elif self.request.form.get('adopteddocument.widgets.search_type_is_adopteddocument'):
GlobalSearchForm.prefix = 'adopteddocument'
2011-11-05 12:16:21 +01:00
f = GlobalSearchForm(self.context, self.request)
f.update()
data, errors = f.extractData()
kw = {}
2011-10-21 19:33:36 +02:00
2011-11-05 18:55:40 +01:00
if not data.get('search_type_is_%s' % search_type):
return None
print 'data:', data
2011-11-05 18:55:40 +01:00
if data.get('ttitle'):
kw['Title'] = data.get('ttitle')
2011-10-21 19:33:36 +02:00
2011-11-05 12:16:21 +01:00
if data.get('nodoc'):
kw['no'] = data.get('nodoc')
if data.get('nosuite'):
kw['nodoc'] = data.get('nosuite')
2011-11-05 12:16:21 +01:00
if data.get('nodos'):
2011-11-17 18:35:39 +01:00
kw['no'] = data.get('nodos')
2011-11-05 12:16:21 +01:00
2011-10-21 19:33:36 +02:00
if data.get('doctype'):
kw['doctype'] = get_docdos_type_id(self.context, data.get('doctype'))
2011-11-05 12:16:21 +01:00
if data.get('dostype'):
kw['dostype'] = get_docdos_type_id(self.context, data.get('dostype'))
if data.get('l_doctypes'):
kw['doctype'] = [get_docdos_type_id(self.context, x) for x in data.get('l_doctypes')]
if data.get('start') and data.get('end'):
kw['dateDoc'] = {'query': [data.get('start'), data.get('end')], 'range': 'minmax'}
elif data.get('start'):
kw['dateDoc'] = {'query': data.get('start'), 'range': 'min'}
elif data.get('end'):
kw['dateDoc'] = {'query': data.get('end'), 'range': 'max'}
2011-11-06 16:13:56 +01:00
if data.get('session'):
kw['session'] = tabellio.config.utils.get_list_of_sessions(data.get('session'))
if data.get('authors'):
kw['authorsDoc'] = {'query': data.get('authors').strip().split(),
'operator': 'and'}
if data.get('participants'):
kw['interveningPersonsDoc'] = {
'query': data.get('participants').strip().split(),
'operator': 'and'}
if data.get('polgroups'):
2011-11-11 14:30:06 +01:00
kw['polgroupsDoc'] = {'query': data.get('polgroups').strip().split(),
'operator': 'and'}
if data.get('l_polgroups'):
kw['polgroupsDoc'] = {'query': [possible_polgroups(self.context).getTermByToken(x).title for x in data.get('l_polgroups')],
'operator': 'and'}
2011-11-11 14:30:06 +01:00
if data.get('topics'):
kw['topics'] = {'query': data.get('topics').strip().split(),
'operator': 'and'}
if data.get('l_topics'):
kw['topics'] = {'query': [possible_topics(self.context).getTermByToken(x).title for x in data.get('l_topics')],
'operator': 'and'}
if data.get('adopted'):
kw['adopted'] = data.get('adopted')
if data.get('decreted'):
kw['decreted'] = data.get('decreted')
if not kw and not data.get('text'):
return []
if data.get('text'):
# plaintext search, get document ids from postgresql
kw['id'] = self.get_ids_from_postgres(data.get('text'))
if data.get('search_type_is_document'):
kw['portal_type'] = 'tabellio.documents.document'
elif data.get('search_type_is_adopteddocument'):
kw['portal_type'] = 'tabellio.documents.document'
elif data.get('search_type_is_dossier'):
kw['portal_type'] = 'tabellio.documents.dossier'
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
elif data.get('search_type_is_adopteddocument'):
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
2011-11-05 18:55:40 +01:00
2011-11-05 12:16:21 +01:00
catalog = getToolByName(self.context, 'portal_catalog')
print 'kw:', kw
return sorted(catalog(**kw), cmp_function)
def deputy_search_form(self):
f = DeputySearchForm(self.context, self.request)
f.update()
return f.render()
def document_pfb_search_form(self):
f = DocumentPfbSearchForm(self.context, self.request)
f.update()
return f.render()
def question_pfb_search_form(self):
f = QuestionPfbSearchForm(self.context, self.request)
f.update()
return f.render()
2011-10-03 14:21:24 +02:00
def document_search_form(self):
f = DocumentSearchForm(self.context, self.request)
f.update()
return f.render()
2011-11-05 18:55:40 +01:00
def document_search_results(self):
return self.search_results(search_type='document')
def adopted_document_pfb_search_form(self):
f = AdoptedDocumentPfbSearchForm(self.context, self.request)
f.update()
return f.render()
def adopted_document_search_results(self):
return self.search_results(search_type='adopteddocument')
2011-10-03 14:21:24 +02:00
def dossier_search_form(self):
f = DossierSearchForm(self.context, self.request)
f.update()
return f.render()
2011-11-05 18:55:40 +01:00
def dossier_search_results(self):
return self.search_results(search_type='dossier')
2011-10-03 14:21:24 +02:00
def question_search_form(self):
f = QuestionSearchForm(self.context, self.request)
f.update()
return f.render()
2011-09-08 08:56:35 +02:00
2011-11-05 18:55:40 +01:00
def question_search_results(self):
return self.search_results(search_type='question')
2011-11-20 20:18:21 +01:00
def event_search_form(self):
f = EventSearchForm(self.context, self.request)
f.update()
return f.render()
def event_search_results(self):
f = EventSearchForm(self.context, self.request)
f.update()
data, errors = f.extractData()
kw = {}
if not data.get('search_type_is_event'):
return None
if data.get('start') and data.get('end'):
kw['start'] = {'query': [data.get('start'), data.get('end')], 'range': 'minmax'}
elif data.get('start'):
kw['start'] = {'query': data.get('start'), 'range': 'min'}
elif data.get('end'):
kw['start'] = {'query': data.get('end'), 'range': 'max'}
kw['sort_on'] = 'start'
kw['sort_order'] = 'descending'
kw['portal_type'] = ('tabellio.agenda.event',
'tabellio.agenda.comevent',
'tabellio.agenda.parlevent')
catalog = getToolByName(self.context, 'portal_catalog')
return catalog(**kw)
2011-11-20 20:37:31 +01:00
def docform_url(self):
catalog = getToolByName(self.context, 'portal_catalog')
try:
return catalog(
portal_type='Folder',
2011-11-20 20:37:31 +01:00
object_provides=('tabellio.searchform.form.IFolderWithPfbDocuments',
'tabellio.searchform.form.IFolderWithDocuments'),
limit=1)[0].getObject().absolute_url()
except IndexError:
return '#'
def deputiesform_url(self):
catalog = getToolByName(self.context, 'portal_catalog')
try:
return catalog(
portal_type='Folder',
object_provides=('tabellio.webviews.deputy.IDeputiesAndOthersFolderView',
'tabellio.webviews.deputy.IPfbDeputiesFolderView',
'tabellio.webviews.deputy.IFolderView'),
limit=1)[0].getObject().absolute_url()
except IndexError:
return '#'
2011-11-20 20:37:31 +01:00
def eventform_url(self):
return self.portal_url() + '/eventsearch'
2011-11-21 13:57:09 +01:00
def decrets_years(self):
now = time.localtime()
return range(1973, now[0]+1)
def decrets_year(self):
now = time.localtime()
year = int(self.request.form.get('decretyear') or now[0])
2011-11-21 13:57:09 +01:00
return year
def decrets(self):
now = time.localtime()
year = int(self.request.form.get('decretyear') or now[0])
2011-11-21 13:57:09 +01:00
start = datetime.datetime(year, 1, 1)
end = datetime.datetime(year+1, 1, 1)
catalog = getToolByName(self.context, 'portal_catalog')
rs = catalog(portal_type='tabellio.documents.document',
doctype='DECCCF',
dateDoc={'query': (start, end), 'range': 'min:max'},
sort_on='dateDoc')
return rs
class Reflex(BrowserView):
def __call__(self):
kw = {}
if self.request.form.get('no'):
kw['no'] = self.request.form.get('no')
if self.request.form.get('sess'):
kw['session'] = self.request.form.get('sess')
if self.request.form.get('nodoc'):
kw['nodoc'] = self.request.form.get('nodoc')
if self.request.form.get('type') == 'document':
kw['portal_type'] = 'tabellio.documents.document'
elif self.request.form.get('type') == 'dossier':
kw['portal_type'] = 'tabellio.documents.dossier'
catalog = getToolByName(self.context, 'portal_catalog')
rs = catalog(**kw)
if not rs:
return self.request.response.redirect('.')
return self.request.response.redirect(rs[0].getURL())