Open TAnas0 opened 2 years ago
I ended up writing an "Adapter Mixin" close to the following:
class WaffleAdapterMixin(WaffleFlagMixin):
def dispatch(self, request, *args, **kwargs):
request = self.initialize_request(request, *args, **kwargs)
...
return super().dispatch(request._request, *args, **kwargs) # https://stackoverflow.com/a/35442073/4017403
I am realizing now that this issue is also due to the fatc that I am using Django REST framework, and that my above code could be part of a DRF sub-library.
If we can hear back from one of the contributors and some guidance, I could work on such a PR
Can you provide an example of a view that breaks?
@clintonb I'll try making a repository which reproduces all the above this week
I have a Django Rest Framework API and I am using Waffle.
I am using JWT authentication, and
ModelViewSet
classes with the mixinWaffleFlagMixin
, and a custom Flag model very similar to the one given as an example in the docs.The Flag has been having weird behaviours where, even if logged in, the
user
variable during Waffle checks was always set to anAnonymousUser
.After many trials and errors and debugging the code under different scenarios, I managed to fix it and it seems to me that the problem is the
dispatch
method ofWaffleFlagMixin
(https://github.com/django-waffle/django-waffle/blob/master/waffle/mixins.py#L29) doesn't call theself.initialize_request
method (https://www.django-rest-framework.org/api-guide/views/#initialize_requestself-request-args-kwargs) This is where it seems that therequest.user
variable is set, at least with JWT authentication.Is this something that should be fixed? I am surprised nobody faced this error before, or maybe this is an edge case For now I can monkey-path the Waffle Mixin and be fine.
Also wondering: How does this affect the other ways a view will use Waffle? What about using a Custom Flag model?
I can make a reproducible project to show the bug if there is interest in addressing this :man_shrugging: