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: OPTIC-1360: [BE] create Account settings endpoints that handle existing functionality in the django template #6736

Draft
wants to merge 4 commits into
base: develop
Choose a base branch
from
Draft
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
31 changes: 28 additions & 3 deletions label_studio/organizations/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from organizations.serializers import (
OrganizationIdSerializer,
OrganizationInviteSerializer,
OrganizationMemberSerializer,
OrganizationMemberUserSerializer,
OrganizationSerializer,
OrganizationsParamsSerializer,
Expand All @@ -25,6 +26,7 @@
from rest_framework.parsers import FormParser, JSONParser, MultiPartParser
from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response
from rest_framework.settings import api_settings
from rest_framework.views import APIView
from users.models import User

Expand Down Expand Up @@ -160,13 +162,36 @@ def get_queryset(self):
)
class OrganizationMemberDetailAPI(GetParentObjectMixin, generics.RetrieveDestroyAPIView):
permission_required = ViewClassPermission(
GET=all_permissions.organizations_view,
DELETE=all_permissions.organizations_change,
)
parent_queryset = Organization.objects.all()
parser_classes = (JSONParser, FormParser, MultiPartParser)
permission_classes = (IsAuthenticated, HasObjectPermission)
serializer_class = OrganizationMemberUserSerializer # Assuming this is the right serializer
http_method_names = ['delete']
serializer_class = OrganizationMemberSerializer
http_method_names = ['delete', 'get']

@property
def permission_classes(self):
if self.request.method == 'DELETE':
return [IsAuthenticated, HasObjectPermission]
return api_settings.DEFAULT_PERMISSION_CLASSES

def get_queryset(self):
return OrganizationMember.objects.filter(organization=self.get_parent_object())

def get_serializer_context(self):
return {
**super().get_serializer_context(),
'organization': self.get_parent_object(),
}

def get(self, request, pk, user_pk):
queryset = self.get_queryset()
user = get_object_or_404(User, pk=user_pk)
member = get_object_or_404(queryset, user=user)
self.check_object_permissions(request, member)
serializer = self.get_serializer(member)
return Response(serializer.data)

def delete(self, request, pk=None, user_pk=None):
org = self.get_parent_object()
Expand Down
25 changes: 18 additions & 7 deletions label_studio/organizations/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
class OrganizationIdSerializer(DynamicFieldsMixin, serializers.ModelSerializer):
class Meta:
model = Organization
fields = ['id', 'title', 'contact_info']
fields = ['id', 'title', 'contact_info', 'created_at']


class OrganizationSerializer(DynamicFieldsMixin, serializers.ModelSerializer):
Expand All @@ -21,12 +21,6 @@ class Meta:
fields = '__all__'


class OrganizationMemberSerializer(DynamicFieldsMixin, serializers.ModelSerializer):
class Meta:
model = OrganizationMember
fields = ['id', 'organization', 'user']


class UserSerializerWithProjects(UserSerializer):
created_projects = serializers.SerializerMethodField(read_only=True)
contributed_to_projects = serializers.SerializerMethodField(read_only=True)
Expand Down Expand Up @@ -104,6 +98,23 @@ class Meta:
fields = ['id', 'organization', 'user']


class OrganizationMemberSerializer(DynamicFieldsMixin, serializers.ModelSerializer):
annotations_count = serializers.SerializerMethodField(read_only=True)
contributed_projects_count = serializers.SerializerMethodField(read_only=True)

def get_annotations_count(self, member):
org = self.context.get('organization')
return member.user.annotations.filter(project__organization=org).count()

def get_contributed_projects_count(self, member):
org = self.context.get('organization')
return member.user.annotations.filter(project__organization=org).values('project').distinct().count()

class Meta:
model = OrganizationMember
fields = ['user', 'organization', 'contributed_projects_count', 'annotations_count', 'created_at']


class OrganizationInviteSerializer(serializers.Serializer):
token = serializers.CharField(required=False)
invite_url = serializers.CharField(required=False)
Expand Down
42 changes: 42 additions & 0 deletions label_studio/tests/test_organizations.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
"""This file and its contents are licensed under the Apache License 2.0. Please see the included NOTICE for copyright information and LICENSE for a copy of the license.
"""
import pytest
from organizations.models import Organization, OrganizationMember
from tasks.models import Task
from tests.utils import make_annotation
from users.models import User


@pytest.mark.django_db
Expand All @@ -16,3 +20,41 @@ def test_api_list_organizations(business_client):
response_data = response.json()
assert len(response_data) == 1
assert response_data[0]['id'] == business_client.organization.id


@pytest.mark.django_db
def test_organization_member_retrieve_same_user(business_client, configured_project):
user = business_client.user
organization = business_client.organization
task = Task.objects.filter(project=configured_project).first()
make_annotation({'completed_by': user}, task_id=task.id)
response = business_client.get(f'/api/organizations/{organization.id}/memberships/{user.id}/')
response_data = response.json()
assert response_data['user'] == user.id
assert response_data['organization'] == organization.id
assert response_data['annotations_count'] == 1
assert response_data['contributed_projects_count'] == 1


@pytest.mark.django_db
def test_organization_member_retrieve_other_user_in_org(business_client):
organization = business_client.organization
other_user = User.objects.create(email='[email protected]')
OrganizationMember.objects.create(user=other_user, organization=organization)
response = business_client.get(f'/api/organizations/{organization.id}/memberships/{other_user.id}/')
response_data = response.json()
print(response_data)
assert response_data['user'] == other_user.id
assert response_data['organization'] == organization.id
assert response_data['annotations_count'] == 0
assert response_data['contributed_projects_count'] == 0


@pytest.mark.django_db
def test_organization_member_retrieve_not_active_org(business_client):
user = business_client.user
other_user = User.objects.create(email='[email protected]')
other_organization = Organization.create_organization(created_by=other_user)
OrganizationMember.objects.create(user=user, organization=other_organization)
response = business_client.get(f'/api/organizations/{other_organization.id}/memberships/{user.id}/')
assert response.status_code == 403
2 changes: 1 addition & 1 deletion label_studio/tests/webhooks/organizations.tavern.yml
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ stages:

- name: soft_delete_user_fails_second_deletion_attempt
request:
url: "{django_live_url}/api/users/{user_pk}/soft-delete"
url: "{django_live_url}/api/organizations/{org_pk}/memberships/{user_pk}"
method: DELETE
response:
status_code: 404
Expand Down
1 change: 1 addition & 0 deletions label_studio/users/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ class Meta:
'phone',
'active_organization',
'allow_newsletters',
'date_joined',
)


Expand Down
Loading