Skip to content

Commit

Permalink
asynchronous mail notification on admission confirmation
Browse files Browse the repository at this point in the history
  • Loading branch information
TareqMonwer committed Nov 8, 2020
1 parent 8488b94 commit 52718ef
Show file tree
Hide file tree
Showing 10 changed files with 186 additions and 42 deletions.
2 changes: 2 additions & 0 deletions Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ django-model-utils = "*"
django-taggit = "*"
braintree = "*"
django-environ = "*"
celery = "*"
redis = "*"

[requires]
python_version = "3.8"
148 changes: 110 additions & 38 deletions Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,7 @@ In order to test this project with dummy data:

then for result, subjects, combinations you can run your script or just
create data manually for now.

* APPLY CARD INFO AT `admission` link
`card number: 5555555555554444`
`expiracy: any valid future date`
4 changes: 3 additions & 1 deletion config/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@ EMAIL_HOST_PASSWORD=
DEFAULT_FROM_EMAIL=
BRAINTREE_MERCHANT_ID=
BRAINTREE_PUBLIC_KEY=
BRAINTREE_PRIVATE_KEY=
BRAINTREE_PRIVATE_KEY=
BROKER_URL=
CELERY_RESULT_BACKEND=
3 changes: 3 additions & 0 deletions config/__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',)
24 changes: 24 additions & 0 deletions config/celery.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import os

from celery import Celery

from django.conf import settings

# 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 app configs.
app.autodiscover_tasks(lambda:settings.INSTALLED_APPS)


@app.task(bind=True)
def debug_task(self):
print(f'Request: {self.request!r}')
13 changes: 10 additions & 3 deletions config/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'
TIME_ZONE = 'Asia/Dhaka'

USE_I18N = True

Expand Down Expand Up @@ -174,7 +174,6 @@
EMAIL_PORT = env('EMAIL_PORT')
EMAIL_HOST_USER = env('EMAIL_HOST_USER')
EMAIL_HOST_PASSWORD = env('EMAIL_HOST_PASSWORD')
DEFAULT_FROM_EMAIL = env('DEFAULT_FROM_EMAIL')

# login/register redirects
LOGIN_REDIRECT_URL = 'index_view'
Expand All @@ -191,4 +190,12 @@
# BRAINTREE FOR HANDLING PAYMENTS
BRAINTREE_MERCHANT_ID = env('BRAINTREE_MERCHANT_ID')
BRAINTREE_PUBLIC_KEY = env('BRAINTREE_PUBLIC_KEY')
BRAINTREE_PRIVATE_KEY = env('BRAINTREE_PRIVATE_KEY')
BRAINTREE_PRIVATE_KEY = env('BRAINTREE_PRIVATE_KEY')

# CELERY BROKER CONFIG
CELERY_BROKER_URL = env('CELERY_BROKER_URL')
CELERY_RESULT_BACKEND = env('CELERY_RESULT_BACKEND')
CELERY_ACCEPT_CONTENT = ['application/json']
CELERY_TASK_SERIALIZER = 'json'
CELERY_RESULT_SERIALIZER = 'json'
CELERY_TIMEZONE = 'Asia/Dhaka'
4 changes: 4 additions & 0 deletions pages/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
from students.forms import StudentForm
from students.models import AdmissionStudent

from students.tasks import send_admission_confirmation_email


def index(request):
return render(request, 'landing/index.html')
Expand Down Expand Up @@ -71,4 +73,6 @@ def payment(request, pk):
if result.is_success:
registrant.paid = True
registrant.save()
print(registrant.email)
send_admission_confirmation_email(registrant.id)
return redirect('pages:online_admission')
23 changes: 23 additions & 0 deletions students/tasks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from celery.decorators import task
from celery.utils.log import get_logger

from django.core.mail import send_mail
from config.settings import EMAIL_HOST_USER

from .models import AdmissionStudent

logger = get_logger(__name__)


@task(name='send_admission_confirmation_email')
def send_admission_confirmation_email(student_id):
student = AdmissionStudent.objects.get(id=student_id)
name = student.name
choosen_dept = student.choosen_department
send_mail(
f'SMS-LIO: Admission confirmed for student {name}',
f'Choosen Dept: {choosen_dept}',
EMAIL_HOST_USER,
[student.email, ],
fail_silently=False
)
3 changes: 3 additions & 0 deletions students/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
from .forms import (StudentForm, AdmissionForm,
StudentRegistrantUpdateForm, CounselingDataForm)

from .tasks import send_admission_confirmation_email


@user_passes_test(user_is_staff)
def students_dashboard_index(request):
Expand Down Expand Up @@ -82,6 +84,7 @@ def admit_student(request, pk):
student.admitted = True
student.admission_date = date.today()
student.save()
send_admission_confirmation_email.delay(student.id)
return redirect('students:admitted_student_list')
else:
form = AdmissionForm()
Expand Down

0 comments on commit 52718ef

Please sign in to comment.