core: add avatars to user api

This commit is contained in:
Jens Langhammer 2020-11-22 19:36:40 +01:00
parent bc02fb04f8
commit 3c311ca527
4 changed files with 72 additions and 6 deletions

View File

@ -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)

View File

@ -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())

View File

@ -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

View File

@ -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: