-
-
Notifications
You must be signed in to change notification settings - Fork 75
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] Use FilterByParent mixin in BaseEmailView #354 #426
base: master
Are you sure you want to change the base?
Conversation
Refactored BaseEmailView to inherit from FilterByParent mixin, simplifying code and ensuring consistency. Fixes openwisp#354
63dd5f9
to
d961dc3
Compare
@@ -107,8 +107,6 @@ def get_queryset(self): | |||
|
|||
def assert_parent_exists(self): | |||
parent_queryset = self.get_parent_queryset() | |||
if not self.request.user.is_superuser: | |||
parent_queryset = self.get_organization_queryset(parent_queryset) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This doesn't look like something that was requested, why are you changing this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the get_organization_queryset method,
def get_organization_queryset(self, qs):
lookup = {self.organization_lookup: getattr(self.request.user, self._user_attr)}
return qs.filter(**lookup)
self.organization_lookup
has the value organization__in
, but in the User
model, there is no direct link to Organization
. It is managed by OrganizationUser
to obtain many-to-many relationships between the Organization and User models. So, it adds openwisp_user
as the prefix of the organization field and makes the field name openwisp_user_organization
. However, we are searching for the field name organization
, which cannot be found and causes a field error
.
What I have gone through:
- Remove this if condition
if not self.request.user.is_superuser
as it was not used in the previous implementation ofBaseEmailView
assert_parent_exists
method. All test case passes so I have gone for this. - I have tried changing the lookup to check for the
openwisp_user_organization
field instead, but it causes other test failures. The models defined in /test folder likeConfig
,Book
,Shelf
etc. have ForeignKey to the organization so test on these expecting field names asorganization
.
Please provide your thoughts on this
Why? What's the reasoning behind this choice?
The fact that no test fails after the removal is concerning, it shows a lack of test coverage, but I am pretty sure it is not ok to remove this code.
Can you explain why you are compelled to change the internal logic of these classes instead of focusing on what the issue description is asking? I don't understand your reasoning behind these changes. Please explain so we can understand. |
@@ -198,7 +197,7 @@ def update(self, request, *args, **kwargs): | |||
) | |||
|
|||
|
|||
class BaseEmailView(ProtectedAPIMixin, GenericAPIView): | |||
class BaseEmailView(ProtectedAPIMixin, FilterByParentOwned, GenericAPIView): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this should be:
class BaseEmailView(ProtectedAPIMixin, FilterByParentOwned, GenericAPIView): | |
class BaseEmailView(ProtectedAPIMixin, FilterByParentManaged, GenericAPIView): |
That allows organization managers to use the endpoint.
Refactored BaseEmailView to inherit from FilterByParent mixin.
Fixes #354
Checklist
Reference to Existing Issue
Closes #354 .
Description of Changes
I have removed the
assert_parent_exists
method and inBaseEmailView
as It is present inFilterByParent
and in the 'assert_parent_exists' method in FilterByParent made some changes which I am not sure to be correct but all the test cases are passing now.I have a few questions as well
In between
FilterByParent
,FilterByParentMembership
,FilterByParentManaged
, andFilterByParentOwned
which class to inherit I have gone forFilterByParentOwned
.Is this okay to remove this in the method
assert_parent_exists
in classFilterByParent
? All test cases are passing after the removal as well.Removed the above code because it is causing issues, in the
get_organization_queryset
method theself.organization_lookup
from theOrgLookup
class hasorganization__in
inside it but the field in theEmailAddress
model isopenwisp_user_orginization
notorganization
causingFieldError
that's why decided to remove this.OrgLookup
class to addorganization_lookup
correctly or removing the code is fine?Please give your thoughts on this!