From 0e994d7f7a06c53a7b59351d6ad39dcba2d4762b Mon Sep 17 00:00:00 2001 From: mahdavi Date: Sun, 1 Nov 2020 23:54:09 +0330 Subject: [PATCH] sync send sms --- apps/users/models.py | 16 ++++++++-------- apps/users/tasks.py | 25 +++++++++++-------------- 2 files changed, 19 insertions(+), 22 deletions(-) diff --git a/apps/users/models.py b/apps/users/models.py index 91cdd5e..cd127b1 100644 --- a/apps/users/models.py +++ b/apps/users/models.py @@ -166,16 +166,16 @@ class User(AbstractUser): return result def send_otp(self): - self.notify(body=str(self.otp), title='OTP ') + self.notify(body=str(self.otp), title='OTP') def notify(self, body, title=None, notification_type='sms'): - print('request send sms') - # send_notification.delay(phone_number=self.phone_number, - # title=title, - # body=body, - # user_uuid=self.uuid, - # notification_type=1, - # ) + # TODO: enable celery + send_notification(phone_number=self.phone_number, + title=title, + body=body, + user_uuid=self.uuid, + notification_type=1, + ) def __str__(self): return f"{self.pk} - {self.username}" diff --git a/apps/users/tasks.py b/apps/users/tasks.py index 8a25d1a..dc7bd82 100644 --- a/apps/users/tasks.py +++ b/apps/users/tasks.py @@ -10,17 +10,14 @@ def send_notification(phone_number=None, user_uuid=None, email=None, notification_type=1): - print('sms sent') - # service_client = Client( - # client_id=settings.CLIENT_ID, - # client_secret=settings.CLIENT_SECRET, - # grant_type=AccountsClient.GRANT_CLIENT_CREDENTIALS - # ) - # service_client.login(scopes=['notification.notification:submit']) - # service_client.notification.submit_notification(phone_number=phone_number, - # title=title, - # body=body, - # user_uuid=user_uuid, - # notification_type=notification_type, - # email=email - # ) + SCOPES = ['notifications.notification:submit'] + client = Client(client_id=settings.CLIENT_ID, + client_secret=settings.CLIENT_SECRET, + scopes=SCOPES, + grant_type=AccountsClient.GRANT_CLIENT_CREDENTIALS) + data = { + 'phone_number': phone_number, + "body": f'{title}:\n{body}' + } + print(client.request(required_scopes=SCOPES, url=f'{settings.BASE_NOTIFICATION_URL}/notifications/', data=data, + method='post').text)