From: olivier <> Date: Mon, 15 Feb 2010 14:00:16 +0000 (+0000) Subject: fix searching by recording date (year range) X-Git-Tag: 1.1~536 X-Git-Url: https://git.parisson.com/?a=commitdiff_plain;h=a1900c72642806fefddad3756e5a7b251ff930b4;p=telemeta.git fix searching by recording date (year range) --- diff --git a/telemeta/htdocs/css/telemeta.css b/telemeta/htdocs/css/telemeta.css index ef411496..5770da99 100644 --- a/telemeta/htdocs/css/telemeta.css +++ b/telemeta/htdocs/css/telemeta.css @@ -290,6 +290,10 @@ label.disabled { color: #d7d7d7 } width: 450px; } +#searchform select.tiny { + width: 12%; +} + /* Navigation (borrowed from Trac) */ .nav h2, .nav hr { display: none } .nav ul { font-size: 10px; list-style: none; margin: 0; text-align: left } diff --git a/telemeta/models/core.py b/telemeta/models/core.py index fef47ce6..0fc8d62e 100644 --- a/telemeta/models/core.py +++ b/telemeta/models/core.py @@ -107,7 +107,7 @@ class Duration(object): def as_seconds(self): return self._delta.days * 24 * 3600 + self._delta.seconds -def normalize_field(args, default_value): +def normalize_field(args, default_value=None): """Normalize field constructor arguments, so that the field is marked blank=True and has a default value by default. @@ -126,7 +126,7 @@ def normalize_field(args, default_value): if not args.has_key('default'): if args.get('null'): args['default'] = None - else: + elif default_value is not None: args['default'] = default_value return args @@ -293,16 +293,13 @@ class TextField(models.TextField): super(TextField, self).__init__(*args, **normalize_field(kwargs, '')) class DateTimeField(models.DateTimeField): - """DateTimeField normalized with normalize_field()""" + """DateTimeField normalized with normalize_field(). This field is allowed to + be null by default unless null=False is passed""" def __init__(self, *args, **kwargs): - super(DateTimeField, self).__init__(*args, **normalize_field(kwargs, '0000-00-00 00:00')) - - def get_db_prep_value(self, value): - if value is None and not self.null: - return '0000-00-00 00:00' - - return super(DateTimeField, self).get_db_prep_value(value) + if not kwargs.has_key('null'): + kwargs['null'] = True + super(DateTimeField, self).__init__(*args, **normalize_field(kwargs)) class FileField(models.FileField): """FileField normalized with normalize_field()""" @@ -317,16 +314,13 @@ class FloatField(models.FloatField): super(FloatField, self).__init__(*args, **normalize_field(kwargs, 0)) class DateField(models.DateField): - """DateField normalized with normalize_field()""" + """DateField normalized with normalize_field(). This field is allowed to + be null by default unless null=False is passed""" def __init__(self, *args, **kwargs): - super(DateField, self).__init__(*args, **normalize_field(kwargs, '0000-00-00')) - - def get_db_prep_value(self, value): - if value is None and not self.null: - return '0000-00-00' - - return super(DateField, self).get_db_prep_value(value) + if not kwargs.has_key('null'): + kwargs['null'] = True + super(DateField, self).__init__(*args, **normalize_field(kwargs)) class RequiredFieldError(Exception): def __init__(self, model, field): @@ -398,14 +392,17 @@ class ModelCore(EnhancedModel): return fields_list @classmethod - def field_label(cls, field_name): - try: - return cls._meta.get_field(field_name).verbose_name - except FieldDoesNotExist: + def field_label(cls, field_name=None): + if field_name: try: - return getattr(cls, field_name).verbose_name - except AttributeError: - return field_name + return cls._meta.get_field(field_name).verbose_name + except FieldDoesNotExist: + try: + return getattr(cls, field_name).verbose_name + except AttributeError: + return field_name + else: + return cls._meta.verbose_name class Meta: abstract = True diff --git a/telemeta/models/location.py b/telemeta/models/location.py index c0621423..dcf89fa7 100644 --- a/telemeta/models/location.py +++ b/telemeta/models/location.py @@ -162,6 +162,7 @@ class LocationRelation(ModelCore): location = ForeignKey('Location', related_name="ancestor_relations", verbose_name=_('location')) ancestor_location = ForeignKey('Location', related_name="descendant_relations", verbose_name=_('ancestor location')) is_direct = BooleanField(db_index=True) + is_authoritative = BooleanField(_('authoritative')) class Meta(MetaCore): db_table = 'location_relations' diff --git a/telemeta/models/query.py b/telemeta/models/query.py index a3f0e08d..1f96d36c 100644 --- a/telemeta/models/query.py +++ b/telemeta/models/query.py @@ -33,7 +33,7 @@ # Authors: Olivier Guilyardi # David LIPSZYC -from django.db.models import Q +from django.db.models import Q, Max, Min from telemeta.models.core import * from telemeta.util.unaccent import unaccent, unaccent_icmp from telemeta.models.enum import EthnicGroup @@ -244,6 +244,17 @@ class MediaCollectionQuerySet(CoreQuerySet): return qs + def recording_year_range(self): + from_max = self.aggregate(Max('recorded_from_year'))['recorded_from_year__max'] + to_max = self.aggregate(Max('recorded_to_year'))['recorded_to_year__max'] + year_max = max(from_max, to_max) + + from_min = self.filter(recorded_from_year__gt=0).aggregate(Min('recorded_from_year'))['recorded_from_year__min'] + to_min = self.filter(recorded_to_year__gt=0).aggregate(Min('recorded_to_year'))['recorded_to_year__min'] + year_min = min(from_min, to_min) + + return year_min, year_max + class MediaCollectionManager(CoreManager): "Manage collection queries" diff --git a/telemeta/templates/telemeta_default/search_criteria.html b/telemeta/templates/telemeta_default/search_criteria.html index 77d9ce12..d0e28336 100644 --- a/telemeta/templates/telemeta_default/search_criteria.html +++ b/telemeta/templates/telemeta_default/search_criteria.html @@ -10,6 +10,24 @@ {% block extra_javascript %} {% endblock %} @@ -30,7 +51,7 @@ $(document).ready(function () {

