]> git.parisson.com Git - telemeta.git/commitdiff
reduce geo navigator loading time, counting related resources on demand ; display...
authorolivier <>
Mon, 8 Feb 2010 16:14:23 +0000 (16:14 +0000)
committerolivier <>
Mon, 8 Feb 2010 16:14:23 +0000 (16:14 +0000)
12 files changed:
telemeta/htdocs/js/resourcemap.js
telemeta/models/cremquery.py
telemeta/templates/telemeta/country_info.html [new file with mode: 0644]
telemeta/templates/telemeta/geo_country_items.html [new file with mode: 0644]
telemeta/templates/telemeta_default/country_info.html [new file with mode: 0644]
telemeta/templates/telemeta_default/geo_continents.html
telemeta/templates/telemeta_default/geo_country_items.html [new file with mode: 0644]
telemeta/templates/telemeta_default/inc/collection_list.html
telemeta/templates/telemeta_default/inc/mediaitem_list.html
telemeta/templatetags/telemeta_utils.py
telemeta/urls.py
telemeta/web/base.py

index f3cd29cd9bfd343fc7b96087d6a3bb05147392df..0fcd16a52105bcbbd3f935d914e02b9d95a33a61 100644 (file)
@@ -5,6 +5,7 @@ function ResourceMap(list, cfg) {
     that.map       = null;
 
     that.init = function(list, cfg) {
+        that.cfg = cfg;
         $(document).ready(function() {
             that.log("init");
             that.list = $(list);
@@ -66,6 +67,18 @@ function ResourceMap(list, cfg) {
         return info.wrap('<div/>').parent().html();
     }
 
+    that.showResourceInfo = function(marker, resourceElement) {
+        var info = $('<div/>').addClass('resourcemap-info');
+        marker.openInfoWindowHtml(info.get(0));
+        var re  = /^resource-/;
+        var id  = resourceElement.attr('id').replace(re, '');
+        var uri = that.cfg.countryInfoUri.replace('RESOURCEID', id);
+        
+        $.get(uri, function(data) {
+            info.html(data);
+        });
+    }
+
     that.parseResources = function() {
         $('.resourcemap-element').each(function(i, e) {
             e = $(e)
@@ -73,13 +86,14 @@ function ResourceMap(list, cfg) {
             if (input.length) {
                 var lat       = parseFloat(input.attr('value'));
                 var lng       = parseFloat(e.find('.resourcemap-lng').attr('value'));
-                var name      = $.trim(e.find('.resourcemap-name').text());
-                var link      = e.find('a').attr('href');
-                var linktitle = e.find('a').attr('title');
+                //var name      = $.trim(e.find('.resourcemap-name').text());
+                //var link      = e.find('a').attr('href');
+                //var linktitle = e.find('a').attr('title');
                 var marker    = new google.maps.Marker(new GLatLng(lat, lng), {title: name});
-                var info      = that.makeInfoBox(name, link, linktitle);
+                //var info      = that.makeInfoBox(name, link, linktitle);
                 google.maps.Event.addListener(marker, "click", function() {
-                    marker.openInfoWindowHtml(info);
+                    that.showResourceInfo(marker, e);
+                    //marker.openInfoWindowHtml(info);
                 });
                 that.map.addOverlay(marker);
             }
index fd4d42e3e0b93fbbfdeeaa7b9824bf1776bcfb3b..bb13949e95a9bc8f0605bbd91decc9ef771fdc78 100644 (file)
@@ -251,7 +251,41 @@ class MediaItemQuerySet(CoreQuerySet):
         from telemeta.models import LocationRelation
         descendants = LocationRelation.objects.filter(ancestor_location=location)
         return self.filter(Q(location=location) | Q(location__in=descendants))
+           
+    @staticmethod
+    def __name_cmp(obj1, obj2):
+        return unaccent_icmp(obj1.name, obj2.name)
+
+    def countries(self, group_by_continent=False):
+        from telemeta.models import Location
+        countries = []
+        for id in self.filter(location__isnull=False).values_list('location', flat=True).distinct():
+            location = Location.objects.get(pk=id)
+            for l in location.countries():
+                if not l in countries:
+                    countries.append(l)
+
+        if group_by_continent:
+            grouped = {}
+
+            for country in countries:
+                for continent in country.continents():
+                    if not grouped.has_key(continent):
+                        grouped[continent] = []
+
+                    grouped[continent].append(country)
+                    
+            keys = grouped.keys()
+            keys.sort(self.__name_cmp)
+            ordered = []
+            for c in keys:
+                grouped[c].sort(self.__name_cmp)
+                ordered.append({'continent': c, 'countries': grouped[c]})
             
+            countries = ordered
+            
+        return countries                    
+
 class MediaItemManager(CoreManager):
     "Manage media items queries"
 
diff --git a/telemeta/templates/telemeta/country_info.html b/telemeta/templates/telemeta/country_info.html
new file mode 100644 (file)
index 0000000..15159c6
--- /dev/null
@@ -0,0 +1,2 @@
+{% extends "telemeta_default/country_info.html" %}
+
diff --git a/telemeta/templates/telemeta/geo_country_items.html b/telemeta/templates/telemeta/geo_country_items.html
new file mode 100644 (file)
index 0000000..ca2104f
--- /dev/null
@@ -0,0 +1 @@
+{% extends "telemeta_default/geo_country_items.html" %}
diff --git a/telemeta/templates/telemeta_default/country_info.html b/telemeta/templates/telemeta_default/country_info.html
new file mode 100644 (file)
index 0000000..feaf87e
--- /dev/null
@@ -0,0 +1,11 @@
+{% load telemeta_utils %}
+{% load i18n %}
+
+<h2>{{ country }}</h2>
+<a href="{% url telemeta-geo-country-items continent.flatname,country.flatname %}">
+{{ country.items|resources_num }}
+</a>
+{% trans "in" %}
+<a href="{% url telemeta-geo-country-collections continent.flatname,country.flatname %}">
+{{ country.collections|resources_num }}
+</a>
index 4f60f5bfa4157f96d72e20a4c5904ac99e043d2e..8a6867aa4e974da2a8e1449b9f6e9e98cdd6954c 100644 (file)
@@ -7,7 +7,9 @@
 <script src="http://www.google.com/jsapi?key={{ gmap_key }}" type="text/javascript"></script>
 <script src="{% url telemeta-js "resourcemap.js" %}" type="text/javascript"></script>
 <script type="text/javascript">
-var resourceMap = new ResourceMap('.continents');
+var resourceMap = new ResourceMap('.continents', {
+    'countryInfoUri': '{% url telemeta-country-info "RESOURCEID" %}'
+});
 </script>
 {% endif %}
 {% endblock %}
@@ -22,22 +24,21 @@ var resourceMap = new ResourceMap('.continents');
 <h3>Geographic Navigator</h3>
 {% if continents %}
 <ul class="continents">
-{% for continent in continents %}
-  <li class="name"><b><a href="{% url telemeta-geo-countries continent.location.flatname %}">{{ continent.location }}</a></b>
+{% for group in continents %}
+  <li class="name"><b><a href="{% url telemeta-geo-countries group.continent.flatname %}">{{ group.continent }}</a></b>
     <ul>
-    {% for country in continent.countries %}
-      <li class="country_name resourcemap-element">
-        <a href="{% url telemeta-geo-country-collections continent.location.flatname,country.location.flatname %}"
-           title="{{ country.count }} {% trans "collections" %}">
-          <span class="resourcemap-name">{{ country.location }}</span></a>
-        {% if not country.location.latitude|is_none and not country.location.longitude|is_none %}
-        <input type="hidden" class="resourcemap-lat" value="{{country.location.latitude}}" />
-        <input type="hidden" class="resourcemap-lng" value="{{country.location.longitude}}" />
+    {% for country in group.countries %}
+      <li id="resource-{{country.id}}" class="country_name resourcemap-element">
+        <a href="{% url telemeta-geo-country-collections group.continent.flatname,country.flatname %}">
+          <span class="resourcemap-name">{{ country }}</span></a>
+        {% if not country.latitude|is_none and not country.longitude|is_none %}
+        <input type="hidden" class="resourcemap-lat" value="{{country.latitude}}" />
+        <input type="hidden" class="resourcemap-lng" value="{{country.longitude}}" />
         {% endif %}
       </li>
     {% endfor %}
-    {% if continent.countries.10 %}
-    <li><a href="{% url telemeta-geo-countries continent.location.flatname %}">More..</a></li>
+    {% if group.countries.10 %}
+    <li><a href="{% url telemeta-geo-countries group.continent.flatname %}">More..</a></li>
     {% endif %}
     </ul>
   </li>
diff --git a/telemeta/templates/telemeta_default/geo_country_items.html b/telemeta/templates/telemeta_default/geo_country_items.html
new file mode 100644 (file)
index 0000000..fae0887
--- /dev/null
@@ -0,0 +1,14 @@
+{% extends "telemeta/base.html" %}
+{% load telemeta_utils %}
+{% load i18n %}
+
+{% block content %}
+<h3><a href="{% url telemeta-geo-continents %}">{% trans "World" %}</a> /
+  <a href="{% url telemeta-geo-countries continent.flatname %}">{{ continent }}</a> 
+  / {{ country }}</h3>
+
+{% with object_list as items %}
+{% include "telemeta/inc/mediaitem_list.html" %}
+{% endwith %}
+
+{% endblock %}
index c666ddb23dd7233448426a41e1fd99655519221a..ab0841e585c5f69a5a8ceacdf3173ea634c50f86 100644 (file)
@@ -1,7 +1,7 @@
 {% load telemeta_utils %}
 {% if collections %}
     {% if hits %}
-    <p class="pagination">Records {{ first_on_page }} to {{ last_on_page }} on {{ hits }} 
+    <p class="pagination">Collections {{ first_on_page }} to {{ last_on_page }} on {{ hits }} 
     &nbsp;&nbsp;
     {% if has_previous %} <a href="?page={{ previous }}&amp;{{criteria|build_query_string}}">&lt;&lt;</a> {% endif %}
     {% if has_next %} <a href="?page={{ next }}&amp;{{criteria|build_query_string}}">&gt;&gt;</a> {% endif %}
index ed9ba755a1f45410cec7d3bf40afa35552707e52..493a6f5401549d87f3d6e5aa552613462f078a01 100644 (file)
@@ -1,7 +1,7 @@
 {% load telemeta_utils %}
 {% if items %}
     {% if hits %}
-    <p class="pagination">Records {{ first_on_page }} to {{ last_on_page }} on {{ hits }} 
+    <p class="pagination">Items {{ first_on_page }} to {{ last_on_page }} on {{ hits }} 
     &nbsp;&nbsp;
     {% if has_previous %} <a href="?page={{ previous }}&amp;{{criteria|build_query_string}}">&lt;&lt;</a> {% endif %}
     {% if has_next %} <a href="?page={{ next }}&amp;{{criteria|build_query_string}}">&gt;&gt;</a> {% endif %}
index 61b61cecae48df44bccd2d126ffeb500490f6137..6223703e21ab98c086eef568ba139a24025e478b 100644 (file)
@@ -6,6 +6,8 @@ import telemeta.models.dublincore as dc
 from django.utils import html
 from django import template
 from django.utils.text import capfirst
+from telemeta import models
+from django.utils.translation import ungettext
 
 register = template.Library()
 
@@ -172,3 +174,18 @@ def field_label(model, field):
 @register.filter
 def is_none(value):
     return value is None
+
+@register.filter
+def resources_num(value):
+    model = value.model
+    count = value.count()
+    label = str(count)
+    if model == models.MediaItem:
+        label = ungettext('%(count)d item', '%(count)d items', count) % {
+            'count': count, }
+    elif model == models.MediaCollection:
+        label = ungettext('%(count)d collection', '%(count)d collections', count) % {
+            'count': count, }
+
+    return label
+    
index 711ae65c8ec7347cb3733390a26c73a60deeb2a5..51668b0e0ebcceeda79f3ac8d2e79d117c2491b9 100644 (file)
@@ -138,10 +138,15 @@ urlpatterns = patterns('',
     url(r'^geo/$', web_view.list_continents, name="telemeta-geo-continents"),
     url(r'^geo/(?P<continent>[a-z_]+)/$', web_view.list_countries, 
         name="telemeta-geo-countries"),
-    url(r'^geo/(?P<continent>[a-z_]+)/(?P<country>[a-z_]+)/$', 
+    url(r'^geo/collections/(?P<continent>[a-z_]+)/(?P<country>[a-z_]+)/$', 
         web_view.list_country_collections, 
         name="telemeta-geo-country-collections"),
+    url(r'^geo/items/(?P<continent>[a-z_]+)/(?P<country>[a-z_]+)/$', 
+        web_view.list_country_items, 
+        name="telemeta-geo-country-items"),
     url(r'^dynjs/continents.js$', web_view.get_continents_js, name="telemeta-continents-js"),
+    url(r'^geo/country_info/(?P<id>[0-9A-Z]+)/$', 
+        web_view.country_info, name="telemeta-country-info"),
 
     # CSS+Images (FIXME: for developement only)
     url(r'^css/(?P<path>.*)$', 'django.views.static.serve', 
index deafece654b14b7cec98537d7c68d770ab815ca5..9236ad7e14dbec30298da742067299bddb64e076 100644 (file)
@@ -337,10 +337,15 @@ class WebView(Component):
         return HttpResponse(template.render(context), mimetype=mimetype)
 
     def list_continents(self, request):
-        continents = MediaCollection.objects.stat_continents()
+        continents = MediaItem.objects.all().countries(group_by_continent=True)
         return render_to_response('telemeta/geo_continents.html', 
                     {'continents': continents, 'gmap_key': settings.TELEMETA_GMAP_KEY })
 
+    def country_info(self, request, id):
+        country = Location.objects.get(pk=id)
+        return render_to_response('telemeta/country_info.html', {
+            'country': country, 'continent': country.continents()[0]})
+
     def get_continents_js(self, request):
         countries = MediaCollection.objects.list_countries()
         return render_to_response('telemeta/geo_continents.js', 
@@ -360,6 +365,14 @@ class WebView(Component):
             template_name='telemeta/geo_country_collections.html', paginate_by=20,
             extra_context={'country': country, 'continent': continent})
 
+    def list_country_items(self, request, continent, country):
+        continent = Location.objects.by_flatname(continent)[0]
+        country = Location.objects.by_flatname(country)[0]
+        objects = MediaItem.objects.by_location(country)
+        return list_detail.object_list(request, objects, 
+            template_name='telemeta/geo_country_items.html', paginate_by=20,
+            extra_context={'country': country, 'continent': continent})
+
     def handle_oai_request(self, request):
         url         = 'http://' + request.META['HTTP_HOST'] + request.path
         datasource  = TelemetaOAIDataSource()