Skip to content

Commit

Permalink
Merge branch 'feat/calendar-automation' into dev/7.0
Browse files Browse the repository at this point in the history
  • Loading branch information
teehamaral committed Nov 23, 2023
2 parents 8f67da1 + 1a9b1cd commit b99fd84
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 0 deletions.
99 changes: 99 additions & 0 deletions temba/orgs/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -1232,6 +1232,7 @@ class OrgCRUDL(SmartCRUDL):
"send_invite",
"translations",
"channels_mapping",
"calendar_automation_flow",
"translate",
"opt_out_message",
"dashboard_setup",
Expand Down Expand Up @@ -4013,6 +4014,11 @@ def derive_formax_sections(self, formax, context):
formax.add_section("org", reverse("orgs.org_edit"), icon="icon-office")
formax.add_section("channels_mapping", reverse("orgs.org_channels_mapping"), icon="icon-feed")

if self.has_org_perm("orgs.org_calendar_automation_flow"):
formax.add_section(
"calendar_automation_flow", reverse("orgs.org_calendar_automation_flow"), icon="icon-flow"
)

# only pro orgs get multiple users
if self.has_org_perm("orgs.org_manage_accounts") and org.is_multi_user:
formax.add_section("accounts", reverse("orgs.org_accounts"), icon="icon-users", action="redirect")
Expand Down Expand Up @@ -4612,6 +4618,99 @@ def form_valid(self, form):
org.save(update_fields=["config"])
return super().form_valid(form)

class CalendarAutomationFlow(InferOrgMixin, OrgPermsMixin, SmartUpdateView):
class CalendarAutomationFlowForm(forms.ModelForm):
flow = forms.ChoiceField(
choices=[],
widget=SelectWidget(attrs={"searchable": True, "clearable": True}),
label=_("Message Flow"),
help_text=_("The flow to be triggered on the calendar automation."),
required=False,
)

def __init__(self, *args, **kwargs):
self.org = kwargs["org"]
del kwargs["org"]

super().__init__(*args, **kwargs)

self.fields["flow"].choices = [("", "")] + list(
self.org.flows.filter(
is_active=True,
is_system=False,
flow_type=Flow.TYPE_MESSAGE,
)
.order_by("name")
.values_list("uuid", "name")
)

def clean(self):
cleaned_data = super().clean()
flow = cleaned_data.get("flow")

is_valid_flow = True
if flow:
is_valid_flow = (
self.org.flows.filter(
uuid=flow,
is_active=True,
is_system=False,
flow_type=Flow.TYPE_MESSAGE,
).first()
is not None
)

if not is_valid_flow:
self.add_error(
"flow", "The flow selected is not associated with this organization or is not active"
)

class Meta:
model = Org
fields = ("flow",)

success_message = ""
form_class = CalendarAutomationFlowForm

def derive_initial(self):
initial = super().derive_initial()
org = self.derive_org()
initial["flow"] = (org.config or {}).get("calendar_automation_flow", "")
return initial

def get_form_kwargs(self):
kwargs = super().get_form_kwargs()
kwargs["org"] = self.request.user.get_org()
return kwargs

def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
org = self.request.user.get_org()

flow = (org.config or {}).get("calendar_automation_flow", None)
if flow:
flow_obj = self.org.flows.filter(uuid=flow).first()
context["flow"] = flow_obj.name

return context

def form_valid(self, form):
org = self.request.user.get_org()
current_config = org.config or {}
flow = form.cleaned_data.get("flow")

if not flow:
try:
current_config.pop("calendar_automation_flow")
except KeyError:
pass
else:
current_config.update({"calendar_automation_flow": flow})

org.config = current_config
org.save(update_fields=["config"])
return super().form_valid(form)

class DashboardSetup(InferOrgMixin, OrgPermsMixin, SmartFormView):
class DashboardSetupForm(forms.Form):
dashboards = forms.MultipleChoiceField(
Expand Down
3 changes: 3 additions & 0 deletions temba/settings_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,7 @@
"workspace",
"translations",
"channels_mapping",
"calendar_automation_flow",
"opt_out_message",
"dashboard_setup",
),
Expand Down Expand Up @@ -611,6 +612,7 @@
"orgs.topup_create",
"orgs.topup_manage",
"orgs.topup_update",
"orgs.org_calendar_automation_flow",
"orgs.org_dashboard_setup",
"policies.policy_create",
"policies.policy_update",
Expand Down Expand Up @@ -712,6 +714,7 @@
"orgs.topup_read",
"orgs.org_translations",
"orgs.org_channels_mapping",
"orgs.org_calendar_automation_flow",
"orgs.org_opt_out_message",
"channels.channel_api",
"channels.channel_bulk_sender_options",
Expand Down
10 changes: 10 additions & 0 deletions templates/orgs/org_calendar_automation_flow.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
-extends "smartmin/update.html"
-load i18n

-block summary
-if flow
-blocktrans trimmed with selected_flow=flow
Calendar automation flow: <b>{{ selected_flow }}</b><br>

-else
-trans "Select your calendar automation flow."

0 comments on commit b99fd84

Please sign in to comment.