Exemple #1
0
        def wrapped_func(self, request, *args, **kwargs):
            response_format = self.perform_content_negotiation(request)[0].format
            etag_user = (':'+str(request.user.pk or 0)) if response_format == 'api' else ''
            raw_etag = '%s%s:%s:%s' % (response_format, etag_user, get_language(),
                                       (etag_func(request) if permissions else MapUpdate.current_cache_key()))
            if base_mapdata_check and self.base_mapdata:
                raw_etag += ':%d' % request.user_permissions.can_access_base_mapdata
            etag = quote_etag(raw_etag)

            response = get_conditional_response(request, etag=etag)
            if response is None:
                cache_key = 'mapdata:api:'+request.path_info[5:].replace('/', '-').strip('-')+':'+raw_etag
                if cache_parameters is not None:
                    for param, type_ in cache_parameters.items():
                        value = int(param in request.GET) if type_ == bool else type_(request.GET.get(param))
                        cache_key += ':'+urlsafe_base64_encode(str(value).encode()).decode()
                data = request_cache.get(cache_key)
                if data is not None:
                    response = Response(data)

                if response is None:
                    with GeometryMixin.dont_keep_originals():
                        response = func(self, request, *args, **kwargs)
                    if cache_parameters is not None and response.status_code == 200:
                        request_cache.set(cache_key, response.data, 900)

            if response.status_code == 200:
                response['ETag'] = etag
                response['Cache-Control'] = 'no-cache'
            return response
Exemple #2
0
        def wrapped_func(self, request, *args, **kwargs):
            response_format = self.perform_content_negotiation(
                request)[0].format
            etag_user = (
                ':' +
                str(request.user.pk or 0)) if response_format == 'api' else ''
            raw_etag = '%s%s:%s:%s' % (response_format, etag_user,
                                       get_language(),
                                       (etag_func(request) if permissions else
                                        MapUpdate.current_cache_key()))
            etag = quote_etag(raw_etag)

            response = get_conditional_response(request, etag=etag)
            if response is None:
                cache_key = 'mapdata:api:' + request.path_info[5:].replace(
                    '/', '-').strip('-') + ':' + raw_etag
                if cache_parameters is not None:
                    for param, type_ in cache_parameters.items():
                        value = int(
                            param in request.GET) if type_ == bool else type_(
                                request.GET.get(param))
                        cache_key += ':' + urlsafe_base64_encode(
                            str(value).encode()).decode()
                    data = cache.get(cache_key)
                    if data is not None:
                        response = Response(data)

                if response is None:
                    response = func(self, request, *args, **kwargs)
                    if cache_parameters is not None and response.status_code == 200:
                        cache.set(cache_key, response.data, 300)

            response['ETag'] = etag
            response['Cache-Control'] = 'no-cache'
            return response
Exemple #3
0
 def get_fields(cls):
     cache_key = MapUpdate.current_cache_key()
     if cls.fields_cache_key != cache_key:
         with cls.fields_cache_lock:
             cls.fields_cache_key = cache_key
             cls.fields_cached = cls.build_fields()
     return cls.fields_cached
Exemple #4
0
 def get_all_access_restrictions():
     cache_key = 'all_access_restrictions:%s' % MapUpdate.current_cache_key()
     access_restriction_ids = cache.get(cache_key, None)
     if access_restriction_ids is None:
         access_restriction_ids = set(AccessRestriction.objects.values_list('pk', flat=True))
         cache.set(cache_key, access_restriction_ids, 300)
     return access_restriction_ids
Exemple #5
0
 def get_fields(cls):
     cache_key = MapUpdate.current_cache_key()
     if cls.fields_cache_key != cache_key:
         with cls.fields_cache_lock:
             cls.fields_cache_key = cache_key
             cls.fields_cached = cls.build_fields()
     return cls.fields_cached
