Skip to content
Snippets Groups Projects
  • Andreas Ellewsen's avatar
    9a806970
    Split Role and Person info in frontend · 9a806970
    Andreas Ellewsen authored
    To make handling info about roles and persons easier we split them into
    separate objects. The Profile page has now gotten its own api endpoint
    where a sponsor has access to all guests instead of only those they are
    sponsor for which is used on the sponsor frontpage. This is necessary
    since a sponsor may want to give a role to someone they are not
    currently sponsor for.
    Split Role and Person info in frontend
    Andreas Ellewsen authored
    To make handling info about roles and persons easier we split them into
    separate objects. The Profile page has now gotten its own api endpoint
    where a sponsor has access to all guests instead of only those they are
    sponsor for which is used on the sponsor frontpage. This is necessary
    since a sponsor may want to give a role to someone they are not
    currently sponsor for.
person.py 1.71 KiB
from django.http.response import JsonResponse
from rest_framework.authentication import SessionAuthentication, BasicAuthentication
from rest_framework.permissions import IsAuthenticated
from rest_framework.views import APIView

from greg.models import Person
from greg.permissions import IsSponsor


class PersonView(APIView):
    """
    Fetch person info for any guest as long as you are a sponsor

    This is required for the functionality where a sponsor wants to add a guest role to
    an already existing person that is not already their guest.

    Returns enough information to fill a profile page in the frontend
    """

    authentication_classes = [SessionAuthentication, BasicAuthentication]
    permission_classes = [IsAuthenticated, IsSponsor]

    def get(self, request, id):
        person = Person.objects.get(id=id)
        response = {
            "pid": person.id,
            "first": person.first_name,
            "last": person.last_name,
            "email": person.private_email and person.private_email.value,
            "mobile": person.private_mobile and person.private_mobile.value,
            "fnr": person.fnr and "".join((person.fnr.value[:-5], "*****")),
            "roles": [
                {
                    "id": role.id,
                    "name_nb": role.type.name_nb,
                    "name_en": role.type.name_en,
                    "ou_nb": role.orgunit_id.name_nb,
                    "ou_en": role.orgunit_id.name_en,
                    "start_date": role.start_date,
                    "end_date": role.end_date,
                    "max_days": role.type.max_days,
                }
                for role in person.roles.all()
            ],
        }
        return JsonResponse(response)