From 853befcdb24ac0ca8f24e377cb961c04cb445d52 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20P=C3=A9ters?= Date: Thu, 18 Aug 2011 19:17:17 +0200 Subject: [PATCH] Add a custom widget for address --- themis/datatypes/address.py | 77 +++++++++++++++++++++++++++++ themis/datatypes/address_display.pt | 13 +++++ themis/datatypes/address_input.pt | 61 +++++++++++++++++++++++ themis/datatypes/configure.zcml | 17 ++++++- themis/datatypes/deputy.py | 27 +--------- 5 files changed, 168 insertions(+), 27 deletions(-) create mode 100644 themis/datatypes/address.py create mode 100644 themis/datatypes/address_display.pt create mode 100644 themis/datatypes/address_input.pt diff --git a/themis/datatypes/address.py b/themis/datatypes/address.py new file mode 100644 index 0000000..db04999 --- /dev/null +++ b/themis/datatypes/address.py @@ -0,0 +1,77 @@ +from zope.interface import Interface +from zope import schema +from zope.schema import Object, Field +from zope.schema.interfaces import IObject, IField, IFromUnicode +from zope.interface import implements, implementer +from zope.component import adapts, adapter, provideAdapter + +from z3c.form.interfaces import IFormLayer, IFieldWidget, NOVALUE +from z3c.form.widget import Widget, FieldWidget +from z3c.form.interfaces import IWidget +from z3c.form.converter import BaseDataConverter + +from themis.datatypes.interfaces import MessageFactory as _ + +class IAddress(IField): + title = schema.TextLine(title=_(u'Title'), required=False) + street = schema.TextLine(title=_(u'Street'), required=False) + zipcode = schema.TextLine(title=_(u'Zip'), required=False) + city = schema.TextLine(title=_(u'City'), required=False) + phone1 = schema.TextLine(title=_(u'Phone'), required=False) + phone2 = schema.TextLine(title=_(u'Phone 2'), required=False) + fax = schema.TextLine(title=_(u'Fax'), required=False) + email = schema.TextLine(title=_(u'Email'), required=False) + +class Address(Field): + implements(IAddress, IFromUnicode) + + title = None + street = None + zipcode = None + city = None + phone1 = None + phone2 = None + fax = None + email = None + +class IAddressWidget(IWidget): + pass + +class AddressWidget(Widget): + implements(IAddressWidget) + + def update(self): + super(AddressWidget, self).update() + + def extract(self, default=NOVALUE): + if not (self.name + '.street') in self.request.form: + return NOVALUE + address = Address() + has_value = None + for attr in ('title', 'street', 'zipcode', 'city', 'phone1', 'phone2', + 'fax', 'email'): + setattr(address, attr, self.request.get(self.name + '.' + attr)) + has_value = has_value or getattr(address, attr) + if not has_value: + return NOVALUE + return address + + +@adapter(IAddress, IFormLayer) +@implementer(IFieldWidget) +def AddressFieldWidget(field, request): + """IFieldWidget factory for Address.""" + return FieldWidget(field, AddressWidget(request)) + + +class AddressConverter(BaseDataConverter): + adapts(IAddress, IAddressWidget) + + def toWidgetValue(self, value): + if value is None: + return Address() + return value + + def toFieldValue(self, value): + return value + diff --git a/themis/datatypes/address_display.pt b/themis/datatypes/address_display.pt new file mode 100644 index 0000000..192e659 --- /dev/null +++ b/themis/datatypes/address_display.pt @@ -0,0 +1,13 @@ +
+ + + TODO + +
diff --git a/themis/datatypes/address_input.pt b/themis/datatypes/address_input.pt new file mode 100644 index 0000000..e293099 --- /dev/null +++ b/themis/datatypes/address_input.pt @@ -0,0 +1,61 @@ +
+ + + +
+
+ + +
+
+ + +
+ +
+ + + + + +
+ +
+ + + + +
+ + + + +
+
+
+
diff --git a/themis/datatypes/configure.zcml b/themis/datatypes/configure.zcml index dafbda2..17a2a78 100644 --- a/themis/datatypes/configure.zcml +++ b/themis/datatypes/configure.zcml @@ -2,6 +2,8 @@ xmlns="http://namespaces.zope.org/zope" xmlns:grok="http://namespaces.zope.org/grok" xmlns:genericsetup="http://namespaces.zope.org/genericsetup" + xmlns:z3c="http://namespaces.zope.org/z3c" + xmlns:browser="http://namespaces.zope.org/browser" i18n_domain="themis.datatypes"> @@ -27,6 +29,19 @@ provides="themis.datatypes.deputy.INameFromPersonNames" factory="themis.datatypes.deputy.NameFromPersonNames" /> - + + + + + + diff --git a/themis/datatypes/deputy.py b/themis/datatypes/deputy.py index c8acaa6..c9d37ee 100644 --- a/themis/datatypes/deputy.py +++ b/themis/datatypes/deputy.py @@ -21,36 +21,11 @@ from z3c.form.browser.text import TextWidget from z3c.form.interfaces import IFormLayer, IFieldWidget from z3c.form.widget import FieldWidget - from themis.datatypes.interfaces import MessageFactory as _ from themis.datatypes.polgroup import IPolGroup -class IAddress(IField): - title = schema.TextLine(title=_(u'Title'), required=False) - street = schema.TextLine(title=_(u'Street'), required=False) - zipcode = schema.TextLine(title=_(u'Zip'), required=False) - city = schema.TextLine(title=_(u'City'), required=False) - phone1 = schema.TextLine(title=_(u'Phone'), required=False) - phone2 = schema.TextLine(title=_(u'Phone 2'), required=False) - fax = schema.TextLine(title=_(u'Fax'), required=False) - email = schema.TextLine(title=_(u'Email'), required=False) - -class Address(Field): - implements(IAddress, IFromUnicode) - - def fromUnicode(self, str): - return None - - def __str__(self): - return u'/'.join([x for x in (self.title, self.street, self.zipcode, self.city) if x]) - -@adapter(IAddress, IFormLayer) -@implementer(IFieldWidget) -def AddressFieldWidget(field, request): - """IFieldWidget factory for Address.""" - return FieldWidget(field, TextWidget(request)) - +from address import Address class IDeputy(form.Schema): firstname = schema.TextLine(title=_(u'First Name'))