X-Git-Url: https://git.auf.org/?p=auf_savoirs_en_partage_django.git;a=blobdiff_plain;f=auf_savoirs_en_partage%2Fsavoirs%2Fforms.py;h=9c5b248ebed48996169ae47fef22e1a75bc915e4;hp=f24955d95985b6f9a868678792f32534bdc09f82;hb=0e5d5bc9ce115895ec0ff92c62a87c1701b8392e;hpb=6cdb7d49a55bb25374d577d14277878589d6f3df diff --git a/auf_savoirs_en_partage/savoirs/forms.py b/auf_savoirs_en_partage/savoirs/forms.py index f24955d..9c5b248 100644 --- a/auf_savoirs_en_partage/savoirs/forms.py +++ b/auf_savoirs_en_partage/savoirs/forms.py @@ -36,19 +36,33 @@ class RecordSearchForm(forms.Form): """Génère dynamiquement les choix possibles pour la recherche par type.""" yield ('', '') cursor = db.connection.cursor() - cursor.execute("SELECT DISTINCT REPLACE(REPLACE(type, ', PeerReviewed', ''), ', NonPeerReviewed', '') FROM savoirs_record") + cursor.execute("""SELECT DISTINCT TRIM(REPLACE(REPLACE(type, ', PeerReviewed', ''), ', NonPeerReviewed', '')) AS clean_type + FROM savoirs_record ORDER BY clean_type""") for result in cursor.fetchall(): - type = result[0].strip() + type = result[0] if type: yield (type, type) - TYPE_CHOICES = TypeChoices() + class PublisherChoices(object): + + def __iter__(self): + """Génère dynamiquement les choix possibles pour la recherche par éditeur.""" + yield ('', '') + cursor = db.connection.cursor() + cursor.execute("SELECT DISTINCT publisher AS publisher FROM savoirs_record ORDER BY publisher") + for result in cursor.fetchall(): + publisher = result[0] + if publisher not in ('', '-'): + yield (publisher, publisher) + PUBLISHER_CHOICES = PublisherChoices() + q = forms.CharField(required=False, label="Mots-clés") auteur = forms.CharField(required=False, label="Auteur ou contributeur") titre = forms.CharField(required=False, label="Titre") sujet = forms.CharField(required=False, label="Sujet") - type = forms.ChoiceField(required=False, label="Type de document", choices = TYPE_CHOICES) + type = forms.ChoiceField(required=False, label="Type de document", choices=TYPE_CHOICES) + publisher = forms.ChoiceField(required=False, label="Éditeur", choices=PUBLISHER_CHOICES) def get_query_set(self): """Retourne l'ensemble des ressources qui correspondent aux valeurs @@ -70,6 +84,9 @@ class RecordSearchForm(forms.Form): type = self.cleaned_data['type'] if type: records = records.filter(type__icontains=type) + publisher = self.cleaned_data['publisher'] + if publisher: + records = records.filter(publisher=publisher) return records def get_search_regexp(self):