source: src/django_gheat/gheat/views.py@ 9092

Last change on this file since 9092 was 9041, checked in by dennisw, 14 years ago

manage.py kan nu worden gerunned vanuit project root met './manage.py'
settings, models en schemes aangepast

File size: 2.2 KB
RevLine 
[9006]1import os.path
2from django.http import HttpResponseRedirect
3from gheat import dots
4from gheat import backend, color_schemes, translate, ROOT, log, \
5 ALWAYS_BUILD
6
7from django.http import HttpResponseBadRequest
8from django.conf import settings
9from django.views.static import serve
10
11# Create your views here.
12def serve_tile(request,color_scheme,zoom,x,y):
13 '''
14 Responsible for serving png files of the tile for the heat map
15
16 This view will try to serve the file from the filesystem in case already
17 exists otherwise just try to genereate it, and serve it.
18 '''
19
20 # Asserting request is a correct one
21 try:
22 assert color_scheme in color_schemes, ( "bad color_scheme: "
23 + color_scheme
24 )
25 assert zoom.isdigit() and x.isdigit() and y.isdigit(), "not digits"
26 zoom = int(zoom)
27 x = int(x)
28 y = int(y)
[9041]29 assert 0 <= zoom <= 17, "bad zoom: %d" % zoom
[9006]30 except AssertionError, err:
31 return HttpResponseBadRequest()
32
33 # @TODO: We should return the file in case is already present
34 # Also we have to implement a redirection to the front end in case we are not in debug mode ... should we ?
35
36 fspath = generate_tile(request,color_scheme,zoom,x,y)
37
38 if settings.DEBUG:
39 return serve(request, fspath, '/')
40 else:
41 return HttpResponseRedirect(fspath.replace(ROOT, '/site_media/gheat/'))
42
43
44def generate_tile(request,color_scheme,zoom,x,y):
45 '''
46 This view will generate the png file for the current request
47 '''
48 path = request.path
49
50 path = path[path.index(color_scheme)-1:] # Removing the /gheat/ from the url
51
52 fspath = translate(ROOT, path)
53
54 if os.path.exists(fspath):
55 return fspath
56
57 color_scheme = color_schemes[color_scheme]
58 tile = backend.Tile(color_scheme, dots, zoom, x, y, fspath)
59 if tile.is_empty():
60 fspath = color_scheme.get_empty_fspath(zoom)
61 log.debug('serving empty tile, request: %s, file %s' % (path,fspath))
62 elif tile.is_stale() or ALWAYS_BUILD:
63 log.debug('rebuilding %s' % path)
64 tile.rebuild()
65 tile.save()
66 else:
67 log.debug('serving cached tile %s' % path)
68
69 return fspath
Note: See TracBrowser for help on using the repository browser.