remove old access app and old cache and inclusion code

This commit is contained in:
Laura Klünder 2017-05-14 14:16:52 +02:00
parent c1d5c0ebef
commit 904e8f199c
31 changed files with 0 additions and 1212 deletions

View file

@ -1,63 +0,0 @@
from collections import OrderedDict
from django.utils.translation import ugettext_lazy as _
def get_default_include_avoid():
include = set()
avoid = set()
locations = list(AreaLocation.objects.exclude(routing_inclusion='default'))
for location in locations:
if location.routing_inclusion != 'allow_avoid':
avoid.add(location.location_id)
return include, avoid
# Todo: add cache
def get_includables_avoidables(request):
includables = []
avoidables = []
locations = list(AreaLocation.objects.exclude(routing_inclusion='default'))
if request.c3nav_full_access:
includables.append((':nonpublic', _('non-public areas')))
avoidables.append((':public', _('public areas')))
for location in locations:
item = (location.location_id, location.title)
if location.location_id not in request.c3nav_access_list and not request.c3nav_full_access:
if location.routing_inclusion == 'needs_permission':
continue
if location.routing_inclusion == 'allow_avoid':
avoidables.append(item)
else:
includables.append(item)
return OrderedDict(includables), OrderedDict(avoidables)
def get_maybe_invisible_areas():
return AreaLocation.objects.exclude(routing_inclusion='default')
def get_maybe_invisible_areas_names():
return tuple(area.name for area in get_maybe_invisible_areas())
def parse_include_avoid(request, include_input, avoid_input):
includable, avoidable = get_includables_avoidables(request)
include_input = set(include_input) & set(includable)
avoid_input = set(avoid_input) & set(avoidable)
default_include, default_avoid = get_default_include_avoid()
include = set(default_include) | include_input
avoid = set(default_avoid) - include_input | avoid_input
return ':nonpublic' in includable, include, avoid

View file

@ -1,33 +0,0 @@
import os
import pickle
from contextlib import contextmanager
from django.conf import settings
from django.utils import timezone
last_mapdata_update_filename = os.path.join(settings.DATA_DIR, 'last_mapdata_update')
last_mapdata_update_decorator_depth = 0
def get_last_mapdata_update(default_now=False):
try:
with open(last_mapdata_update_filename, 'rb') as f:
return pickle.load(f)
except:
return timezone.now() if default_now else None
@contextmanager
def set_last_mapdata_update():
global last_mapdata_update_decorator_depth
if last_mapdata_update_decorator_depth == 0:
try:
os.remove(last_mapdata_update_filename)
except:
pass
last_mapdata_update_decorator_depth += 1
yield
last_mapdata_update_decorator_depth -= 1
if last_mapdata_update_decorator_depth == 0:
with open(last_mapdata_update_filename, 'wb') as f:
pickle.dump(timezone.now(), f)

View file

@ -1,97 +0,0 @@
from calendar import timegm
from collections import OrderedDict
from functools import wraps
from django.core.cache import cache
from django.db.models import Q
from django.utils.http import http_date
from rest_framework.response import Response as APIResponse
from rest_framework.views import APIView
from c3nav.mapdata.lastupdate import get_last_mapdata_update
def cache_result(cache_key, timeout=900):
def decorator(func):
@wraps(func)
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
return inner
return decorator
def cache_mapdata_api_response(timeout=900):
def decorator(func):
@wraps(func)
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
return inner
return decorator
class CachedReadOnlyViewSetMixin():
def _get_add_cache_key(self, request, add_cache_key=''):
cache_key = add_cache_key
return cache_key
def list(self, request, *args, **kwargs):
kwargs['add_cache_key'] = self._get_add_cache_key(request, kwargs.get('add_cache_key', ''))
return self._list(request, *args, **kwargs)
@cache_mapdata_api_response()
def _list(self, request, *args, **kwargs):
return super().list(request, *args, **kwargs)
def retrieve(self, request, *args, **kwargs):
kwargs['add_cache_key'] = self._get_add_cache_key(request, kwargs.get('add_cache_key', ''))
return self._retrieve(request, *args, **kwargs)
@cache_mapdata_api_response()
def _retrieve(self, request, *args, **kwargs):
return super().retrieve(request, *args, **kwargs)
@cache_result('c3nav__mapdata__sections')
def get_sections_cached():
from c3nav.mapdata.models.section import Section
return OrderedDict((section.id, section) for section in Section.objects.all())
@cache_result('c3nav__mapdata__bssids')
def get_bssid_areas_cached():
from c3nav.mapdata.models import AreaLocation
bssids = {}
for area in AreaLocation.objects.filter(~Q(bssids='')):
for bssid in area.bssids.split('\n'):
bssids[bssid.strip()] = area.name
return bssids

View file

@ -1,37 +1,3 @@
import os
from django.conf import settings
from shapely.geometry import box
from shapely.ops import cascaded_union
from c3nav.mapdata.utils.cache import cache_result
@cache_result('c3nav__mapdata__dimensions')
def get_dimensions():
# todo calculate this
return (400, 240)
@cache_result('c3nav__mapdata__render_dimensions')
def get_render_dimensions():
width, height = get_dimensions()
return (width * settings.RENDER_SCALE, height * settings.RENDER_SCALE)
def get_render_path(filetype, level, mode, public):
return os.path.join(settings.RENDER_ROOT,
'%s%s-level-%s.%s' % (('public-' if public else ''), mode, level, filetype))
def get_public_private_area(level):
from c3nav.mapdata.models import AreaLocation
width, height = get_dimensions()
everything = box(0, 0, width, height)
needs_permission = [location.geometry
for location in AreaLocation.objects.filter(level=level,
routing_inclusion='needs_permission')]
public_area = level.public_geometries.areas_and_doors.difference(cascaded_union(needs_permission))
private_area = everything.difference(public_area)
return public_area, private_area