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):
field = template_field
return template_field
def _aq_dynamic(self, name):
if name == 'sub_form':
return self.getTemplateField().sub_form
return None
def _get_sub_form(self, field=None):
if field is None:
field = self
return self.getTemplateField()._get_sub_form(field=field)
security.declareProtected('Access contents information',
'is_delegated')
......
......@@ -764,14 +764,31 @@ class TestProxyField(ERP5TypeTestCase):
'my_date', 'Date', 'ProxyField')
proxy_field.manage_edit_xmlrpc(dict(form_id='Base_viewProxyFieldLibrary',
field_id='my_date',))
self.assertTrue(hasattr(proxy_field, 'sub_form'))
self.assertTrue(aq_base(proxy_field.sub_form) is
aq_base(original_field.sub_form))
self.assertTrue(hasattr(proxy_field, '_get_sub_form'))
self.assertEqual(proxy_field._get_sub_form().render(),
original_field._get_sub_form().render())
# we can render
proxy_field.render()
# and validate
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):
# manage_edit_surcharged_xmlrpc is a method to edit proxyfields
# programmatically
......@@ -942,7 +959,7 @@ class TestFieldValueCache(ERP5TypeTestCase):
addField(DateTimeField('datetime_field'))
form.datetime_field._p_oid = makeDummyOid()
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()
def test_method_field(self):
......@@ -987,41 +1004,6 @@ class TestFieldValueCache(ERP5TypeTestCase):
self.root.form.proxy_field_tales.get_value('title')
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():
import time, random
......
......@@ -403,7 +403,7 @@ class Field:
a form. Works like render() but for sub field.
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)
security.declareProtected('View', 'render_sub_field_from_request')
......@@ -411,7 +411,7 @@ class Field:
"""Convenience method; render the field widget from REQUEST
(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)
security.declarePrivate('_validate_helper')
......@@ -441,7 +441,7 @@ class Field:
def validate_sub_field(self, id, REQUEST, key=None):
"""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)
def PrincipiaSearchSource(self):
......
This diff is collapsed.
......@@ -1455,10 +1455,6 @@ class DateTimeWidget(Widget):
def render(self, field, key, value, REQUEST, render_prefix=None):
use_ampm = field.get_value('ampm_time_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,
# as DateTimeField should be considerer as the other field
......
......@@ -91,11 +91,6 @@ def XMLToForm(s, form, override_encoding=None):
else:
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
tales = entry.first.tales
for name in tales.getElementNames():
......
# -*- coding: utf-8 -*-
import unittest, re
from DateTime import DateTime
import Zope2
# XXX this does not work for zope2.x if x < 3
......@@ -120,14 +121,10 @@ class FormTestCase(unittest.TestCase):
self.form.manage_addProduct['Formulator']\
.manage_addField('date_time','Test Field','DateTimeField')
field = self.form.date_time
sub_form = field.sub_form
if sub_form.has_field('timezone'):
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': 0})
self.assertFalse('<select size="1" name="subfield_field_date_time_timezone" >' in field.render())
field._edit({'timezone_style': 1})
#test if timezone's presented
self.assertTrue(self.form.date_time.sub_form.has_field('timezone'))
self.assertTrue('<select size="1" name="subfield_field_date_time_timezone" >' in field.render())
def test_datetime_css_class_rendering(self):
......@@ -177,6 +174,22 @@ class FormTestCase(unittest.TestCase):
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():
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