From a378ac6eefabec638bc08d52b82dc2d2e354ece2 Mon Sep 17 00:00:00 2001 From: ehsan gheychisaz Date: Thu, 9 May 2024 15:34:56 +0330 Subject: [PATCH 01/17] add gmail smtp configurations --- manage_content_service/settings/base.py | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/manage_content_service/settings/base.py b/manage_content_service/settings/base.py index de3c6f23..f797b1b1 100644 --- a/manage_content_service/settings/base.py +++ b/manage_content_service/settings/base.py @@ -151,14 +151,12 @@ def get_environment_var(var_name, default, prefixed=True): MEDIA_URL = '/api/media/' MEDIA_ROOT = os.path.join(BASE_DIR, 'media') -EMAIL_HOST = 'smtp.zoho.com' -EMAIL_HOST_USER = 'info@rastaiha.ir' -EMAIL_HOST_PASSWORD = 'ET6vmrh.$gHZFjL' -EMAIL_PORT = 587 +EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend' +EMAIL_HOST = 'smtp.gmail.com' EMAIL_USE_TLS = True -EMAIL_USE_SSL = False -DEFAULT_FROM_EMAIL = "Rastaiha <" + EMAIL_HOST_USER + ">" - +EMAIL_PORT = 587 +EMAIL_HOST_USER = 'sepid.platform@gmail.com' +EMAIL_HOST_PASSWORD = 'tmyz glmk cjsj urnw' # Activate Django-Heroku. OK_STATUS = 'ok' From 3433a89984a83de8abc722a71ff4b224159a985d Mon Sep 17 00:00:00 2001 From: ehsan gheychisaz Date: Thu, 9 May 2024 15:35:20 +0330 Subject: [PATCH 02/17] add messenger app --- messenger/__init__.py | 0 messenger/admin.py | 3 +++ messenger/apps.py | 6 ++++++ messenger/migrations/__init__.py | 0 messenger/models.py | 3 +++ messenger/tests.py | 3 +++ messenger/views.py | 34 ++++++++++++++++++++++++++++++++ 7 files changed, 49 insertions(+) create mode 100644 messenger/__init__.py create mode 100644 messenger/admin.py create mode 100644 messenger/apps.py create mode 100644 messenger/migrations/__init__.py create mode 100644 messenger/models.py create mode 100644 messenger/tests.py create mode 100644 messenger/views.py diff --git a/messenger/__init__.py b/messenger/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/messenger/admin.py b/messenger/admin.py new file mode 100644 index 00000000..8c38f3f3 --- /dev/null +++ b/messenger/admin.py @@ -0,0 +1,3 @@ +from django.contrib import admin + +# Register your models here. diff --git a/messenger/apps.py b/messenger/apps.py new file mode 100644 index 00000000..9894f642 --- /dev/null +++ b/messenger/apps.py @@ -0,0 +1,6 @@ +from django.apps import AppConfig + + +class MessengerConfig(AppConfig): + default_auto_field = 'django.db.models.BigAutoField' + name = 'messenger' diff --git a/messenger/migrations/__init__.py b/messenger/migrations/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/messenger/models.py b/messenger/models.py new file mode 100644 index 00000000..71a83623 --- /dev/null +++ b/messenger/models.py @@ -0,0 +1,3 @@ +from django.db import models + +# Create your models here. diff --git a/messenger/tests.py b/messenger/tests.py new file mode 100644 index 00000000..7ce503c2 --- /dev/null +++ b/messenger/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/messenger/views.py b/messenger/views.py new file mode 100644 index 00000000..eb467518 --- /dev/null +++ b/messenger/views.py @@ -0,0 +1,34 @@ +from django.shortcuts import render + +# Create your views here. +# emailsender/views.py + +from django.core.mail import send_mail +from django.http import HttpResponse + + + +from django.core.mail import EmailMessage + + + +def send_email(request): + subject = 'Hello from Sepid' + message = 'This is a test email sent from Ehsan' + from_email = 'your@example.com' # Replace with your email address + to_email = 'amoo.hashem.mehraban@gmail.com' # Replace with recipient's email address + + send_mail(subject, message, from_email, [to_email]) + return HttpResponse("goi") + + +# def send_email(request): +# subject = 'Subject of the email' +# message = 'This is the message body.' +# from_email = 'your_email@gmail.com' # Sender's email address +# +# # List of recipient email addresses +# recipient_list = ['ehsan.ghechisaz82@gmail.com'] +# +# send_mail(subject, message, from_email, recipient_list) +# return HttpResponse('Email sent successfully!') From f80a95350b88312bcb9868737790f4bc79dfadcf Mon Sep 17 00:00:00 2001 From: ehsan gheychisaz Date: Sun, 12 May 2024 15:06:14 +0330 Subject: [PATCH 03/17] add celery and its config in project --- manage_content_service/__init__.py | 3 +++ manage_content_service/celery_app.py | 13 +++++++++++++ manage_content_service/celeryconfig.py | 5 +++++ manage_content_service/settings/base.py | 10 +++++++++- manage_content_service/settings/development.py | 2 ++ manage_content_service/settings/production.py | 2 +- 6 files changed, 33 insertions(+), 2 deletions(-) create mode 100644 manage_content_service/celery_app.py create mode 100644 manage_content_service/celeryconfig.py diff --git a/manage_content_service/__init__.py b/manage_content_service/__init__.py index e69de29b..565e6383 100644 --- a/manage_content_service/__init__.py +++ b/manage_content_service/__init__.py @@ -0,0 +1,3 @@ +from manage_content_service.celery_app import app as celery_app + +__all__ = ("celery_app",) \ No newline at end of file diff --git a/manage_content_service/celery_app.py b/manage_content_service/celery_app.py new file mode 100644 index 00000000..ff7c3db0 --- /dev/null +++ b/manage_content_service/celery_app.py @@ -0,0 +1,13 @@ +import os +from celery import Celery +import celeryconfig + +# os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'manage_content_service.settings.development') +# os.environ.setdefault("DJANGO_SETTINGS_MODULE", "manage_content_service.settings.base") +print(os.environ.get('DJANGO_SETTINGS_MODULE')) +print("hi") +app = Celery() +print('hii') +app.config_from_object(celeryconfig, namespace="CELERY") +print('hijjj') +app.autodiscover_tasks() \ No newline at end of file diff --git a/manage_content_service/celeryconfig.py b/manage_content_service/celeryconfig.py new file mode 100644 index 00000000..15159546 --- /dev/null +++ b/manage_content_service/celeryconfig.py @@ -0,0 +1,5 @@ +CELERY_TIMEZONE = "Australia/Tasmania" +CELERY_TASK_TRACK_STARTED = True +CELERY_TASK_TIME_LIMIT = 30 * 60 +CELERY_BROKER_URL= 'amqp://rabbitmq:lSlaxl020tkIN6lW1sQiRymJ5PN6tpFS@23688f0d-18eb-4a74-9524-148122178ddd.hsvc.ir:31866//' + diff --git a/manage_content_service/settings/base.py b/manage_content_service/settings/base.py index f797b1b1..ac3efe9f 100644 --- a/manage_content_service/settings/base.py +++ b/manage_content_service/settings/base.py @@ -43,6 +43,8 @@ def get_environment_var(var_name, default, prefixed=True): 'rest_framework.authtoken', 'import_export', 'drf_yasg', + 'celery', + 'messenger', 'polymorphic', 'django_extensions', 'django_filters', @@ -72,6 +74,7 @@ def get_environment_var(var_name, default, prefixed=True): 'django.contrib.messages.middleware.MessageMiddleware', 'django.middleware.clickjacking.XFrameOptionsMiddleware', ] + # multi-lingual settings below # LANGUAGES = [ # ('en', _('English')), @@ -90,8 +93,12 @@ def get_environment_var(var_name, default, prefixed=True): # multilingual settings above CORS_ORIGIN_ALLOW_ALL = True - +CELERY_TIMEZONE = "Australia/Tasmania" +CELERY_TASK_TRACK_STARTED = True +CELERY_TASK_TIME_LIMIT = 30 * 60 ROOT_URLCONF = 'manage_content_service.urls' +CELERY_BROKER_URL = 'amqp://rabbitmq:lSlaxl020tkIN6lW1sQiRymJ5PN6tpFS@23688f0d-18eb-4a74-9524-148122178ddd.hsvc.ir:31866//' + TEMPLATES = [ { @@ -109,6 +116,7 @@ def get_environment_var(var_name, default, prefixed=True): }, ] + WSGI_APPLICATION = 'manage_content_service.wsgi.application' # Password validation diff --git a/manage_content_service/settings/development.py b/manage_content_service/settings/development.py index 22c41e1a..048bfb28 100644 --- a/manage_content_service/settings/development.py +++ b/manage_content_service/settings/development.py @@ -13,6 +13,8 @@ ALLOWED_HOSTS = ['*'] + + # Database # https://docs.djangoproject.com/en/3.0/ref/settings/#databases diff --git a/manage_content_service/settings/production.py b/manage_content_service/settings/production.py index 4696bb6e..0c6510b1 100644 --- a/manage_content_service/settings/production.py +++ b/manage_content_service/settings/production.py @@ -29,7 +29,7 @@ 'PORT': DB_PORT, } } - +# CELERY_BROKER_URL = 'amqp://rabbitmq:lSlaxl020tkIN6lW1sQiRymJ5PN6tpFS@23688f0d-18eb-4a74-9524-148122178ddd.hsvc.ir:31866//' CACHES = { 'default': { From ee2348aca3e0ad3b0f9e63075bc2d2a946204f69 Mon Sep 17 00:00:00 2001 From: ehsan gheychisaz Date: Sun, 12 May 2024 23:06:47 +0330 Subject: [PATCH 04/17] update package list --- messenger/tasks.py | 13 +++++++++++++ messenger/urls.py | 0 requirements.txt | 12 ++++++++++++ 3 files changed, 25 insertions(+) create mode 100644 messenger/tasks.py create mode 100644 messenger/urls.py diff --git a/messenger/tasks.py b/messenger/tasks.py new file mode 100644 index 00000000..4220babe --- /dev/null +++ b/messenger/tasks.py @@ -0,0 +1,13 @@ +from celery import shared_task +from django.core.mail import send_mail + + +@shared_task() +def send_emailQeue(name): + subject = 'Hello from Sepid' + message = 'This is a test email sent from Ehsan' + from_email = 'your@example.com' # Replace with your email address + to_email = 'amoo.hashem.mehraban@gmail.com' # Replace with recipient's email address + + send_mail(subject, message, from_email, [to_email]) + return True \ No newline at end of file diff --git a/messenger/urls.py b/messenger/urls.py new file mode 100644 index 00000000..e69de29b diff --git a/requirements.txt b/requirements.txt index 51fe850c..2a7643c0 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,12 +1,19 @@ +amqp==5.2.0 argon2-cffi==23.1.0 argon2-cffi-bindings==21.2.0 asgiref==3.5.2 attrs==22.1.0 autopep8==2.0.0 +billiard==4.2.0 cached-property==1.5.2 +celery==5.4.0 certifi==2022.9.24 cffi==1.16.0 charset-normalizer==2.1.1 +click==8.1.7 +click-didyoumean==0.3.1 +click-plugins==1.1.1 +click-repl==0.3.0 colorclass==2.2.2 coreapi==2.3.3 coreschema==0.0.4 @@ -35,6 +42,7 @@ isodate==0.6.1 itypes==1.2.0 Jinja2==3.1.2 kavenegar==1.1.2 +kombu==5.3.7 lxml==4.9.1 MarkupPy==1.14 MarkupSafe==2.1.1 @@ -47,6 +55,7 @@ pandas==2.0.3 Pillow==9.5.0 pip-upgrader==1.4.15 platformdirs==2.5.2 +prompt-toolkit==3.0.43 psycopg2-binary==2.9.5 pycodestyle==2.9.1 pycparser==2.21 @@ -56,6 +65,7 @@ pyparsing==3.0.9 python-dateutil==2.9.0.post0 pytz==2022.6 PyYAML==6.0 +rabbitmq==0.2.0 redis==5.0.3 requests==2.28.1 requests-file==1.5.1 @@ -71,6 +81,8 @@ tomli==2.0.1 tzdata==2024.1 uritemplate==4.1.1 urllib3==1.26.12 +vine==5.1.0 +wcwidth==0.2.13 xlrd==2.0.1 xlwt==1.3.0 zeep==4.1.0 From 8b5084ed94281adbd6d600cd85a46b6c99d6207c Mon Sep 17 00:00:00 2001 From: Seyed Alireza Hashemi Date: Mon, 13 May 2024 00:13:59 +0330 Subject: [PATCH 05/17] maybe fix celery config --- manage_content_service/celery_app.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/manage_content_service/celery_app.py b/manage_content_service/celery_app.py index ff7c3db0..706d3e3c 100644 --- a/manage_content_service/celery_app.py +++ b/manage_content_service/celery_app.py @@ -1,13 +1,14 @@ import os from celery import Celery -import celeryconfig +# import celeryconfig -# os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'manage_content_service.settings.development') +os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'manage_content_service.settings.development') # os.environ.setdefault("DJANGO_SETTINGS_MODULE", "manage_content_service.settings.base") print(os.environ.get('DJANGO_SETTINGS_MODULE')) print("hi") +# print(celeryconfig) app = Celery() print('hii') -app.config_from_object(celeryconfig, namespace="CELERY") +app.config_from_object('django.conf:settings', namespace="CELERY") print('hijjj') app.autodiscover_tasks() \ No newline at end of file From 228e640fa9ff58aa5a04f53d5c8ead0fb2270394 Mon Sep 17 00:00:00 2001 From: ehsan gheychisaz Date: Mon, 13 May 2024 23:10:44 +0330 Subject: [PATCH 06/17] write task --- messenger/tasks.py | 6 +++--- messenger/urls.py | 7 +++++++ messenger/views.py | 9 ++------- 3 files changed, 12 insertions(+), 10 deletions(-) diff --git a/messenger/tasks.py b/messenger/tasks.py index 4220babe..3e44c2db 100644 --- a/messenger/tasks.py +++ b/messenger/tasks.py @@ -3,11 +3,11 @@ @shared_task() -def send_emailQeue(name): +def send_emailQeue(name, emailAddress): subject = 'Hello from Sepid' message = 'This is a test email sent from Ehsan' - from_email = 'your@example.com' # Replace with your email address - to_email = 'amoo.hashem.mehraban@gmail.com' # Replace with recipient's email address + from_email = name # Replace with your email address + to_email = emailAddress # Replace with recipient's email address send_mail(subject, message, from_email, [to_email]) return True \ No newline at end of file diff --git a/messenger/urls.py b/messenger/urls.py index e69de29b..435b2df9 100644 --- a/messenger/urls.py +++ b/messenger/urls.py @@ -0,0 +1,7 @@ +from django.urls import path +from messenger.views import * + +urlpatterns = [ + + path('test/' , send_email) +] \ No newline at end of file diff --git a/messenger/views.py b/messenger/views.py index eb467518..ab2c240e 100644 --- a/messenger/views.py +++ b/messenger/views.py @@ -5,7 +5,7 @@ from django.core.mail import send_mail from django.http import HttpResponse - +from messenger.tasks import * from django.core.mail import EmailMessage @@ -13,12 +13,7 @@ def send_email(request): - subject = 'Hello from Sepid' - message = 'This is a test email sent from Ehsan' - from_email = 'your@example.com' # Replace with your email address - to_email = 'amoo.hashem.mehraban@gmail.com' # Replace with recipient's email address - - send_mail(subject, message, from_email, [to_email]) + send_emailQeue.delay("ehsna" , "ehsan.ghechisaz82@gmail.com") return HttpResponse("goi") From cfd3b38c79e66a69ad14ef11ade11b32e1fb7c12 Mon Sep 17 00:00:00 2001 From: ehsan gheychisaz Date: Mon, 13 May 2024 23:10:57 +0330 Subject: [PATCH 07/17] test config --- manage_content_service/celery_app.py | 7 ------- manage_content_service/celeryconfig.py | 5 ----- manage_content_service/urls.py | 1 + 3 files changed, 1 insertion(+), 12 deletions(-) delete mode 100644 manage_content_service/celeryconfig.py diff --git a/manage_content_service/celery_app.py b/manage_content_service/celery_app.py index 706d3e3c..415bdb32 100644 --- a/manage_content_service/celery_app.py +++ b/manage_content_service/celery_app.py @@ -1,14 +1,7 @@ import os from celery import Celery -# import celeryconfig os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'manage_content_service.settings.development') -# os.environ.setdefault("DJANGO_SETTINGS_MODULE", "manage_content_service.settings.base") -print(os.environ.get('DJANGO_SETTINGS_MODULE')) -print("hi") -# print(celeryconfig) app = Celery() -print('hii') app.config_from_object('django.conf:settings', namespace="CELERY") -print('hijjj') app.autodiscover_tasks() \ No newline at end of file diff --git a/manage_content_service/celeryconfig.py b/manage_content_service/celeryconfig.py deleted file mode 100644 index 15159546..00000000 --- a/manage_content_service/celeryconfig.py +++ /dev/null @@ -1,5 +0,0 @@ -CELERY_TIMEZONE = "Australia/Tasmania" -CELERY_TASK_TRACK_STARTED = True -CELERY_TASK_TIME_LIMIT = 30 * 60 -CELERY_BROKER_URL= 'amqp://rabbitmq:lSlaxl020tkIN6lW1sQiRymJ5PN6tpFS@23688f0d-18eb-4a74-9524-148122178ddd.hsvc.ir:31866//' - diff --git a/manage_content_service/urls.py b/manage_content_service/urls.py index 45dfd346..bab523f8 100644 --- a/manage_content_service/urls.py +++ b/manage_content_service/urls.py @@ -39,6 +39,7 @@ path('api/report/', include('apps.report.urls')), path('api/scoring/', include('apps.scoring.urls')), path('test/hi/', say_hello), + path('email/' , include('messenger.urls')), # https://pypi.org/project/django-link-shortener/ path('s/', include('shortener.urls')), ] From 130fd804b737de5ce9ea616331f789c1eea70c49 Mon Sep 17 00:00:00 2001 From: ehsan gheychisaz Date: Mon, 13 May 2024 23:18:52 +0330 Subject: [PATCH 08/17] set .env --- manage_content_service/settings/base.py | 5 ++--- manage_content_service/settings/production.py | 1 - 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/manage_content_service/settings/base.py b/manage_content_service/settings/base.py index ac3efe9f..e9a4453b 100644 --- a/manage_content_service/settings/base.py +++ b/manage_content_service/settings/base.py @@ -91,14 +91,13 @@ def get_environment_var(var_name, default, prefixed=True): # os.path.join(BASE_DIR, 'locale'), # ] # multilingual settings above - +BROKER_URL = get_environment_var('BROKER_URL', 'amqp://') CORS_ORIGIN_ALLOW_ALL = True CELERY_TIMEZONE = "Australia/Tasmania" CELERY_TASK_TRACK_STARTED = True CELERY_TASK_TIME_LIMIT = 30 * 60 ROOT_URLCONF = 'manage_content_service.urls' -CELERY_BROKER_URL = 'amqp://rabbitmq:lSlaxl020tkIN6lW1sQiRymJ5PN6tpFS@23688f0d-18eb-4a74-9524-148122178ddd.hsvc.ir:31866//' - +CELERY_BROKER_URL = BROKER_URL TEMPLATES = [ { diff --git a/manage_content_service/settings/production.py b/manage_content_service/settings/production.py index 0c6510b1..7a7e11c0 100644 --- a/manage_content_service/settings/production.py +++ b/manage_content_service/settings/production.py @@ -29,7 +29,6 @@ 'PORT': DB_PORT, } } -# CELERY_BROKER_URL = 'amqp://rabbitmq:lSlaxl020tkIN6lW1sQiRymJ5PN6tpFS@23688f0d-18eb-4a74-9524-148122178ddd.hsvc.ir:31866//' CACHES = { 'default': { From 23fcd0fcdb8d898597b9dbdd6edf919f4d10d7f9 Mon Sep 17 00:00:00 2001 From: ehsan gheychisaz Date: Tue, 14 May 2024 22:42:52 +0330 Subject: [PATCH 09/17] test config --- assets/greeting_mail.html | 390 ++++++++++++++++++++++++ manage_content_service/settings/base.py | 3 +- 2 files changed, 392 insertions(+), 1 deletion(-) create mode 100644 assets/greeting_mail.html diff --git a/assets/greeting_mail.html b/assets/greeting_mail.html new file mode 100644 index 00000000..15f88f95 --- /dev/null +++ b/assets/greeting_mail.html @@ -0,0 +1,390 @@ + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/manage_content_service/settings/base.py b/manage_content_service/settings/base.py index e9a4453b..e01a102a 100644 --- a/manage_content_service/settings/base.py +++ b/manage_content_service/settings/base.py @@ -97,7 +97,8 @@ def get_environment_var(var_name, default, prefixed=True): CELERY_TASK_TRACK_STARTED = True CELERY_TASK_TIME_LIMIT = 30 * 60 ROOT_URLCONF = 'manage_content_service.urls' -CELERY_BROKER_URL = BROKER_URL +# CELERY_BROKER_URL = BROKER_URL +CELERY_BROKER_URL = 'amqp://rabbitmq:lSlaxl020tkIN6lW1sQiRymJ5PN6tpFS@23688f0d-18eb-4a74-9524-148122178ddd.hsvc.ir:31866//' TEMPLATES = [ { From 2b46a0ae9074f3e0df88ccd4dfde051f3e5268b6 Mon Sep 17 00:00:00 2001 From: ehsan gheychisaz Date: Tue, 14 May 2024 22:43:12 +0330 Subject: [PATCH 10/17] add config for prod --- manage_content_service/settings/base.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/manage_content_service/settings/base.py b/manage_content_service/settings/base.py index e01a102a..9f2039cf 100644 --- a/manage_content_service/settings/base.py +++ b/manage_content_service/settings/base.py @@ -97,8 +97,8 @@ def get_environment_var(var_name, default, prefixed=True): CELERY_TASK_TRACK_STARTED = True CELERY_TASK_TIME_LIMIT = 30 * 60 ROOT_URLCONF = 'manage_content_service.urls' -# CELERY_BROKER_URL = BROKER_URL -CELERY_BROKER_URL = 'amqp://rabbitmq:lSlaxl020tkIN6lW1sQiRymJ5PN6tpFS@23688f0d-18eb-4a74-9524-148122178ddd.hsvc.ir:31866//' +CELERY_BROKER_URL = BROKER_URL +# CELERY_BROKER_URL = 'amqp://rabbitmq:lSlaxl020tkIN6lW1sQiRymJ5PN6tpFS@23688f0d-18eb-4a74-9524-148122178ddd.hsvc.ir:31866//' TEMPLATES = [ { From 94a477786383b03505d2efbf6206885ee3c9cde3 Mon Sep 17 00:00:00 2001 From: ehsan gheychisaz Date: Tue, 14 May 2024 22:43:29 +0330 Subject: [PATCH 11/17] fix email sender with html content --- messenger/tasks.py | 13 ++++++++++--- messenger/views.py | 7 +++++-- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/messenger/tasks.py b/messenger/tasks.py index 3e44c2db..dcb7996e 100644 --- a/messenger/tasks.py +++ b/messenger/tasks.py @@ -1,13 +1,20 @@ from celery import shared_task from django.core.mail import send_mail +from django.core.mail import EmailMessage @shared_task() -def send_emailQeue(name, emailAddress): +def send_emailQeue(name, emailAddress , html_file): subject = 'Hello from Sepid' message = 'This is a test email sent from Ehsan' from_email = name # Replace with your email address to_email = emailAddress # Replace with recipient's email address - send_mail(subject, message, from_email, [to_email]) - return True \ No newline at end of file + mail = EmailMessage(subject=subject,body=html_file, from_email=from_email, to=[to_email]) + mail.content_subtype = 'html' + try: + + mail.send() + return True + except: + return False \ No newline at end of file diff --git a/messenger/views.py b/messenger/views.py index ab2c240e..a38f23ea 100644 --- a/messenger/views.py +++ b/messenger/views.py @@ -6,14 +6,17 @@ from django.core.mail import send_mail from django.http import HttpResponse from messenger.tasks import * - +import os +from manage_content_service.settings.base import BASE_DIR from django.core.mail import EmailMessage def send_email(request): - send_emailQeue.delay("ehsna" , "ehsan.ghechisaz82@gmail.com") + html_content =os.path.join(BASE_DIR, "assets/greeting_mail.html") + html_content= open(html_content, "r", encoding="utf-8").read() + send_emailQeue.delay("ehsna" , "ehsan.ghechisaz82@gmail.com" , html_content) return HttpResponse("goi") From 6304998ffbb5738fe7abac8833904bd3f53ca4d7 Mon Sep 17 00:00:00 2001 From: Seyed Alireza Hashemi Date: Fri, 17 May 2024 16:17:48 +0330 Subject: [PATCH 12/17] refactor SMSService --- apps/accounts/models.py | 7 +++-- apps/accounts/serializers.py | 2 +- apps/fsm/views/registration_receipt_view.py | 9 +++--- errors/error_codes.py | 2 +- proxies/sms_system/kavenegar.py | 15 +++++---- proxies/sms_system/main.py | 35 --------------------- proxies/sms_system/settings.py | 2 ++ proxies/sms_system/sms_service_facade.py | 21 +++++++++++++ proxies/sms_system/sms_service_interface.py | 31 ++++++++++++++++++ 9 files changed, 73 insertions(+), 51 deletions(-) delete mode 100644 proxies/sms_system/main.py create mode 100644 proxies/sms_system/settings.py create mode 100644 proxies/sms_system/sms_service_facade.py create mode 100644 proxies/sms_system/sms_service_interface.py diff --git a/apps/accounts/models.py b/apps/accounts/models.py index 1c29cd6d..628e503b 100644 --- a/apps/accounts/models.py +++ b/apps/accounts/models.py @@ -10,7 +10,8 @@ from apps.accounts.validators import percentage_validator from manage_content_service.settings.base import VOUCHER_CODE_LENGTH, DISCOUNT_CODE_LENGTH, PURCHASE_UNIQ_CODE_LENGTH -from proxies.sms_system.main import SMS_CODE_DELAY, SMS_CODE_LENGTH, get_sms_service_proxy +from proxies.sms_system.settings import SMS_CODE_DELAY, SMS_CODE_LENGTH +from proxies.sms_system.sms_service_facade import SMSServiceFacade class User(AbstractUser): @@ -356,8 +357,8 @@ class VerificationType(models.TextChoices): objects = VerificationCodeManager() def notify(self, verification_type, party_display_name='سپید'): - sms_service_proxy = get_sms_service_proxy(type='kavenegar', token='todo') - sms_service_proxy.send_otp(self.phone_number, verification_type, token=party_display_name, token2=str(self.code)) + sms_service_facade = SMSServiceFacade(provider='kavenegar') + sms_service_facade.send_otp(self.phone_number, verification_type, token=party_display_name, token2=str(self.code)) def __str__(self): return f'{self.phone_number}\'s code is: {self.code} {"+" if self.is_valid else "-"}' diff --git a/apps/accounts/serializers.py b/apps/accounts/serializers.py index 34fe2b2b..a7485169 100644 --- a/apps/accounts/serializers.py +++ b/apps/accounts/serializers.py @@ -8,7 +8,7 @@ from errors.error_codes import serialize_error from manage_content_service.settings.base import DISCOUNT_CODE_LENGTH -from proxies.sms_system.main import SMS_CODE_LENGTH +from proxies.sms_system.settings import SMS_CODE_LENGTH from .models import User, VerificationCode, EducationalInstitute, School, University, SchoolStudentship, Studentship, \ AcademicStudentship, Merchandise, DiscountCode, Purchase from .validators import phone_number_validator, grade_validator, price_validator diff --git a/apps/fsm/views/registration_receipt_view.py b/apps/fsm/views/registration_receipt_view.py index a709039a..e76edf2c 100644 --- a/apps/fsm/views/registration_receipt_view.py +++ b/apps/fsm/views/registration_receipt_view.py @@ -13,7 +13,7 @@ from apps.fsm.permissions import IsRegistrationReceiptOwner, IsReceiptsFormModifier from apps.fsm.serializers.answer_sheet_serializers import RegistrationReceiptSerializer, RegistrationStatusSerializer from apps.fsm.serializers.certificate_serializer import create_certificate -from proxies.sms_system.main import get_sms_service_proxy +from proxies.sms_system.sms_service_facade import SMSServiceFacade class RegistrationReceiptViewSet(GenericViewSet, RetrieveModelMixin, DestroyModelMixin): @@ -82,11 +82,10 @@ def validate(self, request, pk=None): # todo: fix academy name if older_status != receipt.status: - sms_service_proxy = get_sms_service_proxy( - type='kavenegar', token='todo') - sms_service_proxy.send_otp( + sms_service_facade = SMSServiceFacade(provider='kavenegar') + sms_service_facade.send_otp( receptor_phone_number=receipt.user.phone_number, - type=sms_service_proxy.RegularSMSTypes.UpdateRegistrationReceiptState, + type=sms_service_facade.RegularSMSTypes.UpdateRegistrationReceiptState, # todo: get real academy name from mps token='کاموا', token2=receipt.answer_sheet_of.event_or_fsm.name diff --git a/errors/error_codes.py b/errors/error_codes.py index f75275b0..ce08eaa2 100644 --- a/errors/error_codes.py +++ b/errors/error_codes.py @@ -1,7 +1,7 @@ import logging from rest_framework.exceptions import ValidationError -from proxies.sms_system.main import SMS_CODE_LENGTH +from proxies.sms_system.settings import SMS_CODE_LENGTH logger = logging.getLogger(__name__) diff --git a/proxies/sms_system/kavenegar.py b/proxies/sms_system/kavenegar.py index e86e7b1e..395ea170 100644 --- a/proxies/sms_system/kavenegar.py +++ b/proxies/sms_system/kavenegar.py @@ -2,18 +2,15 @@ from kavenegar import * -from proxies.sms_system.main import SMSServiceProxy +from proxies.sms_system.sms_service_interface import SMSService -class KaveNegarSMSServiceProxy(SMSServiceProxy): +class KaveNegarSMSService(SMSService): - def __init__(self, token) -> None: + def __init__(self, token: str) -> None: super().__init__() self.api = KavenegarAPI(token) - def send_sms(self): - pass - def send_otp(self, receptor_phone_number, type, token, token2=None, token3=None): params = { 'receptor': receptor_phone_number, @@ -26,3 +23,9 @@ def send_otp(self, receptor_phone_number, type, token, token2=None, token3=None) if token3: params['token3'] = token3 self.api.verify_lookup(params) + + def send_bulk(self): + return super().send_bulk() + + def send_sms(self): + return super().send_sms() diff --git a/proxies/sms_system/main.py b/proxies/sms_system/main.py deleted file mode 100644 index f4102e83..00000000 --- a/proxies/sms_system/main.py +++ /dev/null @@ -1,35 +0,0 @@ -SMS_CODE_DELAY = 5 - -SMS_CODE_LENGTH = 5 - - -def get_sms_service_proxy(type, token): - if type == 'kavenegar': - from proxies.sms_system.kavenegar import KaveNegarSMSServiceProxy - # todo: get token from backend - from django.conf import settings - return KaveNegarSMSServiceProxy(token=settings.KAVENEGAR_TOKEN) - - -class SMSServiceProxy: - class OtpTypes: - CreateUserAccount = 'create-user-account' - ChangeUserPassword = 'change-user-password' - ChangeUserPhoneNumber = 'change-user-phone-number' - - class RegularSMSTypes: - UpdateRegistrationReceiptState = 'update-registration-receipt-state' - - def send_otp(self, receptor_phone_number, action, token, token2=None, token3=None): - pass - - def send_sms(): - pass - - def send_bulk(): - pass - - -class VoiceCallServiceProxy: - def make_voice_call(): - pass diff --git a/proxies/sms_system/settings.py b/proxies/sms_system/settings.py new file mode 100644 index 00000000..f0f855a0 --- /dev/null +++ b/proxies/sms_system/settings.py @@ -0,0 +1,2 @@ +SMS_CODE_DELAY = 5 +SMS_CODE_LENGTH = 5 diff --git a/proxies/sms_system/sms_service_facade.py b/proxies/sms_system/sms_service_facade.py new file mode 100644 index 00000000..255dbc8a --- /dev/null +++ b/proxies/sms_system/sms_service_facade.py @@ -0,0 +1,21 @@ +from proxies.sms_system.sms_service_interface import SMSService + + +class SMSServiceFacade(SMSService): + provider: SMSService = None + + def __init__(self, provider: str) -> None: + if provider == 'kavenegar': + from proxies.sms_system.kavenegar import KaveNegarSMSService + from django.conf import settings + self.provider = KaveNegarSMSService(token=settings.KAVENEGAR_TOKEN) + + def send_otp(self, receptor_phone_number, action, token, token2=None, token3=None): + self.provider.send_otp(receptor_phone_number, + action, token, token2, token3) + + def send_sms(self): + self.provider.send_sms() + + def send_bulk(self): + self.provider.send_bulk() diff --git a/proxies/sms_system/sms_service_interface.py b/proxies/sms_system/sms_service_interface.py new file mode 100644 index 00000000..95fbeba6 --- /dev/null +++ b/proxies/sms_system/sms_service_interface.py @@ -0,0 +1,31 @@ +from abc import ABC, abstractmethod + + +class SMSService(ABC): + + class OtpTypes: + CreateUserAccount = 'create-user-account' + ChangeUserPassword = 'change-user-password' + ChangeUserPhoneNumber = 'change-user-phone-number' + + class RegularSMSTypes: + UpdateRegistrationReceiptState = 'update-registration-receipt-state' + + @abstractmethod + def send_otp(self, receptor_phone_number, action, token, token2=None, token3=None): + pass + + @abstractmethod + def send_sms(self): + pass + + @abstractmethod + def send_bulk(self): + pass + + +class VoiceCallService(ABC): + + @abstractmethod + def make_voice_call(self): + pass From 55f48e5645b64dffb09006a6ceb4cf318f8dbf33 Mon Sep 17 00:00:00 2001 From: Seyed Alireza Hashemi Date: Fri, 17 May 2024 16:19:06 +0330 Subject: [PATCH 13/17] refactor base settings --- manage_content_service/settings/base.py | 12 ------------ messenger/views.py | 17 ++++------------- 2 files changed, 4 insertions(+), 25 deletions(-) diff --git a/manage_content_service/settings/base.py b/manage_content_service/settings/base.py index 9f2039cf..f7ea036d 100644 --- a/manage_content_service/settings/base.py +++ b/manage_content_service/settings/base.py @@ -165,23 +165,11 @@ def get_environment_var(var_name, default, prefixed=True): EMAIL_PORT = 587 EMAIL_HOST_USER = 'sepid.platform@gmail.com' EMAIL_HOST_PASSWORD = 'tmyz glmk cjsj urnw' -# Activate Django-Heroku. - -OK_STATUS = 'ok' -ERROR_STATUS = 'err' -HELP_STATUS = 'help' - -THUMBNAIL_ALIASES = { - '': { - 'avatar': {'size': (80, 80), 'crop': True}, - }, -} STATICFILES_STORAGE = 'django.contrib.staticfiles.storage.ManifestStaticFilesStorage' CONSTANTS = { "PAGINATION_NUMBER": 50, - } # Custom user model diff --git a/messenger/views.py b/messenger/views.py index a38f23ea..a1cc2330 100644 --- a/messenger/views.py +++ b/messenger/views.py @@ -1,23 +1,14 @@ -from django.shortcuts import render - -# Create your views here. -# emailsender/views.py - -from django.core.mail import send_mail from django.http import HttpResponse from messenger.tasks import * import os from manage_content_service.settings.base import BASE_DIR -from django.core.mail import EmailMessage - - def send_email(request): - html_content =os.path.join(BASE_DIR, "assets/greeting_mail.html") - html_content= open(html_content, "r", encoding="utf-8").read() - send_emailQeue.delay("ehsna" , "ehsan.ghechisaz82@gmail.com" , html_content) - return HttpResponse("goi") + html_content = os.path.join(BASE_DIR, "assets/greeting_mail.html") + html_content = open(html_content, "r", encoding="utf-8").read() + send_emailQeue.delay("ehsna", "ehsan.ghechisaz82@gmail.com", html_content) + return HttpResponse("goi") # def send_email(request): From 4c438697361742b3472359e90524f401cb7cd0b1 Mon Sep 17 00:00:00 2001 From: Seyed Alireza Hashemi Date: Fri, 17 May 2024 16:44:51 +0330 Subject: [PATCH 14/17] refactor --- manage_content_service/settings/base.py | 36 ++++++++----------------- messenger/tasks.py | 15 ++++------- messenger/urls.py | 5 ++-- messenger/views.py | 11 ++++---- 4 files changed, 24 insertions(+), 43 deletions(-) diff --git a/manage_content_service/settings/base.py b/manage_content_service/settings/base.py index f7ea036d..9cc69c31 100644 --- a/manage_content_service/settings/base.py +++ b/manage_content_service/settings/base.py @@ -19,6 +19,8 @@ def get_environment_var(var_name, default, prefixed=True): return os.getenv(var_name, default) +CORS_ORIGIN_ALLOW_ALL = True + # Build paths inside the project like this: os.path.join(BASE_DIR, ...) BASE_DIR = os.path.dirname(os.path.dirname( os.path.dirname(os.path.abspath(__file__)))) @@ -75,31 +77,6 @@ def get_environment_var(var_name, default, prefixed=True): 'django.middleware.clickjacking.XFrameOptionsMiddleware', ] -# multi-lingual settings below -# LANGUAGES = [ -# ('en', _('English')), -# ('fa', _('Persian')), -# ] - -# USE_I18N = True -# -# USE_L10N = True -# -# LANGUAGE_CODE = 'en' -# -# LOCALE_PATHS = [ -# os.path.join(BASE_DIR, 'locale'), -# ] -# multilingual settings above -BROKER_URL = get_environment_var('BROKER_URL', 'amqp://') -CORS_ORIGIN_ALLOW_ALL = True -CELERY_TIMEZONE = "Australia/Tasmania" -CELERY_TASK_TRACK_STARTED = True -CELERY_TASK_TIME_LIMIT = 30 * 60 -ROOT_URLCONF = 'manage_content_service.urls' -CELERY_BROKER_URL = BROKER_URL -# CELERY_BROKER_URL = 'amqp://rabbitmq:lSlaxl020tkIN6lW1sQiRymJ5PN6tpFS@23688f0d-18eb-4a74-9524-148122178ddd.hsvc.ir:31866//' - TEMPLATES = [ { 'BACKEND': 'django.template.backends.django.DjangoTemplates', @@ -219,3 +196,12 @@ def get_environment_var(var_name, default, prefixed=True): DISCOUNT_CODE_LENGTH = 10 PURCHASE_UNIQ_CODE_LENGTH = 10 + + +########## Celery Settings ########## + +CELERY_TIMEZONE = TIME_ZONE +CELERY_TASK_TRACK_STARTED = True +CELERY_TASK_TIME_LIMIT = 30 * 60 +ROOT_URLCONF = 'manage_content_service.urls' +CELERY_BROKER_URL = get_environment_var('BROKER_URL', 'amqp://') diff --git a/messenger/tasks.py b/messenger/tasks.py index dcb7996e..e1f19d56 100644 --- a/messenger/tasks.py +++ b/messenger/tasks.py @@ -1,20 +1,15 @@ from celery import shared_task -from django.core.mail import send_mail from django.core.mail import EmailMessage @shared_task() -def send_emailQeue(name, emailAddress , html_file): - subject = 'Hello from Sepid' - message = 'This is a test email sent from Ehsan' - from_email = name # Replace with your email address - to_email = emailAddress # Replace with recipient's email address - - mail = EmailMessage(subject=subject,body=html_file, from_email=from_email, to=[to_email]) +def send_email_task(subject, recipients_emails, body): + sender_email = 'sepid.org@gmail.com' + mail = EmailMessage(subject=subject, body=body, + from_email=sender_email, to=recipients_emails) mail.content_subtype = 'html' try: - mail.send() return True except: - return False \ No newline at end of file + return False diff --git a/messenger/urls.py b/messenger/urls.py index 435b2df9..3e45fc09 100644 --- a/messenger/urls.py +++ b/messenger/urls.py @@ -2,6 +2,5 @@ from messenger.views import * urlpatterns = [ - - path('test/' , send_email) -] \ No newline at end of file + path('test/', send_email) +] diff --git a/messenger/views.py b/messenger/views.py index a1cc2330..16358a76 100644 --- a/messenger/views.py +++ b/messenger/views.py @@ -1,13 +1,14 @@ -from django.http import HttpResponse -from messenger.tasks import * import os +from django.http import HttpResponse +from messenger.tasks import send_email_task from manage_content_service.settings.base import BASE_DIR def send_email(request): - html_content = os.path.join(BASE_DIR, "assets/greeting_mail.html") - html_content = open(html_content, "r", encoding="utf-8").read() - send_emailQeue.delay("ehsna", "ehsan.ghechisaz82@gmail.com", html_content) + html_content_path = os.path.join(BASE_DIR, "assets/greeting_mail.html") + html_content = open(html_content_path, "r", encoding="utf-8").read() + send_email_task.delay( + "Email Subject", ["ehsan.ghechisaz82@gmail.com"], html_content) return HttpResponse("goi") From 853d7962fc61c31361e28e6e8d652c6a8e555c57 Mon Sep 17 00:00:00 2001 From: Seyed Alireza Hashemi Date: Fri, 17 May 2024 16:52:26 +0330 Subject: [PATCH 15/17] fix email address --- messenger/tasks.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/messenger/tasks.py b/messenger/tasks.py index e1f19d56..41849a46 100644 --- a/messenger/tasks.py +++ b/messenger/tasks.py @@ -4,7 +4,7 @@ @shared_task() def send_email_task(subject, recipients_emails, body): - sender_email = 'sepid.org@gmail.com' + sender_email = 'sepid.platform@gmail.com' mail = EmailMessage(subject=subject, body=body, from_email=sender_email, to=recipients_emails) mail.content_subtype = 'html' From 2c251daf790a3c7e9f134c8c4acf45e043980b00 Mon Sep 17 00:00:00 2001 From: ehsan gheychisaz Date: Fri, 24 May 2024 17:30:58 +0330 Subject: [PATCH 16/17] fix email sender for fast-api --- messenger/views.py | 32 +++++++++++++++++++++++++++----- 1 file changed, 27 insertions(+), 5 deletions(-) diff --git a/messenger/views.py b/messenger/views.py index a38f23ea..5b7649c8 100644 --- a/messenger/views.py +++ b/messenger/views.py @@ -11,13 +11,35 @@ from django.core.mail import EmailMessage +import requests +from django.http import JsonResponse +from django.views.decorators.csrf import csrf_exempt +from django.shortcuts import redirect - +@csrf_exempt def send_email(request): - html_content =os.path.join(BASE_DIR, "assets/greeting_mail.html") - html_content= open(html_content, "r", encoding="utf-8").read() - send_emailQeue.delay("ehsna" , "ehsan.ghechisaz82@gmail.com" , html_content) - return HttpResponse("goi") + if request.method == 'POST': + email_data = { + "email": request.POST.get("email"), + "subject": request.POST.get("subject"), + "body": request.POST.get("body") + } + response = requests.post("http://127.0.0.1:8080/send-email/", json=email_data) + + if response.status_code == 200: + return JsonResponse({"message": "Email has been sent"}, status=200) + else: + return JsonResponse({"error": "Failed to send email"}, status=500) + + return JsonResponse({"error": "Invalid request method"}, status=405) + + + +# def send_email(request): +# html_content =os.path.join(BASE_DIR, "assets/greeting_mail.html") +# html_content= open(html_content, "r", encoding="utf-8").read() +# send_emailQeue.delay("ehsna" , "ehsan.ghechisaz82@gmail.com" , html_content) +# return HttpResponse("goi") # def send_email(request): From 308f336005fe77f6c9f88d0918df2c7d9bb8d983 Mon Sep 17 00:00:00 2001 From: Seyed Alireza Hashemi Date: Fri, 24 May 2024 18:11:20 +0330 Subject: [PATCH 17/17] add some todos for Ehsan --- messenger/views.py | 2 ++ proxies/email_service/send_email.py | 7 +++++++ 2 files changed, 9 insertions(+) create mode 100644 proxies/email_service/send_email.py diff --git a/messenger/views.py b/messenger/views.py index 7bfac538..9410cc10 100644 --- a/messenger/views.py +++ b/messenger/views.py @@ -13,9 +13,11 @@ def send_email(request): if request.method == 'POST': email_data = { + # todo: convert it to recipients: "email": request.POST.get("email"), "subject": request.POST.get("subject"), "body": request.POST.get("body") + # todo: add a field for choosing template } response = requests.post("http://127.0.0.1:8080/send-email/", json=email_data) diff --git a/proxies/email_service/send_email.py b/proxies/email_service/send_email.py new file mode 100644 index 00000000..f7549a65 --- /dev/null +++ b/proxies/email_service/send_email.py @@ -0,0 +1,7 @@ +# email api: https://smtp.sepid.org/send-email/ + +# todo: remove messenger app + +def send_email(): + # todo + pass