Merge pull request #6299 from normanjaeckel/GetUser2

Changed GetUser view. Now it retrieves one or more users.
This commit is contained in:
Emanuel Schütze 2021-12-20 08:55:39 +01:00 committed by GitHub
commit 1e22ffbf7e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 21 deletions

View File

@ -39,7 +39,6 @@ from ..utils.autoupdate import AutoupdateElement, inform_changed_data, inform_el
from ..utils.cache import element_cache
from ..utils.rest_api import (
ModelViewSet,
NotFound,
Response,
SimpleMetadata,
ValidationError,
@ -1173,10 +1172,10 @@ class PasswordResetConfirmView(APIView):
class GetUserView(APIView):
"""
View to retrieve a single user.
View to retrieve users.
Use query parameters "id", "username", "email" or "number" to look for
a user and retrieve its data.
users and retrieve their data.
"""
http_method_names = ["get"]
@ -1211,19 +1210,17 @@ class GetUserView(APIView):
query = query.filter(number=number)
# Execute queryset
try:
self.user = query.get()
except User.DoesNotExist:
raise NotFound({"detail": "User does not exist."})
except User.MultipleObjectsReturned:
raise ValidationError({"detail": "Found more than one user."})
self.users = list(query)
return super().get(request, *args, **kwargs)
def get_context_data(self, **context):
users_full_data = []
for user in self.users:
user_full_data = async_to_sync(element_cache.get_element_data)(
self.user.get_collection_string(), self.user.pk
user.get_collection_string(), user.pk
)
user_full_data.pop("session_auth_hash")
context.update({"user": user_full_data})
users_full_data.append(user_full_data)
context.update({"users": users_full_data})
return context

View File

@ -185,9 +185,9 @@ class TestGetUserView(TestCase):
response = self.client.get(self.url, {"username": "admin"})
self.assertEqual(response.status_code, 200)
user = json.loads(response.content.decode()).get("user")
self.assertEqual(user["username"], "admin")
self.assertEqual(user["last_name"], "Administrator")
users = json.loads(response.content.decode()).get("users")
self.assertEqual(users[0]["username"], "admin")
self.assertEqual(users[0]["last_name"], "Administrator")
def test_post(self):
response = self.client.post(self.url)
@ -199,9 +199,9 @@ class TestGetUserView(TestCase):
response = self.client.get(self.url, {"username": "not-existing-username"})
self.assertEqual(response.status_code, 404)
self.assertEqual(response.status_code, 200)
content = json.loads(response.content.decode())
self.assertEqual(content.get("detail"), "User does not exist.")
self.assertEqual(content.get("users"), [])
def test_multiple_objects(self):
self.client.login(username="admin", password="admin")
@ -214,9 +214,9 @@ class TestGetUserView(TestCase):
response = self.client.get(self.url, {"number": "Number#1234567890"})
self.assertEqual(response.status_code, 400)
self.assertEqual(response.status_code, 200)
content = json.loads(response.content.decode())
self.assertEqual(content.get("detail"), "Found more than one user.")
self.assertEqual(len(content.get("users")), 2)
def test_delegate(self):
self.make_admin_delegate()