maposmatic-dev
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Maposmatic-dev] [PATCH 03/24] Add by-letter maps pagination


From: Maxime Petazzoni
Subject: [Maposmatic-dev] [PATCH 03/24] Add by-letter maps pagination
Date: Sun, 10 Jan 2010 15:53:57 +0100

Adds a by-letter browsing mechanism for maps, with local by-letter
paginators, accessible by /maps/[A-Z].

Some URL mapping cleanup and Django-isation, too.
---
 www/maposmatic/views.py                |   31 +++++++++++++++++++++++++++++--
 www/media/style.css                    |   18 ++++++++++++++++++
 www/templates/maposmatic/all_maps.html |   16 +++++++++++++---
 www/templates/maposmatic/base.html     |   12 ++++++------
 www/templates/maposmatic/index.html    |    2 +-
 www/templates/maposmatic/job.html      |    2 +-
 www/urls.py                            |   32 +++++++++++++++++++++-----------
 7 files changed, 89 insertions(+), 24 deletions(-)

diff --git a/www/maposmatic/views.py b/www/maposmatic/views.py
index 85c86c7..a071383 100644
--- a/www/maposmatic/views.py
+++ b/www/maposmatic/views.py
@@ -260,10 +260,15 @@ def all_jobs(request):
                               { 'jobs' : jobs },
                               context_instance=RequestContext(request))
 
+def get_letters():
+    # Should we improve this to differenciate letters that have maps from those
+    # who don't?
+    return [chr(i) for i in xrange(ord('A'), ord('Z')+1)]
+
 def all_maps(request):
     map_list = (MapRenderingJob.objects.filter(status=2)
             .filter(resultmsg="ok")
-            .order_by("-submission_time"))
+            .order_by("maptitle"))
     paginator = Paginator(map_list, www.settings.ITEMS_PER_PAGE)
 
     try:
@@ -276,9 +281,31 @@ def all_maps(request):
     except (EmptyPage, InvalidPage):
         maps = paginator.page(paginator.num_pages)
     return render_to_response('maposmatic/all_maps.html',
-                              { 'maps': maps },
+                              { 'maps': maps, 'letters': get_letters() },
                               context_instance=RequestContext(request))
 
+def all_maps_by_letter(request, letter):
+    letter = letter[:1].upper()
+    map_list = (MapRenderingJob.objects.filter(status=2)
+            .filter(resultmsg="ok")
+            .filter(maptitle__startswith=letter)
+            .order_by("maptitle"))
+
+    paginator = Paginator(map_list, www.settings.ITEMS_PER_PAGE)
+
+    try:
+        page = int(request.GET.get('page', '1'))
+    except ValueError:
+        page = 1
+
+    try:
+        maps = paginator.page(page)
+    except (EmptyPage, InvalidPage):
+        maps = paginator.page(paginator.num_pages)
+    return render_to_response('maposmatic/all_maps.html',
+                              { 'maps': maps, 'letters': get_letters(),
+                                'current_letter': letter},
+                              context_instance=RequestContext(request))
 
 def query_nominatim(request, format, squery):
     if not format:
diff --git a/www/media/style.css b/www/media/style.css
index b306f9c..4619976 100644
--- a/www/media/style.css
+++ b/www/media/style.css
@@ -430,3 +430,21 @@ table.jobinfo h4
 {
   margin: 1em 0 0 0;
 }
+
+a.selectedletter
+{
+  font-weight: bold;
+}
+
+div.mapsearch
+{
+  float: right;
+  margin-top: 2em;
+  font-style: italic;
+}
+
+p#letterpagination
+{
+  margin-bottom: 2em;
+  text-align: center;
+}
diff --git a/www/templates/maposmatic/all_maps.html 
b/www/templates/maposmatic/all_maps.html
index 9ecfae3..cc99a1f 100644
--- a/www/templates/maposmatic/all_maps.html
+++ b/www/templates/maposmatic/all_maps.html
@@ -32,19 +32,29 @@ class="activelink"
 {% endblock %}
 
 {% block page %}
