Changeset fc6b123
- Timestamp:
- 01/27/10 22:46:14 (3 years ago)
- Branches:
- master, crem, crem2, dev, dev2, diadems, forma, generic, instru_search, lam, nlivemulti, production, release/1.4.4, security, social, storage, test, video
- Children:
- b3ffdab
- Parents:
- 01268f9
- git-author:
- olivier <> (01/27/10 22:46:14)
- git-committer:
- olivier <> (01/27/10 22:46:14)
- Location:
- telemeta
- Files:
-
- 7 edited
-
models/__init__.py (modified) (1 diff)
-
models/cremquery.py (modified) (3 diffs)
-
templates/telemeta_default/geo_continents.html (modified) (1 diff)
-
templates/telemeta_default/geo_countries.html (modified) (1 diff)
-
templates/telemeta_default/geo_country_collections.html (modified) (1 diff)
-
urls.py (modified) (1 diff)
-
web/base.py (modified) (4 diffs)
Legend:
- Unmodified
- Added
- Removed
-
telemeta/models/__init__.py
r7308fad rfc6b123 37 37 # PhysicalFormat, PublishingStatus 38 38 39 from django.db.models.signals import post_syncdb 40 41 def syncdb_callback(sender, **kwargs): 42 from django.db import connection 43 import _mysql_exceptions 44 cursor = connection.cursor() 45 print "Creating MySQL stored procedure" 46 try: 47 cursor.execute("DROP FUNCTION IF EXISTS telemeta_location_ascendant") 48 except _mysql_exceptions.Warning: 49 pass 50 try: 51 cursor.execute(""" 52 CREATE FUNCTION telemeta_location_ascendant(loc CHAR(150), asc_type CHAR(16)) 53 RETURNS CHAR(150) 54 READS SQL DATA 55 BEGIN 56 DECLARE t, n CHAR(150); 57 DECLARE c INT; 58 SELECT COUNT(*) INTO c FROM locations WHERE name = loc; 59 IF c = 0 THEN 60 RETURN NULL; 61 END IF; 62 SELECT name, type INTO n, t FROM locations WHERE name = loc; 63 WHILE t <> asc_type DO 64 SELECT COUNT(*) INTO c FROM location_relations WHERE location_name = n; 65 IF c = 0 THEN 66 RETURN NULL; 67 END IF; 68 SELECT parent_location_name INTO n FROM location_relations WHERE location_name = n LIMIT 1; 69 SELECT type INTO t FROM locations WHERE name = n; 70 END WHILE; 71 RETURN n; 72 END""") 73 except _mysql_exceptions.Warning: 74 pass 75 76 post_syncdb.connect(syncdb_callback) 77 78 -
telemeta/models/cremquery.py
ra2e58a9 rfc6b123 34 34 # David LIPSZYC <davidlipszyc@gmail.com> 35 35 36 from django import db 36 37 from django.db.models import Manager, Q 37 38 from telemeta.models.core import EnhancedQuerySet, EnhancedManager 38 39 import re 39 40 from django.core.exceptions import ObjectDoesNotExist 41 from django import db 42 import _mysql_exceptions 40 43 41 44 class CoreQuerySet(EnhancedQuerySet): … … 103 106 def by_country(self, country): 104 107 "Find collections by country" 105 return self.filter(items__location__type="country", items__location=country).distinct() 108 db.connection.cursor() # Need this to establish connection 109 country = db.connection.connection.literal(country) 110 return self.extra(where=["media_items.collection_id = media_collections.id", 111 "telemeta_location_ascendant(media_items.location_name, 'country') = %s" % country], 112 tables=['media_items']).distinct() 106 113 107 114 def by_continent(self, continent): … … 166 173 by_change_time.__doc__ = MediaCollectionQuerySet.by_change_time.__doc__ 167 174 168 def stat_continents(self, order_by='n um'):175 def stat_continents(self, order_by='nitems'): 169 176 "Return the number of collections by continents and countries as a tree" 170 177 from django.db import connection 171 178 cursor = connection.cursor() 172 if order_by == 'n um':179 if order_by == 'nitems': 173 180 order_by = 'items_num DESC' 174 else: 175 order_by = 'etat' 176 cursor.execute("SELECT continent, etat, count(*) AS items_num " 177 "FROM media_collections INNER JOIN media_items " 178 "ON media_collections.id = media_items.collection_id " 179 "WHERE (continent IN " 180 " ('EUROPE', 'OCEANIE', 'ASIE', 'AMERIQUE', 'AFRIQUE')) " 181 "AND etat <> '' " 182 "GROUP BY etat ORDER BY continent, " + order_by) 181 elif order_by != 'country': 182 raise Exception("stat_continents() can only order by nitems or country") 183 184 try: 185 cursor.execute(""" 186 SELECT telemeta_location_ascendant(location_name, 'continent') as continent, 187 telemeta_location_ascendant(location_name, 'country') as country, 188 count(*) AS items_num 189 FROM media_collections INNER JOIN media_items 190 ON media_collections.id = media_items.collection_id 191 GROUP BY country ORDER BY continent, """ + order_by) 192 except _mysql_exceptions.Warning: 193 pass 183 194 result_set = cursor.fetchall() 184 195 stat = {} 185 196 for continent, country, count in result_set: 186 if stat.has_key(continent): 187 stat[continent].append({'name':country, 'count':count}) 188 else: 189 stat[continent] = [{'name':country, 'count':count}] 197 if continent and country: 198 if stat.has_key(continent): 199 stat[continent].append({'name':country, 'count':count}) 200 else: 201 stat[continent] = [{'name':country, 'count':count}] 190 202 191 203 keys = stat.keys() -
telemeta/templates/telemeta_default/geo_continents.html
r9d3f1f3 rfc6b123 7 7 <ul class="continents"> 8 8 {% for continent in continents %} 9 <li class="name"><b><a href="{% url telemeta-geo-countries continent. name|urlencode %}">{{ continent.name }}</a></b>9 <li class="name"><b><a href="{% url telemeta-geo-countries continent.flatname %}">{{ continent.name }}</a></b> 10 10 <ul> 11 11 {% for country in continent.countries|slice:":10" %} 12 12 <li class="country_name"> 13 <a href="{% url telemeta-geo-country-collections continent. name,country.name|urlencode %}">13 <a href="{% url telemeta-geo-country-collections continent.flatname,country.flatname %}"> 14 14 {{ country.name|lower|capfirst }}</a></li> 15 15 {% endfor %} 16 16 {% if continent.countries.10 %} 17 <li><a href="{% url telemeta-geo-countries continent. name|urlencode %}">More..</a></li>17 <li><a href="{% url telemeta-geo-countries continent.flatname %}">More..</a></li> 18 18 {% endif %} 19 19 </ul> -
telemeta/templates/telemeta_default/geo_countries.html
r9d3f1f3 rfc6b123 1 1 {% extends "telemeta/base.html" %} 2 2 {% load telemeta_utils %} 3 {% load i18n %} 3 4 4 5 {% block content %} 5 <h3><a href="{% url telemeta-geo-continents %}"> WORLD</a> /6 <h3><a href="{% url telemeta-geo-continents %}">{% trans "World" %}</a> / 6 7 {{ continent.name }}</h3> 7 8 <ul> 8 9 {% for country in continent.countries %} 9 <li><a href="{% url telemeta-geo-country-collections continent. name,country.name|urlencode %}">10 <li><a href="{% url telemeta-geo-country-collections continent.flatname,country.flatname %}"> 10 11 {{ country.name|lower|capfirst }} ({{ country.count }})</a></li> 11 12 {% endfor %} -
telemeta/templates/telemeta_default/geo_country_collections.html
r9d3f1f3 rfc6b123 1 1 {% extends "telemeta/base.html" %} 2 2 {% load telemeta_utils %} 3 {% load i18n %} 3 4 4 5 {% block content %} 5 <h3><a href="{% url telemeta-geo-continents %}"> WORLD</a> /6 <a href="{% url telemeta-geo-countries continent |urlencode %}">{{ continent }}</a>6 <h3><a href="{% url telemeta-geo-continents %}">{% trans "World" %}</a> / 7 <a href="{% url telemeta-geo-countries continent_flatname %}">{{ continent }}</a> 7 8 / {{ country }}</h3> 8 9 -
telemeta/urls.py
ra2e58a9 rfc6b123 137 137 # Geographic browsing 138 138 url(r'^geo/$', web_view.list_continents, name="telemeta-geo-continents"), 139 url(r'^geo/(?P<continent>[ A-Za-z]+)/$', web_view.list_countries,139 url(r'^geo/(?P<continent>[a-z_]+)/$', web_view.list_countries, 140 140 name="telemeta-geo-countries"), 141 url( '^geo/(?P<continent>[A-Za-z]+)/(?P<country>[-A-Za-z0-9%;.,"& \']+)/$',141 url(r'^geo/(?P<continent>[a-z_]+)/(?P<country>[a-z_]+)/$', 142 142 web_view.list_country_collections, 143 143 name="telemeta-geo-country-collections"), -
telemeta/web/base.py
ra2e58a9 rfc6b123 56 56 from telemeta.interop.oaidatasource import TelemetaOAIDataSource 57 57 from django.core.exceptions import ObjectDoesNotExist 58 from telemeta.util.unaccent import unaccent 58 59 59 60 class WebView(Component): … … 337 338 return HttpResponse(template.render(context), mimetype=mimetype) 338 339 340 def make_continents_flatnames(self, continents): 341 map = {} 342 for c in continents: 343 flat = unaccent(c['name']).lower() 344 flat = re.sub('[^a-z]', '_', flat) 345 while map.has_key(flat): 346 flat = '_' + flat 347 c['flatname'] = flat 348 map[flat] = c['name'] 349 for d in c['countries']: 350 flat = unaccent(d['name']).lower() 351 flat = re.sub('[^a-z]', '_', flat) 352 while map.has_key(flat): 353 flat = '_' + flat 354 d['flatname'] = flat 355 map[flat] = d['name'] 356 return map 357 339 358 def list_continents(self, request): 340 359 continents = MediaCollection.objects.stat_continents() 360 self.make_continents_flatnames(continents) 341 361 return render_to_response('telemeta/geo_continents.html', 342 362 {'continents': continents }) … … 349 369 def list_countries(self, request, continent): 350 370 continents = MediaCollection.objects.stat_continents() 371 self.make_continents_flatnames(continents) 351 372 for c in continents: 352 if c[" name"] == continent:373 if c["flatname"] == continent: 353 374 break 354 if c[" name"] != continent:375 if c["flatname"] != continent: 355 376 raise Http404 356 377 … … 358 379 359 380 def list_country_collections(self, request, continent, country): 360 objects = MediaCollection.objects.by_country(country) 381 continents = MediaCollection.objects.stat_continents() 382 map = self.make_continents_flatnames(continents) 383 objects = MediaCollection.objects.by_country(map[country]) 361 384 return list_detail.object_list(request, objects, 362 385 template_name='telemeta/geo_country_collections.html', paginate_by=20, 363 extra_context={'country': country, 'continent': continent})386 extra_context={'country': map[country], 'continent_flatname': continent, 'continent': map[continent]}) 364 387 365 388 def handle_oai_request(self, request):
Note: See TracChangeset
for help on using the changeset viewer.
