add celery
This commit is contained in:
parent
3f106ecd1b
commit
87e8eda4c6
8 changed files with 66 additions and 22 deletions
|
|
@ -10,10 +10,9 @@ from django.utils import timezone
|
||||||
from django.utils.translation import gettext_lazy as _
|
from django.utils.translation import gettext_lazy as _
|
||||||
from rest_framework.exceptions import APIException
|
from rest_framework.exceptions import APIException
|
||||||
import uuid
|
import uuid
|
||||||
|
from .tasks import send_notification
|
||||||
|
|
||||||
from apps.users.constans import MAX_OTP_TRY, DEVELOPMENT_PHONE_NUMBERS
|
from apps.users.constans import MAX_OTP_TRY, DEVELOPMENT_PHONE_NUMBERS
|
||||||
from utils.service_client import service_client
|
|
||||||
|
|
||||||
|
|
||||||
class UserManager(BaseUserManager):
|
class UserManager(BaseUserManager):
|
||||||
use_in_migrations = True
|
use_in_migrations = True
|
||||||
|
|
@ -168,15 +167,13 @@ class User(AbstractUser):
|
||||||
def send_otp(self):
|
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'):
|
def notify(self, body, title=None, notification_type='sms'):
|
||||||
if settings.DEBUG:
|
send_notification.delay(phone_number=self.phone_number,
|
||||||
print(service_client.notification.submit_notification(phone_number=self.phone_number,
|
title=title,
|
||||||
title=title,
|
body=body,
|
||||||
body=body,
|
user_uuid=self.uuid,
|
||||||
user_uuid=self.uuid,
|
notification_type=1,
|
||||||
notification_type=1,
|
)
|
||||||
))
|
|
||||||
|
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return f"{self.pk} - {self.username}"
|
return f"{self.pk} - {self.username}"
|
||||||
|
|
|
||||||
26
apps/users/tasks.py
Normal file
26
apps/users/tasks.py
Normal file
|
|
@ -0,0 +1,26 @@
|
||||||
|
from celery import shared_task
|
||||||
|
from django.conf import settings
|
||||||
|
from service_clients import Client, AccountsClient
|
||||||
|
|
||||||
|
|
||||||
|
@shared_task
|
||||||
|
def send_notification(phone_number=None,
|
||||||
|
title=None,
|
||||||
|
body=None,
|
||||||
|
user_uuid=None,
|
||||||
|
email=None,
|
||||||
|
notification_type=1):
|
||||||
|
|
||||||
|
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
|
||||||
|
)
|
||||||
|
|
@ -0,0 +1,3 @@
|
||||||
|
from .celery import app as celery_app
|
||||||
|
|
||||||
|
__all__ = ['celery_app']
|
||||||
25
gooyal_accounts/celery.py
Normal file
25
gooyal_accounts/celery.py
Normal file
|
|
@ -0,0 +1,25 @@
|
||||||
|
from __future__ import absolute_import, unicode_literals
|
||||||
|
|
||||||
|
import os
|
||||||
|
|
||||||
|
from celery import Celery
|
||||||
|
from django.conf import settings
|
||||||
|
|
||||||
|
# set the default Django settings module for the 'celery' program.
|
||||||
|
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'gooyal_accounts.settings')
|
||||||
|
|
||||||
|
app = Celery('notification_service')
|
||||||
|
|
||||||
|
# Using a string here means the worker doesn't have to serialize
|
||||||
|
# the configuration object to child processes.
|
||||||
|
# - namespace='CELERY' means all celery-related configuration keys
|
||||||
|
# should have a `CELERY_` prefix.
|
||||||
|
app.config_from_object('django.conf:settings', namespace='CELERY')
|
||||||
|
|
||||||
|
# Load task modules from all registered Django app configs.
|
||||||
|
app.autodiscover_tasks()
|
||||||
|
|
||||||
|
|
||||||
|
@app.task(bind=True)
|
||||||
|
def debug_task(self):
|
||||||
|
print('Request: {0!r}'.format(self.request))
|
||||||
|
|
@ -179,3 +179,5 @@ STATIC_URL = '/static/'
|
||||||
BASE_NOTIFICATION_URL = config('BASE_NOTIFICATION_URL')
|
BASE_NOTIFICATION_URL = config('BASE_NOTIFICATION_URL')
|
||||||
CLIENT_ID = config('CLIENT_ID')
|
CLIENT_ID = config('CLIENT_ID')
|
||||||
CLIENT_SECRET = config('CLIENT_SECRET')
|
CLIENT_SECRET = config('CLIENT_SECRET')
|
||||||
|
|
||||||
|
CELERY_BROKER_URL = config('CELERY_BROKER_URL')
|
||||||
|
|
|
||||||
|
|
@ -12,3 +12,4 @@ django-mysql
|
||||||
python-decouple
|
python-decouple
|
||||||
gunicorn
|
gunicorn
|
||||||
gevent
|
gevent
|
||||||
|
redis
|
||||||
|
|
@ -1,10 +0,0 @@
|
||||||
from django.conf import settings
|
|
||||||
|
|
||||||
from service_clients import Client, AccountsClient
|
|
||||||
|
|
||||||
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'])
|
|
||||||
Loading…
Add table
Reference in a new issue