-<h1>{% trans "Maps" %}</h1>
+<div class="mapsearch"><!-- <input type="text" name="map_search" /><input 
type="submit" value="{% trans "Search" %}" />-->{% trans "Search coming soon!" 
%}</div>
+
+<h1><a href="{% url maps %}">{% trans "Maps" %}</a></h1>
+
+<p id="letterpagination">
+  {% for l in letters %}<a href="{% url maps-by-letter l %}"{% ifequal l 
current_letter %} class="selectedletter"{% endifequal %}>{{ l }}</a>{% if not 
forloop.last %} - {% endif %}{% endfor %}
+</p>
 
 {% for job in maps.object_list %}
 <div class="job {% cycle jobodd,jobeven %}">
   {% include "maposmatic/map.html" %}
 </div>
+{% empty %}
+<p>
+  {% blocktrans %}No map starts with {{ current_letter }} in our database!{% 
endblocktrans %}
+</p>
 {% endfor %}
 
 {% ifnotequal maps.paginator.num_pages 1 %}
 <div class="pagination">
   <span class="step-links">
     {% if maps.has_previous %}
-      <a href="?page={{ maps.previous_page_number }}">{% trans "Previous" 
%}</a>
+      <a href="?page={{ maps.previous_page_number }}">&laquo; {% trans 
"Previous" %}</a>
     {% endif %}
 
     <span class="current">
@@ -52,7 +62,7 @@ class="activelink"
     </span>
 
     {% if maps.has_next %}
-      <a href="?page={{ maps.next_page_number }}">{% trans "Next" %}</a>
+      <a href="?page={{ maps.next_page_number }}">{% trans "Next" %} 
&raquo;</a>
     {% endif %}
   </span>
 </div>
diff --git a/www/templates/maposmatic/base.html 
b/www/templates/maposmatic/base.html
index 68f1855..f389fe5 100644
--- a/www/templates/maposmatic/base.html
+++ b/www/templates/maposmatic/base.html
@@ -50,11 +50,11 @@
 
  <div id="menu">
   <ul id="nav">
-   <li {% block menu-home %}{% endblock %}><a href="/">{% trans "Home" 
%}</a></li>
-   <li {% block menu-jobs %}{% endblock %}><a href="/jobs">{% trans "Jobs" 
%}</a></li>
-   <li {% block menu-maps %}{% endblock %}><a href="/maps">{% trans "Maps" 
%}</a></li>
+   <li {% block menu-home %}{% endblock %}><a href="{% url main %}">{% trans 
"Home" %}</a></li>
+   <li {% block menu-jobs %}{% endblock %}><a href="{% url jobs %}">{% trans 
"Jobs" %}</a></li>
+   <li {% block menu-maps %}{% endblock %}><a href="{% url maps %}">{% trans 
"Maps" %}</a></li>
    <li><a href="http://news.maposmatic.org";>{% trans "News" %}</a></li>
-   <li {% block menu-about %}{% endblock %}><a href="/about">{% trans "About" 
%}</a></li>
+   <li {% block menu-about %}{% endblock %}><a href="{% url about %}">{% trans 
"About" %}</a></li>
   </ul>
  </div>
 </div>
@@ -69,8 +69,8 @@
   <h2>{% trans "Random map" %}</h2>
   {% if randommap %}
   <p class="randommap">
-    <a href="/jobs/{{randommap.id}}">
-      <img src="{{randommap.get_thumbnail}}"
+    <a href="{% url job-by-id randommap.id %}">
+      <img src="{{ randommap.get_thumbnail }}"
            style="margin-top: 20px; margin-bottom: 20px"/><br />
       <i>{{randommap.maptitle}}</i>
     </a>
