Source code for intranet.apps.users.api

import io
import os

from rest_framework import generics
from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response

from django.conf import settings
from django.contrib.auth import get_user_model

from intranet.apps.search.views import get_search_results

from ..auth.rest_permissions import DenyRestrictedPermission
from .models import Grade
from .renderers import JPEGRenderer
from .serializers import CounselorTeacherSerializer, StudentSerializer, UserSerializer


[docs]class ProfileDetail(generics.RetrieveAPIView): """API endpoint that retrieves an Ion profile. /api/profile: retrieve your profile /api/profile/<pk>: retrieve the profile of the user with id <pk> /api/profile/<username>: retrieve the profile of the user with username <username> """ serializer_class = UserSerializer permission_classes = (IsAuthenticated,)
[docs] def retrieve(self, request, *args, **kwargs): if "pk" in kwargs: user = get_user_model().objects.get(pk=kwargs["pk"]) elif "username" in kwargs: user = get_user_model().objects.get(username__iexact=kwargs["username"]) else: user = request.user if request.user.is_restricted and user != request.user: raise get_user_model().DoesNotExist return Response(self.get_serializer(user).data)
[docs]class ProfilePictureDetail(generics.RetrieveAPIView): """API endpoint that retrieves an Ion profile picture. /api/profile/<pk>/picture: retrieve default profile picture /api/profile/<pk>/picture/<photo_year>: retrieve profile picture for year <photo_year> """ serializer_class = UserSerializer permission_classes = (DenyRestrictedPermission,) renderer_classes = (JPEGRenderer,)
[docs] def retrieve(self, request, *args, **kwargs): if "pk" in kwargs: user = get_user_model().objects.get(pk=kwargs["pk"]) elif "username" in kwargs: user = get_user_model().objects.get(username=kwargs["username"]) else: user = request.user binary = None if "photo_year" in kwargs: photo_year = kwargs["photo_year"] if photo_year in Grade.names: grade_number = Grade.number_from_name(photo_year) if user.photos.filter(grade_number=grade_number).exists(): binary = user.photos.filter(grade_number=grade_number).first().binary else: binary = None else: binary = user.default_photo if binary is None: default_image_path = os.path.join(settings.PROJECT_ROOT, "static/img/default_profile_pic.png") binary = io.open(default_image_path, mode="rb").read() return Response(binary, content_type="image/jpeg")