from django import http
from django.shortcuts import render, redirect
from qmsk.pngtile import pngtile
import pypngtile
import json
import os.path
def index (request, name):
store = pngtile.STORE
try:
tree = store.tree(name)
items = store.items(name)
except pngtile.NotFound:
raise http.Http404
except pngtile.InvalidImage:
return http.HttpResponseBadRequest(name)
except pngtile.UncachedImage:
return http.HttpResponseBadRequest(name)
except pngtile.Error as error:
return http.HttpResponseServerError(str(error))
except pypngtile.Error as error:
return http.HttpResponseServerError(str(error))
return render(request, 'qmsk.pngtile/index.html', dict(
site_name = u"...",
name = name,
title = store.title(name),
breadcrumb = store.breadcrumb(name),
tree = tree,
index_items = sorted(items),
))
def image (request, name):
store = pngtile.STORE
try:
image, name = store.open(name)
tree = store.tree(os.path.dirname(name))
except pngtile.NotFound:
raise http.Http404
except pngtile.InvalidImage:
return http.HttpResponseBadRequest(name)
except pngtile.UncachedImage:
return http.HttpResponseBadRequest(name)
except pngtile.Error as error:
return http.HttpResponseServerError(str(error))
except pypngtile.Error as error:
return http.HttpResponseServerError(str(error))
image_info = image.info()
map_config = dict(
tiles_url = store.tiles_url(name),
tiles_mtime = 0,
tile_url = '{tiles_url}?t={tiles_mtime}&x={x}&y={y}&zoom={z}',
tile_size = store.TILE_SIZE,
tile_zoom = store.TILE_ZOOM,
image_url = '{tiles_url}?w={w}&h={h}&x={x}&y={y}&zoom={z}',
image_width = image_info['img_width'],
image_height = image_info['img_height'],
)
return render(request, 'qmsk.pngtile/image.html', dict(
site_name = u"...",
name = name,
title = store.title(name),
breadcrumb = store.breadcrumb(name),
tree = tree,
map_config = json.dumps(map_config),
))