2019-10-28 13:44:46 +00:00
|
|
|
"""User API Views"""
|
2020-11-22 12:43:41 +00:00
|
|
|
from rest_framework.decorators import action
|
|
|
|
from rest_framework.request import Request
|
|
|
|
from rest_framework.response import Response
|
2020-11-22 18:36:40 +00:00
|
|
|
from rest_framework.serializers import (
|
|
|
|
BooleanField,
|
|
|
|
ModelSerializer,
|
|
|
|
SerializerMethodField,
|
|
|
|
)
|
|
|
|
from rest_framework.viewsets import ModelViewSet
|
2019-10-28 13:27:43 +00:00
|
|
|
|
|
|
|
from passbook.core.models import User
|
2020-11-22 18:36:40 +00:00
|
|
|
from passbook.lib.templatetags.passbook_utils import avatar
|
2019-10-28 13:27:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
class UserSerializer(ModelSerializer):
|
2019-10-28 13:44:46 +00:00
|
|
|
"""User Serializer"""
|
2019-10-28 13:27:43 +00:00
|
|
|
|
2020-09-15 20:37:31 +00:00
|
|
|
is_superuser = BooleanField(read_only=True)
|
2020-11-22 18:36:40 +00:00
|
|
|
avatar = SerializerMethodField()
|
|
|
|
|
|
|
|
def get_avatar(self, user: User) -> str:
|
|
|
|
"""Add user's avatar as URL"""
|
|
|
|
return avatar(user)
|
2020-09-15 20:37:31 +00:00
|
|
|
|
2019-10-28 13:27:43 +00:00
|
|
|
class Meta:
|
|
|
|
|
|
|
|
model = User
|
2020-11-22 18:36:40 +00:00
|
|
|
fields = ["pk", "username", "name", "is_superuser", "email", "avatar"]
|
2019-10-28 13:27:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
class UserViewSet(ModelViewSet):
|
2019-10-28 13:44:46 +00:00
|
|
|
"""User Viewset"""
|
2019-10-28 13:27:43 +00:00
|
|
|
|
|
|
|
queryset = User.objects.all()
|
|
|
|
serializer_class = UserSerializer
|
2020-11-22 12:43:41 +00:00
|
|
|
|
|
|
|
@action(detail=False)
|
2020-11-22 18:36:40 +00:00
|
|
|
# pylint: disable=invalid-name
|
2020-11-22 12:43:41 +00:00
|
|
|
def me(self, request: Request) -> Response:
|
|
|
|
"""Get information about current user"""
|
|
|
|
return Response(UserSerializer(request.user).data)
|