Exemple #6
0
 def max_bounds(cls):
     cache_key = 'mapdata:max_bounds:%s:%s' % (cls.__name__, MapUpdate.current_cache_key())
     result = cache.get(cache_key, None)
     if result is not None:
         return result
     result = cls.objects.all().aggregate(models.Min('left'), models.Min('bottom'),
                                          models.Max('right'), models.Max('top'))
     result = ((float(result['left__min']), float(result['bottom__min'])),
               (float(result['right__max']), float(result['top__max'])))
     cache.set(cache_key, result, 900)
     return result
Exemple #7
0
    def _get_keys_for_model(self, request, model, key):
        if hasattr(model, 'qs_for_request'):
            cache_key = 'mapdata:api:keys:%s:%s:%s' % (model.__name__, key,
                                                       AccessPermission.cache_key_for_request(request))
            qs = model.qs_for_request(request)
        else:
            cache_key = 'mapdata:api:keys:%s:%s:%s' % (model.__name__, key,
                                                       MapUpdate.current_cache_key())
            qs = model.objects.all()

        result = cache.get(cache_key, None)
        if result is not None:
            return result

        result = set(qs.values_list(key, flat=True))
        cache.set(cache_key, result, 300)

        return result
Exemple #8
0
def get_better_space_geometries():
    # change space geometries for better representative points
    cache_key = 'mapdata:better_space_geometries:%s' % MapUpdate.current_cache_key()
    result = proxied_cache.get(cache_key, None)
    if result is not None:
        return result

    result = {}
    for space in Space.objects.prefetch_related('columns', 'holes'):
        geometry = space.geometry.difference(
            cascaded_union(tuple(obj.geometry for obj in chain(space.columns.all(), space.holes.all())))
        )
        if not geometry.is_empty:
            result[space.pk] = geometry

    proxied_cache.set(cache_key, result, 1800)

    return result
Exemple #9
0
    def _get_keys_for_model(self, request, model, key):
        if hasattr(model, 'qs_for_request'):
            cache_key = 'mapdata:api:keys:%s:%s:%s' % (
                model.__name__, key,
                AccessPermission.cache_key_for_request(request))
            qs = model.qs_for_request(request)
        else:
            cache_key = 'mapdata:api:keys:%s:%s:%s' % (
                model.__name__, key, MapUpdate.current_cache_key())
            qs = model.objects.all()

        result = cache.get(cache_key, None)
        if result is not None:
            return result

        result = set(qs.values_list(key, flat=True))
        cache.set(cache_key, result, 300)

        return result
Exemple #10
0
 def raw_cache_key_by_changes(self):
     if self.pk is None:
         return MapUpdate.current_cache_key()
     return ':'.join((str(self.pk), MapUpdate.current_cache_key(), self.last_change_cache_key))
Exemple #11
0
 def cache_key_for_request(cls, request, with_update=True):
     return ((
         (MapUpdate.current_cache_key() + ':') if with_update else ''
     ) + '-'.join(
         str(i)
         for i in sorted(AccessPermission.get_for_request(request)) or '0'))
Exemple #12
0
 def cache_key_for_request(cls, request, with_update=True):
     return (
         ((MapUpdate.current_cache_key()+':') if with_update else '') +
         '-'.join(str(i) for i in sorted(AccessPermission.get_for_request(request)) or '0')
     )
Exemple #13
0
 def _check_mapupdate(self):
     mapupdate = MapUpdate.current_cache_key()
     if self._mapupdate != mapupdate:
         self._items = OrderedDict()
         self._mapupdate = mapupdate
Exemple #14
0
 def raw_cache_key_by_changes(self):
     if self.pk is None:
         return MapUpdate.current_cache_key()
     return ':'.join((str(self.pk), MapUpdate.current_cache_key(),
                      self.last_change_cache_key))
Exemple #15
0
 def raw_cache_key_without_changes(self):
     if self.pk is None:
         return MapUpdate.current_cache_key()
     return ':'.join((str(self.pk), MapUpdate.current_cache_key()))