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
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.
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
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()"""
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):
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
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'
# 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
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"
{% 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,
return data[0].replace(/ *\([0-9]+.*\) *$/, '');
}
});
+ update_rec_period();
+ $('#rec_year_from, #rec_year_to').change(update_rec_period);
});
+
</script>
{% endblock %}
<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>
<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>
<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>
{% 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>
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"""
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):
'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))