Shinya Okano avatar Shinya Okano committed 7a09b1d Merge

merge t28

Comments (0)

Files changed (2)

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):
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.