]> git.parisson.com Git - telemeta.git/commitdiff
fix searching by recording date (year range)
authorolivier <>
Mon, 15 Feb 2010 14:00:16 +0000 (14:00 +0000)
committerolivier <>
Mon, 15 Feb 2010 14:00:16 +0000 (14:00 +0000)
telemeta/htdocs/css/telemeta.css
telemeta/models/core.py
telemeta/models/location.py
telemeta/models/query.py
telemeta/templates/telemeta_default/search_criteria.html
telemeta/templates/telemeta_default/search_results.html
telemeta/web/base.py

index ef411496478e0fcfd3ba5c88b67fec1a91e44d1f..5770da991518fdfed191ab1a1ba9918708dc2a5e 100644 (file)
@@ -290,6 +290,10 @@ label.disabled { color: #d7d7d7 }
     width: 450px;\r
 }\r
 \r
+#searchform select.tiny {\r
+    width: 12%;\r
+}\r
+\r
 /* Navigation (borrowed from Trac) */\r
 .nav h2, .nav hr { display: none }\r
 .nav ul { font-size: 10px; list-style: none; margin: 0; text-align: left }\r
index fef47ce697cca9643d33092f3b854f7a81a529a1..0fc8d62e968e83acee30df7564bf69464283ec34 100644 (file)
@@ -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
index c06214230cccb8cbedcd2c1f374c15567a15e9e2..dcf89fa73af5b9a60b96704d65486af7f93acd70 100644 (file)
@@ -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'
index a3f0e08d08ec455b8c6f9becbd7820b1d65960ec..1f96d36ca938f832f3fda45d79ab39a800ba91b6 100644 (file)
@@ -33,7 +33,7 @@
 # Authors: Olivier Guilyardi <olivier@samalyse.com>
 #          David LIPSZYC <davidlipszyc@gmail.com>
 
-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"
 
index 77d9ce12351fc17eca4c00cb102f64947eee0a94..d0e283360755e83c72d20ae93672c5231b42b205 100644 (file)
 {% block extra_javascript %}
 <script src="{% url telemeta-js "jquery.autocomplete.js" %}" type="text/javascript"></script>
 <script type="text/javascript">
+function update_rec_period() {
+    var from_year = $('#rec_year_from');
+    var to_year = $('#rec_year_to');
+
+    if (from_year.val() == "0") {
+        to_year.attr('disabled', '1');
+        to_year.val('0');
+    } else {
+        to_year.removeAttr('disabled');
+        if (this == to_year.get(0)) {
+            if (from_year.val() > to_year.val())
+                from_year.val(to_year.val());
+        } else if ((from_year.val() > to_year.val())) {
+                to_year.val(from_year.val());
+        }
+    }
+}
+
 $(document).ready(function () {
     $('#location').autocomplete('{% url telemeta-complete-location %}', {
         max: 20,
@@ -18,7 +36,10 @@ $(document).ready(function () {
             return data[0].replace(/ *\([0-9]+.*\) *$/, '');
         }
     });
+    update_rec_period();
+    $('#rec_year_from, #rec_year_to').change(update_rec_period);
 });
+
 </script>
 {% endblock %}
 
@@ -30,7 +51,7 @@ $(document).ready(function () {
 
     <p>
     <label for="location">{% field_label "Location" %}</label>
-    <input type="text" name="location" id="location" />
+    <input type="text" name="location" id="location" value="{{ criteria.location }}" />
     </p>
 
     <p>
@@ -38,7 +59,7 @@ $(document).ready(function () {
     <select id="ethnic_group" name="ethnic_group">
         <option value="">All ethnic groups</option>
     {% for group in ethnic_groups %}
-        <option value="{{group.id}}">{{group|escape}}</option>
+        <option value="{{group.id}}" {% ifequal criteria.ethnic_group.id group.id %}selected {% endifequal %}>{{group|escape}}</option>
     {% endfor %}
     </select>
     </p>
@@ -56,10 +77,24 @@ $(document).ready(function () {
     <input type="text" id="title" name="title" />
     </p>
 
+    {% if rec_years %} 
     <p>
-    <label for="rec_date">Recording date</label>
-    <input type="text" id="rec_date" name="rec_date" />
+    <label for="rec_date">{% trans "Year of recording" %}</label>
+    <select id="rec_year_from" name="rec_year_from" class="tiny">
+        <option value="0"></option>
+        {% for year in rec_years %}
+        <option value="{{ year }}" {% ifequal criteria.rec_year_from year %}selected {% endifequal %}>{{year}}</option>
+        {% endfor %}
+    </select>
+    {% trans "to" %}
+    <select id="rec_year_to" name="rec_year_to" class="tiny">
+        <option value="0"></option>
+        {% for year in rec_years %}
+        <option value="{{ year }}" {% ifequal criteria.rec_year_to year %}selected {% endifequal %}>{{year}}</option>
+        {% endfor %}
+    </select>
     </p>
+    {% endif %}
 
     <p>
     <label for="pub_date">Publishing date</label>
index 0992678a9ca624558e92309436452ec1adb76631..2d58402b550299fcfbeef4315a60772c4c498a46 100644 (file)
   {% if criteria.title %}
     <li><b>Title:</b> {{criteria.title}}</li>
   {% endif %}
-  {% if criteria.rec_date %}
-    <li><b>{% trans "Year of recording" %}:</b> {{criteria.rec_date}}</li>
+  {% if criteria.rec_year_from %}
+    <li><b>{% trans "Year of recording" %}:</b> {{criteria.rec_year_from}}
+     {% ifnotequal criteria.rec_year_to criteria.rec_year_from %}
+     {% trans "to" %} {{criteria.rec_year_to}}
+     {% endifnotequal %}
+    </li>
   {% endif %}
   {% if criteria.pub_date %}
     <li><b>Publishing date:</b> {{criteria.pub_date}}</li>
index 45db6db78afc23b290d19021527d2ec84e9e92dc..7a64f6a879d2b6a8428523229fe8b845f5432b59 100644 (file)
@@ -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))