Skip to content

Commit

Permalink
feat: add shortener custom domain
Browse files Browse the repository at this point in the history
  • Loading branch information
teehamaral committed Jan 19, 2024
1 parent b5b7671 commit c22ab27
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 0 deletions.
73 changes: 73 additions & 0 deletions temba/orgs/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -1233,6 +1233,7 @@ class OrgCRUDL(SmartCRUDL):
"translations",
"channels_mapping",
"calendar_automation_flow",
"shortener_custom_domain",
"translate",
"opt_out_message",
"dashboard_setup",
Expand Down Expand Up @@ -4019,6 +4020,11 @@ def derive_formax_sections(self, formax, context):
"calendar_automation_flow", reverse("orgs.org_calendar_automation_flow"), icon="icon-flow"
)

if self.has_org_perm("orgs.org_shortener_custom_domain"):
formax.add_section(
"shortener_custom_domain", reverse("orgs.org_shortener_custom_domain"), icon="icon-earth"
)

# 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 @@ -4711,6 +4717,73 @@ def form_valid(self, form):
org.save(update_fields=["config"])
return super().form_valid(form)

class ShortenerCustomDomain(InferOrgMixin, OrgPermsMixin, SmartUpdateView):
class ShortenerCustomDomainForm(forms.ModelForm):
domain = forms.CharField(
required=False,
label=_("Domain"),
widget=InputWidget,
help_text=_("Please provide your domain without to be used on your shorten link."),
)

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

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

def clean_domain(self):
domain = self.cleaned_data.get("domain")
if domain:
domain = str(domain).replace("https", "").replace("/", "").replace(":", "")

return domain

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

success_message = ""
form_class = ShortenerCustomDomainForm

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

domain = (org.config or {}).get("shortener_custom_domain", None)
if domain:
context["domain"] = domain

return context

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

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

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 @@ -464,6 +464,7 @@
"translations",
"channels_mapping",
"calendar_automation_flow",
"shortener_custom_domain",
"opt_out_message",
"dashboard_setup",
),
Expand Down Expand Up @@ -613,6 +614,7 @@
"orgs.topup_manage",
"orgs.topup_update",
"orgs.org_calendar_automation_flow",
"orgs.org_shortener_custom_domain",
"orgs.org_dashboard_setup",
"policies.policy_create",
"policies.policy_update",
Expand Down Expand Up @@ -715,6 +717,7 @@
"orgs.org_translations",
"orgs.org_channels_mapping",
"orgs.org_calendar_automation_flow",
"orgs.org_shortener_custom_domain",
"orgs.org_opt_out_message",
"channels.channel_api",
"channels.channel_bulk_sender_options",
Expand Down
9 changes: 9 additions & 0 deletions templates/orgs/org_shortener_custom_domain.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
-extends "smartmin/update.html"
-load i18n

-block summary
-if domain
-blocktrans trimmed with custom_domain=domain
Shorten links custom domain: <b>{{ custom_domain }}</b><br>
-else
-trans "You can set a custom domain for your shorten links."

0 comments on commit c22ab27

Please sign in to comment.