From b36d0cb58a29dfce5c3ef4d360868485f01e32f9 Mon Sep 17 00:00:00 2001
From: Andreas Ellewsen <ae@uio.no>
Date: Thu, 7 Oct 2021 15:09:34 +0200
Subject: [PATCH] Make userinfo work if user has no person info

Before this commit, the userinfo endpoint would fail if the user
did not have a person object connected to their GregUserProfile. We now
check that this is the case before checking the person objects roles.
---
 gregui/api/views/userinfo.py | 44 ++++++++++++++++++------------------
 1 file changed, 22 insertions(+), 22 deletions(-)

diff --git a/gregui/api/views/userinfo.py b/gregui/api/views/userinfo.py
index 8abf0c9a..aef0b932 100644
--- a/gregui/api/views/userinfo.py
+++ b/gregui/api/views/userinfo.py
@@ -52,7 +52,6 @@ class UserInfoView(APIView):
                 "person_id": person_id,
             }
             person = user_profile.person
-            roles = person.roles
             if person:
                 content.update(
                     {
@@ -62,27 +61,28 @@ class UserInfoView(APIView):
                         "mobile_phone": person.mobile_phone,
                     }
                 )
-            if roles:
-                content.update(
-                    {
-                        "roles": [
-                            {
-                                "ou_name_nb": role.orgunit_id.name_nb,
-                                "ou_name_en": role.orgunit_id.name_en,
-                                "role_name_nb": role.type.name_nb,
-                                "role_name_en": role.type.name_en,
-                                "start": role.start_date,
-                                "end": role.end_date,
-                                "comments": role.comments,
-                                "sponsor": {
-                                    "first_name": role.sponsor_id.first_name,
-                                    "last_name": role.sponsor_id.last_name,
-                                },
-                            }
-                            for role in roles.all()
-                        ],
-                    }
-                )
+                roles = person.roles
+                if roles:
+                    content.update(
+                        {
+                            "roles": [
+                                {
+                                    "ou_name_nb": role.orgunit_id.name_nb,
+                                    "ou_name_en": role.orgunit_id.name_en,
+                                    "role_name_nb": role.type.name_nb,
+                                    "role_name_en": role.type.name_en,
+                                    "start": role.start_date,
+                                    "end": role.end_date,
+                                    "comments": role.comments,
+                                    "sponsor": {
+                                        "first_name": role.sponsor_id.first_name,
+                                        "last_name": role.sponsor_id.last_name,
+                                    },
+                                }
+                                for role in roles.all()
+                            ],
+                        }
+                    )
             return Response(content)
 
         # Invitation cookie, allow access
-- 
GitLab