Skip to content

Commit 63dd5f9

Browse files
committed
[refactor] Use FilterByParent mixin in BaseEmailView #354
Refactored BaseEmailView to inherit from FilterByParent mixin. Fixes #354
1 parent c3d2e94 commit 63dd5f9

File tree

2 files changed

+2
-11
lines changed

2 files changed

+2
-11
lines changed

openwisp_users/api/mixins.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,6 @@ def get_queryset(self):
107107

108108
def assert_parent_exists(self):
109109
parent_queryset = self.get_parent_queryset()
110-
if not self.request.user.is_superuser:
111-
parent_queryset = self.get_organization_queryset(parent_queryset)
112110
try:
113111
assert parent_queryset.exists()
114112
except (AssertionError, ValidationError):

openwisp_users/api/views.py

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
from openwisp_users.api.permissions import DjangoModelPermissions
2222

23-
from .mixins import ProtectedAPIMixin as BaseProtectedAPIMixin
23+
from .mixins import ProtectedAPIMixin as BaseProtectedAPIMixin, FilterByParentOwned
2424
from .serializers import (
2525
ChangePasswordSerializer,
2626
EmailAddressSerializer,
@@ -198,7 +198,7 @@ def update(self, request, *args, **kwargs):
198198
)
199199

200200

201-
class BaseEmailView(ProtectedAPIMixin, GenericAPIView):
201+
class BaseEmailView(ProtectedAPIMixin, FilterByParentOwned, GenericAPIView):
202202
model = EmailAddress
203203
serializer_class = EmailAddressSerializer
204204

@@ -209,13 +209,6 @@ def initial(self, *args, **kwargs):
209209
super().initial(*args, **kwargs)
210210
self.assert_parent_exists()
211211

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-
219212
def get_parent_queryset(self):
220213
user = self.request.user
221214

0 commit comments

Comments
 (0)