From a460674c772c2c0685148c8eb94b1743afea71d5 Mon Sep 17 00:00:00 2001 From: Eduardo Cuducos Date: Thu, 28 Sep 2017 21:39:46 -0300 Subject: [PATCH 1/6] Fix bug when using full text search w/ filters --- jarbas/dashboard/admin.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/jarbas/dashboard/admin.py b/jarbas/dashboard/admin.py index cba1f5d..fca25d8 100644 --- a/jarbas/dashboard/admin.py +++ b/jarbas/dashboard/admin.py @@ -370,17 +370,17 @@ def formfield_for_dbfield(self, db_field, **kwargs): return super().formfield_for_dbfield(db_field, **kwargs) def get_search_results(self, request, queryset, search_term): - if not search_term: - return super(ReimbursementModelAdmin, self) \ - .get_search_results(request, queryset, search_term) + queryset, distinct = super(ReimbursementModelAdmin, self) \ + .get_search_results(request, queryset, None) - query = SearchQuery(search_term, config='portuguese') - rank = SearchRank(F('search_vector'), query) - queryset = Reimbursement.objects.annotate(rank=rank) \ - .filter(search_vector=query) \ - .order_by('-rank') + if search_term: + query = SearchQuery(search_term, config='portuguese') + rank = SearchRank(F('search_vector'), query) + queryset = queryset.annotate(rank=rank) \ + .filter(search_vector=query) \ + .order_by('-rank') - return queryset, False + return queryset, distinct dashboard.register(Reimbursement, ReimbursementModelAdmin) From df68579ef571fef2c45374ebd26a68a0dd95d79e Mon Sep 17 00:00:00 2001 From: Giovani Date: Mon, 2 Oct 2017 20:00:14 -0300 Subject: [PATCH 2/6] fix 'no' option for suspicions filter --- jarbas/core/querysets.py | 4 ++-- jarbas/dashboard/admin.py | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/jarbas/core/querysets.py b/jarbas/core/querysets.py index 8b61bea..22d617f 100644 --- a/jarbas/core/querysets.py +++ b/jarbas/core/querysets.py @@ -29,8 +29,8 @@ def list_distinct(self, field, order_by_field, query=None): self = self.values(field, order_by_field).order_by(order_by_field) return self.distinct() - def suspicions(self): - return self.exclude(suspicions=None) + def suspicions(self, boolean): + return self.exclude(suspicions=None) if boolean else self.filter(suspicions=None) def in_latest_dataset(self, boolean): return self.filter(available_in_latest_dataset=boolean) diff --git a/jarbas/dashboard/admin.py b/jarbas/dashboard/admin.py index cba1f5d..9e3ff2c 100644 --- a/jarbas/dashboard/admin.py +++ b/jarbas/dashboard/admin.py @@ -85,7 +85,7 @@ class SuspiciousListFilter(JarbasListFilter): ) def queryset(self, request, queryset): - return queryset.suspicions() if self.value() == 'yes' else queryset + return queryset.suspicions(self.value() == 'yes') if self.value() else queryset class MonthListFilter(JarbasListFilter): From 720365772a025f9bec882331823a2d556271169e Mon Sep 17 00:00:00 2001 From: Giovani Date: Mon, 2 Oct 2017 20:00:49 -0300 Subject: [PATCH 3/6] add tests --- jarbas/core/tests/test_reimbursement_model.py | 23 ++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/jarbas/core/tests/test_reimbursement_model.py b/jarbas/core/tests/test_reimbursement_model.py index 37c6b1f..8f91e34 100644 --- a/jarbas/core/tests/test_reimbursement_model.py +++ b/jarbas/core/tests/test_reimbursement_model.py @@ -85,9 +85,30 @@ def test_suspicions(self): data['document_id'] = 42 * 2 del data['suspicions'] del data['probability'] + Reimbursement.objects.create(**self.data) + Reimbursement.objects.create(**data) + self.assertEqual(1, Reimbursement.objects.suspicions(True).count()) + + def test_not_suspicions(self): + data = self.data.copy() + data['document_id'] = 42 * 2 + del data['suspicions'] + del data['probability'] + Reimbursement.objects.create(**self.data) Reimbursement.objects.create(**data) + self.assertEqual(1, Reimbursement.objects.suspicions(False).count()) + + def test_suspicions_filter(self): + data = self.data.copy() + data['document_id'] = 42 * 2 + del data['suspicions'] + del data['probability'] Reimbursement.objects.create(**self.data) - self.assertEqual(1, Reimbursement.objects.suspicions().count()) + Reimbursement.objects.create(**data) + suspects = Reimbursement.objects.suspicions(True) + not_suspects = Reimbursement.objects.suspicions(False) + intersection = suspects & not_suspects + self.assertEqual(0, intersection.count()) def test_in_latest_dataset(self): data = self.data.copy() From 08e4323ceed9db24e95aacc49a7b3e401628970e Mon Sep 17 00:00:00 2001 From: Giovani Date: Mon, 2 Oct 2017 20:02:21 -0300 Subject: [PATCH 4/6] Add the new param to the use of the queryset --- jarbas/api/views.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/jarbas/api/views.py b/jarbas/api/views.py index 072222f..c33fb84 100644 --- a/jarbas/api/views.py +++ b/jarbas/api/views.py @@ -27,8 +27,9 @@ def get(self, request): filters = {k: v for k, v in zip(params, values) if v} # filter suspicions - if self._bool_param('suspicions'): - self.queryset = self.queryset.suspicions() + suspicions = self._bool_param('suspicions') + if suspicions is not None: + self.queryset = self.queryset.suspicions(suspicions) # filter reimbursement in latest dataset in_latest = self._bool_param('in_latest_dataset') From afa33b763b339b7c59cb3d93978d91b4a452f49e Mon Sep 17 00:00:00 2001 From: Steve Boyd Date: Mon, 2 Oct 2017 21:38:28 -0400 Subject: [PATCH 5/6] Update README.md Add Donate tag to README. #hacktoberfest #first-timers-only --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 23ed49c..05f78a2 100644 --- a/README.md +++ b/README.md @@ -4,6 +4,7 @@ [![Code Climate](https://codeclimate.com/github/datasciencebr/jarbas/badges/gpa.svg)](https://codeclimate.com/github/datasciencebr/jarbas) [![Coverage Status](https://coveralls.io/repos/github/datasciencebr/jarbas/badge.svg?branch=master)](https://coveralls.io/github/datasciencebr/jarbas?branch=master) [![Updates](https://pyup.io/repos/github/datasciencebr/jarbas/shield.svg)](https://pyup.io/repos/github/datasciencebr/jarbas/) +[![donate](https://img.shields.io/badge/donate-apoia.se-EB4A3B.svg)](https://apoia.se/serenata) [Jarbas](http://jarbas.serenatadeamor.org/) is part of [Serenata de Amor](http://github.com/datasciencebr/serenata-de-amor) — we fight corruption with data science. @@ -291,4 +292,4 @@ If **you are not** using Docker copy `contrib/.env.sample` as `.env` in the proj * `TWITTER_ACCESS_SECRET` (_str_) Twitter access token secret To get this credentials follow [`python-twitter` -instructions](https://python-twitter.readthedocs.io/en/latest/getting_started.html#getting-your-application-tokens). \ No newline at end of file +instructions](https://python-twitter.readthedocs.io/en/latest/getting_started.html#getting-your-application-tokens). From a44084ac3083581465915342abd3d69cb3850e47 Mon Sep 17 00:00:00 2001 From: Giovani Date: Wed, 4 Oct 2017 18:10:27 -0300 Subject: [PATCH 6/6] Enhance readability for suspicions queryset --- jarbas/dashboard/admin.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/jarbas/dashboard/admin.py b/jarbas/dashboard/admin.py index 9e3ff2c..c5bf979 100644 --- a/jarbas/dashboard/admin.py +++ b/jarbas/dashboard/admin.py @@ -85,7 +85,11 @@ class SuspiciousListFilter(JarbasListFilter): ) def queryset(self, request, queryset): - return queryset.suspicions(self.value() == 'yes') if self.value() else queryset + filter_option = { + 'yes': queryset.suspicions(True), + 'no': queryset.suspicions(False) + } + return filter_option.get(self.value(), queryset) class MonthListFilter(JarbasListFilter):