Changed GetUser view. Now it retrieves one or more users.
This commit is contained in:
parent
87b8fcd42b
commit
b0bc4129ce
@ -39,7 +39,6 @@ from ..utils.autoupdate import AutoupdateElement, inform_changed_data, inform_el
|
|||||||
from ..utils.cache import element_cache
|
from ..utils.cache import element_cache
|
||||||
from ..utils.rest_api import (
|
from ..utils.rest_api import (
|
||||||
ModelViewSet,
|
ModelViewSet,
|
||||||
NotFound,
|
|
||||||
Response,
|
Response,
|
||||||
SimpleMetadata,
|
SimpleMetadata,
|
||||||
ValidationError,
|
ValidationError,
|
||||||
@ -1173,10 +1172,10 @@ class PasswordResetConfirmView(APIView):
|
|||||||
|
|
||||||
class GetUserView(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
|
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"]
|
http_method_names = ["get"]
|
||||||
@ -1211,19 +1210,17 @@ class GetUserView(APIView):
|
|||||||
query = query.filter(number=number)
|
query = query.filter(number=number)
|
||||||
|
|
||||||
# Execute queryset
|
# Execute queryset
|
||||||
try:
|
self.users = list(query)
|
||||||
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."})
|
|
||||||
|
|
||||||
return super().get(request, *args, **kwargs)
|
return super().get(request, *args, **kwargs)
|
||||||
|
|
||||||
def get_context_data(self, **context):
|
def get_context_data(self, **context):
|
||||||
user_full_data = async_to_sync(element_cache.get_element_data)(
|
users_full_data = []
|
||||||
self.user.get_collection_string(), self.user.pk
|
for user in self.users:
|
||||||
)
|
user_full_data = async_to_sync(element_cache.get_element_data)(
|
||||||
user_full_data.pop("session_auth_hash")
|
user.get_collection_string(), user.pk
|
||||||
context.update({"user": user_full_data})
|
)
|
||||||
|
user_full_data.pop("session_auth_hash")
|
||||||
|
users_full_data.append(user_full_data)
|
||||||
|
context.update({"users": users_full_data})
|
||||||
return context
|
return context
|
||||||
|
@ -185,9 +185,9 @@ class TestGetUserView(TestCase):
|
|||||||
response = self.client.get(self.url, {"username": "admin"})
|
response = self.client.get(self.url, {"username": "admin"})
|
||||||
|
|
||||||
self.assertEqual(response.status_code, 200)
|
self.assertEqual(response.status_code, 200)
|
||||||
user = json.loads(response.content.decode()).get("user")
|
users = json.loads(response.content.decode()).get("users")
|
||||||
self.assertEqual(user["username"], "admin")
|
self.assertEqual(users[0]["username"], "admin")
|
||||||
self.assertEqual(user["last_name"], "Administrator")
|
self.assertEqual(users[0]["last_name"], "Administrator")
|
||||||
|
|
||||||
def test_post(self):
|
def test_post(self):
|
||||||
response = self.client.post(self.url)
|
response = self.client.post(self.url)
|
||||||
@ -199,9 +199,9 @@ class TestGetUserView(TestCase):
|
|||||||
|
|
||||||
response = self.client.get(self.url, {"username": "not-existing-username"})
|
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())
|
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):
|
def test_multiple_objects(self):
|
||||||
self.client.login(username="admin", password="admin")
|
self.client.login(username="admin", password="admin")
|
||||||
@ -214,9 +214,9 @@ class TestGetUserView(TestCase):
|
|||||||
|
|
||||||
response = self.client.get(self.url, {"number": "Number#1234567890"})
|
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())
|
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):
|
def test_delegate(self):
|
||||||
self.make_admin_delegate()
|
self.make_admin_delegate()
|
||||||
|
Loading…
Reference in New Issue
Block a user