push
This commit is contained in:
parent
308faff858
commit
be838d1d19
21 changed files with 119 additions and 0 deletions
|
|
@ -13,6 +13,7 @@ import logging
|
||||||
from apps.promotions.handlers import ProcessorTypeChoices, BasePromotionHandler, PromotionTypeChoices
|
from apps.promotions.handlers import ProcessorTypeChoices, BasePromotionHandler, PromotionTypeChoices
|
||||||
|
|
||||||
from apps.users.models import User
|
from apps.users.models import User
|
||||||
|
from utils.clients.notifications_client import notifications_push_user
|
||||||
from utils.clients.wallet_client import deposit_to_user_wallet_submit, deposit_to_user_wallet_verify
|
from utils.clients.wallet_client import deposit_to_user_wallet_submit, deposit_to_user_wallet_verify
|
||||||
from utils.models import BaseModel
|
from utils.models import BaseModel
|
||||||
|
|
||||||
|
|
@ -409,6 +410,12 @@ class Promotion(BaseModel):
|
||||||
)
|
)
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
try:
|
||||||
|
notifications_push_user(self.user_uuid, self.recipient.label, self.recipient.label, 5, extras={})
|
||||||
|
except:
|
||||||
|
# TODO: log
|
||||||
|
pass
|
||||||
|
|
||||||
with transaction.atomic():
|
with transaction.atomic():
|
||||||
# updated = Plan.objects.filter(pk=self.plan.pk).update(balance=F('balance') - self.promotion_amount, **plan_data)
|
# updated = Plan.objects.filter(pk=self.plan.pk).update(balance=F('balance') - self.promotion_amount, **plan_data)
|
||||||
updated = Plan.objects.filter(pk=self.plan.pk).update(**plan_data)
|
updated = Plan.objects.filter(pk=self.plan.pk).update(**plan_data)
|
||||||
|
|
|
||||||
112
utils/clients/notifications_client.py
Normal file
112
utils/clients/notifications_client.py
Normal file
|
|
@ -0,0 +1,112 @@
|
||||||
|
import logging
|
||||||
|
|
||||||
|
import requests
|
||||||
|
import time
|
||||||
|
from django.conf import settings
|
||||||
|
from django.core.cache import cache
|
||||||
|
from django.db import models
|
||||||
|
from httpx import Request
|
||||||
|
|
||||||
|
from utils.clients.gooyal_notifications_client import AuthenticatedClient
|
||||||
|
from utils.clients.gooyal_notifications_client.api.email import email_application_application_create
|
||||||
|
from utils.clients.gooyal_notifications_client.api.push import push_application_application_create
|
||||||
|
from utils.clients.gooyal_notifications_client.models import Email
|
||||||
|
from utils.clients.gooyal_notifications_client.models import PushMessage
|
||||||
|
|
||||||
|
# logging.basicConfig(
|
||||||
|
# format="%(levelname)s [%(asctime)s] %(name)s - %(message)s",
|
||||||
|
# datefmt="%Y-%m-%d %H:%M:%S",
|
||||||
|
# level=logging.DEBUG
|
||||||
|
# )
|
||||||
|
logger = logging.getLogger('ads.utils.notifications_client')
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def login_as_client_credentials():
|
||||||
|
access_token = cache.get('ads_access_token')
|
||||||
|
# expires_in = self.auth_data.get('expires_in')
|
||||||
|
# created_at = self.auth_data.get('created_at')
|
||||||
|
#
|
||||||
|
# if access_token and expires_in > time.time() - created_at:
|
||||||
|
# return self.auth_data
|
||||||
|
if access_token:
|
||||||
|
logger.debug('token available in cache')
|
||||||
|
return access_token
|
||||||
|
|
||||||
|
logger.debug('no token available in cache')
|
||||||
|
|
||||||
|
data = {
|
||||||
|
"grant_type": 'client_credentials',
|
||||||
|
"scope": settings.OAUTH2_PROVIDER_SCOPES,
|
||||||
|
}
|
||||||
|
auth = (settings.OAUTH2_PROVIDER_CLIENT_ID, settings.OAUTH2_PROVIDER_CLIENT_SECRET)
|
||||||
|
|
||||||
|
response = requests.post(f'{settings.OAUTH2_PROVIDER_BASE_PUBLIC_URL}/token/',
|
||||||
|
data=data,
|
||||||
|
auth=auth)
|
||||||
|
logger.debug(response.content)
|
||||||
|
|
||||||
|
auth_data = response.json()
|
||||||
|
if 'access_token' in auth_data:
|
||||||
|
auth_data['created_at'] = time.time()
|
||||||
|
cache.set('ads_access_token', auth_data, timeout=auth_data.get('expires_in') - 5)
|
||||||
|
|
||||||
|
return auth_data
|
||||||
|
|
||||||
|
|
||||||
|
def log_request(request: Request):
|
||||||
|
logger.debug(f'authorization: {request.headers["authorization"]}')
|
||||||
|
logger.debug(
|
||||||
|
f"Request event hook: {request.method} {request.url} {request.method} {request.headers} - Waiting for response")
|
||||||
|
|
||||||
|
|
||||||
|
def log_response(response):
|
||||||
|
request = response.request
|
||||||
|
logger.debug(f"Response event hook: {request.method} {request.url} - Status {response.status_code}")
|
||||||
|
# logger.debug(f"Response event hook: {response.read()}")
|
||||||
|
|
||||||
|
|
||||||
|
def get_notifications_client():
|
||||||
|
access_token = login_as_client_credentials()['access_token']
|
||||||
|
base_url = settings.NOTIFICATIONS_BASE_PUBLIC_URL
|
||||||
|
client = AuthenticatedClient(base_url=base_url, token=access_token,
|
||||||
|
httpx_args={"event_hooks": {"request": [log_request], "response": [log_response]}},
|
||||||
|
)
|
||||||
|
return client
|
||||||
|
|
||||||
|
|
||||||
|
def notifications_push_user(user_uuid, title, message, priority=5, extras=None):
|
||||||
|
class Tmp():
|
||||||
|
def to_dict(self):
|
||||||
|
return {
|
||||||
|
'title': title,
|
||||||
|
'message': message,
|
||||||
|
'priority': priority,
|
||||||
|
'extras': extras,
|
||||||
|
}
|
||||||
|
|
||||||
|
push_data = Tmp()
|
||||||
|
client = get_notifications_client()
|
||||||
|
response: PushMessage = push_application_application_create.sync(client=client, body=push_data, user_uuid=user_uuid)
|
||||||
|
|
||||||
|
return response
|
||||||
|
|
||||||
|
|
||||||
|
def submit_event_in_queue(title=None, message=None):
|
||||||
|
try:
|
||||||
|
class Tmp():
|
||||||
|
def to_dict(self):
|
||||||
|
return {
|
||||||
|
"queue": settings.NOTIFICATIONS_EVENT_QUEUE,
|
||||||
|
"title": title,
|
||||||
|
"message": message
|
||||||
|
}
|
||||||
|
|
||||||
|
email_data = Tmp()
|
||||||
|
client = get_notifications_client()
|
||||||
|
response: Email = email_application_application_create.sync(client=client, body=email_data,
|
||||||
|
user_uuid=settings.NOTIFICATIONS_OPERATOR_USER_UUID)
|
||||||
|
|
||||||
|
return response
|
||||||
|
except Exception as e:
|
||||||
|
logger.exception(e)
|
||||||
Loading…
Add table
Reference in a new issue