From 077abbe9619ec4ba3e58f77c573bae9f73a828fd Mon Sep 17 00:00:00 2001 From: shamoon <4887959+shamoon@users.noreply.github.com> Date: Mon, 12 Jun 2023 08:15:25 -0700 Subject: [PATCH] Return user first & last name from backend --- src/documents/tests/test_api.py | 14 ++++++++++++++ src/documents/views.py | 19 +++++++++++++------ 2 files changed, 27 insertions(+), 6 deletions(-) diff --git a/src/documents/tests/test_api.py b/src/documents/tests/test_api.py index 82f5b219a..90684b338 100644 --- a/src/documents/tests/test_api.py +++ b/src/documents/tests/test_api.py @@ -2594,11 +2594,25 @@ class TestApiUiSettings(DirectoriesMixin, APITestCase): def setUp(self): super().setUp() self.test_user = User.objects.create_superuser(username="test") + self.test_user.first_name = "Test" + self.test_user.last_name = "User" + self.test_user.save() self.client.force_authenticate(user=self.test_user) def test_api_get_ui_settings(self): response = self.client.get(self.ENDPOINT, format="json") self.assertEqual(response.status_code, status.HTTP_200_OK) + self.assertDictEqual( + response.data["user"], + { + "id": self.test_user.id, + "username": self.test_user.username, + "is_superuser": True, + "groups": [], + "first_name": self.test_user.first_name, + "last_name": self.test_user.last_name, + }, + ) self.assertDictEqual( response.data["settings"], { diff --git a/src/documents/views.py b/src/documents/views.py index db25e6934..d60cf459d 100644 --- a/src/documents/views.py +++ b/src/documents/views.py @@ -1028,16 +1028,23 @@ class UiSettingsView(GenericAPIView): ui_settings["update_checking"] = { "backend_setting": settings.ENABLE_UPDATE_CHECK, } + user_resp = { + "id": user.id, + "username": user.username, + "is_superuser": user.is_superuser, + "groups": list(user.groups.values_list("id", flat=True)), + } + + if len(user.first_name) > 0: + user_resp["first_name"] = user.first_name + if len(user.last_name) > 0: + user_resp["last_name"] = user.last_name + # strip . roles = map(lambda perm: re.sub(r"^\w+.", "", perm), user.get_all_permissions()) return Response( { - "user": { - "id": user.id, - "username": user.username, - "is_superuser": user.is_superuser, - "groups": user.groups.values_list("id", flat=True), - }, + "user": user_resp, "settings": ui_settings, "permissions": roles, },