Skip to content

Commit

Permalink
fix: Use ProjectMembershipNodes in Project mutation return types (#860)
Browse files Browse the repository at this point in the history
This matches what the frontend expects, especially wrt to roles etc. I
had to redefine the utility method `get_member` to explicity use the
ProjectMembership object manager. There might be a more elegant way of
doing this with extending object managers, but I think that this will
do the trick for now.
  • Loading branch information
David Code Howard authored Oct 3, 2023
1 parent 404db6b commit 49e1584
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 16 deletions.
7 changes: 5 additions & 2 deletions terraso_backend/apps/collaboration/models/memberships.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,10 @@ class MembershipList(BaseModel):
default=DEFAULT_MEMERBSHIP_TYPE,
)

def save_membership(self, user_email, user_role, membership_status, validation_func):
def save_membership(
self, user_email, user_role, membership_status, validation_func, membership_class=None
):
membership_class = membership_class or Membership
user = User.objects.filter(email=user_email).first()
user_exists = user is not None

Expand All @@ -73,7 +76,7 @@ def save_membership(self, user_email, user_role, membership_status, validation_f
raise ValidationError("User cannot request membership")

if is_new:
membership = Membership(
membership = membership_class(
membership_list=self,
user=user if user_exists else None,
pending_email=user_email if not user_exists else None,
Expand Down
6 changes: 3 additions & 3 deletions terraso_backend/apps/graphql/schema/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -1861,7 +1861,7 @@ input ProjectDeleteMutationInput {
type ProjectAddUserMutationPayload {
errors: GenericScalar
project: ProjectNode!
membership: CollaborationMembershipNode!
membership: ProjectMembershipNode!
clientMutationId: String
}

Expand All @@ -1875,7 +1875,7 @@ input ProjectAddUserMutationInput {
type ProjectDeleteUserMutationPayload {
errors: GenericScalar
project: ProjectNode!
membership: CollaborationMembershipNode!
membership: ProjectMembershipNode!
clientMutationId: String
}

Expand All @@ -1888,7 +1888,7 @@ input ProjectDeleteUserMutationInput {
type ProjectUpdateUserRoleMutationPayload {
errors: GenericScalar
project: ProjectNode!
membership: CollaborationMembershipNode!
membership: ProjectMembershipNode!
clientMutationId: String
}

Expand Down
18 changes: 8 additions & 10 deletions terraso_backend/apps/project_management/graphql/projects.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,6 @@
from graphene_django.filter import DjangoFilterConnectionField, TypedFilter

from apps.audit_logs import api as log_api
from apps.collaboration.graphql.memberships import (
CollaborationMembershipNode as MembershipNode,
)
from apps.collaboration.graphql.memberships import (
MembershipListNodeMixin,
MembershipNodeMixin,
Expand Down Expand Up @@ -73,7 +70,7 @@ def resolve_user_role(self, info):
match self.user_role:
case "viewer":
return UserRole.viewer
case "constributor":
case "contributor":
return UserRole.contributor
case "manager":
return UserRole.manager
Expand Down Expand Up @@ -287,9 +284,9 @@ def mutate_and_get_payload(cls, root, info, **kwargs):

class ProjectAddUserMutation(BaseWriteMutation):
project = graphene.Field(ProjectNode, required=True)
membership = graphene.Field(MembershipNode, required=True)
membership = graphene.Field(ProjectMembershipNode, required=True)

model_class = Membership
model_class = ProjectMembership

class Input:
project_id = graphene.ID(required=True)
Expand Down Expand Up @@ -322,9 +319,10 @@ def validate(context):
user_role=role.value,
membership_status=Membership.APPROVED,
validation_func=validate,
membership_class=ProjectMembership,
)
except ValidationError as e:
cls.not_allowed_create(model=Membership, msg=e.message)
cls.not_allowed_create(model=cls.model_class, msg=e.message)

membership_added_signal.send(sender=cls, membership=membership, user=request_user)

Expand All @@ -333,7 +331,7 @@ def validate(context):

class ProjectDeleteUserMutation(BaseWriteMutation):
project = graphene.Field(ProjectNode, required=True)
membership = graphene.Field(MembershipNode, required=True)
membership = graphene.Field(ProjectMembershipNode, required=True)

model_class = Membership

Expand Down Expand Up @@ -380,10 +378,10 @@ def mutate_and_get_payload(cls, root, info, project_id, user_id):


class ProjectUpdateUserRoleMutation(BaseWriteMutation):
model_class = Membership
model_class = ProjectMembership

project = graphene.Field(ProjectNode, required=True)
membership = graphene.Field(MembershipNode, required=True)
membership = graphene.Field(ProjectMembershipNode, required=True)

class Input:
project_id = graphene.ID(required=True)
Expand Down
4 changes: 3 additions & 1 deletion terraso_backend/apps/project_management/models/projects.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,9 @@ def add_user_with_role(self, user: User, role: str):
)

def get_membership(self, user: User):
return self.membership_list.memberships.filter(user=user).first()
return ProjectMembership.objects.filter(
membership_list=self.membership_list, user=user
).first()

def mark_seen_by(self, user: User):
self.seen_by.add(user)
Expand Down

0 comments on commit 49e1584

Please sign in to comment.