Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added contract search filters #68

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion contratospr/api/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,10 +185,15 @@ class EntityFilter(django_filters.FilterSet):
queryset=Contractor.objects.all(),
method="filter_contractors_by_id",
)
# source_id = django_filters.NumberFilter()
source_id = django_filters.ModelMultipleChoiceFilter(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@froi no creo que podemos hacer esto aqui. Este filter espera un arreglo de entity ids no de source ids. Maybe podemos usar un TypedMultipleChoiceFilter while keeping the filter_entities_source_id method.

queryset=Entity.objects.all().only("source_id"),
method="filter_entities_source_id",
)

class Meta:
model = Entity
fields = ["id", "contractor_id"]
fields = ["id", "contractor_id", "source_id"]

def filter_entities(self, queryset, name, value):
if not value:
Expand All @@ -203,6 +208,13 @@ def filter_contractors_by_id(self, queryset, name, value):

return queryset.filter(contract__contractors__in=value).distinct()

def filter_entities_source_id(self, queryset, name, value):
if not value:
return queryset

source_ids = [entity.source_id for entity in value]
return queryset.filter(source_id__in=source_ids)


class ServiceFilter(django_filters.FilterSet):
id = django_filters.ModelMultipleChoiceFilter(
Expand Down
8 changes: 7 additions & 1 deletion contratospr/contracts/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def index_contract(obj):
return contract.save(update_fields=["search_vector"])


def search_contracts(query, service_id, service_group_id):
def search_contracts(query, service_id, service_group_id, entity_id, entity_source_id):
filter_kwargs = {}

if query:
Expand All @@ -38,6 +38,12 @@ def search_contracts(query, service_id, service_group_id):
if service_group_id:
filter_kwargs["service__group_id"] = service_group_id

if entity_id:
filter_kwargs["entity__id"] = entity_id

if entity_source_id:
filter_kwargs["entity__source_id"] = entity_source_id

if not filter_kwargs:
return []

Expand Down