Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[refactor] Refactor BaseEmailView to use the FilterByParent mixin #354

Open
nemesifier opened this issue Jun 14, 2023 · 2 comments
Open

Comments

@nemesifier
Copy link
Member

This view should be refactored to inherit the FilterByParent mixin:

class BaseEmailView(ProtectedAPIMixin, GenericAPIView):
model = EmailAddress
serializer_class = EmailAddressSerializer
def get_queryset(self):
return EmailAddress.objects.select_related('user').order_by('id')
def initial(self, *args, **kwargs):
super().initial(*args, **kwargs)
self.assert_parent_exists()
def assert_parent_exists(self):
try:
assert self.get_parent_queryset().exists()
except (AssertionError, ValidationError):
user_id = self.kwargs['pk']
raise NotFound(detail=_("User with ID '{}' not found.".format(user_id)))
def get_parent_queryset(self):
user = self.request.user
if user.is_superuser:
return User.objects.filter(pk=self.kwargs['pk'])
org_users = OrganizationUser.objects.filter(user=user).select_related(
'organization'
)
qs_user = User.objects.none()
for org_user in org_users:
if org_user.is_admin:
qs_user = qs_user | org_user.organization.users.all().distinct()
qs_user = qs_user.filter(is_superuser=False)
return qs_user.filter(pk=self.kwargs['pk'])
def get_serializer_context(self):
if getattr(self, 'swagger_fake_view', False):
# To get rid of assertion error raised in
# the dev server, and for schema generation
return None
context = super().get_serializer_context()
context['user'] = self.get_parent_queryset().first()
return context

@nemesifier nemesifier changed the title [refactor] [refactor] Refactor BaseEmailView to use the FilterByParent mixin Jun 14, 2023
@MihirKohli
Copy link

Can you assign me this issue?

@MihirKohli
Copy link

Resolved #358

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: To do (general)
Development

Successfully merging a pull request may close this issue.

2 participants