Mitsukuni Sato avatar Mitsukuni Sato committed 0f0233c

add mobile_fontsize on templatetags. it returns optimized "fontsize value" per carrier

Comments (0)

Files changed (1)

bpmobile/templatetags/mobile.py

         raise template.TemplateSyntaxError("'gif_or_png' tag are no arguments")
     else:
         return GIFOrPNGNode()
+
+# fontsize指定でとりうる値
+ACCEPTABLE_FONTSIZE = ['small', 'medium', 'large']
+
+# キャリアとfontsize指定のマッピング
+CARRIER_TO_FONTSIZE = {
+    'docomo': {'small':'xx-small', 'medium':'medium', 'large':'xx-large'},
+    'ezweb': {'small':'x-small', 'medium':'medium', 'large':'x-large'},
+    'softbank': {'small':'small', 'medium':'medium', 'large':'large'},
+    }
+
+class MobileFontsizeNode(template.Node):
+    def __init__(self, fontsize):
+        self.fontsize = fontsize.lower()
+
+    def __repr__(self):
+        return "<MobileFontsizeNode>"
+
+    def render(self, context):
+        agent = context.get('agent', None)
+        if agent is None:
+            return self.fontsize
+
+        return CARRIER_TO_FONTSIZE[agent.carrier.lower()][self.fontsize]
+
+@register.tag
+def mobile_fontsize(parser, token):
+    """
+    {% mobile_fontsize "small" %}
+    """
+    args = token.split_contents()
+
+    if len(args) == 2:
+        fontsize = args[1].strip('"\'')
+        if fontsize not in ACCEPTABLE_FONTSIZE:
+            raise template.TemplateSyntaxError("'mobile_fontsize' tag require arguments below: small / normal / large")
+
+        return MobileFontsizeNode(fontsize)
+    else:
+        raise template.TemplateSyntaxError("'mobile_fontsize' tag require one arguments")
+
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.