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

feat: Add filter option to UserNode to filter users based on projects to which they belong #845

Merged
merged 3 commits into from
Oct 4, 2023
Merged
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
2 changes: 1 addition & 1 deletion terraso_backend/apps/graphql/schema/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ type Query {
): GroupAssociationNode!
groups(offset: Int, before: String, after: String, first: Int, last: Int, name: String, name_Icontains: String, name_Istartswith: String, slug: String, slug_Icontains: String, description_Icontains: String, memberships_Email: String, associatedLandscapes_IsDefaultLandscapeGroup: Boolean, associatedLandscapes_Isnull: Boolean, associatedLandscapes_IsPartnership: Boolean): GroupNodeConnection
landscapes(offset: Int, before: String, after: String, first: Int, last: Int, name_Icontains: String, description_Icontains: String, slug: String, slug_Icontains: String, website_Icontains: String, location_Icontains: String): LandscapeNodeConnection
users(offset: Int, before: String, after: String, first: Int, last: Int, email: String, email_Icontains: String, firstName_Icontains: String, lastName_Icontains: String): UserNodeConnection
users(offset: Int, before: String, after: String, first: Int, last: Int, email: String, email_Icontains: String, firstName_Icontains: String, lastName_Icontains: String, project: String): UserNodeConnection
landscapeGroups(offset: Int, before: String, after: String, first: Int, last: Int, landscape: ID, landscape_Slug_Icontains: String, group: ID, group_Slug_Icontains: String, isDefaultLandscapeGroup: Boolean, isPartnership: Boolean): LandscapeGroupNodeConnection
memberships(offset: Int, before: String, after: String, first: Int, last: Int, group: ID, group_In: [ID], group_Slug_Icontains: String, group_Slug_In: [String], user: ID, user_In: [ID], userRole: CoreMembershipUserRoleChoices, user_Email_Icontains: String, user_Email_In: [String], membershipStatus: CoreMembershipMembershipStatusChoices): MembershipNodeConnection
groupAssociations(offset: Int, before: String, after: String, first: Int, last: Int, parentGroup: ID, childGroup: ID, parentGroup_Slug_Icontains: String, childGroup_Slug_Icontains: String): GroupAssociationNodeConnection
Expand Down
18 changes: 14 additions & 4 deletions terraso_backend/apps/graphql/schema/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@
import graphene
import rules
import structlog
from django_filters import FilterSet
from graphene import relay
from graphene_django import DjangoObjectType
from graphene_django.filter import TypedFilter

from apps.auth.services import JWTService
from apps.core.models import User, UserPreference
Expand All @@ -35,19 +37,27 @@
logger = structlog.get_logger(__name__)


class UserNode(DjangoObjectType):
id = graphene.ID(source="pk", required=True)
class UserFilter(FilterSet):
project = TypedFilter(field_name="collaboration_memberships__membership_list__project")

class Meta:
model = User
filter_fields = {
fields = {
"email": ["exact", "icontains"],
"first_name": ["icontains"],
"last_name": ["icontains"],
}
fields = ("email", "first_name", "last_name", "profile_image", "memberships", "preferences")


class UserNode(DjangoObjectType):
id = graphene.ID(source="pk", required=True)

class Meta:
model = User
filterset_class = UserFilter
interfaces = (relay.Node,)
connection_class = TerrasoConnection
fields = ("email", "first_name", "last_name", "profile_image", "memberships", "preferences")


class UserPreferenceNode(DjangoObjectType):
Expand Down
20 changes: 20 additions & 0 deletions terraso_backend/tests/graphql/test_users.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,23 @@ def test_users_query_has_total_count(client_query, users):
total_count = response.json()["data"]["users"]["totalCount"]

assert total_count == len(users)


def test_users_query_in_project(client_query, project, project_user):
response = client_query(
"""
query userInProject($projectId: String!, $email: String!) {
users(project: $projectId, email: $email) {
edges {
node {
id
}
}
}
}
""",
variables={"projectId": str(project.id), "email": project_user.email},
)
contents = response.json()
assert "errors" not in contents
assert contents["data"]["users"]["edges"][0]["node"]["id"] == str(project_user.id)