source: subversion/sites/other/tilesAtHome_tahngo/browse/views.py @ 10579

Last change on this file since 10579 was 10328, checked in by spaetz, 11 years ago

don't let http://tah.openstreetmap.org/Browse/details/ return an error 500. Spotted by Marteen.

File size: 3.6 KB
Line 
1import os
2from struct import unpack, calcsize
3from datetime import datetime
4from django.contrib.auth.models import User
5from django.db.models import Q
6from django.conf import settings
7from django.shortcuts import render_to_response
8from django.http import HttpResponse
9from tah.requests.models import Request
10from tah.requests.forms import CreateForm
11from tah.tah_intern.Tile import Tile
12from tah.tah_intern.Tileset import Tileset
13from tah.tah_intern.models import Layer
14
15
16def index(request):
17  layer =  request.GET.get("layer","tile")
18  z     =  int(request.GET.get("z",2))
19  x     =  int(request.GET.get("x",2))
20  y     =  int(request.GET.get("y",1))
21
22  t=Tile(layer,z,x,y)
23  if not t.is_valid():
24    return render_to_response('base_errormessage.html',{'header': 'Browse map','reason':'The tile coordinates were invalid. Please check zoom, x, and y values.'})
25  (layer, base_z,base_x,base_y) = t.basetileset()
26  RequestForm = CreateForm({'min_z': base_z, 'x': base_x, 'y': base_y, 'priority': 1, 'src': 'browse map request'})
27
28  return render_to_response('base_browse.html',{'layer': layer,'z':z, 'x':x, 'y':y, 'x_range': range(x-1,x+2), 'y_range': range(y-1,y+2), 'RequestForm': RequestForm, 'base_z': base_z})
29
30def slippymap(request):
31  layer =  request.GET.get("layer","tile")
32  z     =  int(request.GET.get("z",2))
33  x     =  int(request.GET.get("x",1))
34  y     =  int(request.GET.get("y",1))
35
36
37  return render_to_response('base_browse_slippy.html',{'layer': layer,'z':z, 'x':x, 'y':y})
38
39def tiledetails_base(request):
40 return tiledetails(request,'tile',2,1,1)
41
42def tiledetails(request,layername,z,x,y):
43  z=int(z)
44  x=int(x)
45  y=int(y)
46  userid=None
47  layer = Layer.objects.get(name=layername)
48  t=Tile(layer,z,x,y)
49  # bail out if the tile coordinates were not valid.
50  if not t.is_valid():
51    return render_to_response('base_errormessage.html',{'header': 'Tile detail view','reason':'The tile coordinates were invalid. Please check zoom, x, and y values.'})
52
53  (layer, base_z,base_x,base_y) = t.basetileset()
54  # basetileset returns (None,None,None,None) if invalid
55  (tilepath, tilefile) = Tileset(layer, base_z, base_x, base_y).get_filename(settings.TILES_ROOT)
56  tilefile = os.path.join(tilepath, tilefile)
57  try: 
58    fstat = os.stat(tilefile)
59    (basetile_fsize,basetile_mtime) = (fstat[6], datetime.fromtimestamp(fstat[8]))
60    f=open(tilefile, 'rb')
61    FILEVER, userid = unpack('BI',f.read(calcsize("BI")))
62    f.close()
63  except OSError: 
64    (basetile_fsize,basetile_mtime) = None, None
65
66  if userid != None: 
67    try: user = User.objects.get(pk=userid)
68    except User.DoesNotExist: user = "Nonexistent User ID %d" % userid
69  else: user='Unknown'
70
71  #reqs = Request.objects.filter((Q(status__lt=2,layers__id__exact=1)|Q(status=2)),min_z=base_z,x=base_x,y=base_y).distinct().order_by('status','-request_time')
72  reqs = Request.objects.filter(min_z=base_z,x=base_x,y=base_y).order_by('status','-request_time')
73
74  #Use regular HTML template, or short machine readable version?
75  if request.GET.get('format','') == 'short':
76    template = 'tile_details_machine.html'
77    RequestForm = None
78  else:
79    template = 'tile_details.html'
80    RequestForm = CreateForm({'min_z': base_z, 'x': base_x, 'y': base_y, 'priority': 1, 'src':'tiledetail page request'})
81  return render_to_response(template,{'tile':t,'basetile_fsize':basetile_fsize,'basetile_mtime':basetile_mtime, 'user': user, 'reqs':reqs, 'RequestForm': RequestForm, 'base_z': base_z})
82
83def show_map_of(request):
84  return HttpResponse("not implemented",mimetype="text/plain")
85
86def serve(request,layername,z,x,y):
87  return HttpResponse(serve_tile(layername,z,x,y),mimetype="image/png")
Note: See TracBrowser for help on using the repository browser.