Commit e2535554 authored by Kazuhiko Shiozaki's avatar Kazuhiko Shiozaki

Formulator: prepare DateTimeField's sub_form dynamically.

otherwise TALES in input_style does not work and changes in the original proxy field will not be reflected.
parent 0db642f9
...@@ -493,10 +493,10 @@ class ProxyField(ZMIField): ...@@ -493,10 +493,10 @@ class ProxyField(ZMIField):
field = template_field field = template_field
return template_field return template_field
def _aq_dynamic(self, name): def _get_sub_form(self, field=None):
if name == 'sub_form': if field is None:
return self.getTemplateField().sub_form field = self
return None return self.getTemplateField()._get_sub_form(field=field)
security.declareProtected('Access contents information', security.declareProtected('Access contents information',
'is_delegated') 'is_delegated')
......
...@@ -764,14 +764,31 @@ class TestProxyField(ERP5TypeTestCase): ...@@ -764,14 +764,31 @@ class TestProxyField(ERP5TypeTestCase):
'my_date', 'Date', 'ProxyField') 'my_date', 'Date', 'ProxyField')
proxy_field.manage_edit_xmlrpc(dict(form_id='Base_viewProxyFieldLibrary', proxy_field.manage_edit_xmlrpc(dict(form_id='Base_viewProxyFieldLibrary',
field_id='my_date',)) field_id='my_date',))
self.assertTrue(hasattr(proxy_field, 'sub_form')) self.assertTrue(hasattr(proxy_field, '_get_sub_form'))
self.assertTrue(aq_base(proxy_field.sub_form) is self.assertEqual(proxy_field._get_sub_form().render(),
aq_base(original_field.sub_form)) original_field._get_sub_form().render())
# we can render # we can render
proxy_field.render() proxy_field.render()
# and validate # and validate
self.container.Base_view.validate_all_to_request(self.portal.REQUEST) self.container.Base_view.validate_all_to_request(self.portal.REQUEST)
# change style in the original field
original_field.manage_edit_xmlrpc(dict(input_style='number'))
self.assertTrue('type="number"' in original_field.render())
self.assertTrue('type="number"' in proxy_field.render())
# override style in the proxy field
original_field.manage_edit_xmlrpc(dict(input_style='text'))
proxy_field._surcharged_edit({'input_style': 'number'}, ['input_style'])
self.assertTrue('type="text"' in original_field.render())
self.assertTrue('type="number"' in proxy_field.render())
# unproxify the proxy field
self.container.Base_view.unProxifyField({'my_date': 'on'})
unproxified_field = self.container.Base_view.my_date
self.assertTrue('type="number"' in unproxified_field.render())
def test_manage_edit_surcharged_xmlrpc(self): def test_manage_edit_surcharged_xmlrpc(self):
# manage_edit_surcharged_xmlrpc is a method to edit proxyfields # manage_edit_surcharged_xmlrpc is a method to edit proxyfields
# programmatically # programmatically
...@@ -942,7 +959,7 @@ class TestFieldValueCache(ERP5TypeTestCase): ...@@ -942,7 +959,7 @@ class TestFieldValueCache(ERP5TypeTestCase):
addField(DateTimeField('datetime_field')) addField(DateTimeField('datetime_field'))
form.datetime_field._p_oid = makeDummyOid() form.datetime_field._p_oid = makeDummyOid()
form.datetime_field._edit(dict(input_style='list')) form.datetime_field._edit(dict(input_style='list'))
for i in form.datetime_field.sub_form.fields.values(): for i in form.datetime_field._get_sub_form().fields.values():
i._p_oid = makeDummyOid() i._p_oid = makeDummyOid()
def test_method_field(self): def test_method_field(self):
...@@ -987,41 +1004,6 @@ class TestFieldValueCache(ERP5TypeTestCase): ...@@ -987,41 +1004,6 @@ class TestFieldValueCache(ERP5TypeTestCase):
self.root.form.proxy_field_tales.get_value('title') self.root.form.proxy_field_tales.get_value('title')
self.assertEqual(True, cache_size == self._getCacheSize('ProxyField.get_value')) self.assertEqual(True, cache_size == self._getCacheSize('ProxyField.get_value'))
def test_datetime_field(self):
field_value_cache.clear()
# make sure that boundmethod must not be cached.
year_field = self.root.form.datetime_field.sub_form.get_field('year', include_disabled=1)
self.assertEqual(True, type(year_field.overrides['items']) is BoundMethod)
cache_size = len(field_value_cache)
year_field.get_value('items')
# See Formulator/StandardFields.py(line:174)
# there are two get_value, start_datetime and end_datetime
cache_size += 2
# make sure that boundmethod is not cached(cache size does not change)
self.assertEqual(True, ('Form.get_value',
self.root.form.datetime_field._p_oid,
self.root.form.datetime_field._p_oid,
'start_datetime'
) in field_value_cache)
self.assertEqual(True, ('Form.get_value',
self.root.form.datetime_field._p_oid,
self.root.form.datetime_field._p_oid,
'end_datetime'
) in field_value_cache)
self.assertEqual(False, ('Form.get_value',
year_field._p_oid,
year_field._p_oid,
'items'
) in field_value_cache)
self.assertEqual(cache_size, len(field_value_cache))
year_field.get_value('size')
year_field.get_value('default')
self.assertEqual(cache_size+2, len(field_value_cache))
def makeDummyOid(): def makeDummyOid():
import time, random import time, random
......
...@@ -403,7 +403,7 @@ class Field: ...@@ -403,7 +403,7 @@ class Field:
a form. Works like render() but for sub field. a form. Works like render() but for sub field.
Added key parameter for ERP5 in order to be compatible with listbox/matrixbox Added key parameter for ERP5 in order to be compatible with listbox/matrixbox
""" """
return self.sub_form.get_field(id)._render_helper( return self._get_sub_form().get_field(id)._render_helper(
self.generate_subfield_key(id, key=key), value, REQUEST, render_prefix) self.generate_subfield_key(id, key=key), value, REQUEST, render_prefix)
security.declareProtected('View', 'render_sub_field_from_request') security.declareProtected('View', 'render_sub_field_from_request')
...@@ -411,7 +411,7 @@ class Field: ...@@ -411,7 +411,7 @@ class Field:
"""Convenience method; render the field widget from REQUEST """Convenience method; render the field widget from REQUEST
(unvalidated data), or default if no raw data is found. (unvalidated data), or default if no raw data is found.
""" """
return self.sub_form.get_field(id)._render_helper( return self._get_sub_form().get_field(id)._render_helper(
self.generate_subfield_key(id), None, REQUEST) self.generate_subfield_key(id), None, REQUEST)
security.declarePrivate('_validate_helper') security.declarePrivate('_validate_helper')
...@@ -441,7 +441,7 @@ class Field: ...@@ -441,7 +441,7 @@ class Field:
def validate_sub_field(self, id, REQUEST, key=None): def validate_sub_field(self, id, REQUEST, key=None):
"""Validates a subfield (as part of field validation). """Validates a subfield (as part of field validation).
""" """
return self.sub_form.get_field(id)._validate_helper( return self._get_sub_form().get_field(id)._validate_helper(
self.generate_subfield_key(id, validation=1, key=key), REQUEST) self.generate_subfield_key(id, validation=1, key=key), REQUEST)
def PrincipiaSearchSource(self): def PrincipiaSearchSource(self):
......
This diff is collapsed.
...@@ -1455,10 +1455,6 @@ class DateTimeWidget(Widget): ...@@ -1455,10 +1455,6 @@ class DateTimeWidget(Widget):
def render(self, field, key, value, REQUEST, render_prefix=None): def render(self, field, key, value, REQUEST, render_prefix=None):
use_ampm = field.get_value('ampm_time_style') use_ampm = field.get_value('ampm_time_style')
use_timezone = field.get_value('timezone_style') use_timezone = field.get_value('timezone_style')
# FIXME: backwards compatibility hack:
if not hasattr(field, 'sub_form'):
from StandardFields import create_datetime_text_sub_form
field.sub_form = create_datetime_text_sub_form()
# Is it still usefull to test the None value, # Is it still usefull to test the None value,
# as DateTimeField should be considerer as the other field # as DateTimeField should be considerer as the other field
......
...@@ -91,11 +91,6 @@ def XMLToForm(s, form, override_encoding=None): ...@@ -91,11 +91,6 @@ def XMLToForm(s, form, override_encoding=None):
else: else:
field.values[name] = encode(value.text, encoding) field.values[name] = encode(value.text, encoding)
# special hack for the DateTimeField
if field.meta_type=='DateTimeField':
field.on_value_input_style_changed(
field.get_value('input_style'))
# set tales # set tales
tales = entry.first.tales tales = entry.first.tales
for name in tales.getElementNames(): for name in tales.getElementNames():
......
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
import unittest, re import unittest, re
from DateTime import DateTime
import Zope2 import Zope2
# XXX this does not work for zope2.x if x < 3 # XXX this does not work for zope2.x if x < 3
...@@ -120,14 +121,10 @@ class FormTestCase(unittest.TestCase): ...@@ -120,14 +121,10 @@ class FormTestCase(unittest.TestCase):
self.form.manage_addProduct['Formulator']\ self.form.manage_addProduct['Formulator']\
.manage_addField('date_time','Test Field','DateTimeField') .manage_addField('date_time','Test Field','DateTimeField')
field = self.form.date_time field = self.form.date_time
sub_form = field.sub_form field._edit({'timezone_style': 0})
if sub_form.has_field('timezone'): self.assertFalse('<select size="1" name="subfield_field_date_time_timezone" >' in field.render())
del sub_form.fields['timezone']
#now timezone is not presented
self.assertFalse(self.form.date_time.sub_form.has_field('timezone'))
field._edit({'timezone_style': 1}) field._edit({'timezone_style': 1})
#test if timezone's presented self.assertTrue('<select size="1" name="subfield_field_date_time_timezone" >' in field.render())
self.assertTrue(self.form.date_time.sub_form.has_field('timezone'))
def test_datetime_css_class_rendering(self): def test_datetime_css_class_rendering(self):
...@@ -177,6 +174,22 @@ class FormTestCase(unittest.TestCase): ...@@ -177,6 +174,22 @@ class FormTestCase(unittest.TestCase):
self.assertEqual(0, len(css_matches)) self.assertEqual(0, len(css_matches))
def test_datetime_number_style(self):
# test that a bug is fixed, which causing the css_class value
# not to be rendered
self.form.manage_addProduct['Formulator']\
.manage_addField('date_time','Test Field','DateTimeField')
field = self.form.date_time
field._edit({'input_style': 'number'})
self.assertTrue('<input name="subfield_field_date_time_year" value="" maxlength="4" type="number" size="4" min="0" max="9999" />' in field.render())
field._edit({'start_datetime': DateTime('1900/01/01'), 'end_datetime': None})
self.assertTrue('<input name="subfield_field_date_time_year" value="" maxlength="4" type="number" size="4" min="1900" max="9999" />' in field.render())
field._edit({'start_datetime': None, 'end_datetime': DateTime('2099/12/31')})
self.assertTrue('<input name="subfield_field_date_time_year" value="" maxlength="4" type="number" size="4" min="0" max="2099" />' in field.render())
field._edit({'start_datetime': DateTime('1900/01/01'), 'end_datetime': DateTime('2099/12/31')})
self.assertTrue('<input name="subfield_field_date_time_year" value="" maxlength="4" type="number" size="4" min="1900" max="2099" />' in field.render())
def test_suite(): def test_suite():
suite = unittest.TestSuite() suite = unittest.TestSuite()
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment