Commits

Shinya Okano committed 7a09b1d Merge

merge t28

  • Participants
  • Parent commits cb24183, d168a35

Comments (0)

Files changed (2)

File bpmobile/templatetags/mobile.py

         return "<MobileInputFormatNode>"
 
     def render(self, context):
+        from copy import copy
         bd_field = self.field.resolve(context)
         agent = context.get('agent', None)
-        # widgetを切り替える
+        widget = copy(bd_field.field.widget)  # 元のwidgetは書き換えない
         if agent and agent.is_docomo():
-            widget = forms.widgets.TextInput(attrs=MOBILE_INPUT_FORMAT_DOCOMO[self.mode])
+            widget.attrs.update(MOBILE_INPUT_FORMAT_DOCOMO[self.mode])
         elif agent and agent.is_ezweb():
-            widget = forms.widgets.TextInput(attrs=MOBILE_INPUT_FORMAT_EZWEB[self.mode])
+            widget.attrs.update(MOBILE_INPUT_FORMAT_EZWEB[self.mode])
         elif agent and agent.is_softbank():
-            widget = forms.widgets.TextInput(attrs=MOBILE_INPUT_FORMAT_SOFTBANK[self.mode])
-        else:
-            widget = bd_field.field.widget
+            widget.attrs.update(MOBILE_INPUT_FORMAT_SOFTBANK[self.mode])
+        # InputFormatを適用したwidgetを使う
         return bd_field.as_widget(widget=widget)
 
+
 @register.tag
 def mobile_input_format(parser, token):
     """
 import sys
 import unittest
 
+from django import forms
 from django.http import HttpRequest
 from django.conf import settings
 from django.template import Template, RequestContext
 from bpmobile.wsgi import DetectEncodingWSGIRequest
 from bpmobile.utils import useragent
 
+class MobileInputForm(forms.Form):
+    foo_field = forms.CharField(widget=forms.TextInput(attrs={'size': '30'}))
+
+
 class BaseTestCase(unittest.TestCase):
     def setUp(self):
         sys.modules['tests.models'] = None
         content = t.render(c)
         self.assertEqual(content, '/testapp/?guid=on&amp;foo=123')
 
+    def test_mobile_input_format(self):
+        req = HttpRequest()
+        req.agent = self.agent_docomo
+        form = MobileInputForm()
+        t = Template('{% load mobile %}{% mobile_input_format form.foo_field hiragana %}')
+        c = RequestContext(req)
+        c['form'] = form
+        content = t.render(c)
+        self.assertEqual(content, '<input id="id_foo_field" style="-wap-input-format:\'*<ja:h>\'" type="text" name="foo_field" size="30" />')
+
 
 class SessionMiddlewareTest(BaseTestCase):
     def test_guid_redirect(self):