Skip to content

Commit

Permalink
Exclude amendments from totals and counts
Browse files Browse the repository at this point in the history
  • Loading branch information
jpadilla committed Aug 21, 2019
1 parent 853ccb4 commit a9573be
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 12 deletions.
22 changes: 16 additions & 6 deletions contratospr/api/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,13 @@ def filter_entities(self, queryset, name, value):
.distinct()
.annotate(
contracts_total=Sum(
"contract__amount_to_pay", filter=Q(contract__entity__in=value)
"contract__amount_to_pay",
filter=Q(contract__parent=None, contract__entity__in=value),
),
contracts_count=Count(
"contract",
filter=Q(contract__parent=None, contract__entity__in=value),
),
contracts_count=Count("contract", filter=Q(contract__entity__in=value)),
)
)

Expand Down Expand Up @@ -224,9 +228,12 @@ def filter_entity_by_id(self, queryset, name, value):
.distinct()
.annotate(
contracts_total=Sum(
"contract__amount_to_pay", filter=Q(contract__in=contracts)
"contract__amount_to_pay",
filter=Q(contract__parent=None, contract__in=contracts),
),
contracts_count=Count(
"contract", filter=Q(contract__parent=None, contract__in=contracts)
),
contracts_count=Count("contract", filter=Q(contract__in=contracts)),
)
)

Expand All @@ -241,8 +248,11 @@ def filter_contractors_by_id(self, queryset, name, value):
.distinct()
.annotate(
contracts_total=Sum(
"contract__amount_to_pay", filter=Q(contract__in=contracts)
"contract__amount_to_pay",
filter=Q(contract__parent=None, contract__in=contracts),
),
contracts_count=Count(
"contract", filter=Q(contract__parent=None, contract__in=contracts)
),
contracts_count=Count("contract", filter=Q(contract__in=contracts)),
)
)
7 changes: 3 additions & 4 deletions contratospr/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,8 @@ def get(self, request, format=None):
start_date, end_date = get_fiscal_year_range(fiscal_year)

contracts = (
Contract.objects.select_related(
Contract.objects.without_amendments()
.select_related(
"document",
"entity",
"service",
Expand All @@ -142,9 +143,7 @@ def get(self, request, format=None):
"contractors", "parent__contractors", "amendments", "parent__amendments"
)
.filter(
parent=None,
effective_date_from__gte=start_date,
effective_date_from__lte=end_date,
effective_date_from__gte=start_date, effective_date_from__lte=end_date
)
)

Expand Down
3 changes: 1 addition & 2 deletions contratospr/api/viewsets.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,7 @@ def spending_over_time(self, request):
)

queryset = (
# TODO: should amendments be excluded?
queryset.filter(parent=None)
queryset.without_amendments()
.annotate(month=TruncMonth("date_of_grant"))
.values("month")
.annotate(total=Sum("amount_to_pay"), count=Count("id"))
Expand Down
3 changes: 3 additions & 0 deletions contratospr/contracts/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from ..utils.filepreviews import FilePreviews
from ..utils.models import BaseModel
from ..utils.pdftotext import pdf_to_text
from .queryset import ContractQuerySet

if settings.FILEPREVIEWS_API_KEY and settings.FILEPREVIEWS_API_SECRET:
filepreviews = FilePreviews(
Expand Down Expand Up @@ -257,6 +258,8 @@ class Contract(BaseModel):

search_vector = SearchVectorField(null=True)

objects = ContractQuerySet.as_manager()

class Meta:
indexes = [GinIndex(fields=["search_vector"])]

Expand Down
11 changes: 11 additions & 0 deletions contratospr/contracts/queryset.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from django.db import models


class ContractQuerySet(models.QuerySet):
def amendments(self):
# Amended contracts
return self.filter(parent__isnull=False)

def without_amendments(self):
# Contracts excluding amendments
return self.filter(parent=None)

0 comments on commit a9573be

Please sign in to comment.