Skip to content

Commit

Permalink
Merge pull request #5546 from nyaruka/require_features_mixin
Browse files Browse the repository at this point in the history
Add mixin for views that require a feature
  • Loading branch information
rowanseymour authored Oct 10, 2024
2 parents b34b4ca + 06595c3 commit 4943d87
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 14 deletions.
16 changes: 15 additions & 1 deletion temba/orgs/views/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,23 @@ def pre_process(self, request, *args, **kwargs):
)


class RequireFeatureMixin:
"""
Mixin for views that require that the org has a feature enabled
"""

require_feature = None # feature or tuple of features (requires any of them)

def pre_process(self, request, *args, **kwargs):
require_any = self.require_feature if isinstance(self.require_feature, tuple) else (self.require_feature,)

if set(request.org.features).isdisjoint(require_any):
return HttpResponseRedirect(reverse("orgs.org_workspace"))


class InferOrgMixin:
"""
Mixin for view whose object is the current org
Mixin for views whose object is the current request org
"""

@classmethod
Expand Down
20 changes: 7 additions & 13 deletions temba/orgs/views/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@
)
from .base import BaseMenuView
from .forms import SignupForm, SMTPForm
from .mixins import InferOrgMixin, OrgObjPermsMixin, OrgPermsMixin
from .mixins import InferOrgMixin, OrgObjPermsMixin, OrgPermsMixin, RequireFeatureMixin

# session key for storing a two-factor enabled user's id once we've checked their password
TWO_FACTOR_USER_SESSION_KEY = "_two_factor_user_id"
Expand Down Expand Up @@ -1349,7 +1349,9 @@ def post(self, request, *args, **kwargs):
self.object.release(request.user)
return self.render_modal_response()

class ManageAccounts(SpaMixin, InferOrgMixin, ContextMenuMixin, OrgPermsMixin, SmartUpdateView):
class ManageAccounts(
SpaMixin, InferOrgMixin, RequireFeatureMixin, ContextMenuMixin, OrgPermsMixin, SmartUpdateView
):
class AccountsForm(forms.ModelForm):
def __init__(self, org, *args, **kwargs):
super().__init__(*args, **kwargs)
Expand Down Expand Up @@ -1455,10 +1457,7 @@ class Meta:
success_url = "@orgs.org_manage_accounts"
title = _("Users")
menu_path = "/settings/users"

def pre_process(self, request, *args, **kwargs):
if Org.FEATURE_USERS not in request.org.features:
return HttpResponseRedirect(reverse("orgs.org_workspace"))
require_feature = Org.FEATURE_USERS

def build_context_menu(self, menu):
menu.add_modax(_("Invite"), "invite-create", reverse("orgs.invitation_create"), as_button=True)
Expand Down Expand Up @@ -1586,7 +1585,7 @@ def get_context_data(self, **kwargs):

return context

class Create(NonAtomicMixin, ModalFormMixin, InferOrgMixin, OrgPermsMixin, SmartCreateView):
class Create(NonAtomicMixin, RequireFeatureMixin, ModalFormMixin, InferOrgMixin, OrgPermsMixin, SmartCreateView):
class Form(forms.ModelForm):
TYPE_CHILD = "child"
TYPE_NEW = "new"
Expand All @@ -1607,12 +1606,7 @@ class Meta:
fields = ("type", "name", "timezone")

form_class = Form

def pre_process(self, request, *args, **kwargs):
# if org has neither feature then redirect
features = self.request.org.features
if Org.FEATURE_NEW_ORGS not in features and Org.FEATURE_CHILD_ORGS not in features:
return HttpResponseRedirect(reverse("orgs.org_workspace"))
require_feature = (Org.FEATURE_NEW_ORGS, Org.FEATURE_CHILD_ORGS)

def get_form_kwargs(self):
kwargs = super().get_form_kwargs()
Expand Down

0 comments on commit 4943d87

Please sign in to comment.