[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Maposmatic-dev] [PATCH maposmatic 1/2] Extend the rendering_already_exi
From: |
Maxime Petazzoni |
Subject: |
[Maposmatic-dev] [PATCH maposmatic 1/2] Extend the rendering_already_exists method |
Date: |
Fri, 14 May 2010 11:57:18 +0200 |
In preparation for the "Recreate Map" feature, extend the
rendering_already_exists method to work on bounding boxes as well,
making it possible to redirect to a recent rendering for the exact same
bounding box.
The original method was split in two distinct methods,
rendering_already_exists_by_osmid and rendering_already_exists_by_bbox,
each working in one of the map selection modes. A new
rendering_already_exists method replaces the old one, now working on on
MapRenderingJob object, and handling the dispatch between the two new
rendering_already_exists_by_{osmid,bbox} methods.
Signed-off-by: Maxime Petazzoni <address@hidden>
---
www/maposmatic/helpers.py | 48 ++++++++++++++++++++++++++++++++++++++++++++-
www/maposmatic/views.py | 11 ++++-----
2 files changed, 52 insertions(+), 7 deletions(-)
diff --git a/www/maposmatic/helpers.py b/www/maposmatic/helpers.py
index 8faa240..0a70e26 100644
--- a/www/maposmatic/helpers.py
+++ b/www/maposmatic/helpers.py
@@ -69,7 +69,7 @@ def check_osm_id(osm_id, table='polygon'):
finally:
conn.close()
-def rendering_already_exists(osmid):
+def rendering_already_exists_by_osmid(osmid):
"""Returns the ID of a rendering matching the given OpenStreetMap city ID
from the last 24 hours, or None if no rendering can be found matching this
criteria."""
@@ -100,6 +100,52 @@ def rendering_already_exists(osmid):
# No rendering found
return None
+def rendering_already_exists_by_bbox(lat_upper_left, lon_upper_left,
+ lat_bottom_right, lon_bottom_right):
+ """Returns the ID of a rendering matching the given bounding box from the
+ last 24 hours, or None if no rendering can be found matching this
+ criteria."""
+
+ # First try to find rendered items
+ rendered_items = (MapRenderingJob.objects
+ .filter(submission_time__gte=(datetime.datetime.now()
+ - datetime.timedelta(1)))
+ .filter(lat_upper_left=lat_upper_left)
+ .filter(lon_upper_left=lon_upper_left)
+ .filter(lat_bottom_right=lat_bottom_right)
+ .filter(lon_bottom_right=lon_bottom_right)
+ .filter(status=2)
+ .filter(resultmsg="ok")
+ .order_by("-submission_time")[:1])
+
+ if len(rendered_items) and rendered_items[0].has_output_files():
+ return rendered_items[0].id
+
+ # Then try to find items being rendered or waiting for rendering
+ rendered_items = (MapRenderingJob.objects
+ .filter(submission_time__gte=(datetime.datetime.now()
+ - datetime.timedelta(1)))
+ .filter(lat_upper_left=lat_upper_left)
+ .filter(lon_upper_left=lon_upper_left)
+ .filter(lat_bottom_right=lat_bottom_right)
+ .filter(lon_bottom_right=lon_bottom_right)
+ .filter(status__in=[0,1])
+ .order_by("-submission_time")[:1])
+
+ if len(rendered_items):
+ return rendered_items[0].id
+
+ # No rendering found
+ return None
+
+def rendering_already_exists(job):
+ if job.administrative_osmid:
+ return rendering_already_exists_by_osmid(job.administrative_osmid)
+ return rendering_already_exists_by_bbox(job.lat_upper_left,
+ job.lon_upper_left,
+ job.lat_bottom_right,
+ job.lon_bottom_right)
+
def get_letters():
"""Returns the list of map first-letter selectors. For now, it only returns
the ASCII letters of the latin alphabet. This should be improved for
diff --git a/www/maposmatic/views.py b/www/maposmatic/views.py
index 76f7684..5cc02ae 100644
--- a/www/maposmatic/views.py
+++ b/www/maposmatic/views.py
@@ -70,12 +70,11 @@ def new(request):
job = form.save(commit=False)
job.administrative_osmid =
form.cleaned_data.get('administrative_osmid')
- if job.administrative_osmid:
- existing =
helpers.rendering_already_exists(job.administrative_osmid)
- if existing:
- request.session['redirected'] = True
- return HttpResponseRedirect(reverse('job-by-id',
- args=[existing]))
+ existing = helpers.rendering_already_exists(job)
+ if existing:
+ request.session['redirected'] = True
+ return HttpResponseRedirect(reverse('job-by-id',
+ args=[existing]))
job.status = 0 # Submitted
job.submitterip = request.META['REMOTE_ADDR']
--
1.6.3.3.341.g9b22d
- [Maposmatic-dev] [PATCH maposmatic 1/2] Extend the rendering_already_exists method,
Maxime Petazzoni <=