Skip to content
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

Include IVR/SMS channel mapping #391

Merged
merged 1 commit into from
Oct 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
127 changes: 127 additions & 0 deletions temba/orgs/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -1231,6 +1231,7 @@ class OrgCRUDL(SmartCRUDL):
"parse_data_import",
"send_invite",
"translations",
"channels_mapping",
"translate",
"opt_out_message",
"dashboard_setup",
Expand Down Expand Up @@ -4010,6 +4011,7 @@ def derive_formax_sections(self, formax, context):

if self.has_org_perm("orgs.org_edit"):
formax.add_section("org", reverse("orgs.org_edit"), icon="icon-office")
formax.add_section("channels_mapping", reverse("orgs.org_channels_mapping"), icon="icon-feed")

# only pro orgs get multiple users
if self.has_org_perm("orgs.org_manage_accounts") and org.is_multi_user:
Expand Down Expand Up @@ -4485,6 +4487,131 @@ def form_valid(self, form):
org.save(update_fields=["config"])
return super().form_valid(form)

class ChannelsMapping(InferOrgMixin, OrgPermsMixin, SmartUpdateView):
class ChannelsMappingForm(forms.ModelForm):
voice_channel = forms.ChoiceField(
choices=[],
widget=SelectWidget(attrs={"searchable": True, "clearable": True}),
label=_("IVR/Voice"),
help_text=_("The preferred channel for IVR/Voice."),
required=False,
)

sms_channel = forms.ChoiceField(
choices=[],
widget=SelectWidget(attrs={"searchable": True, "clearable": True}),
label=_("SMS/MMS"),
help_text=_("The preferred channel for SMS/MMS."),
required=False,
)

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

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

self.fields["voice_channel"].choices = [("", "")] + list(
self.org.channels.filter(
Q(role__contains=Channel.ROLE_CALL) | Q(role__contains=Channel.ROLE_ANSWER),
schemes=["tel"],
is_active=True,
)
.order_by("name")
.values_list("uuid", "name")
)

self.fields["sms_channel"].choices = [("", "")] + list(
self.org.channels.filter(
Q(role__contains=Channel.ROLE_SEND) | Q(role__contains=Channel.ROLE_RECEIVE),
schemes=["tel"],
is_active=True,
)
.order_by("name")
.values_list("uuid", "name")
)

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

is_voice_valid_channel = True
is_sms_valid_channel = True

if voice_channel:
is_voice_valid_channel = self.org.channels.filter(uuid=voice_channel).first() is not None

if sms_channel:
is_sms_valid_channel = self.org.channels.filter(uuid=sms_channel).first() is not None

if not is_voice_valid_channel:
self.add_error("voice_channel", "Voice channel is not valid or is not on the organization anymore")

if not is_sms_valid_channel:
self.add_error("sms_channel", "SMS channel is not validor is not on the organization anymore")

class Meta:
model = Org
fields = ("voice_channel", "sms_channel")

success_message = ""
form_class = ChannelsMappingForm

def derive_initial(self):
initial = super().derive_initial()
org = self.derive_org()
initial["voice_channel"] = (org.config or {}).get("voice_preferred_channel", "")
initial["sms_channel"] = (org.config or {}).get("sms_preferred_channel", "")
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()

voice_channel = (org.config or {}).get("voice_preferred_channel", None)
if voice_channel:
channel = self.org.channels.filter(uuid=voice_channel).first()
context["voice_preferred_channel"] = channel.name

sms_channel = (org.config or {}).get("sms_preferred_channel", None)
if sms_channel:
channel = self.org.channels.filter(uuid=sms_channel).first()
context["sms_preferred_channel"] = channel.name

return context

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

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

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

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
2 changes: 2 additions & 0 deletions temba/settings_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,7 @@
"token",
"workspace",
"translations",
"channels_mapping",
"opt_out_message",
"dashboard_setup",
),
Expand Down Expand Up @@ -710,6 +711,7 @@
"orgs.topup_list",
"orgs.topup_read",
"orgs.org_translations",
"orgs.org_channels_mapping",
"orgs.org_opt_out_message",
"channels.channel_api",
"channels.channel_bulk_sender_options",
Expand Down
13 changes: 13 additions & 0 deletions templates/orgs/org_channels_mapping.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
-extends "smartmin/update.html"
-load i18n

-block summary
-if voice_preferred_channel or sms_preferred_channel
-if voice_preferred_channel
-blocktrans trimmed with ivr_channel=voice_preferred_channel
Preferred IVR channel: <b>{{ ivr_channel }}</b><br>
-if sms_preferred_channel
-blocktrans trimmed with sms_channel=sms_preferred_channel
Preferred SMS channel: <b>{{ sms_channel }}</b>
-else
-trans "Choose your preferred channels for IVR/Voice and SMS/MMS."
Loading