From a4e844bafbd314120bd0b1ba9f785aa500b7bbb6 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jean-S=C3=A9bastien=20Caux?= <git@jscaux.org>
Date: Sun, 17 Oct 2021 21:27:48 +0200
Subject: [PATCH] Add acad_field and specialties to pool search form

---
 scipost_django/submissions/forms.py           | 32 +++++++++++++++++++
 .../templates/submissions/pool/pool2.html     |  1 -
 2 files changed, 32 insertions(+), 1 deletion(-)

diff --git a/scipost_django/submissions/forms.py b/scipost_django/submissions/forms.py
index 1ccc1ff59..4614a62d0 100644
--- a/scipost_django/submissions/forms.py
+++ b/scipost_django/submissions/forms.py
@@ -64,6 +64,19 @@ OSFPREPRINTS_IDENTIFIER_PATTERN = r'^[a-z0-9]+$'
 class SubmissionPoolSearchForm(forms.Form):
     """Filter a Submission queryset using basic search fields."""
 
+    acad_field = forms.ModelChoiceField(
+        queryset=AcademicField.objects.all(),
+        required=False
+    )
+    specialties = forms.ModelMultipleChoiceField(
+        queryset=Specialty.objects.all(),
+        widget=autocomplete.ModelSelect2Multiple(
+            url='/ontology/specialty-autocomplete',
+            attrs={'data-html': True}
+        ),
+        label='Specialties',
+        required=False
+    )
     author = forms.CharField(max_length=100, required=False, label="Author(s)")
     title = forms.CharField(max_length=100, required=False)
     identifier = forms.CharField(
@@ -76,6 +89,17 @@ class SubmissionPoolSearchForm(forms.Form):
         super().__init__(*args, **kwargs)
         self.helper = FormHelper()
         self.helper.layout = Layout(
+            Div(
+                Div(
+                    FloatingField('acad_field'),
+                    css_class='col-lg-6'
+                ),
+                Div(
+                    FloatingField('specialties'),
+                    css_class='col-lg-6'
+                ),
+                css_class='row mb-0'
+            ),
             Div(
                 Div(
                     FloatingField('author'),
@@ -103,6 +127,14 @@ class SubmissionPoolSearchForm(forms.Form):
     def search_results(self, user):
         """Return all Submission objects according to search."""
         submissions = Submission.objects.pool(user)
+        if self.cleaned_data.get('acad_field'):
+            submissions = submissions.filter(
+                acad_field=self.cleaned_data.get('acad_field')
+            )
+        if self.cleaned_data.get('specialties'):
+            submissions = submissions.filter(
+                specialties__in=self.cleaned_data.get('specialties')
+            )
         if self.cleaned_data.get('author'):
             submissions = submissions.filter(author_list__icontains=self.cleaned_data.get('author'))
         if self.cleaned_data.get('title'):
diff --git a/scipost_django/submissions/templates/submissions/pool/pool2.html b/scipost_django/submissions/templates/submissions/pool/pool2.html
index 58276c036..6e889a219 100644
--- a/scipost_django/submissions/templates/submissions/pool/pool2.html
+++ b/scipost_django/submissions/templates/submissions/pool/pool2.html
@@ -33,7 +33,6 @@
 	  hx-target="#search-results"
 	  hx-indicator="#indicator-search"
       >
-	{% csrf_token %}
 	{% crispy form %}
       </form>
     </div>
-- 
GitLab