Source

django-flext / django_flext / middleware.py

from django.conf import settings
import django_flext
import re

MOBILE_DOMAIN_RE = getattr(settings, 'MOBILE_DOMAIN_MATCH', None)

if not MOBILE_DOMAIN_RE:
    MOBILE_DOMAIN_RE = re.compile(r'^(m|mobile)\.')
if isinstance(MOBILE_DOMAIN_RE, basestring):
    MOBILE_DOMAIN_RE = re.compile(MOBILE_DOMAIN_RE)

MOBILE_TEMPLATE_DIRS = getattr(settings, 'MOBILE_TEMPLATE_DIRS', None)

class BaseFlextMiddleware(object):
    """
    A middleware base class that provides the common flext reset methods for
    each request.

    """

    def process_response(self, request, response):
        django_flext.reset()
        return response

    def process_exception(self, request, exception):
        django_flext.reset()

class MobileDomainMiddleware(BaseFlextMiddleware):
    """
    Middleware which adds one or more mobile-only template directories if
    the user is visiting from a mobile domain.
    
    """

    def process_request(self, request):
        if not MOBILE_TEMPLATE_DIRS:
            return
        host = request.get_host()
        if not host:
            return
        if MOBILE_DOMAIN_RE.search(host):
            print host
            django_flext.add_dirs(self, MOBILE_TEMPLATE_DIRS)