Changeset 552

Show
Ignore:
Timestamp:
02/15/10 15:00:16 (5 months ago)
Author:
olivier
Message:

fix searching by recording date (year range)

Location:
trunk/telemeta
Files:
7 modified

Legend:

Unmodified
Added
Removed
  • trunk/telemeta/htdocs/css/telemeta.css

    r550 r552  
    291291} 
    292292 
     293#searchform select.tiny { 
     294    width: 12%; 
     295} 
     296 
    293297/* Navigation (borrowed from Trac) */ 
    294298.nav h2, .nav hr { display: none } 
  • trunk/telemeta/models/core.py

    r538 r552  
    108108        return self._delta.days * 24 * 3600 + self._delta.seconds 
    109109             
    110 def normalize_field(args, default_value): 
     110def normalize_field(args, default_value=None): 
    111111    """Normalize field constructor arguments, so that the field is marked blank=True 
    112112       and has a default value by default. 
     
    127127            if args.get('null'): 
    128128                args['default'] = None 
    129             else: 
     129            elif default_value is not None: 
    130130                args['default'] = default_value 
    131131 
     
    294294 
    295295class DateTimeField(models.DateTimeField): 
    296     """DateTimeField normalized with normalize_field()""" 
    297  
    298     def __init__(self, *args, **kwargs): 
    299         super(DateTimeField, self).__init__(*args, **normalize_field(kwargs, '0000-00-00 00:00')) 
    300  
    301     def get_db_prep_value(self, value): 
    302         if value is None and not self.null:  
    303             return '0000-00-00 00:00' 
    304  
    305         return super(DateTimeField, self).get_db_prep_value(value) 
     296    """DateTimeField normalized with normalize_field(). This field is allowed to 
     297    be null by default unless null=False is passed""" 
     298 
     299    def __init__(self, *args, **kwargs): 
     300        if not kwargs.has_key('null'): 
     301            kwargs['null'] = True 
     302        super(DateTimeField, self).__init__(*args, **normalize_field(kwargs)) 
    306303 
    307304class FileField(models.FileField): 
     
    318315 
    319316class DateField(models.DateField): 
    320     """DateField normalized with normalize_field()""" 
    321  
    322     def __init__(self, *args, **kwargs): 
    323         super(DateField, self).__init__(*args, **normalize_field(kwargs, '0000-00-00')) 
    324  
    325     def get_db_prep_value(self, value): 
    326         if value is None and not self.null:  
    327             return '0000-00-00' 
    328  
    329         return super(DateField, self).get_db_prep_value(value) 
     317    """DateField normalized with normalize_field(). This field is allowed to 
     318    be null by default unless null=False is passed""" 
     319 
     320    def __init__(self, *args, **kwargs): 
     321        if not kwargs.has_key('null'): 
     322            kwargs['null'] = True 
     323        super(DateField, self).__init__(*args, **normalize_field(kwargs)) 
    330324 
    331325class RequiredFieldError(Exception): 
     
    399393 
    400394    @classmethod 
    401     def field_label(cls, field_name): 
    402         try: 
    403             return cls._meta.get_field(field_name).verbose_name 
    404         except FieldDoesNotExist: 
     395    def field_label(cls, field_name=None): 
     396        if field_name: 
    405397            try: 
    406                 return getattr(cls, field_name).verbose_name 
    407             except AttributeError: 
    408                 return field_name 
     398                return cls._meta.get_field(field_name).verbose_name 
     399            except FieldDoesNotExist: 
     400                try: 
     401                    return getattr(cls, field_name).verbose_name 
     402                except AttributeError: 
     403                    return field_name 
     404        else: 
     405            return cls._meta.verbose_name 
    409406 
    410407    class Meta: 
  • trunk/telemeta/models/location.py

    r539 r552  
    163163    ancestor_location      = ForeignKey('Location', related_name="descendant_relations",  verbose_name=_('ancestor location')) 
    164164    is_direct            = BooleanField(db_index=True) 
     165    is_authoritative = BooleanField(_('authoritative')) 
    165166 
    166167    class Meta(MetaCore): 
  • trunk/telemeta/models/query.py

    r551 r552  
    3434#          David LIPSZYC <davidlipszyc@gmail.com> 
    3535 
    36 from django.db.models import Q 
     36from django.db.models import Q, Max, Min 
    3737from telemeta.models.core import * 
    3838from telemeta.util.unaccent import unaccent, unaccent_icmp 
     
    245245        return qs                 
    246246 
     247    def recording_year_range(self): 
     248        from_max = self.aggregate(Max('recorded_from_year'))['recorded_from_year__max'] 
     249        to_max   = self.aggregate(Max('recorded_to_year'))['recorded_to_year__max'] 
     250        year_max = max(from_max, to_max) 
     251 
     252        from_min = self.filter(recorded_from_year__gt=0).aggregate(Min('recorded_from_year'))['recorded_from_year__min'] 
     253        to_min   = self.filter(recorded_to_year__gt=0).aggregate(Min('recorded_to_year'))['recorded_to_year__min'] 
     254        year_min = min(from_min, to_min)  
     255 
     256        return year_min, year_max 
     257 
    247258class MediaCollectionManager(CoreManager): 
    248259    "Manage collection queries" 
  • trunk/telemeta/templates/telemeta_default/search_criteria.html

    r551 r552  
    1111<script src="{% url telemeta-js "jquery.autocomplete.js" %}" type="text/javascript"></script> 
    1212<script type="text/javascript"> 
     13function update_rec_period() { 
     14    var from_year = $('#rec_year_from'); 
     15    var to_year = $('#rec_year_to'); 
     16 
     17    if (from_year.val() == "0") { 
     18        to_year.attr('disabled', '1'); 
     19        to_year.val('0'); 
     20    } else { 
     21        to_year.removeAttr('disabled'); 
     22        if (this == to_year.get(0)) { 
     23            if (from_year.val() > to_year.val()) 
     24                from_year.val(to_year.val()); 
     25        } else if ((from_year.val() > to_year.val())) { 
     26                to_year.val(from_year.val()); 
     27        } 
     28    } 
     29} 
     30 
    1331$(document).ready(function () { 
    1432    $('#location').autocomplete('{% url telemeta-complete-location %}', { 
     
    1937        } 
    2038    }); 
     39    update_rec_period(); 
     40    $('#rec_year_from, #rec_year_to').change(update_rec_period); 
    2141}); 
     42 
    2243</script> 
    2344{% endblock %} 
     
    3152    <p> 
    3253    <label for="location">{% field_label "Location" %}</label> 
    33     <input type="text" name="location" id="location" /> 
     54    <input type="text" name="location" id="location" value="{{ criteria.location }}" /> 
    3455    </p> 
    3556 
     
    3960        <option value="">All ethnic groups</option> 
    4061    {% for group in ethnic_groups %} 
    41         <option value="{{group.id}}">{{group|escape}}</option> 
     62        <option value="{{group.id}}" {% ifequal criteria.ethnic_group.id group.id %}selected {% endifequal %}>{{group|escape}}</option> 
    4263    {% endfor %} 
    4364    </select> 
     
    5778    </p> 
    5879 
     80    {% if rec_years %}  
    5981    <p> 
    60     <label for="rec_date">Recording date</label> 
    61     <input type="text" id="rec_date" name="rec_date" /> 
     82    <label for="rec_date">{% trans "Year of recording" %}</label> 
     83    <select id="rec_year_from" name="rec_year_from" class="tiny"> 
     84        <option value="0"></option> 
     85        {% for year in rec_years %} 
     86        <option value="{{ year }}" {% ifequal criteria.rec_year_from year %}selected {% endifequal %}>{{year}}</option> 
     87        {% endfor %} 
     88    </select> 
     89    {% trans "to" %} 
     90    <select id="rec_year_to" name="rec_year_to" class="tiny"> 
     91        <option value="0"></option> 
     92        {% for year in rec_years %} 
     93        <option value="{{ year }}" {% ifequal criteria.rec_year_to year %}selected {% endifequal %}>{{year}}</option> 
     94        {% endfor %} 
     95    </select> 
    6296    </p> 
     97    {% endif %} 
    6398 
    6499    <p> 
  • trunk/telemeta/templates/telemeta_default/search_results.html

    r551 r552  
    2323    <li><b>Title:</b> {{criteria.title}}</li> 
    2424  {% endif %} 
    25   {% if criteria.rec_date %} 
    26     <li><b>{% trans "Year of recording" %}:</b> {{criteria.rec_date}}</li> 
     25  {% if criteria.rec_year_from %} 
     26    <li><b>{% trans "Year of recording" %}:</b> {{criteria.rec_year_from}} 
     27     {% ifnotequal criteria.rec_year_to criteria.rec_year_from %} 
     28     {% trans "to" %} {{criteria.rec_year_to}} 
     29     {% endifnotequal %} 
     30    </li> 
    2731  {% endif %} 
    2832  {% if criteria.pub_date %} 
  • trunk/telemeta/web/base.py

    r551 r552  
    5858from telemeta.util.unaccent import unaccent 
    5959from telemeta.web import pages 
     60import datetime 
    6061 
    6162class WebView(Component): 
     
    167168        return response 
    168169 
    169     def edit_search(self, request): 
    170  
     170    def edit_search(self, request, criteria=None): 
     171        year_min, year_max = MediaCollection.objects.all().recording_year_range() 
     172        years = year_min and year_max and range(year_min, year_max + 1) \ 
     173                or year_min and [year_min] or year_max and [year_max] 
    171174        return render_to_response('telemeta/search_criteria.html', { 
     175            'rec_years': years, 
    172176            'ethnic_groups': MediaItem.objects.all().ethnic_groups(), 
     177            'criteria': criteria 
    173178        }) 
    174179 
     
    215220                collections.word_search('creator', value), 
    216221                items.word_search('auteur', value)), 
    217             'rec_date': lambda value: ( 
    218                 collections.by_recording_date(value),  
    219                 items.by_recording_date(value)), 
     222            'rec_year_from': lambda value: ( 
     223                collections.by_recording_year(int(value), int(input['rec_year_to'])),  
     224                items.by_recording_date(datetime.date(int(value), 1, 1),  
     225                                        datetime.date(int(input['rec_year_to']), 12, 31))), 
     226            'rec_year_to': lambda value: (collections, items), 
    220227            'pub_date': lambda value: ( 
    221228                collections.by_publish_date(value),