From 4b65a3a020dc657ef07dd438ac3a2c96812bc274 Mon Sep 17 00:00:00 2001 From: Jorran de Wit <jorrandewit@outlook.com> Date: Thu, 4 Apr 2019 18:48:11 +0200 Subject: [PATCH] Fix search queries mostly empty --- scipost/forms.py | 3 ++- submissions/managers.py | 40 ++++++++++++++++++++-------------------- 2 files changed, 22 insertions(+), 21 deletions(-) diff --git a/scipost/forms.py b/scipost/forms.py index 79d1074f8..853ad7010 100644 --- a/scipost/forms.py +++ b/scipost/forms.py @@ -716,6 +716,7 @@ class SearchForm(HayStackSearchForm): start = forms.DateField(widget=MonthYearWidget(), required=False) # Month end = forms.DateField(widget=MonthYearWidget(end=True), required=False) # Month + def search(self): if not self.is_valid(): return self.no_query_found() @@ -723,7 +724,7 @@ class SearchForm(HayStackSearchForm): if not self.cleaned_data.get("q"): return self.no_query_found() - sqs = self.searchqueryset.filter(content__contains=self.cleaned_data["q"]) + sqs = self.searchqueryset.auto_query(self.cleaned_data["q"]) if self.load_all: sqs = sqs.load_all() diff --git a/submissions/managers.py b/submissions/managers.py index 5be237945..d3f1adc90 100644 --- a/submissions/managers.py +++ b/submissions/managers.py @@ -15,23 +15,23 @@ now = timezone.now() class SubmissionQuerySet(models.QuerySet): - # def _newest_version_only(self, queryset): - # """ - # TODO: Make more efficient... with agregation or whatever. - # - # The current Queryset should return only the latest version - # of the Arxiv submissions known to SciPost. - # - # Method only compatible with PostGresQL - # """ - # # This method used a double query, which is a consequence of the complex distinct() - # # filter combined with the PostGresQL engine. Without the double query, ordering - # # on a specific field after filtering seems impossible. - # ids = (queryset - # .order_by('preprint__identifier_wo_vn_nr', '-preprint__vn_nr') - # .distinct('preprint__identifier_wo_vn_nr') - # .values_list('id', flat=True)) - # return queryset.filter(id__in=ids) + def _newest_version_only(self, queryset): + """ + TODO: Make more efficient... with agregation or whatever. + + The current Queryset should return only the latest version + of the Arxiv submissions known to SciPost. + + Method only compatible with PostGresQL + """ + # This method used a double query, which is a consequence of the complex distinct() + # filter combined with the PostGresQL engine. Without the double query, ordering + # on a specific field after filtering seems impossible. + ids = (queryset + .order_by('preprint__identifier_wo_vn_nr', '-preprint__vn_nr') + .distinct('preprint__identifier_wo_vn_nr') + .values_list('id', flat=True)) + return queryset.filter(id__in=ids) def user_filter(self, user): """Filter on basic conflict of interests. @@ -143,7 +143,7 @@ class SubmissionQuerySet(models.QuerySet): This query contains set of public() submissions, filtered to only the newest available version. """ - return self.filter(is_resubmission_of__isnull=True).public() + return self._newest_version_only(self.public()) def treated(self): """This query returns all Submissions that are presumed to be 'done'.""" @@ -189,11 +189,11 @@ class SubmissionQuerySet(models.QuerySet): def rejected(self): """Return rejected Submissions.""" - return self.filter(is_resubmission_of__isnull=True, status=constants.STATUS_REJECTED) + return self._newest_version_only(self.filter(status=constants.STATUS_REJECTED)) def withdrawn(self): """Return withdrawn Submissions.""" - return self.filter(is_resubmission_of__isnull=True, status=constants.STATUS_WITHDRAWN) + return self._newest_version_only(self.filter(status=constants.STATUS_WITHDRAWN)) def open_for_reporting(self): """Return Submission that allow for reporting.""" -- GitLab