Skip to content
This repository has been archived by the owner on Feb 28, 2018. It is now read-only.

Commit

Permalink
Merge branch 'master' into receipt-filter
Browse files Browse the repository at this point in the history
  • Loading branch information
cuducos authored Oct 7, 2017
2 parents 64b9d8a + 10f48a9 commit f95addd
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 18 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down Expand Up @@ -292,4 +293,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).
instructions](https://python-twitter.readthedocs.io/en/latest/getting_started.html#getting-your-application-tokens).
5 changes: 3 additions & 2 deletions jarbas/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 receipt_url
receipt_url = self._bool_param('receipt_url')
Expand Down
12 changes: 8 additions & 4 deletions jarbas/core/querysets.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,16 @@ 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, boolean):
if not boolean:
return self.filter(suspicions=None)
return self.exclude(suspicions=None)

def has_receipt_url(self, boolean):
return self.exclude(receipt_url=None) if boolean else self.filter(receipt_url=None)

def suspicions(self):
return self.exclude(suspicions=None)
if not boolean:
return self.filter(receipt_url=None)
return self.exclude(receipt_url=None)

def in_latest_dataset(self, boolean):
return self.filter(available_in_latest_dataset=boolean)
Expand Down
23 changes: 22 additions & 1 deletion jarbas/core/tests/test_reimbursement_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -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_has_receipt_url(self):
# let's create a reimbursement with some receipt_url (self.data has none)
Expand Down
24 changes: 14 additions & 10 deletions jarbas/dashboard/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,11 @@ class SuspiciousListFilter(JarbasListFilter):
)

def queryset(self, request, queryset):
return queryset.suspicions() if self.value() == 'yes' else queryset
filter_option = {
'yes': queryset.suspicions(True),
'no': queryset.suspicions(False)
}
return filter_option.get(self.value(), queryset)


class HasReceiptFilter(JarbasListFilter):
Expand Down Expand Up @@ -394,17 +398,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)

0 comments on commit f95addd

Please sign in to comment.