def inner(self, request, *args, add_cache_key=None, **kwargs): last_update = get_last_mapdata_update() if last_update is None: return func(self, request, *args, **kwargs) cache_key = '__'.join(( 'c3nav__mapdata__api', last_update.isoformat(), add_cache_key if add_cache_key is not None else '', request.accepted_renderer.format if isinstance(self, APIView) else '', request.path, )) response = cache.get(cache_key) if not response: response = func(self, request, *args, **kwargs) response['Last-Modifed'] = http_date( timegm(last_update.utctimetuple())) if isinstance(response, APIResponse): response = self.finalize_response(request, response, *args, **kwargs) response.render() if response.status_code < 400: cache.set(cache_key, response, timeout) return response
def map_image(request, area, level): level = get_object_or_404(Level, name=level, intermediate=False) if area == ':base': img = get_render_path('png', level.name, 'full', True) elif area == ':full': if not request.c3nav_full_access: raise Http404 img = get_render_path('png', level.name, 'full', False) elif area in request.c3nav_access_list: img = get_render_path(area + '.png', level.name, 'full', False) else: raise Http404 last_update = get_last_mapdata_update() etag = '-'.join(str(i) for i in last_update.timetuple()) if_none_match = request.META.get('HTTP_IF_NONE_MATCH') if if_none_match: if if_none_match == etag: return HttpResponseNotModified() response = HttpResponse(content_type='image/png') for chunk in File(open(img, 'rb')).chunks(): response.write(chunk) response['ETag'] = etag response['Cache-Control'] = 'no-cache' return response
def inner(*args, **kwargs): last_update = get_last_mapdata_update() if last_update is None: return func(*args, **kwargs) result = cache.get(cache_key) if not result: result = func(*args, **kwargs) cache.set(cache_key, result, timeout) return result
def get_in_areas(self): last_update = get_last_mapdata_update() if last_update is None: return self._get_in_areas() cache_key = 'c3nav__mapdata__location__in_areas__'+last_update.isoformat()+'__'+self.name, in_areas = cache.get(cache_key) if not in_areas: in_areas = self._get_in_areas() cache.set(cache_key, in_areas, 900) return in_areas
def list(self, request, **kwargs): etag = hashlib.sha256(json.dumps({ 'full_access': request.c3nav_full_access, 'access_list': request.c3nav_access_list, 'last_update': get_last_mapdata_update().isoformat() }).encode()).hexdigest() if_none_match = request.META.get('HTTP_IF_NONE_MATCH') if if_none_match: if if_none_match == etag: return HttpResponseNotModified() locations = [] locations += list(filter_queryset_by_access(request, self._filter(LocationGroup.objects.all()))) locations += sorted(filter_arealocations_by_access(request, self._filter(AreaLocation.objects.all())), key=AreaLocation.get_sort_key, reverse=True) response = Response([location.to_location_json() for location in locations]) response['ETag'] = etag response['Cache-Control'] = 'no-cache' return response