-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2667 from bcgov/2525-contact-grid-bug
Put operator and operation in contacts grid
- Loading branch information
Showing
12 changed files
with
169 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
from typing import Optional | ||
from ninja import ModelSchema, Field | ||
from registration.models import Contact | ||
from ninja import FilterSchema | ||
|
||
|
||
class ContactListOutV2(ModelSchema): | ||
operators__legal_name: Optional[str] = None | ||
|
||
class Meta: | ||
model = Contact | ||
fields = ['id', 'first_name', 'last_name', 'email'] | ||
|
||
|
||
class ContactFilterSchemaV2(FilterSchema): | ||
# NOTE: we could simply use the `q` parameter to filter by related fields but, | ||
# due to this issue: https://github.com/vitalik/django-ninja/issues/1037 mypy is unhappy so I'm using the `json_schema_extra` parameter | ||
# If we want to achieve more by using the `q` parameter, we should use it and ignore the mypy error | ||
first_name: Optional[str] = Field(None, json_schema_extra={'q': 'first_name__icontains'}) | ||
last_name: Optional[str] = Field(None, json_schema_extra={'q': 'last_name__icontains'}) | ||
email: Optional[str] = Field(None, json_schema_extra={'q': 'email__icontains'}) | ||
operators__legal_name: Optional[str] = Field(None, json_schema_extra={'q': 'operators__legal_name__icontains'}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
from typing import Optional, cast | ||
from django.db.models import QuerySet | ||
from uuid import UUID | ||
|
||
|
||
from registration.models.contact import Contact | ||
from registration.schema.v2.contact import ContactFilterSchemaV2 | ||
from service.data_access_service.contact_service import ContactDataAccessService | ||
from service.data_access_service.user_service import UserDataAccessService | ||
from ninja import Query | ||
|
||
|
||
class ContactServiceV2: | ||
@classmethod | ||
def list_contacts_v2( | ||
cls, | ||
user_guid: UUID, | ||
sort_field: Optional[str], | ||
sort_order: Optional[str], | ||
filters: ContactFilterSchemaV2 = Query(...), | ||
) -> QuerySet[Contact]: | ||
user = UserDataAccessService.get_by_guid(user_guid) | ||
sort_direction = "-" if sort_order == "desc" else "" | ||
sort_by = f"{sort_direction}{sort_field}" | ||
base_qs = ContactDataAccessService.get_all_contacts_for_user(user) | ||
# we have filter before .values or else we'll get duplicate rows from the m2m relationship between operations_contacts and operators | ||
queryset = ( | ||
filters.filter(base_qs) | ||
.order_by(sort_by) | ||
.values('id', 'first_name', 'last_name', 'email', 'operators__legal_name') | ||
.distinct() | ||
) | ||
return cast(QuerySet[Contact], queryset) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
from registration.schema.v1.contact import ContactFilterSchema | ||
import pytest | ||
from service.contact_service_v2 import ContactServiceV2 | ||
from model_bakery import baker | ||
|
||
pytestmark = pytest.mark.django_db | ||
|
||
|
||
class TestListContactService: | ||
@staticmethod | ||
def test_list_contacts(): | ||
|
||
user = baker.make_recipe('utils.cas_admin') | ||
contact1 = baker.make_recipe('utils.contact') | ||
contact2 = baker.make_recipe('utils.contact') | ||
|
||
operator1 = baker.make_recipe('utils.operator') | ||
|
||
operator2a = baker.make_recipe('utils.operator') | ||
operator2b = baker.make_recipe('utils.operator') | ||
|
||
# contact 1 is associated with one operator, count = 1 | ||
operator1.contacts.set([contact1]) | ||
|
||
# contact 2 belongs to two operators count = 3 | ||
operator2a.contacts.set([contact2]) | ||
operator2b.contacts.set([contact2]) | ||
assert ( | ||
ContactServiceV2.list_contacts_v2( | ||
user_guid=user.user_guid, sort_field="created_at", sort_order="desc", filters=ContactFilterSchema() | ||
).count() | ||
== 3 | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.