Using celery workers to handle email sending
This commit is contained in:
parent
054c430fe9
commit
8ab751e2ae
3
.gitignore
vendored
3
.gitignore
vendored
@ -146,3 +146,6 @@ media/registration_form/*
|
||||
!media/result_sheet/
|
||||
media/result_sheet/*
|
||||
!media/result_sheet/README.txt
|
||||
|
||||
# Custom
|
||||
dump.rdb
|
||||
13
README.md
13
README.md
@ -50,6 +50,7 @@ If you would like to contribute, simply begin by implementing one from the list
|
||||
|
||||
- [Python3.8+](https://www.python.org/downloads/)
|
||||
- [PostgreSQL database](https://www.postgresql.org/download/)
|
||||
- [Redis](https://redis.io/docs/latest/operate/oss_and_stack/install/install-redis/)
|
||||
|
||||
# Installation
|
||||
|
||||
@ -97,6 +98,18 @@ python manage.py createsuperuser
|
||||
python manage.py runserver
|
||||
```
|
||||
|
||||
Make sure your Redis server is running
|
||||
|
||||
```bash
|
||||
redis-server
|
||||
```
|
||||
|
||||
Start the celery worker
|
||||
|
||||
```bash
|
||||
celery -A config.celery worker -l INFO
|
||||
```
|
||||
|
||||
Last but not least, go to this address http://127.0.0.1:8000
|
||||
|
||||
### References
|
||||
|
||||
@ -1,7 +1,6 @@
|
||||
from datetime import datetime
|
||||
|
||||
from django.conf import settings
|
||||
from django.core.mail import send_mail
|
||||
from django import forms
|
||||
from django.db import transaction
|
||||
from django.contrib.auth.forms import (
|
||||
@ -11,6 +10,7 @@ from django.contrib.auth.forms import (
|
||||
from django.contrib.auth.forms import PasswordResetForm
|
||||
from course.models import Program
|
||||
from .models import User, Student, Parent, RELATION_SHIP, LEVEL, GENDERS
|
||||
from .tasks import send_new_student_email, send_new_lecturer_email
|
||||
|
||||
|
||||
class StaffAddForm(UserCreationForm):
|
||||
@ -134,13 +134,7 @@ class StaffAddForm(UserCreationForm):
|
||||
user.save()
|
||||
|
||||
# Send email with the generated credentials
|
||||
send_mail(
|
||||
"Your Django LMS account credentials",
|
||||
f"Your username: {generated_username}\nYour password: {generated_password}",
|
||||
"from@example.com",
|
||||
[user.email],
|
||||
fail_silently=False,
|
||||
)
|
||||
send_new_lecturer_email.delay(user.pk, generated_password)
|
||||
|
||||
return user
|
||||
|
||||
@ -299,13 +293,7 @@ class StudentAddForm(UserCreationForm):
|
||||
)
|
||||
|
||||
# Send email with the generated credentials
|
||||
send_mail(
|
||||
"Your Django LMS account credentials",
|
||||
f"Your ID: {generated_username}\nYour password: {generated_password}",
|
||||
settings.EMAIL_FROM_ADDRESS,
|
||||
[user.email],
|
||||
fail_silently=False,
|
||||
)
|
||||
send_new_student_email.delay(user.pk, generated_password)
|
||||
|
||||
return user
|
||||
|
||||
|
||||
31
accounts/tasks.py
Normal file
31
accounts/tasks.py
Normal file
@ -0,0 +1,31 @@
|
||||
import time
|
||||
from celery import shared_task
|
||||
from django.conf import settings
|
||||
from django.contrib.auth import get_user_model
|
||||
from django.core.mail import send_mail
|
||||
|
||||
|
||||
def send_email(user, subject, msg):
|
||||
send_mail(
|
||||
subject,
|
||||
msg,
|
||||
settings.EMAIL_FROM_ADDRESS,
|
||||
[user.email],
|
||||
fail_silently=False,
|
||||
)
|
||||
|
||||
|
||||
@shared_task
|
||||
def send_new_student_email(user_pk, password):
|
||||
user = get_user_model().objects.get(pk=user_pk)
|
||||
subject = "Your Dj LMS account credentials"
|
||||
msg = f"Dear Student {user.first_name},\n\nHere are the login credentials for your DJ LMS account.\n\nYour ID: {user.username}\nYour password: {password}\n\nBe sure to change your password for security."
|
||||
send_email(user, subject, msg)
|
||||
|
||||
|
||||
@shared_task
|
||||
def send_new_lecturer_email(user_pk, password):
|
||||
user = get_user_model().objects.get(pk=user_pk)
|
||||
subject = "Your Dj LMS account credentials"
|
||||
msg = f"Dear Lecturer {user.first_name},\n\nHere are the login credentials for your DJ LMS account.\n\nYour ID: {user.username}\nYour password: {password}\n\nBe sure to change your password for security."
|
||||
send_email(user, subject, msg)
|
||||
@ -0,0 +1,5 @@
|
||||
# This will make sure the app is always imported when
|
||||
# Django starts so that shared_task will use this app.
|
||||
from .celery import app as celery_app
|
||||
|
||||
__all__ = ("celery_app",)
|
||||
22
config/celery.py
Normal file
22
config/celery.py
Normal file
@ -0,0 +1,22 @@
|
||||
import os
|
||||
|
||||
from celery import Celery
|
||||
|
||||
# Set the default Django settings module for the 'celery' program.
|
||||
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "config.settings")
|
||||
|
||||
app = Celery("config")
|
||||
|
||||
# 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 apps.
|
||||
app.autodiscover_tasks()
|
||||
|
||||
|
||||
@app.task(bind=True, ignore_result=True)
|
||||
def debug_task(self):
|
||||
print(f"Request: {self.request!r}")
|
||||
@ -251,3 +251,15 @@ STATICFILES_STORAGE = "whitenoise.storage.CompressedManifestStaticFilesStorage"
|
||||
|
||||
STUDENT_ID_PREFIX = config("STUDENT_ID_PREFIX", "ugr")
|
||||
LECTURER_ID_PREFIX = config("LECTURER_ID_PREFIX", "lec")
|
||||
|
||||
CACHES = {
|
||||
"default": {
|
||||
"BACKEND": "django.core.cache.backends.redis.RedisCache",
|
||||
"LOCATION": "redis://127.0.0.1:6379",
|
||||
}
|
||||
}
|
||||
|
||||
# celery setting.
|
||||
CELERY_BROKER_URL = "redis://localhost:6379/0"
|
||||
CELERY_CACHE_BACKEND = "default"
|
||||
CELERY_BROKER_CONNECTION_RETRY_ON_STARTUP = True
|
||||
|
||||
@ -24,6 +24,11 @@ django-jet-reboot==1.3.5
|
||||
# Environment variable
|
||||
python-decouple==3.8
|
||||
|
||||
# Celery/redis
|
||||
celery==5.4.0
|
||||
redis==5.0.4
|
||||
hiredis==2.3.2
|
||||
|
||||
# Payments
|
||||
stripe==5.5.0
|
||||
gopay==2.0.1
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user