Return user first & last name from backend

This commit is contained in:
shamoon 2023-06-12 08:15:25 -07:00
parent e3ea5dd13c
commit 077abbe961
2 changed files with 27 additions and 6 deletions

View File

@ -2594,11 +2594,25 @@ class TestApiUiSettings(DirectoriesMixin, APITestCase):
def setUp(self): def setUp(self):
super().setUp() super().setUp()
self.test_user = User.objects.create_superuser(username="test") 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) self.client.force_authenticate(user=self.test_user)
def test_api_get_ui_settings(self): def test_api_get_ui_settings(self):
response = self.client.get(self.ENDPOINT, format="json") response = self.client.get(self.ENDPOINT, format="json")
self.assertEqual(response.status_code, status.HTTP_200_OK) 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( self.assertDictEqual(
response.data["settings"], response.data["settings"],
{ {

View File

@ -1028,16 +1028,23 @@ class UiSettingsView(GenericAPIView):
ui_settings["update_checking"] = { ui_settings["update_checking"] = {
"backend_setting": settings.ENABLE_UPDATE_CHECK, "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 <app_label>. # strip <app_label>.
roles = map(lambda perm: re.sub(r"^\w+.", "", perm), user.get_all_permissions()) roles = map(lambda perm: re.sub(r"^\w+.", "", perm), user.get_all_permissions())
return Response( return Response(
{ {
"user": { "user": user_resp,
"id": user.id,
"username": user.username,
"is_superuser": user.is_superuser,
"groups": user.groups.values_list("id", flat=True),
},
"settings": ui_settings, "settings": ui_settings,
"permissions": roles, "permissions": roles,
}, },