From 80fa39bd1b94f57ce8d382f1ef0829120cd348d2 Mon Sep 17 00:00:00 2001 From: A'zamov Samandar Date: Mon, 21 Apr 2025 19:53:10 +0500 Subject: [PATCH] HealthView'da status kod qo'shildi; RabbitMQ ulanishi qo'shildi; .gitignore yaratildi. --- payment/api/views.py | 2 +- rabbitmq.py | 31 +++++++++++++++++++++++++++++++ template/.gitignore | 2 ++ template/config/settings.py | 1 + template/docker-compose.yml | 7 +++++++ test.py | 17 ----------------- user/core/services/sms.py | 7 +++++-- user/core/utils/notification.py | 21 +++++++++++++++++++++ user/core/utils/rabbitmq.py | 15 +++++++++++++++ 9 files changed, 83 insertions(+), 20 deletions(-) create mode 100644 rabbitmq.py create mode 100644 template/.gitignore delete mode 100644 test.py create mode 100644 user/core/utils/notification.py create mode 100644 user/core/utils/rabbitmq.py diff --git a/payment/api/views.py b/payment/api/views.py index 3877edb..7789a6b 100644 --- a/payment/api/views.py +++ b/payment/api/views.py @@ -4,4 +4,4 @@ from rest_framework.response import Response class HealthView(APIView): def get(self, *args, **kwargs): - return Response(data={"detail": "OK"}) \ No newline at end of file + return Response(data={"detail": "OK"}, status=200) \ No newline at end of file diff --git a/rabbitmq.py b/rabbitmq.py new file mode 100644 index 0000000..2721dc9 --- /dev/null +++ b/rabbitmq.py @@ -0,0 +1,31 @@ +# rabbitmq.py + +from kombu import Connection, Exchange, Producer + +rabbit_url = 'amqp://guest:guest@rabbitmq:5672/' +_connection = None +_channel = None +_exchange = Exchange('notification', type='direct') +_producer = None + + +def get_connection(): + global _connection + if _connection is None or not _connection.connected: + _connection = Connection(rabbit_url) + _connection.ensure_connection(max_retries=3) + return _connection + + +def get_producer(): + global _producer, _channel + if _producer is None: + conn = get_connection() + _channel = conn.channel() + _producer = Producer(_channel, exchange=_exchange, routing_key="notification") + return _producer + + +def send_notification(message: dict): + producer = get_producer() + producer.publish(message, serializer='json') diff --git a/template/.gitignore b/template/.gitignore new file mode 100644 index 0000000..780f5f3 --- /dev/null +++ b/template/.gitignore @@ -0,0 +1,2 @@ +./staticfiles +.env \ No newline at end of file diff --git a/template/config/settings.py b/template/config/settings.py index bb269d2..c63b6da 100644 --- a/template/config/settings.py +++ b/template/config/settings.py @@ -119,6 +119,7 @@ USE_TZ = True # https://docs.djangoproject.com/en/5.1/howto/static-files/ STATIC_URL = "static/" +STATIC_ROOT = BASE_DIR / "staticfiles" # Default primary key field type # https://docs.djangoproject.com/en/5.1/ref/settings/#default-auto-field diff --git a/template/docker-compose.yml b/template/docker-compose.yml index 5a38f6e..3a944a1 100644 --- a/template/docker-compose.yml +++ b/template/docker-compose.yml @@ -3,6 +3,9 @@ networks: external: true name: lamenu +volumes: + pycache: null + services: app: labels: @@ -15,5 +18,9 @@ services: build: dockerfile: Dockerfile context: . + networks: + - lamenu + restart: always volumes: - ./:/code + - pycache:/var/cache/pycache diff --git a/test.py b/test.py deleted file mode 100644 index 67d8c87..0000000 --- a/test.py +++ /dev/null @@ -1,17 +0,0 @@ -from kombu import Connection, Exchange, Producer - -# RabbitMQ ulanishi -rabbit_url = 'amqp://guest:guest@127.0.0.1:5672/' -connection = Connection(rabbit_url) -channel = connection.channel() - -exchange = Exchange('notification', type='direct') - -# Producer yaratish -producer = Producer(channel, exchange=exchange, routing_key="notification") - -# Xabar yuborish -message = {'type': 'sms', 'message': "classcom.uz sayti va mobil ilovasiga ro'yxatdan o'tishingingiz uchun tasdiqlash kodi: 1234", "to": ["+998888112309", "+998943990509"]} -producer.publish(message) - -print("Message sent to all workers!") diff --git a/user/core/services/sms.py b/user/core/services/sms.py index 7eade1d..bac2a20 100644 --- a/user/core/services/sms.py +++ b/user/core/services/sms.py @@ -1,6 +1,8 @@ from datetime import datetime, timedelta -from django_core import exceptions, models, tasks +from django_core import exceptions, models +from core.utils.notification import send_notification +from config.env import env class SmsService: @@ -28,7 +30,8 @@ class SmsService: ) # noqa sms_confirm.save() - tasks.SendConfirm.delay(phone, code) + # tasks.SendConfirm.delay(phone, code) + send_notification({'type': 'sms', 'message': env.str("SMS_TEMPLATE") % {"code": code}, "to": [phone]}) return True @staticmethod diff --git a/user/core/utils/notification.py b/user/core/utils/notification.py new file mode 100644 index 0000000..5a84500 --- /dev/null +++ b/user/core/utils/notification.py @@ -0,0 +1,21 @@ +# rabbitmq.py + +from kombu import Exchange, Producer +from .rabbitmq import get_connection + +_channel = None +_exchange = Exchange('notification', type='direct') +_producer = None + +def get_producer(): + global _producer, _channel + if _producer is None: + conn = get_connection() + _channel = conn.channel() + _producer = Producer(_channel, exchange=_exchange, routing_key="notification") + return _producer + + +def send_notification(message: dict): + producer = get_producer() + producer.publish(message, serializer='json') diff --git a/user/core/utils/rabbitmq.py b/user/core/utils/rabbitmq.py new file mode 100644 index 0000000..890dbc5 --- /dev/null +++ b/user/core/utils/rabbitmq.py @@ -0,0 +1,15 @@ +# rabbitmq.py + +from kombu import Connection + +rabbit_url = 'amqp://guest:guest@rabbitmq:5672/' +_connection = None + + +def get_connection(): + global _connection + if _connection is None or not _connection.connected: + _connection = Connection(rabbit_url) + _connection.ensure_connection(max_retries=3) + return _connection +