jazzband / django-waffle

A feature flipper for Django
https://waffle.readthedocs.io
BSD 3-Clause "New" or "Revised" License
1.13k stars 259 forks source link

Support multiple switches/flags on [API] view #373

Closed jheld closed 4 years ago

jheld commented 4 years ago

I initially tried (only for an hour or so, several months ago) to extend this project to allow a view to support multiple flags/switches/toggles. I was not successful, although I didn't have the time to see it through back then.

Is this something that if implemented would potentially not be a huge undertaking? That is, mostly reuse the flow & internals of the project, and thus not a rewrite, etc.

I'd be up for trying again but am hoping a core dev here may have some insight.

My team specifically works on performance, optimization, and technology improvements and so we fairly often require experimentation/utilization of multiple (usually) toggles on a particular view.

clintonb commented 4 years ago

Are you referring to views or templates? Using multiple toggles in a view is as simple as nesting if-else statements. Using multiple toggles in a template requires nesting template tags.

{% load waffle_tags %}

{% flag "foo" %}
  {% flag "bar" %}
    foo and bar are active!
  {% else %}
    foo is active, but bar is inactive
  {% endflag %}
{% else %}
  foo is inactive
{% endflag %}
jheld commented 4 years ago

Definitely views. I'm updating the title now to reflect that. Actually it's in the title. I'll update that it's for APIs.

Based on the documentation it appeared that while using the decorator on the view it only supported just 1 decorator (as opposed to multiple). Am I mistaken?

I think there may be a conceptual mismatch (though that may just be my misunderstanding). For instance if I'm testing different code paths (e.g. for optimizations), "disabling" a toggle/switch should still run the view (and the view is responsible for managing the actual logic/flow), whereas in the docs (it's been several months since I read them) it seemed it would return an error to the user when it's off.

clintonb commented 4 years ago

I’m referring to https://waffle.readthedocs.io/en/stable/usage/views.html. You can chain a couple if clauses and raise Http404 in the else case. It’s not as pretty as decorators, but it gets the job done.

jheld commented 4 years ago

Thank you for the link!

This appears that it would support the majority of our use case, in terms of multiple support.

It seems we may have a more complex design necessity in that our use cases aren't always on/off (e.g. larger than a binary value). That is currently only in a minority of our experiments; we don't like supporting/testing out "greater than 2" values of a particular experiment type long term either!