Files
website-v2/users/views.py
Greg Newman c6192f9537 🚧 hooks up libraries to profile
* Breaks library markup into a template
* Adds authored and maintained context objects
* Adds loops to include template for authored and maintained
2022-11-14 18:23:58 -05:00

61 lines
1.6 KiB
Python

from django.views.generic import DetailView
from rest_framework import viewsets
from rest_framework import generics
from rest_framework.permissions import IsAuthenticated
from .serializers import UserSerializer, FullUserSerializer, CurrentUserSerializer
from .permissions import CustomUserPermissions
from .models import User
class UserViewSet(viewsets.ModelViewSet):
"""
Main User API ViewSet
"""
queryset = User.objects.all()
permission_classes = [CustomUserPermissions]
def get_serializer_class(self):
"""Pick the right serializer based on the user"""
if self.request.user.is_staff or self.request.user.is_superuser:
return FullUserSerializer
else:
return UserSerializer
class CurrentUserView(generics.RetrieveUpdateAPIView):
"""
This gives the current user a convenient way to retrieve or
update slightly more detailed information about themselves.
Typically set to a route of `/api/v1/user/me`
"""
serializer_class = CurrentUserSerializer
permission_classes = [IsAuthenticated]
def get_object(self):
return self.request.user
class ProfileViewSet(DetailView):
"""
ViewSet to show statistics about a user to include
stats, badges, forum posts, reviews, etc.
"""
model = User
queryset = User.objects.all()
template_name = "users/profile.html"
context_object_name = "user"
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
user = self.get_object()
context["authored"] = user.authors.all()
context["maintained"] = user.maintainers.all()
return context