Skip to content

Commit

Permalink
Add excludeMe in user filter
Browse files Browse the repository at this point in the history
  • Loading branch information
thenav56 committed Jul 31, 2023
1 parent f3eaaea commit f441f2d
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 4 deletions.
8 changes: 8 additions & 0 deletions apps/user/filters.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import strawberry
import strawberry_django
from strawberry.types import Info
from django.db import models
from django.db.models.functions import Concat

Expand All @@ -11,6 +12,7 @@ class UserFilter:
id: strawberry.auto
search: str | None
members_exclude_project: strawberry.ID | None
exclude_me: bool = False

def filter_search(self, queryset):
value = self.search
Expand All @@ -37,3 +39,9 @@ def filter_members_exclude_project(self, queryset):
~models.Q(projectmembership__project_id=value)
).distinct()
return queryset

def filter_exclude_me(self, queryset, info: Info):
value = self.exclude_me
if value:
queryset = queryset.exclude(id=info.context.request.user.id)
return queryset
3 changes: 2 additions & 1 deletion apps/user/queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from asgiref.sync import sync_to_async
from utils.strawberry.paginations import CountList, pagination_field

from .types import UserType, UserMeType
from .types import UserType, UserMeType, UserOrder
from .filters import UserFilter


Expand All @@ -26,4 +26,5 @@ class PrivateQuery:
users: CountList[UserType] = pagination_field(
pagination=True,
filters=UserFilter,
order=UserOrder,
)
6 changes: 4 additions & 2 deletions apps/user/tests/test_queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class Query:
USERS = '''
query MyQuery($filters: UserFilter) {
private {
users(pagination: {limit: 10, offset: 0}, filters: $filters) {
users(order: {id: ASC}, pagination: {limit: 10, offset: 0}, filters: $filters) {
limit
offset
count
Expand Down Expand Up @@ -94,7 +94,9 @@ def test_users(self):
({'search': '@vil'}, [user2]),
({'search': 'sample'}, [user1, user2]),
({'search': 'sample@'}, [user1, user2]),
({'membersExcludeProject': str(project.pk)}, [user2, user3]),
({'membersExcludeProject': str(project.pk)}, [self.user, user2, user3]),
({}, [self.user, *self.users]),
({'excludeMe': True}, self.users),
]:
content = self.query_check(self.Query.USERS, variables={'filters': filters})
assert content['data']['private']['users'] == {
Expand Down
5 changes: 5 additions & 0 deletions apps/user/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@
from .enums import OptEmailNotificationTypeEnum


@strawberry_django.ordering.order(User)
class UserOrder:
id: strawberry.auto


@strawberry_django.type(User)
class UserType:
id: strawberry.ID
Expand Down
7 changes: 6 additions & 1 deletion schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ type PrivateMutation {

type PrivateQuery {
user: UserType!
users(filters: UserFilter, pagination: OffsetPaginationInput): UserTypeCountList!
users(filters: UserFilter, order: UserOrder, pagination: OffsetPaginationInput): UserTypeCountList!
projects(filters: ProjectFilter, order: ProjectOrder, pagination: OffsetPaginationInput): ProjectTypeCountList!
projectScope(pk: ID!): ProjectScopeType
id: ID!
Expand Down Expand Up @@ -272,6 +272,7 @@ input UserFilter {
id: IDFilterLookup
search: String
membersExcludeProject: ID
excludeMe: Boolean = false
}

input UserMeInput {
Expand All @@ -295,6 +296,10 @@ type UserMeTypeMutationResponseType {
result: UserMeType
}

input UserOrder {
id: Ordering
}

type UserType {
id: ID!
firstName: String!
Expand Down

0 comments on commit f441f2d

Please sign in to comment.