team-3/src/c3nav/site/views.py

79 lines
2.3 KiB
Python
Raw Normal View History

import os
2016-12-15 17:30:55 +01:00
from django.conf import settings
from django.http import HttpResponse
from django.shortcuts import get_object_or_404, redirect, render
from PIL import Image, ImageDraw
from c3nav.mapdata.models import Level
from c3nav.mapdata.models.locations import get_location
from c3nav.mapdata.render.compose import composer
from c3nav.mapdata.utils.misc import get_dimensions
2016-12-16 17:38:13 +01:00
from c3nav.routing.graph import Graph
from c3nav.routing.utils.draw import _line_coords
2016-12-15 17:30:55 +01:00
2016-12-15 18:28:04 +01:00
def main(request, origin=None, destination=None):
do_redirect = False
if origin:
origin_obj = get_location(request, origin)
if origin_obj.name != origin:
do_redirect = True
origin = origin_obj
if destination:
destination_obj = get_location(request, destination)
if destination_obj.name != destination:
do_redirect = True
destination = destination_obj
if do_redirect:
new_url = '/'
if origin:
new_url += origin.name+'/'
if destination:
new_url += destination.name + '/'
elif destination:
new_url += '_/' + destination.name + '/'
redirect(new_url)
route = None
2016-12-16 17:38:13 +01:00
if origin and destination:
graph = Graph.load()
route = graph.get_route(origin, destination, ('', 'steps_down', 'steps_up', 'elevator_down', 'elevator_up'))
print(route)
route = route.split()
print(route)
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))
2016-12-16 17:38:13 +01:00
response = HttpResponse(content_type="image/png")
im.save(response, "PNG")
return response
width, height = get_dimensions()
2016-12-16 17:38:13 +01:00
2016-12-15 18:28:04 +01:00
return render(request, 'site/main.html', {
'origin': origin,
'destination': destination,
'route': route,
'width': width,
'height': height,
'svg_width': width*6,
'svg_height': height*6,
2016-12-15 18:28:04 +01:00
})
def level_image(request, level):
level = get_object_or_404(Level, name=level, intermediate=False)
return composer.get_level_image(request, level)