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

Add endpoint for getting a role's users #1576

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions backend/root/utils/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -540,6 +540,7 @@
samfundet__merch_detail = 'samfundet:merch-detail'
samfundet__role_list = 'samfundet:role-list'
samfundet__role_detail = 'samfundet:role-detail'
samfundet__role_users = 'samfundet:role-users'
samfundet__recruitment_list = 'samfundet:recruitment-list'
samfundet__recruitment_detail = 'samfundet:recruitment-detail'
samfundet__recruitment_gangs = 'samfundet:recruitment-gangs'
Expand Down
57 changes: 56 additions & 1 deletion backend/samfundet/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from root.constants import PHONE_NUMBER_REGEX
from root.utils.mixins import CustomBaseSerializer

from .models.role import Role
from .models.role import Role, UserOrgRole, UserGangRole, UserGangSectionRole
from .models.event import Event, EventGroup, EventCustomTicket, PurchaseFeedbackModel, PurchaseFeedbackQuestion, PurchaseFeedbackAlternative
from .models.billig import BilligEvent, BilligPriceGroup, BilligTicketGroup
from .models.general import (
Expand All @@ -40,6 +40,7 @@
KeyValue,
MenuItem,
TextItem,
GangSection,
Reservation,
ClosedPeriod,
FoodCategory,
Expand Down Expand Up @@ -426,6 +427,12 @@ class Meta:
fields = '__all__'


class GangSectionSerializer(CustomBaseSerializer):
class Meta:
model = GangSection
fields = '__all__'


class RecruitmentGangSerializer(CustomBaseSerializer):
recruitment_positions = serializers.SerializerMethodField(method_name='get_positions_count', read_only=True)

Expand Down Expand Up @@ -499,6 +506,54 @@ class Meta:
fields = '__all__'


class UserOrgRoleSerializer(CustomBaseSerializer):
user = UserSerializer()
org_role = serializers.SerializerMethodField()

class Meta:
model = UserOrgRole
fields = ('user', 'org_role')

def get_org_role(self, obj: UserOrgRole) -> dict:
return {
'created_at': obj.created_at,
'created_by': UserSerializer(obj.created_by).data,
'organization': OrganizationSerializer(obj.obj).data,
}


class UserGangRoleSerializer(CustomBaseSerializer):
user = UserSerializer()
gang_role = serializers.SerializerMethodField()

class Meta:
model = UserGangRole
fields = ('user', 'gang_role')

def get_gang_role(self, obj: UserGangRole) -> dict:
return {
'created_at': obj.created_at,
'created_by': UserSerializer(obj.created_by).data,
'gang': GangSerializer(obj.obj).data,
}


class UserGangSectionRoleSerializer(CustomBaseSerializer):
user = UserSerializer()
section_role = serializers.SerializerMethodField()

class Meta:
model = UserGangSectionRole
fields = ('user', 'section_role')

def get_section_role(self, obj: UserGangSectionRole) -> dict:
return {
'created_at': obj.created_at,
'created_by': UserSerializer(obj.created_by).data,
'section': GangSectionSerializer(obj.obj).data,
}


class SaksdokumentSerializer(CustomBaseSerializer):
# Read only url file path used in frontend
url = serializers.SerializerMethodField(method_name='get_url', read_only=True)
Expand Down
22 changes: 21 additions & 1 deletion backend/samfundet/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import hmac
import hashlib
from typing import Any
from itertools import chain

from guardian.shortcuts import get_objects_for_user

Expand Down Expand Up @@ -40,7 +41,7 @@

from .utils import event_query, generate_timeslots, get_occupied_timeslots_from_request
from .homepage import homepage
from .models.role import Role
from .models.role import Role, UserOrgRole, UserGangRole, UserGangSectionRole
from .serializers import (
TagSerializer,
GangSerializer,
Expand All @@ -67,11 +68,13 @@
EventGroupSerializer,
PermissionSerializer,
RecruitmentSerializer,
UserOrgRoleSerializer,
ClosedPeriodSerializer,
FoodCategorySerializer,
OrganizationSerializer,
SaksdokumentSerializer,
UserFeedbackSerializer,
UserGangRoleSerializer,
InterviewRoomSerializer,
FoodPreferenceSerializer,
UserPreferenceSerializer,
Expand All @@ -82,6 +85,7 @@
ReservationCheckSerializer,
UserForRecruitmentSerializer,
RecruitmentPositionSerializer,
UserGangSectionRoleSerializer,
RecruitmentStatisticsSerializer,
RecruitmentForRecruiterSerializer,
RecruitmentSeparatePositionSerializer,
Expand Down Expand Up @@ -324,6 +328,22 @@ class RoleView(ModelViewSet):
serializer_class = RoleSerializer
queryset = Role.objects.all()

@action(detail=True, methods=['get'])
def users(self, request: Request, pk: int) -> Response:
role = get_object_or_404(Role, id=pk)

org_roles = UserOrgRole.objects.filter(role=role).select_related('user', 'obj')
gang_roles = UserGangRole.objects.filter(role=role).select_related('user', 'obj')
section_roles = UserGangSectionRole.objects.filter(role=role).select_related('user', 'obj')

org_data = UserOrgRoleSerializer(org_roles, many=True).data
gang_data = UserGangRoleSerializer(gang_roles, many=True).data
section_data = UserGangSectionRoleSerializer(section_roles, many=True).data

combined = list(chain(org_data, gang_data, section_data))

return Response(combined)


# =============================== #
# Sulten #
Expand Down
1 change: 1 addition & 0 deletions frontend/src/routes/backend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -539,6 +539,7 @@ export const ROUTES_BACKEND = {
samfundet__merch_detail: '/api/merch/:pk/',
samfundet__role_list: '/api/role/',
samfundet__role_detail: '/api/role/:pk/',
samfundet__role_users: '/api/role/:pk/users/',
samfundet__recruitment_list: '/api/recruitment/',
samfundet__recruitment_detail: '/api/recruitment/:pk/',
samfundet__recruitment_gangs: '/api/recruitment/:pk/gangs/',
Expand Down
Loading