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

Merged
merged 9 commits into from
Dec 4, 2024
50 changes: 47 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 @@ -135,6 +137,25 @@ def get_queryset(self):
return org.members.order_by('user__username')


@method_decorator(
name='get',
decorator=swagger_auto_schema(
tags=['Organizations'],
x_fern_sdk_group_name=['organizations', 'members'],
x_fern_sdk_method_name='get',
operation_summary='Get organization member details',
operation_description='Get organization member details by user ID.',
manual_parameters=[
openapi.Parameter(
name='user_pk',
type=openapi.TYPE_INTEGER,
in_=openapi.IN_PATH,
description='A unique integer value identifying the user to get organization details for.',
),
],
responses={200: OrganizationMemberSerializer()},
),
)
@method_decorator(
name='delete',
decorator=swagger_auto_schema(
Expand All @@ -160,13 +181,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()
mcanu marked this conversation as resolved.
Show resolved Hide resolved

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}"
mcanu marked this conversation as resolved.
Show resolved Hide resolved
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
20 changes: 10 additions & 10 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ django-migration-linter = "^5.1.0"
setuptools = ">=75.4.0"

# Humansignal repo dependencies
label-studio-sdk = {url = "https://github.com/HumanSignal/label-studio-sdk/archive/179cec94896d615e6fa56deaaf14e782f052c877.zip"}
label-studio-sdk = {url = "https://github.com/HumanSignal/label-studio-sdk/archive/70fbc90d83effe65b9ba3804a3a1a395310e3a9f.zip"}

[tool.poetry.group.test.dependencies]
pytest = "7.2.2"
Expand Down
Loading