diff --git a/passbook/core/api/users.py b/passbook/core/api/users.py index 447dbce36..d410ad1b4 100644 --- a/passbook/core/api/users.py +++ b/passbook/core/api/users.py @@ -1,22 +1,32 @@ """User API Views""" -from rest_framework.serializers import BooleanField, ModelSerializer -from rest_framework.viewsets import ModelViewSet from rest_framework.decorators import action from rest_framework.request import Request from rest_framework.response import Response +from rest_framework.serializers import ( + BooleanField, + ModelSerializer, + SerializerMethodField, +) +from rest_framework.viewsets import ModelViewSet from passbook.core.models import User +from passbook.lib.templatetags.passbook_utils import avatar class UserSerializer(ModelSerializer): """User Serializer""" is_superuser = BooleanField(read_only=True) + avatar = SerializerMethodField() + + def get_avatar(self, user: User) -> str: + """Add user's avatar as URL""" + return avatar(user) class Meta: model = User - fields = ["pk", "username", "name", "is_superuser", "email"] + fields = ["pk", "username", "name", "is_superuser", "email", "avatar"] class UserViewSet(ModelViewSet): @@ -26,6 +36,7 @@ class UserViewSet(ModelViewSet): serializer_class = UserSerializer @action(detail=False) + # pylint: disable=invalid-name def me(self, request: Request) -> Response: """Get information about current user""" return Response(UserSerializer(request.user).data) diff --git a/passbook/core/tests/test_impersonation.py b/passbook/core/tests/test_impersonation.py index ba365e421..d88bbbad8 100644 --- a/passbook/core/tests/test_impersonation.py +++ b/passbook/core/tests/test_impersonation.py @@ -29,7 +29,7 @@ class TestImpersonation(TestCase): self.client.get(reverse("passbook_core:impersonate-end")) - response = self.client.get(reverse("passbook_core:overview")) + response = self.client.get(reverse("passbook_api:user-me")) self.assertNotIn(self.other_user.username, response.content.decode()) self.assertIn(self.pbadmin.username, response.content.decode()) @@ -43,7 +43,7 @@ class TestImpersonation(TestCase): ) ) - response = self.client.get(reverse("passbook_core:overview")) + response = self.client.get(reverse("passbook_api:user-me")) self.assertIn(self.other_user.username, response.content.decode()) self.assertNotIn(self.pbadmin.username, response.content.decode()) diff --git a/passbook/core/urls.py b/passbook/core/urls.py index c46027c91..7367019de 100644 --- a/passbook/core/urls.py +++ b/passbook/core/urls.py @@ -1,7 +1,7 @@ """passbook URL Configuration""" from django.urls import path -from passbook.core.views import impersonate, overview, user, shell +from passbook.core.views import impersonate, overview, shell, user urlpatterns = [ # User views diff --git a/swagger.yaml b/swagger.yaml index 2bf026fb3..f431efd5b 100755 --- a/swagger.yaml +++ b/swagger.yaml @@ -620,6 +620,57 @@ paths: tags: - core parameters: [] + /core/users/me/: + get: + operationId: core_users_me + description: Get information about current user + parameters: + - name: ordering + in: query + description: Which field to use when ordering the results. + required: false + type: string + - name: search + in: query + description: A search term. + required: false + type: string + - name: limit + in: query + description: Number of results to return per page. + required: false + type: integer + - name: offset + in: query + description: The initial index from which to return the results. + required: false + type: integer + responses: + '200': + description: '' + schema: + required: + - count + - results + type: object + properties: + count: + type: integer + next: + type: string + format: uri + x-nullable: true + previous: + type: string + format: uri + x-nullable: true + results: + type: array + items: + $ref: '#/definitions/User' + tags: + - core + parameters: [] /core/users/{id}/: get: operationId: core_users_read @@ -6564,6 +6615,10 @@ definitions: type: string format: email maxLength: 254 + avatar: + title: Avatar + type: string + readOnly: true CertificateKeyPair: description: CertificateKeyPair Serializer required: