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="form-group col-md-2">
|
||||
<label for="stairs-select">{% trans 'Stairs' %}</label>
|
||||
<select name="stairs" id="stairs-select" class="form-control">
|
||||
<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>
|
||||
{% include 'site/fragment_select.html' with name='stairs' value=stairs %}
|
||||
</div>
|
||||
<div class="form-group col-md-2">
|
||||
<label for="escalators-select">{% trans 'Escalators' %}</label>
|
||||
<select name="escalators" id="escalator-select" class="form-control">
|
||||
<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>
|
||||
{% include 'site/fragment_select.html' with name='escalators' value=escalators %}
|
||||
</div>
|
||||
<div class="form-group col-md-2">
|
||||
<label for="elevators-select">{% trans 'Elevators' %}</label>
|
||||
<select name="elevators" id="elevators-select" class="form-control">
|
||||
<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>
|
||||
{% include 'site/fragment_select.html' with name='elevators' value=elevators %}
|
||||
</div>
|
||||
<div class="form-group col-md-6">
|
||||
<label>{% trans 'Include / Avoid:' %}</label>
|
||||
<label>{% trans 'Include / Avoid areas' %}</label>
|
||||
<p class="form-control-static">
|
||||
{% for name, includable in includables %}
|
||||
<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>
|
||||
{% endfor %}
|
||||
{% for name, excludable in excludables %}
|
||||
<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>
|
||||
{% endfor %}
|
||||
</p>
|
||||
|
@ -61,7 +46,7 @@
|
|||
<div class="form-group col-md-6">
|
||||
<div class="checkbox">
|
||||
<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>
|
||||
</div>
|
||||
</div>
|
||||
|
|
|
@ -1,9 +1,8 @@
|
|||
import os
|
||||
from datetime import timedelta
|
||||
|
||||
from django.conf import settings
|
||||
from django.http import Http404, HttpResponse
|
||||
from django.http import Http404
|
||||
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.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.utils.misc import get_dimensions
|
||||
from c3nav.routing.graph import Graph
|
||||
from c3nav.routing.utils.draw import _line_coords
|
||||
|
||||
ctype_mapping = {
|
||||
'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')))
|
||||
|
||||
|
||||
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):
|
||||
if origin:
|
||||
origin = get_location(request, origin)
|
||||
|
@ -36,52 +41,79 @@ def main(request, origin=None, destination=None):
|
|||
if destination is None:
|
||||
raise Http404
|
||||
|
||||
excludables, includables = get_excludables_includables()
|
||||
include = ()
|
||||
avoid = ()
|
||||
stairs = 'yes'
|
||||
escalators = 'yes'
|
||||
elevators = 'yes'
|
||||
|
||||
route = None
|
||||
if request.method in ('GET', 'POST') and origin and destination:
|
||||
graph = Graph.load()
|
||||
save_settings = False
|
||||
if 'c3nav_settings' in request.COOKIES:
|
||||
cookie_value = request.COOKIES['c3nav_settings']
|
||||
print(cookie_value)
|
||||
|
||||
allowed_ctypes = ('', )
|
||||
allowed_ctypes += get_ctypes('stairs', request.POST.get('stairs'))
|
||||
allowed_ctypes += get_ctypes('escalator', request.POST.get('escalators'))
|
||||
allowed_ctypes += get_ctypes('elevator', request.POST.get('elevators'))
|
||||
if isinstance(cookie_value, dict):
|
||||
stairs = cookie_value.get('stairs', stairs)
|
||||
escalators = cookie_value.get('escalators', escalators)
|
||||
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')
|
||||
avoid = request.POST.getlist('avoid')
|
||||
|
||||
allowed_ctypes = ('', )
|
||||
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
|
||||
nonpublic = ':nonpublic' in include
|
||||
|
||||
graph = Graph.load()
|
||||
route = graph.get_route(origin, destination, allowed_ctypes, public=public, nonpublic=nonpublic,
|
||||
avoid=avoid-set(':public'), include=include-set(':nonpublic'))
|
||||
route = route.split()
|
||||
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()
|
||||
|
||||
return render(request, 'site/main.html', {
|
||||
response = render(request, 'site/main.html', {
|
||||
'origin': origin,
|
||||
'destination': destination,
|
||||
|
||||
'stairs': stairs,
|
||||
'escalators': escalators,
|
||||
'elevators': elevators,
|
||||
'excludables': excludables.items(),
|
||||
'includables': includables.items(),
|
||||
'include': include,
|
||||
'avoid': avoid,
|
||||
'save_settings': save_settings,
|
||||
|
||||
'route': route,
|
||||
'width': width,
|
||||
|
@ -90,6 +122,18 @@ def main(request, origin=None, destination=None):
|
|||
'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):
|
||||
level = get_object_or_404(Level, name=level, intermediate=False)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue