def handle_GET(self, request, context): if not getattr(self.conf, 'expose_view', False): raise Http404 ua = request.GET.get('ua', request.META.get('HTTP_USER_AGENT', '')) ua = ua.decode('ascii', 'ignore') try: device = devices.select_ua( ua, search=vsa ) except (KeyError, DeviceNotFound): device = devices.select_id('generic_xhtml') accepts = self.parse_accept_header(request.META.get('HTTP_ACCEPT', '')) renderers = MediaType.resolve(accepts, self.FORMATS_BY_MIMETYPE) formats = [renderer.format for renderer in renderers] context.update({ 'id': device.devid, 'is_mobile': not 'generic_web_browser' in device_parents[device.devid], 'brand_name': device.brand_name, 'model_name': device.model_name, 'ua': ua, 'matched_ua': device.devua, 'accept': request.META.get('HTTP_ACCEPT', ''), 'formats': formats }) if request.GET.get('capabilities') == 'true': context['capabilities'] = dict((k, getattr(device, k)) for k in dir(device) if (not k.startswith('_') and not k.startswith('dev') and not k in ('groups','children'))) return self.render(request, context, 'wurfl/index')
def render(self, request, context, template_name, expires=None): """ Given a request, a context dictionary and a template name, this renders the template with the given context according to the capabilities and requested format of the client. An optional final argument is that of a timedelta object, which sets additional caching headers for the content. """ context.pop('exposes_user_data', None) if 'format' in request.REQUEST: formats = request.REQUEST['format'].split(',') renderers, seen_formats = [], set() for format in formats: if format in self.FORMATS and format not in seen_formats: renderers.append(self.FORMATS[format]) elif request.META.get('HTTP_ACCEPT'): accepts = self.parse_accept_header(request.META['HTTP_ACCEPT']) renderers = MediaType.resolve(accepts, self.FORMATS_BY_MIMETYPE) else: renderers = [self.FORMATS['html']] # Stop external sites from grabbing JSON representations of pages # which contain sensitive user information. try: offsite_referrer = 'HTTP_REFERER' in request.META and \ request.META['HTTP_REFERER'].split('/')[2] != \ request.META.get('HTTP_HOST') except IndexError: # Malformed referrers (i.e., those not containing a full URL) throw # this offsite_referrer = True for renderer in renderers: if renderer.format != 'html' and context.get('exposes_user_data') \ and offsite_referrer: continue try: response = renderer(request, context, template_name) except NotImplementedError: continue else: if expires is not None: response['Expires'] = formatdate( mktime((datetime.now() + expires).timetuple())) # if expires is negative, then consider this to be no-cache if expires < timedelta(seconds=0): response['Cache-Control'] = 'no-cache' else: response['Cache-Control'] = 'max-age=%d' % \ (expires.seconds + expires.days * 24 * 3600) return response else: if 'format' not in request.REQUEST: tried_mimetypes = list(itertools.chain(*[r.mimetypes for r in renderers])) response = HttpResponse( "Your Accept header didn't contain any supported media " + "ranges.\n\nSupported ranges are:\n\n * %s\n" % '\n * '.join( sorted('%s (%s)' % (f[0].value, f[1].format) for f in self.FORMATS_BY_MIMETYPE if not f[0] in tried_mimetypes)), mimetype="text/plain") else: print self.FORMATS response = HttpResponse( "Unable to render this document in this format.\n\n" + "Supported formats are:\n\n * %s\n" \ % '\n * '.join(self.FORMATS.keys()), mimetype="text/plain") response.status_code = 406 # Not Acceptable return response