Skip to content

Commit 635d776

Browse files
committed
[refactor] Inherit from FilterByParent #354
Refactored BaseEmailView to inherit from FilterByParent and for that updated get_organization_queryset to override FilterByParent's method. Fixes #354
1 parent c3d2e94 commit 635d776

File tree

2 files changed

+8
-16
lines changed

2 files changed

+8
-16
lines changed

openwisp_users/api/views.py

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
from allauth.account.models import EmailAddress
22
from django.contrib.auth import get_user_model
3-
from django.core.exceptions import ValidationError
43
from django.utils.translation import gettext_lazy as _
54
from drf_yasg.utils import swagger_auto_schema
65
from rest_framework import pagination
76
from rest_framework.authtoken.views import ObtainAuthToken
8-
from rest_framework.exceptions import NotFound
97
from rest_framework.generics import (
108
GenericAPIView,
119
ListCreateAPIView,
@@ -20,6 +18,7 @@
2018

2119
from openwisp_users.api.permissions import DjangoModelPermissions
2220

21+
from .mixins import FilterByParent
2322
from .mixins import ProtectedAPIMixin as BaseProtectedAPIMixin
2423
from .serializers import (
2524
ChangePasswordSerializer,
@@ -198,7 +197,7 @@ def update(self, request, *args, **kwargs):
198197
)
199198

200199

201-
class BaseEmailView(ProtectedAPIMixin, GenericAPIView):
200+
class BaseEmailView(ProtectedAPIMixin, FilterByParent, GenericAPIView):
202201
model = EmailAddress
203202
serializer_class = EmailAddressSerializer
204203

@@ -209,28 +208,21 @@ def initial(self, *args, **kwargs):
209208
super().initial(*args, **kwargs)
210209
self.assert_parent_exists()
211210

212-
def assert_parent_exists(self):
213-
try:
214-
assert self.get_parent_queryset().exists()
215-
except (AssertionError, ValidationError):
216-
user_id = self.kwargs['pk']
217-
raise NotFound(detail=_("User with ID '{}' not found.".format(user_id)))
218-
219211
def get_parent_queryset(self):
220-
user = self.request.user
221-
222-
if user.is_superuser:
212+
if self.request.user.is_superuser:
223213
return User.objects.filter(pk=self.kwargs['pk'])
214+
return self.get_organization_queryset(User.objects.all())
224215

216+
def get_organization_queryset(self, qs):
217+
user = self.request.user
225218
org_users = OrganizationUser.objects.filter(user=user).select_related(
226219
'organization'
227220
)
228221
qs_user = User.objects.none()
229222
for org_user in org_users:
230223
if org_user.is_admin:
231224
qs_user = qs_user | org_user.organization.users.all().distinct()
232-
qs_user = qs_user.filter(is_superuser=False)
233-
return qs_user.filter(pk=self.kwargs['pk'])
225+
return qs_user.filter(is_superuser=False, pk=self.kwargs['pk'])
234226

235227
def get_serializer_context(self):
236228
if getattr(self, 'swagger_fake_view', False):

openwisp_users/tests/test_api/test_api.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -458,7 +458,7 @@ def test_get_email_list_multitenancy_api(self):
458458
self._create_org_user(user=org2_user, organization=org2)
459459
self.client.force_login(org1_user)
460460
path = reverse('users:email_list', args=(org2_user.pk,))
461-
with self.assertNumQueries(5):
461+
with self.assertNumQueries(6):
462462
response = self.client.get(path)
463463
self.assertEqual(response.status_code, 404)
464464

0 commit comments

Comments
 (0)