add option to save settings in cookie
This commit is contained in:
parent
91b9bc92b8
commit
45a40782c1
3 changed files with 88 additions and 51 deletions
8
src/c3nav/site/templates/site/fragment_select.html
Normal file
8
src/c3nav/site/templates/site/fragment_select.html
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
{% load i18n %}
|
||||||
|
|
||||||
|
<select name="{{ name }}" id="{{ name }}-select" class="form-control">
|
||||||
|
<option value="yes"{% if value == 'yes' %} selected{% endif %}>{% trans 'allow' %}</option>
|
||||||
|
<option value="up"{% if value == 'up' %} selected{% endif %}>{% trans 'only upwards' %}</option>
|
||||||
|
<option value="down"{% if value == 'down' %} selected{% endif %}>{% trans 'only downwards' %}</option>
|
||||||
|
<option value="no"{% if value == 'no' %} selected{% endif %}>{% trans 'avoid' %}</option>
|
||||||
|
</select>
|
|
@ -16,42 +16,27 @@
|
||||||
<div class="row settings">
|
<div class="row settings">
|
||||||
<div class="form-group col-md-2">
|
<div class="form-group col-md-2">
|
||||||
<label for="stairs-select">{% trans 'Stairs' %}</label>
|
<label for="stairs-select">{% trans 'Stairs' %}</label>
|
||||||
<select name="stairs" id="stairs-select" class="form-control">
|
{% include 'site/fragment_select.html' with name='stairs' value=stairs %}
|
||||||
<option value="yes">{% trans 'allow' %}</option>
|
|
||||||
<option value="up">{% trans 'only upwards' %}</option>
|
|
||||||
<option value="down">{% trans 'only downwards' %}</option>
|
|
||||||
<option value="no">{% trans 'avoid' %}</option>
|
|
||||||
</select>
|
|
||||||
</div>
|
</div>
|
||||||
<div class="form-group col-md-2">
|
<div class="form-group col-md-2">
|
||||||
<label for="escalators-select">{% trans 'Escalators' %}</label>
|
<label for="escalators-select">{% trans 'Escalators' %}</label>
|
||||||
<select name="escalators" id="escalator-select" class="form-control">
|
{% include 'site/fragment_select.html' with name='escalators' value=escalators %}
|
||||||
<option value="yes">{% trans 'allow' %}</option>
|
|
||||||
<option value="up">{% trans 'only upwards' %}</option>
|
|
||||||
<option value="down">{% trans 'only downwards' %}</option>
|
|
||||||
<option value="no">{% trans 'avoid' %}</option>
|
|
||||||
</select>
|
|
||||||
</div>
|
</div>
|
||||||
<div class="form-group col-md-2">
|
<div class="form-group col-md-2">
|
||||||
<label for="elevators-select">{% trans 'Elevators' %}</label>
|
<label for="elevators-select">{% trans 'Elevators' %}</label>
|
||||||
<select name="elevators" id="elevators-select" class="form-control">
|
{% include 'site/fragment_select.html' with name='elevators' value=elevators %}
|
||||||
<option value="yes">{% trans 'allow' %}</option>
|
|
||||||
<option value="up">{% trans 'only upwards' %}</option>
|
|
||||||
<option value="down">{% trans 'only downwards' %}</option>
|
|
||||||
<option value="no">{% trans 'avoid' %}</option>
|
|
||||||
</select>
|
|
||||||
</div>
|
</div>
|
||||||
<div class="form-group col-md-6">
|
<div class="form-group col-md-6">
|
||||||
<label>{% trans 'Include / Avoid:' %}</label>
|
<label>{% trans 'Include / Avoid areas' %}</label>
|
||||||
<p class="form-control-static">
|
<p class="form-control-static">
|
||||||
{% for name, includable in includables %}
|
{% for name, includable in includables %}
|
||||||
<label class="checkbox-inline">
|
<label class="checkbox-inline">
|
||||||
<input type="checkbox" name="include" value="{{ name }}"> {% blocktrans %}Include {{ includable }}{% endblocktrans %}
|
<input type="checkbox" name="include" value="{{ name }}"{% if name in include %} checked{% endif %}> {% blocktrans %}Include {{ includable }}{% endblocktrans %}
|
||||||
</label>
|
</label>
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
{% for name, excludable in excludables %}
|
{% for name, excludable in excludables %}
|
||||||
<label class="checkbox-inline">
|
<label class="checkbox-inline">
|
||||||
<input type="checkbox" name="avoid" value="{{ name }}"> {% blocktrans %}Avoid {{ excludable }}{% endblocktrans %}
|
<input type="checkbox" name="avoid" value="{{ name }}"{% if name in avoid %} checked{% endif %}> {% blocktrans %}Avoid {{ excludable }}{% endblocktrans %}
|
||||||
</label>
|
</label>
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
</p>
|
</p>
|
||||||
|
@ -61,7 +46,7 @@
|
||||||
<div class="form-group col-md-6">
|
<div class="form-group col-md-6">
|
||||||
<div class="checkbox">
|
<div class="checkbox">
|
||||||
<label>
|
<label>
|
||||||
<input type="checkbox"> {% trans 'Save Settings in cookie' %}
|
<input type="checkbox" name="save_settings" value="1"{% if save_settings %} checked{% endif %}> {% trans 'Save Settings in cookie' %}
|
||||||
</label>
|
</label>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
|
@ -1,9 +1,8 @@
|
||||||
import os
|
from datetime import timedelta
|
||||||
|
|
||||||
from django.conf import settings
|
from django.http import Http404
|
||||||
from django.http import Http404, HttpResponse
|
|
||||||
from django.shortcuts import get_object_or_404, render
|
from django.shortcuts import get_object_or_404, render
|
||||||
from PIL import Image, ImageDraw
|
from django.utils import timezone
|
||||||
|
|
||||||
from c3nav.mapdata.models import Level
|
from c3nav.mapdata.models import Level
|
||||||
from c3nav.mapdata.models.locations import get_location
|
from c3nav.mapdata.models.locations import get_location
|
||||||
|
@ -11,7 +10,6 @@ from c3nav.mapdata.permissions import get_excludables_includables
|
||||||
from c3nav.mapdata.render.compose import composer
|
from c3nav.mapdata.render.compose import composer
|
||||||
from c3nav.mapdata.utils.misc import get_dimensions
|
from c3nav.mapdata.utils.misc import get_dimensions
|
||||||
from c3nav.routing.graph import Graph
|
from c3nav.routing.graph import Graph
|
||||||
from c3nav.routing.utils.draw import _line_coords
|
|
||||||
|
|
||||||
ctype_mapping = {
|
ctype_mapping = {
|
||||||
'yes': ('up', 'down'),
|
'yes': ('up', 'down'),
|
||||||
|
@ -25,6 +23,13 @@ def get_ctypes(prefix, value):
|
||||||
return tuple((prefix+'_'+direction) for direction in ctype_mapping.get(value, ('up', 'down')))
|
return tuple((prefix+'_'+direction) for direction in ctype_mapping.get(value, ('up', 'down')))
|
||||||
|
|
||||||
|
|
||||||
|
def reverse_ctypes(ctypes, name):
|
||||||
|
if name+'_up' in ctypes:
|
||||||
|
return 'yes' if name + '_down' in ctypes else 'up'
|
||||||
|
else:
|
||||||
|
return 'down' if name + '_down' in ctypes else 'no'
|
||||||
|
|
||||||
|
|
||||||
def main(request, origin=None, destination=None):
|
def main(request, origin=None, destination=None):
|
||||||
if origin:
|
if origin:
|
||||||
origin = get_location(request, origin)
|
origin = get_location(request, origin)
|
||||||
|
@ -36,52 +41,79 @@ def main(request, origin=None, destination=None):
|
||||||
if destination is None:
|
if destination is None:
|
||||||
raise Http404
|
raise Http404
|
||||||
|
|
||||||
excludables, includables = get_excludables_includables()
|
include = ()
|
||||||
|
avoid = ()
|
||||||
|
stairs = 'yes'
|
||||||
|
escalators = 'yes'
|
||||||
|
elevators = 'yes'
|
||||||
|
|
||||||
route = None
|
save_settings = False
|
||||||
if request.method in ('GET', 'POST') and origin and destination:
|
if 'c3nav_settings' in request.COOKIES:
|
||||||
graph = Graph.load()
|
cookie_value = request.COOKIES['c3nav_settings']
|
||||||
|
print(cookie_value)
|
||||||
|
|
||||||
allowed_ctypes = ('', )
|
if isinstance(cookie_value, dict):
|
||||||
allowed_ctypes += get_ctypes('stairs', request.POST.get('stairs'))
|
stairs = cookie_value.get('stairs', stairs)
|
||||||
allowed_ctypes += get_ctypes('escalator', request.POST.get('escalators'))
|
escalators = cookie_value.get('escalators', escalators)
|
||||||
allowed_ctypes += get_ctypes('elevator', request.POST.get('elevators'))
|
elevators = cookie_value.get('elevators', elevators)
|
||||||
|
|
||||||
|
if isinstance(cookie_value.get('include'), list):
|
||||||
|
include = cookie_value.get('include')
|
||||||
|
|
||||||
|
if isinstance(cookie_value.get('avoid'), list):
|
||||||
|
avoid = cookie_value.get('avoid')
|
||||||
|
|
||||||
|
save_settings = True
|
||||||
|
|
||||||
|
if request.method in 'POST':
|
||||||
|
stairs = request.POST.get('stairs', stairs)
|
||||||
|
escalators = request.POST.get('escalators', escalators)
|
||||||
|
elevators = request.POST.get('elevators', elevators)
|
||||||
|
|
||||||
include = request.POST.getlist('include')
|
include = request.POST.getlist('include')
|
||||||
avoid = request.POST.getlist('avoid')
|
avoid = request.POST.getlist('avoid')
|
||||||
|
|
||||||
include = set(include) & set(includables)
|
allowed_ctypes = ('', )
|
||||||
avoid = set(avoid) & set(excludables)
|
allowed_ctypes += get_ctypes('stairs', request.POST.get('stairs', stairs))
|
||||||
|
allowed_ctypes += get_ctypes('escalator', request.POST.get('escalators', escalators))
|
||||||
|
allowed_ctypes += get_ctypes('elevator', request.POST.get('elevators', elevators))
|
||||||
|
|
||||||
|
stairs = reverse_ctypes(allowed_ctypes, 'stairs')
|
||||||
|
escalators = reverse_ctypes(allowed_ctypes, 'escalator')
|
||||||
|
elevators = reverse_ctypes(allowed_ctypes, 'elevator')
|
||||||
|
|
||||||
|
excludables, includables = get_excludables_includables()
|
||||||
|
include = set(include) & set(includables)
|
||||||
|
avoid = set(avoid) & set(excludables)
|
||||||
|
|
||||||
|
if request.method in 'POST':
|
||||||
|
save_settings = request.POST.get('save_settings', '') == '1'
|
||||||
|
|
||||||
|
route = None
|
||||||
|
if request.method in 'POST' and origin and destination:
|
||||||
public = ':public' not in avoid
|
public = ':public' not in avoid
|
||||||
nonpublic = ':nonpublic' in include
|
nonpublic = ':nonpublic' in include
|
||||||
|
|
||||||
|
graph = Graph.load()
|
||||||
route = graph.get_route(origin, destination, allowed_ctypes, public=public, nonpublic=nonpublic,
|
route = graph.get_route(origin, destination, allowed_ctypes, public=public, nonpublic=nonpublic,
|
||||||
avoid=avoid-set(':public'), include=include-set(':nonpublic'))
|
avoid=avoid-set(':public'), include=include-set(':nonpublic'))
|
||||||
route = route.split()
|
route = route.split()
|
||||||
route.create_routeparts()
|
route.create_routeparts()
|
||||||
|
|
||||||
if False:
|
|
||||||
filename = os.path.join(settings.RENDER_ROOT, 'base-level-0.png')
|
|
||||||
|
|
||||||
im = Image.open(filename)
|
|
||||||
height = im.size[1]
|
|
||||||
draw = ImageDraw.Draw(im)
|
|
||||||
for connection in route.connections:
|
|
||||||
draw.line(_line_coords(connection.from_point, connection.to_point, height), fill=(255, 100, 100))
|
|
||||||
|
|
||||||
response = HttpResponse(content_type="image/png")
|
|
||||||
im.save(response, "PNG")
|
|
||||||
return response
|
|
||||||
|
|
||||||
width, height = get_dimensions()
|
width, height = get_dimensions()
|
||||||
|
|
||||||
return render(request, 'site/main.html', {
|
response = render(request, 'site/main.html', {
|
||||||
'origin': origin,
|
'origin': origin,
|
||||||
'destination': destination,
|
'destination': destination,
|
||||||
|
|
||||||
|
'stairs': stairs,
|
||||||
|
'escalators': escalators,
|
||||||
|
'elevators': elevators,
|
||||||
'excludables': excludables.items(),
|
'excludables': excludables.items(),
|
||||||
'includables': includables.items(),
|
'includables': includables.items(),
|
||||||
|
'include': include,
|
||||||
|
'avoid': avoid,
|
||||||
|
'save_settings': save_settings,
|
||||||
|
|
||||||
'route': route,
|
'route': route,
|
||||||
'width': width,
|
'width': width,
|
||||||
|
@ -90,6 +122,18 @@ def main(request, origin=None, destination=None):
|
||||||
'svg_height': height*6,
|
'svg_height': height*6,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
if request.method in 'POST' and save_settings:
|
||||||
|
cookie_value = {
|
||||||
|
'stairs': stairs,
|
||||||
|
'escalators': escalators,
|
||||||
|
'elevators': elevators,
|
||||||
|
'include': tuple(include),
|
||||||
|
'avoid': tuple(avoid),
|
||||||
|
}
|
||||||
|
response.set_cookie('c3nav_settings', cookie_value, expires=timezone.now() + timedelta(days=30))
|
||||||
|
|
||||||
|
return response
|
||||||
|
|
||||||
|
|
||||||
def level_image(request, level):
|
def level_image(request, level):
|
||||||
level = get_object_or_404(Level, name=level, intermediate=False)
|
level = get_object_or_404(Level, name=level, intermediate=False)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue