-
Notifications
You must be signed in to change notification settings - Fork 25
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
Add ChannelType.check_credentials
and use for Twilio claims and updates
#4275
Changes from all commits
4ff9ed7
e0d8406
e81a405
171e09b
1613637
2d8bfd8
f02a0f6
0e10eb5
fdbd9e8
1ae190e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,27 @@ | ||
from typing import Any, Dict | ||
|
||
import phonenumbers | ||
from phonenumbers.phonenumberutil import region_code_for_number | ||
from smartmin.views import SmartFormView | ||
from twilio.base.exceptions import TwilioException, TwilioRestException | ||
from twilio.rest import Client as TwilioClient | ||
|
||
from django import forms | ||
from django.conf import settings | ||
from django.core.exceptions import ValidationError | ||
from django.http import HttpResponseRedirect, JsonResponse | ||
from django.urls import reverse | ||
from django.utils.translation import gettext_lazy as _ | ||
|
||
from temba.orgs.models import Org | ||
from temba.orgs.views import OrgPermsMixin | ||
from temba.utils import countries | ||
from temba.utils.fields import SelectWidget | ||
from temba.utils.fields import InputWidget, SelectWidget | ||
from temba.utils.timezones import timezone_to_country_code | ||
from temba.utils.uuid import uuid4 | ||
|
||
from ...models import Channel | ||
from ...views import ALL_COUNTRIES, BaseClaimNumberMixin, ClaimViewMixin | ||
from ...views import ALL_COUNTRIES, BaseClaimNumberMixin, ClaimViewMixin, UpdateChannelForm | ||
|
||
SUPPORTED_COUNTRIES = { | ||
"AU", # Australia | ||
|
@@ -315,3 +319,54 @@ def form_valid(self, form, *args, **kwargs): | |
return JsonResponse({"error": str(msg)}) | ||
|
||
return JsonResponse(numbers, safe=False) | ||
|
||
|
||
class UpdateForm(UpdateChannelForm): | ||
def __init__(self, *args, **kwargs): | ||
super().__init__(*args, **kwargs) | ||
|
||
self.add_config_field( | ||
"account_sid", | ||
forms.CharField( | ||
max_length=128, | ||
label=_("Twilio Account SID"), | ||
required=True, | ||
widget=InputWidget(attrs={"disabled": "disabled"}), | ||
), | ||
default="", | ||
) | ||
|
||
self.add_config_field( | ||
"auth_token", | ||
forms.CharField( | ||
max_length=128, | ||
label=_("Twilio Account Auth Token"), | ||
required=True, | ||
widget=InputWidget(), | ||
), | ||
default="", | ||
) | ||
|
||
def clean(self) -> Dict[str, Any]: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. def clean(self) -> dict[str, Any]:
"""
We override the clean method for Twilio we need to make sure we grab the primary auth tokens
""" |
||
""" | ||
We override the clean method for Twilio we need to make sure we grab the primary auth tokens | ||
""" | ||
account_sid = self.cleaned_data.get("account_sid", None) | ||
account_token = self.cleaned_data.get("auth_token", None) | ||
|
||
try: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We want to use self.instance.check_credentials({**self.instance.config, "account_sid": account_sid, "auth_token": auth_token}) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the main thing here is we want to grab the primary credentials so otherwise we would have relied on the base class clean from that use the method that way |
||
client = TwilioClient(account_sid, account_token) | ||
|
||
# get the actual primary auth tokens from twilio and use them | ||
account = client.api.account.fetch() | ||
self.cleaned_data["account_sid"] = account.sid | ||
self.cleaned_data["auth_token"] = account.auth_token | ||
except Exception: # pragma: needs cover | ||
raise ValidationError( | ||
_("The Twilio account SID and Token seem invalid. Please check them again and retry.") | ||
) | ||
|
||
return super().clean() | ||
|
||
class Meta(UpdateChannelForm.Meta): | ||
fields = ("name",) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I exatended the channel update to include the credentials field for for the channel type