def process_request(self, request): if request.path.startswith("/admin"): return None # Use hash as the key since UA's can be quite llong, dont want to hit memcache 250 byte limit device_cache_key = hash(request.META['HTTP_USER_AGENT']) template_set = cache.get(device_cache_key) if template_set == "unsupported": return HttpResponse(loader.get_template('pages/418_service_error.html').render(RequestContext(request))) full_path = request.get_full_path() media_request = (full_path.startswith(settings.MEDIA_URL) or full_path.startswith(settings.ADMIN_MEDIA_PREFIX) or full_path.startswith('/favicon')) if not template_set: template_set = ua_mapper.get_template_set(request.META['HTTP_USER_AGENT']) if not template_set: mapper = UAMapper() user_agent, device, template_set = mapper.map_by_request(request) device_cache_timeout = getattr(settings, 'DEVICE_CACHE_TIMEOUT', 72000) cache.set(device_cache_key, template_set, device_cache_timeout) if not media_request and template_set: # switch the template dir for the given device settings.TEMPLATE_DIRS = settings.DEVICE_TEMPLATE_DIRS[template_set] request.template_set = template_set return None
def handle(self, force, *args, **options): mapper = UAMapper() if self.fetch_latest_wurfl() or force: self.wurfl_to_python() from wurfl import devices print "Updating Redis..." for i, ua in enumerate(devices.uas): device = devices.select_ua(ua) mapper.map(user_agent=ua, device=device) print "Done." else: print "Done. Redis unchanged."
def map_request(request): mapper = UAMapper() user_agent, device, value = mapper.map_by_request(request) return HttpResponse(value)