diff --git a/www/templates/maposmatic/index.html 
b/www/templates/maposmatic/index.html
index b2baf0c..2026c76 100644
--- a/www/templates/maposmatic/index.html
+++ b/www/templates/maposmatic/index.html
@@ -315,7 +315,7 @@ href="http://wiki.openstreetmap.org/wiki/License";>terms of
 OpenStreetMap license</a>) reuse, sell, modify, ... the generated
 maps.{% endblocktrans %}</p>
 
-<p><a href="/about/">{% trans "More details" %} &raquo;</a></p>
+<p><a href="{% url about %}">{% trans "More details" %} &raquo;</a></p>
 
 <h2 style="clear: right"><a href="#submitmapform">{% trans "Generate your own 
map" %}</a></h2>
 
diff --git a/www/templates/maposmatic/job.html 
b/www/templates/maposmatic/job.html
index a269da6..b6a1fc1 100644
--- a/www/templates/maposmatic/job.html
+++ b/www/templates/maposmatic/job.html
@@ -28,7 +28,7 @@
 
 {% load extratags %}
 
-{% if not single %}<h2 class="jobtitle"><a href="/jobs/{{ job.id }}">{{ 
job.maptitle }}</a></h2>{% endif %}
+{% if not single %}<h2 class="jobtitle"><a href="{% url job-by-id job.id 
%}">{{ job.maptitle }}</a></h2>{% endif %}
 <table class="jobinfo"><tbody><tr>
   <td class="status">
     <img {% if job.is_done_ok %}src="/smedia/job-done.png"{% else %}{% if 
job.is_done_failed %}src="/smedia/job-error.png"{% else %}{% if job.is_waiting 
%}src="/smedia/job-in-queue.png"{% else %}src="/smedia/job-in-progress.png"{% 
endif %}{% endif %}{% endif %} title="{{ 
job.status|job_status_to_str:job.resultmsg }}" />
diff --git a/www/urls.py b/www/urls.py
index 8af2dde..30d8ef6 100644
--- a/www/urls.py
+++ b/www/urls.py
@@ -32,19 +32,29 @@ import maposmatic.views
 import settings
 
 urlpatterns = patterns('',
-    (r'^$', maposmatic.views.index),
-    (r'^smedia/(?P<path>.*)$',
-     'django.views.static.serve',
-     {'document_root': settings.LOCAL_MEDIA_PATH}),
-    (r'^jobs/(?P<job_id>\d+)$', maposmatic.views.job),
-    (r'^jobs/$', maposmatic.views.all_jobs),
-    (r'^maps/$', maposmatic.views.all_maps),
-    (r'^about/$', maposmatic.views.about),
+    url(r'^$', maposmatic.views.index,
+        name='main'),
+    url(r'^about/$', maposmatic.views.about,
+        name='about'),
+
+    url(r'^jobs/(?P<job_id>\d+)$', maposmatic.views.job,
+        name='job-by-id'),
+    url(r'^jobs/$', maposmatic.views.all_jobs,
+        name='jobs'),
+
+    url(r'^maps/(?P<letter>[A-Z])$', maposmatic.views.all_maps_by_letter,
+        name='maps-by-letter'),
+    url(r'^maps/$', maposmatic.views.all_maps,
+        name='maps'),
+
     (r'^nominatim/([^/]*/)?(.*)$', maposmatic.views.query_nominatim),
+
+    # Internationalization
     (r'^i18n/', include('django.conf.urls.i18n')),
 
-    (r'^results/(?P<path>.*)$',
-     'django.views.static.serve',
+    # Static data
+    (r'^results/(?P<path>.*)$', 'django.views.static.serve',
      {'document_root': '/tmp/foo/'}),
-
+    (r'^smedia/(?P<path>.*)$', 'django.views.static.serve',
+     {'document_root': settings.LOCAL_MEDIA_PATH}),
 )
-- 
1.6.3.3.261.g85c6





reply via email to

[Prev in Thread] Current Thread [Next in Thread]