Skip to content

Commit

Permalink
add celery to project, and setup shared task to send email
Browse files Browse the repository at this point in the history
  • Loading branch information
aahnik committed Dec 29, 2023
1 parent f606015 commit 9aa80e3
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/temple_web/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from .celery import app as celery_app

__all__ = "celery_app"
7 changes: 7 additions & 0 deletions src/temple_web/celery.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import os
from celery import Celery

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "django_celery.settings")
app = Celery("django_celery")
app.config_from_object("django.conf:settings", namespace="CELERY")
app.autodiscover_tasks()
9 changes: 9 additions & 0 deletions src/temple_web/myconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,15 @@ class MyDjangoSettings:
PROD_DOMAIN = config("PROD_DOMAIN", cast=str)
SECRET_KEY = config("SECRET_KEY", cast=str)
MORE_ALLOWED_HOSTS = config("MORE_ALLOWED_HOSTS", cast=Csv(), default="127.0.0.1")
CELERY_BROKER_URL = config(
"CELERY_BROKER_URL", default="redis://localhost:6379", cast=str
)
CELERY_RESULT_BACKEND = config(
"CELERY_RESULT_BACKEND", default="redis://localhost:6379", cast=str
)
DEFAULT_FROM_EMAIL = config("DEFAULT_FROM_EMAIL", cast=str)
EMAIL_HOST_USER = config("EMAIL_HOST_USER", cast=str)
EMAIL_HOST_PASSWORD = config("EMAIL_HOST_PASSWORD", cast=str)


# print(metadata())
Expand Down
13 changes: 13 additions & 0 deletions src/temple_web/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,14 @@
"tester.apps.TesterConfig",
"users.apps.UsersConfig",
"haps",
"blog",
# more third party apps
# recommended to be placed at last
"django_cleanup.apps.CleanupConfig",
"crispy_forms",
"crispy_tailwind",
"ckeditor",
"ckeditor_uploader",
]

MIDDLEWARE = [
Expand Down Expand Up @@ -145,6 +148,7 @@
},
]

DATA_UPLOAD_MAX_MEMORY_SIZE = 5242880

# Internationalization
# https://docs.djangoproject.com/en/4.2/topics/i18n/
Expand Down Expand Up @@ -205,3 +209,12 @@
CRISPY_ALLOWED_TEMPLATE_PACKS = "tailwind"

CRISPY_TEMPLATE_PACK = "tailwind"
CKEDITOR_UPLOAD_PATH = "ckeditor-uploads/"
CKEDITOR_FILENAME_GENERATOR = "utils.ckeditor.get_filename"

CELERY_BROKER_URL = MyDjS.CELERY_BROKER_URL
CELERY_RESULT_BACKEND = MyDjS.CELERY_RESULT_BACKEND

DEFAULT_FROM_EMAIL = MyDjS.DEFAULT_FROM_EMAIL
EMAIL_HOST_USER = MyDjS.EMAIL_HOST_USER
EMAIL_HOST_PASSWORD = MyDjS.EMAIL_HOST_PASSWORD
16 changes: 16 additions & 0 deletions src/temple_web/tasks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from django.core.mail import send_mail
from celery import shared_task


@shared_task()
def send_email_in_bg(
subject: str, message: str, recipient_list: list[str], html_message: str
):
"""A wrapper around Django's send_mail to send emails using celery"""
send_mail(
subject=subject,
message=message,
recipient_list=recipient_list,
html_message=html_message,
fail_silently=False,
)
2 changes: 2 additions & 0 deletions src/temple_web/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,10 @@
path("tester/", include("tester.urls")),
path("users/", include("users.urls")),
path("events/", include("haps.urls")),
path("blog/", include("blog.urls")),
# path("404/", views.page_not_found_view, name="404"),
path("admin/", admin.site.urls),
path('ckeditor/', include('ckeditor_uploader.urls')),
]
+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
+ static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
Expand Down

0 comments on commit 9aa80e3

Please sign in to comment.