From a90d43aae7f9ed8e75b8b56d318d12c77a4a0e27 Mon Sep 17 00:00:00 2001 From: Thomas Fillon Date: Fri, 16 Jun 2017 17:35:31 +0200 Subject: [PATCH] Fix #177 --- telemeta/forms/haystack_form.py | 124 +++++++++++++++++++++++--------- 1 file changed, 91 insertions(+), 33 deletions(-) diff --git a/telemeta/forms/haystack_form.py b/telemeta/forms/haystack_form.py index 1642a8e7..bc4c5fdc 100644 --- a/telemeta/forms/haystack_form.py +++ b/telemeta/forms/haystack_form.py @@ -27,11 +27,12 @@ from haystack.query import SearchQuerySet, SQ from datetime import date from django.utils.translation import ugettext_lazy as _ import operator -#from telemeta.views.boolean_search import * +# from telemeta.views.boolean_search import * + class HaySearchForm(FacetedSearchForm): - q = forms.CharField(required=False, widget=forms.TextInput(attrs={'class': 'form-control', 'type' : 'text'})) + q = forms.CharField(required=False, widget=forms.TextInput(attrs={'class': 'form-control', 'type': 'text'})) def search(self): sqs = SearchQuerySet().load_all() @@ -40,10 +41,10 @@ class HaySearchForm(FacetedSearchForm): return sqs if self.cleaned_data['q']: - #search input of a code, contains at least '_YYYY_' - #if not re.match('([a-zA-Z]*_?[EI])?_[0-9]{4}_([0-9]{3}_[0-9]{3})?', self.cleaned_data.get('q')): + # search input of a code, contains at least '_YYYY_' + # if not re.match('([a-zA-Z]*_?[EI])?_[0-9]{4}_([0-9]{3}_[0-9]{3})?', self.cleaned_data.get('q')): sqs = sqs.filter(content__startswith=self.cleaned_data['q']).facet('item_acces').facet('item_status').facet('digitized').facet('recording_context').facet('physical_format').facet('media_type') - #else: + # else: # sqs = sqs.filter(code__contains=self.cleaned_data['q']).facet('item_acces').facet('item_status').facet('digitized').facet('recording_context').facet('physical_format').facet('media_type') for facet in self.selected_facets: @@ -63,7 +64,7 @@ class HaySearchForm(FacetedSearchForm): class HayAdvanceForm(SearchForm): - #to replace de basic search form field + # to replace de basic search form field q = forms.CharField(required=False, label=(_('title')), widget=forms.TextInput(attrs={'class': 'form-control', 'type': 'search'})) location = forms.CharField(required=False, label=(_('location')), widget=forms.TextInput(attrs={'class': 'form-control', 'type': 'search'})) @@ -77,11 +78,16 @@ class HayAdvanceForm(SearchForm): type_name.append((ethnic.value, ethnic.value)) return type_name - ethnic_group = forms.CharField(required=False, label=(_('population / social group')), widget=forms.Select(attrs={'style': 'width:100%'}, choices=list_ethnic_group())) + ethnic_group = forms.ChoiceField( + required=False, + label=(_('population / social group')), + choices=list_ethnic_group, + widget=forms.Select(attrs={'style': 'width:100%'}) + ) instruments = forms.CharField(required=False, label=(_('instruments')), widget=forms.TextInput(attrs={'class': 'form-control', 'type': 'search'})) collectors = forms.CharField(required=False, label=(_('recordist')), widget=forms.TextInput(attrs={'class': 'form-control', 'type': 'search'})) - #to create a dynamic list of publishing years + # to create a dynamic list of publishing years def list_recorded_year(): list_all_year = [] list_collect = MediaCollection.objects.all() @@ -97,10 +103,22 @@ class HayAdvanceForm(SearchForm): list_year.append((str(year), year)) return list_year - recorded_from_date = forms.IntegerField(required=False, label=(_('recording date (from)')), widget=forms.Select(attrs={'style': 'width:47%'}, choices=list_recorded_year())) - recorded_to_date = forms.IntegerField(required=False, label=(_('recording date (until')), widget=forms.Select(attrs={'style': 'width:47%'}, choices=list_recorded_year())) - - #to create a dynamic list of publishing years + recorded_from_date = forms.TypedChoiceField( + coerce=int, + required=False, + label=(_('recording date (from)')), + choices=list_recorded_year, + widget=forms.Select(attrs={'style': 'width:47%'}), + ) + recorded_to_date = forms.TypedChoiceField( + coerce=int, + required=False, + label=(_('recording date (until')), + choices=list_recorded_year, + widget=forms.Select(attrs={'style': 'width:47%'}) + ) + + # to create a dynamic list of publishing years def list_publish_year(): list_all_year = [] list_collect = MediaCollection.objects.all() @@ -114,15 +132,36 @@ class HayAdvanceForm(SearchForm): list_year.append((year, year)) return list_year - year_published_from = forms.IntegerField(required=False, label=(_('year published from')), widget=forms.Select(attrs={'style': 'width:47%'}, choices=list_publish_year())) - year_published_to = forms.IntegerField(required=False, label=(_('year published to')), widget=forms.Select(attrs={'style': 'width:47%'}, choices=list_publish_year())) - - viewable_choice = (('1', 'no preference'), ('2', 'online and public'), ('3', 'online (account required)')) - viewable = forms.CharField(required=False, label=(_('viewable')), widget=forms.RadioSelect(choices=viewable_choice), initial=1) - - item_status = forms.CharField(required=False, label=(_('Document status')), widget=forms.RadioSelect(choices=(('1', 'no preference'), ('pub', 'Published'), ('unpub', 'Unpublished'))), initial=1) - - #to create a dynamic list of media types + year_published_from = forms.TypedChoiceField( + coerce=int, + required=False, + label=(_('year published from')), + choices=list_publish_year, + widget=forms.Select(attrs={'style': 'width:47%'}) + ) + year_published_to = forms.TypedChoiceField( + coerce=int, + required=False, + label=(_('year published to')), + choices=list_publish_year, + widget=forms.Select(attrs={'style': 'width:47%'}) + ) + + VIEWABLE_CHOICE = (('1', 'no preference'), ('2', 'online and public'), + ('3', 'online (account required)')) + viewable = forms.CharField( + required=False, label=(_('viewable')), + widget=forms.RadioSelect(choices=VIEWABLE_CHOICE), + initial=1) + STATUS_CHOICES = (('1', 'no preference'), ('pub', 'Published'), + ('unpub', 'Unpublished')) + item_status = forms.CharField( + required=False, + label=(_('Document status')), + widget=forms.RadioSelect(choices=STATUS_CHOICES), + initial=1) + + # to create a dynamic list of media types def list_media_type(): type_name = [] type_name.append(('1', 'no preference')) @@ -131,9 +170,14 @@ class HayAdvanceForm(SearchForm): type_name.append((mt.value, mt.value)) return type_name - media_type = forms.CharField(required=False, label=(_('media')), widget=forms.RadioSelect(choices=(list_media_type())), initial=1) + media_type = forms.ChoiceField( + required=False, + label=(_('media')), + choices=list_media_type, + widget=forms.RadioSelect(), + initial=1) - #to create a dynamic list of recording contexts + # to create a dynamic list of recording contexts def list_recording_context(): type_name = [] type_name.append(('', 'no preference')) @@ -142,9 +186,14 @@ class HayAdvanceForm(SearchForm): type_name.append((context.value, context.value)) return type_name - recording_context = forms.CharField(required=False, label=(_('recording context')), widget=forms.Select(attrs={'style': 'width:100%'}, choices=list_recording_context())) + recording_context = forms.ChoiceField( + required=False, + label=(_('recording context')), + choices=list_recording_context, + widget=forms.Select(attrs={'style': 'width:100%'}) + ) - #to create a dynamic list of physical formats + # to create a dynamic list of physical formats def list_physical_format(): type_name = [] type_name.append(('', 'no preference')) @@ -153,10 +202,19 @@ class HayAdvanceForm(SearchForm): type_name.append((physical_format.value, physical_format.value)) return type_name - physical_format = forms.CharField(required=False, label=(_('physical format')), widget=forms.Select(attrs={'style': 'width:100%'}, choices=list_physical_format())) - code = forms.CharField(required=False, label=(_('code')), widget=forms.TextInput(attrs={'class': 'form-control', 'type': 'search'})) - - #def filter_instru(self, query): + physical_format = forms.ChoiceField( + required=False, + label=(_('physical format')), + choices=list_physical_format, + widget=forms.Select(attrs={'style': 'width:100%'}) + ) + code = forms.CharField( + required=False, + label=(_('code')), + widget=forms.TextInput( + attrs={'class': 'form-control', 'type': 'search'})) + + # def filter_instru(self, query): # if isinstance(query, str) or isinstance(query, unicode): # try: # BooleanSearchView().is_correct_query(query) @@ -205,14 +263,14 @@ class HayAdvanceForm(SearchForm): sqs = sqs.filter(code__contains=self.cleaned_data['code']) if self.cleaned_data.get('location'): - sqs = sqs.filter(Q(location_principal__startswith=self.cleaned_data['location'])|Q(location_relation__startswith=self.cleaned_data['location'])) + sqs = sqs.filter(Q(location_principal__startswith=self.cleaned_data['location']) | Q(location_relation__startswith=self.cleaned_data['location'])) if self.cleaned_data['ethnic_group']: if self.cleaned_data.get('ethnic_group') != '': sqs = sqs.filter(ethnic_group__contains=self.cleaned_data['ethnic_group']) if self.cleaned_data.get('instruments'): - #sqs = sqs.filter(self.filter_instru(self.cleaned_data['instruments'])) + # sqs = sqs.filter(self.filter_instru(self.cleaned_data['instruments'])) sqs = sqs.filter(instruments__startswith=self.cleaned_data['instruments']) if self.cleaned_data.get('collectors'): @@ -244,7 +302,7 @@ class HayAdvanceForm(SearchForm): if self.cleaned_data['media_type']: if self.cleaned_data.get('media_type') != '1': - sqs = sqs.filter(media_type=self.cleaned_data['media_type']) + sqs = sqs.filter(media_type=self.cleaned_data['media_type']) if self.cleaned_data['recording_context']: if self.cleaned_data.get('recording_context') != '': @@ -254,4 +312,4 @@ class HayAdvanceForm(SearchForm): if self.cleaned_data.get('physical_format') != '': sqs = sqs.filter(physical_format=self.cleaned_data['physical_format']) - return sqs \ No newline at end of file + return sqs -- 2.39.5