add celery

This commit is contained in:
Sayyid Hamid Mahdavi 2020-09-15 11:54:53 +04:30
parent 3f106ecd1b
commit 87e8eda4c6
8 changed files with 66 additions and 22 deletions

View file

@ -10,10 +10,9 @@ from django.utils import timezone
from django.utils.translation import gettext_lazy as _
from rest_framework.exceptions import APIException
import uuid
from .tasks import send_notification
from apps.users.constans import MAX_OTP_TRY, DEVELOPMENT_PHONE_NUMBERS
from utils.service_client import service_client
class UserManager(BaseUserManager):
use_in_migrations = True
@ -169,14 +168,12 @@ class User(AbstractUser):
self.notify(body=str(self.otp), title='OTP ')
def notify(self, body, title=None, notification_type='sms'):
if settings.DEBUG:
print(service_client.notification.submit_notification(phone_number=self.phone_number,
send_notification.delay(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}"

26
apps/users/tasks.py Normal file
View 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
)

View file

@ -0,0 +1,3 @@
from .celery import app as celery_app
__all__ = ['celery_app']

25
gooyal_accounts/celery.py Normal file
View 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))

View file

@ -179,3 +179,5 @@ STATIC_URL = '/static/'
BASE_NOTIFICATION_URL = config('BASE_NOTIFICATION_URL')
CLIENT_ID = config('CLIENT_ID')
CLIENT_SECRET = config('CLIENT_SECRET')
CELERY_BROKER_URL = config('CELERY_BROKER_URL')

View file

@ -12,3 +12,4 @@ django-mysql
python-decouple
gunicorn
gevent
redis

View file

View file

@ -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'])