From d671948d044526d4c8f6bf3b2a16e46524422f74 Mon Sep 17 00:00:00 2001
From: Andreas Ellewsen <ae@uio.no>
Date: Mon, 22 Nov 2021 15:55:55 +0100
Subject: [PATCH] Move guest info and ou views to better locations

The OU view deserves its own file, and the guest info is actually a
person view and should live with  the other person views.
---
 gregui/api/views/ou.py     | 29 +++++++++++++
 gregui/api/views/person.py | 63 ++++++++++++++++++++++++++++-
 gregui/urls.py             |  3 +-
 gregui/views.py            | 83 --------------------------------------
 4 files changed, 92 insertions(+), 86 deletions(-)
 create mode 100644 gregui/api/views/ou.py

diff --git a/gregui/api/views/ou.py b/gregui/api/views/ou.py
new file mode 100644
index 00000000..4dc2065e
--- /dev/null
+++ b/gregui/api/views/ou.py
@@ -0,0 +1,29 @@
+from django.http 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 Sponsor
+from greg.permissions import IsSponsor
+from gregui.models import GregUserProfile
+
+
+class OusView(APIView):
+    """Fetch Ous related to the authenticated sponsor."""
+
+    authentication_classes = [SessionAuthentication, BasicAuthentication]
+    permission_classes = [IsAuthenticated, IsSponsor]
+
+    @staticmethod
+    # pylint: disable=W0622
+    def get(request, format=None):
+        profile = GregUserProfile.objects.get(user=request.user)
+        sponsor = Sponsor.objects.get(id=profile.sponsor.id)
+        return JsonResponse(
+            {
+                "ous": [
+                    {"id": i.id, "nb": i.name_nb, "en": i.name_en}
+                    for i in sponsor.units.all()
+                ]
+            }
+        )
diff --git a/gregui/api/views/person.py b/gregui/api/views/person.py
index efcd19b9..fd8c8305 100644
--- a/gregui/api/views/person.py
+++ b/gregui/api/views/person.py
@@ -1,14 +1,14 @@
 from django.http.response import JsonResponse
-from django.utils.timezone import now
 from rest_framework import status
 from rest_framework.authentication import SessionAuthentication, BasicAuthentication
 from rest_framework.permissions import IsAuthenticated
 from rest_framework.response import Response
 from rest_framework.views import APIView
 
-from greg.models import Identity, Person, InvitationLink
+from greg.models import Identity, Person
 from greg.permissions import IsSponsor
 from gregui.api.serializers.guest import create_identity_or_update
+from gregui.models import GregUserProfile
 from gregui.validation import validate_email
 
 
@@ -106,3 +106,62 @@ class PersonSearchView(APIView):
             ]
         }
         return JsonResponse(response)
+
+
+class GuestInfoView(APIView):
+    """Fetch all the sponsors guests"""
+
+    authentication_classes = [SessionAuthentication, BasicAuthentication]
+    permission_classes = [IsAuthenticated, IsSponsor]
+
+    @staticmethod
+    # pylint: disable=W0622
+    def get(request, format=None):
+        user = GregUserProfile.objects.get(user=request.user)
+
+        return JsonResponse(
+            {
+                "persons": [
+                    {
+                        "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 {
+                            "id": person.fnr.id,
+                            "value": "".join((person.fnr.value[:-5], "*****")),
+                            "type": person.fnr.type,
+                            "verified_at": person.fnr.verified_at,
+                        },
+                        "passport": person.passport
+                        and {
+                            "id": person.passport.id,
+                            "value": person.passport.value,
+                            "type": person.passport.type,
+                            "verified_at": person.passport.verified_at,
+                        },
+                        "active": person.is_registered and person.is_verified,
+                        "registered": person.is_registered,
+                        "verified": person.is_verified,
+                        "roles": [
+                            {
+                                "id": role.id,
+                                "name_nb": role.type.name_nb,
+                                "name_en": role.type.name_en,
+                                "ou_nb": role.orgunit.name_nb,
+                                "ou_en": role.orgunit.name_en,
+                                "start_date": role.start_date,
+                                "end_date": role.end_date,
+                                "max_days": role.type.max_days,
+                            }
+                            for role in person.roles.all()
+                        ],
+                    }
+                    for person in Person.objects.filter(
+                        roles__sponsor=user.sponsor
+                    ).distinct()
+                ]
+            }
+        )
diff --git a/gregui/urls.py b/gregui/urls.py
index c8a47cbf..cbdf2960 100644
--- a/gregui/urls.py
+++ b/gregui/urls.py
@@ -5,8 +5,9 @@ from django.urls import path
 from django.urls.resolvers import URLResolver
 
 from gregui.api import urls as api_urls
