From afa4fa559c1b46f8ea70ca3226141d45f54bcf38 Mon Sep 17 00:00:00 2001 From: Lidavic <148764396+Lidavic@users.noreply.github.com> Date: Thu, 14 Nov 2024 20:15:24 +0100 Subject: [PATCH] 1493 add search to users endpoint (#1609) * add ability to search for users at endpoint localhost:8000/users * reverted permissions --- backend/samfundet/utils.py | 9 +++++++++ backend/samfundet/views.py | 6 +++++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/backend/samfundet/utils.py b/backend/samfundet/utils.py index 6755ad78d..6964f4003 100644 --- a/backend/samfundet/utils.py +++ b/backend/samfundet/utils.py @@ -42,6 +42,15 @@ def event_query(*, query: QueryDict, events: QuerySet[Event] = None) -> QuerySet return events +def user_query(*, query: QueryDict, users: QuerySet[User] = None) -> QuerySet[User]: + if not users: + users = User.objects.all() + search = query.get('search', None) + if search: + users = users.filter(Q(username__icontains=search) | Q(first_name__icontains=search) | Q(last_name__icontains=search)) + return users + + def generate_timeslots(start_time: datetime.time, end_time: datetime.time, interval_minutes: int) -> list[str]: # Convert from datetime.time objects to datetime.datetime start_datetime = datetime.datetime.combine(datetime.datetime.today(), start_time) diff --git a/backend/samfundet/views.py b/backend/samfundet/views.py index e4099a47c..4f3f8c509 100644 --- a/backend/samfundet/views.py +++ b/backend/samfundet/views.py @@ -39,7 +39,7 @@ REQUESTED_IMPERSONATE_USER, ) -from .utils import event_query, generate_timeslots, get_occupied_timeslots_from_request +from .utils import user_query, event_query, generate_timeslots, get_occupied_timeslots_from_request from .homepage import homepage from .models.role import Role, UserOrgRole, UserGangRole, UserGangSectionRole from .serializers import ( @@ -492,6 +492,10 @@ class AllUsersView(ListAPIView): serializer_class = UserSerializer queryset = User.objects.all() + def get(self, request: Request) -> Response: + users = user_query(query=request.query_params) + return Response(data=UserSerializer(users, many=True).data) + class ImpersonateView(APIView): permission_classes = [IsAuthenticated] # TODO: Permission check.