Changeset c1a2c1c


Ignore:
Timestamp:
Feb 10, 2010 7:25:21 PM (5 years ago)
Author:
olivier <>
Branches:
master, crem, crem2, dev, dev2, diadems, dj1.6, feature/breadcrumbs, feature/ts-0.5, feature/ts-0.5.4, feature/writecache, forma, generic, instru_search, lam, lam2, mapsv3, merge, nlivemulti, production, release/1.4.4, sabiod, security, server, social, storage, telecaster, test, video
Children:
12d665f
Parents:
622588f
git-author:
olivier <> (10/02/2010 19:25:21)
git-committer:
olivier <> (10/02/2010 19:25:21)
Message:

add autocompleted location input field to advanced search

Location:
telemeta
Files:
2 added
2 deleted
5 edited

Legend:

Unmodified
Added
Removed
  • telemeta/models/query.py

    r622588f rc1a2c1c  
    8080    def by_location(self, location): 
    8181        "Find items by location" 
    82         from telemeta.models import LocationRelation 
    83         descendants = LocationRelation.objects.filter(ancestor_location=location) 
    84         return self.filter(Q(location=location) | Q(location__in=descendants)) 
     82        return self.filter(Q(location=location) | Q(location__in=location.descendants())) 
    8583            
    8684    @staticmethod 
    8785    def __name_cmp(obj1, obj2): 
    8886        return unaccent_icmp(obj1.name, obj2.name) 
     87 
     88    def locations(self): 
     89        from telemeta.models import Location, LocationRelation 
     90        l = self.values('location') 
     91        r = LocationRelation.objects.filter(location__in=l).values('ancestor_location') 
     92        return Location.objects.filter(Q(pk__in=l) | Q(pk__in=r)) 
    8993 
    9094    def countries(self, group_by_continent=False): 
  • telemeta/templates/telemeta_default/search_criteria.html

    r9d3f1f3 rc1a2c1c  
    11{% extends "telemeta/base.html" %} 
    22{% load telemeta_utils %} 
     3{% load i18n %} 
     4 
     5{% block stylesheets %} 
     6{{ block.super }} 
     7<link rel="stylesheet" type="text/css" href="{% url telemeta-css "jquery.autocomplete.css" %}" /> 
     8{% endblock %} 
    39 
    410{% block extra_javascript %} 
    5 <script src="{% url telemeta-continents-js %}" type="text/javascript"></script> 
     11<script src="{% url telemeta-js "jquery.autocomplete.js" %}" type="text/javascript"></script> 
     12<script type="text/javascript"> 
     13$(document).ready(function () { 
     14    $('#location').autocomplete('{% url telemeta-complete-location %}', { 
     15        max: 20, 
     16        formatResult: function(data) { 
     17            console.log(data); 
     18            return data[0].replace(/ *\([0-9]+.*\) *$/, ''); 
     19        } 
     20    }); 
     21}); 
     22</script> 
    623{% endblock %} 
    724 
     
    1330 
    1431    <p> 
    15     <label for="continent">Continent</label> 
    16     <select id="continent" name="continent" onchange="update_countries(this, document.getElementById('country'));"> 
    17         <option value="">All continents</option> 
    18     {% for continent in continents %} 
    19         <option value="{{continent|escape}}">{{continent|escape}}</option> 
    20     {% endfor %} 
    21     </select> 
    22     </p> 
    23  
    24     <p> 
    25     <label for="country">Country</label> 
    26     <select name="country" id="country"> 
    27         <option value="">All countries</option> 
    28     {% for country in countries %} 
    29         <option value="{{country.1|escape}}">{{country.1|escape}}</option> 
    30     {% endfor %} 
    31     </select> 
     32    <label for="location">{% trans "Location" %}</label> 
     33    <input type="text" name="location" id="location" /> 
    3234    </p> 
    3335 
     
    3739        <option value="">All ethnic groups</option> 
    3840    {% for group in ethnic_groups %} 
    39         <option value="{{group|escape}}">{{group|escape}}</option> 
     41        <option value="{{group.id}}">{{group|escape}}</option> 
    4042    {% endfor %} 
    4143    </select> 
  • telemeta/templates/telemeta_default/search_results.html

    r9d3f1f3 rc1a2c1c  
    1010    <li><b>Pattern:</b> {{criteria.pattern}}</li> 
    1111  {% endif %} 
    12   {% if criteria.continent %} 
    13     <li><b>Continent:</b> {{criteria.continent}}</li> 
    14   {% endif %} 
    15   {% if criteria.country %} 
    16     <li><b>Country:</b> {{criteria.country}}</li> 
     12  {% if criteria.location %} 
     13    <li><b>Location:</b> {{criteria.location}}</li> 
    1714  {% endif %} 
    1815  {% if criteria.ethnic_group %} 
  • telemeta/urls.py

    r619d1a8 rc1a2c1c  
    112112        name="telemeta-search-items"), 
    113113    url(r'^search/criteria/$', web_view.edit_search, name="telemeta-search-criteria"), 
     114    url(r'^complete_location/$', web_view.complete_location, name="telemeta-complete-location"), 
    114115 
    115116    # administration         
     
    145146        web_view.list_country_items,  
    146147        name="telemeta-geo-country-items"), 
    147     url(r'^dynjs/continents.js$', web_view.get_continents_js, name="telemeta-continents-js"), 
    148148    url(r'^geo/country_info/(?P<id>[0-9A-Z]+)/$',  
    149149        web_view.country_info, name="telemeta-country-info"), 
  • telemeta/web/base.py

    r619d1a8 rc1a2c1c  
    162162 
    163163    def edit_search(self, request): 
    164         continents = MediaCollection.objects.list_continents() 
    165         countries = MediaCollection.objects.list_countries() 
    166         ethnic_groups = MediaItem.objects.list_ethnic_groups() 
     164        ethnic_groups = MediaItem.objects.all().ethnic_groups() 
    167165        return render_to_response('telemeta/search_criteria.html',  
    168             {'continents': continents, 'countries': countries,  
    169             'ethnic_groups': ethnic_groups}) 
     166            {'ethnic_groups': ethnic_groups}) 
     167 
     168    def complete_location(self, request, with_items=True): 
     169        input = request.REQUEST 
     170        
     171        token = input['q'] 
     172        limit = int(input['limit']) 
     173        if with_items: 
     174            locations = MediaItem.objects.all().locations() 
     175        else: 
     176            locations = Location.objects.all() 
     177 
     178        locations = locations.filter(name__istartswith=token).order_by('name')[:limit] 
     179        data = [unicode(l) + " (%d items)" % l.items().count() for l in locations] 
     180 
     181        return HttpResponse("\n".join(data)) 
    170182 
    171183    def search(self, request, type = None): 
    172184        """Perform a search through collections and items metadata""" 
    173         collections = MediaCollection.objects.all() 
    174         items = MediaItem.objects.all() 
     185        collections = MediaCollection.objects.enriched() 
     186        items = MediaItem.objects.enriched() 
    175187        input = request.REQUEST 
    176188        criteria = {} 
     
    183195                collections.word_search('title', value),  
    184196                items.by_title(value)), 
    185             'country': lambda value: ( 
    186                 collections.by_country(value),  
    187                 items.filter(etat = value)), 
     197            'location': lambda value: ( 
     198                collections.by_location(Location.objects.get(name=value)),  
     199                items.by_location(Location.objects.get(name=value))), 
    188200            'continent': lambda value: ( 
    189201                collections.by_continent(value),  
     
    347359            'country': country, 'continent': country.continents()[0]}) 
    348360 
    349     def get_continents_js(self, request): 
    350         countries = MediaCollection.objects.list_countries() 
    351         return render_to_response('telemeta/geo_continents.js',  
    352                     {'countries': countries}) 
    353  
    354361    def list_countries(self, request, continent):                     
    355362        continent = Location.objects.by_flatname(continent)[0] 
Note: See TracChangeset for help on using the changeset viewer.