+from gregui.api.views.ou import OusView
 from gregui.api.views.userinfo import UserInfoView
-from gregui.views import OusView, GuestInfoView
+from gregui.api.views.person import GuestInfoView
 from . import views
 
 urlpatterns: List[URLResolver] = [
diff --git a/gregui/views.py b/gregui/views.py
index 11ce1973..a60d3b76 100644
--- a/gregui/views.py
+++ b/gregui/views.py
@@ -6,10 +6,7 @@ from rest_framework.authentication import SessionAuthentication, BasicAuthentica
 from rest_framework.permissions import IsAuthenticated
 from rest_framework.views import APIView
 
-from greg.models import Person, Sponsor
-from greg.permissions import IsSponsor
 from gregui import mailutils
-from gregui.models import GregUserProfile
 
 
 def get_csrf(request):
@@ -61,83 +58,3 @@ class WhoAmIView(APIView):
     # pylint: disable=W0622
     def get(request, format=None):
         return JsonResponse({"username": request.user.username})
-
-
-class OusView(APIView):
-    """Fetch Ous related to the authenticated sponsor."""
-
-    authentication_classes = [SessionAuthentication, BasicAuthentication]
-    permission_classes = [IsAuthenticated, IsSponsor]
-
-    @staticmethod
-    # pylint: disable=W0622
-    def get(request, format=None):
-        profile = GregUserProfile.objects.get(user=request.user)
-        sponsor = Sponsor.objects.get(id=profile.sponsor.id)
-        return JsonResponse(
-            {
-                "ous": [
-                    {"id": i.id, "nb": i.name_nb, "en": i.name_en}
-                    for i in sponsor.units.all()
-                ]
-            }
-        )
-
-
-class GuestInfoView(APIView):
-    """Fetch all the sponsors guests"""
-
-    authentication_classes = [SessionAuthentication, BasicAuthentication]
-    permission_classes = [IsAuthenticated, IsSponsor]
-
-    @staticmethod
-    # pylint: disable=W0622
-    def get(request, format=None):
-        user = GregUserProfile.objects.get(user=request.user)
-
-        return JsonResponse(
-            {
-                "persons": [
-                    {
-                        "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 {
-                            "id": person.fnr.id,
-                            "value": "".join((person.fnr.value[:-5], "*****")),
-                            "type": person.fnr.type,
-                            "verified_at": person.fnr.verified_at,
-                        },
-                        "passport": person.passport
-                        and {
-                            "id": person.passport.id,
-                            "value": person.passport.value,
-                            "type": person.passport.type,
-                            "verified_at": person.passport.verified_at,
-                        },
-                        "active": person.is_registered and person.is_verified,
-                        "registered": person.is_registered,
-                        "verified": person.is_verified,
-                        "roles": [
-                            {
-                                "id": role.id,
-                                "name_nb": role.type.name_nb,
-                                "name_en": role.type.name_en,
-                                "ou_nb": role.orgunit.name_nb,
-                                "ou_en": role.orgunit.name_en,
-                                "start_date": role.start_date,
-                                "end_date": role.end_date,
-                                "max_days": role.type.max_days,
-                            }
-                            for role in person.roles.all()
-                        ],
-                    }
-                    for person in Person.objects.filter(
-                        roles__sponsor=user.sponsor
-                    ).distinct()
-                ]
-            }
-        )
-- 
GitLab