Commits

Shinya Okano committed 8916b60

#29 テストアプリ追加とmobileurlのバグ修正

Comments (0)

Files changed (6)

bpmobile/templatetags/mobile.py

 
     def render(self, context):
         url = super(MobileURLNode, self).render(context)
+        if self.asvar:
+            url = context[self.asvar]
         p, d, path, params, anc = urlparse.urlsplit(url)
         agent = context.get('agent', None)
         params_list = params and cgi.parse_qs(params) or {}
             if val is None:
                 val = ''
             val = force_unicode(val, errors='replace')
-            if agent.is_softbank():
+            if agent and agent.is_softbank():
                 params_list[k] = val.encode('utf-8')
             else:
                 params_list[k] = val.encode('cp932')
     guid = True
         
     if len(bits) > 2:
-        bits = iter(bits[2:])
+        bits = bits[2:]
         for bit in bits:
             if bit == 'as':
-                asvar = bits.next()
+                asvar = bits[-1]
                 break
             else:
                 for arg in bit.split(","):

testapp/__init__.py

Empty file added.

testapp/models.py

Empty file added.
+from django.conf.urls.defaults import *
+
+urlpatterns = patterns('',
+    url(r'^testapp/$', 'testapp.views.index',name='testapp_index'),
+)
+from django.http import HttpResponse
+
+def index(request):
+    return HttpResponse('OK')
 You can play with a django model without a complete django app installation.
 http://www.djangosnippets.org/snippets/1044/
 """
-
 import os
 
 APP_LABEL = os.path.splitext(os.path.basename(__file__))[0]
     'django.contrib.auth',
     'django.contrib.contenttypes',
     'bpmobile',
+    'testapp',
 )
 global_settings.DATABASE_ENGINE = "sqlite3"
 global_settings.DATABASE_NAME = ":memory:"
 global_settings.DATABASE_SUPPORTS_TRANSACTIONS = False
+global_settings.ROOT_URLCONF = 'testapp.urls'
+global_settings.TEMPLATE_CONTEXT_PROCESSORS = global_settings.TEMPLATE_CONTEXT_PROCESSORS + (
+    'bpmobile.context_processors.agent',
+)
 
 from django.core.management import sql
 
             self.fail()
         self.failUnlessEqual(content, u'\ue63e')
 
+    def test_mobileurl_tag(self):
+        req = HttpRequest()
+        req.agent = self.agent_docomo
+        t = Template('{% load mobile %}{% mobileurl testapp_index _params=foo=123 %}')
+        c = RequestContext(req)
+        content = t.render(c)
+        self.assertEqual(content, '/testapp/?guid=on&foo=123')
+
+    def test_mobileurl_tag_as(self):
+        req = HttpRequest()
+        req.agent = self.agent_docomo
+        t = Template('{% load mobile %}{% mobileurl testapp_index  _params=foo=123 as foourl %}{{ foourl }}')
+        c = RequestContext(req)
+        content = t.render(c)
+        self.assertEqual(content, '/testapp/?guid=on&foo=123')
+
 
 class SessionMiddlewareTest(BaseTestCase):
     def test_guid_redirect(self):
         self.failUnlessEqual(res['Location'], 'http://example.com/?abc=def&foo=bar&guid=on')
 
 
-