- +

@@ -38,7 +59,7 @@ $(document).ready(function () {

@@ -56,10 +77,24 @@ $(document).ready(function () {

+ {% if rec_years %}

- - + + + {% trans "to" %} +

+ {% endif %}

diff --git a/telemeta/templates/telemeta_default/search_results.html b/telemeta/templates/telemeta_default/search_results.html index 0992678a..2d58402b 100644 --- a/telemeta/templates/telemeta_default/search_results.html +++ b/telemeta/templates/telemeta_default/search_results.html @@ -22,8 +22,12 @@ {% if criteria.title %}

  • Title: {{criteria.title}}
  • {% endif %} - {% if criteria.rec_date %} -
  • {% trans "Year of recording" %}: {{criteria.rec_date}}
  • + {% if criteria.rec_year_from %} +
  • {% trans "Year of recording" %}: {{criteria.rec_year_from}} + {% ifnotequal criteria.rec_year_to criteria.rec_year_from %} + {% trans "to" %} {{criteria.rec_year_to}} + {% endifnotequal %} +
  • {% endif %} {% if criteria.pub_date %}
  • Publishing date: {{criteria.pub_date}}
  • diff --git a/telemeta/web/base.py b/telemeta/web/base.py index 45db6db7..7a64f6a8 100644 --- a/telemeta/web/base.py +++ b/telemeta/web/base.py @@ -57,6 +57,7 @@ from telemeta.interop.oaidatasource import TelemetaOAIDataSource from django.core.exceptions import ObjectDoesNotExist from telemeta.util.unaccent import unaccent from telemeta.web import pages +import datetime class WebView(Component): """Provide web UI methods""" @@ -166,10 +167,14 @@ class WebView(Component): response['Content-Disposition'] = 'attachment' return response - def edit_search(self, request): - + def edit_search(self, request, criteria=None): + year_min, year_max = MediaCollection.objects.all().recording_year_range() + years = year_min and year_max and range(year_min, year_max + 1) \ + or year_min and [year_min] or year_max and [year_max] return render_to_response('telemeta/search_criteria.html', { + 'rec_years': years, 'ethnic_groups': MediaItem.objects.all().ethnic_groups(), + 'criteria': criteria }) def complete_location(self, request, with_items=True): @@ -214,9 +219,11 @@ class WebView(Component): 'creator': lambda value: ( collections.word_search('creator', value), items.word_search('auteur', value)), - 'rec_date': lambda value: ( - collections.by_recording_date(value), - items.by_recording_date(value)), + 'rec_year_from': lambda value: ( + collections.by_recording_year(int(value), int(input['rec_year_to'])), + items.by_recording_date(datetime.date(int(value), 1, 1), + datetime.date(int(input['rec_year_to']), 12, 31))), + 'rec_year_to': lambda value: (collections, items), 'pub_date': lambda value: ( collections.by_publish_date(value), items.by_publish_date(value))