diff --git a/README.md b/README.md index e338b26..56a9ff1 100644 --- a/README.md +++ b/README.md @@ -51,7 +51,6 @@ If you would like to contribute, simply begin by implementing one from the list > The following programs are required to run the project - [Python3.8+](https://www.python.org/downloads/) -- [Redis](https://redis.io/docs/latest/operate/oss_and_stack/install/install-redis/) # Installation @@ -83,18 +82,6 @@ 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 diff --git a/accounts/signals.py b/accounts/signals.py index db2b145..b549fe0 100644 --- a/accounts/signals.py +++ b/accounts/signals.py @@ -1,5 +1,8 @@ -from .tasks import send_new_student_email, send_new_lecturer_email -from .utils import generate_student_credentials, generate_lecturer_credentials +from .utils import ( + generate_student_credentials, + generate_lecturer_credentials, + send_new_account_email, +) def post_save_account_receiver(sender, instance=None, created=False, *args, **kwargs): @@ -13,7 +16,7 @@ def post_save_account_receiver(sender, instance=None, created=False, *args, **kw instance.set_password(password) instance.save() # Send email with the generated credentials - send_new_student_email.delay(instance.pk, password) + send_new_account_email(instance, password) if instance.is_lecturer: username, password = generate_lecturer_credentials() @@ -21,4 +24,4 @@ def post_save_account_receiver(sender, instance=None, created=False, *args, **kw instance.set_password(password) instance.save() # Send email with the generated credentials - send_new_lecturer_email.delay(instance.pk, password) + send_new_account_email(instance, password) diff --git a/accounts/tasks.py b/accounts/tasks.py deleted file mode 100644 index ff863af..0000000 --- a/accounts/tasks.py +++ /dev/null @@ -1,25 +0,0 @@ -from celery import shared_task -from django.contrib.auth import get_user_model -from core.utils import send_html_email - - -@shared_task -def send_new_student_email(user_pk, password): - user = get_user_model().objects.get(pk=user_pk) - send_html_email( - subject="Your Dj LMS account confirmation and credentials", - recipient_list=[user.email], - template="accounts/email/new_student_account_confirmation.html", - context={"user": user, "password": password}, - ) - - -@shared_task -def send_new_lecturer_email(user_pk, password): - user = get_user_model().objects.get(pk=user_pk) - send_html_email( - subject="Your Dj LMS account confirmation and credentials", - recipient_list=[user.email], - template="accounts/email/new_lecturer_account_confirmation.html", - context={"user": user, "password": password}, - ) diff --git a/accounts/tests.pys b/accounts/tests.py similarity index 100% rename from accounts/tests.pys rename to accounts/tests.py diff --git a/accounts/utils.py b/accounts/utils.py index 712d9c6..7902ea3 100644 --- a/accounts/utils.py +++ b/accounts/utils.py @@ -1,6 +1,8 @@ from datetime import datetime from django.contrib.auth import get_user_model from django.conf import settings +import threading +from core.utils import send_html_email def generate_password(): @@ -27,3 +29,34 @@ def generate_student_credentials(): def generate_lecturer_credentials(): return generate_lecturer_id(), generate_password() + + +class EmailThread(threading.Thread): + def __init__(self, subject, recipient_list, template_name, context): + self.subject = subject + self.recipient_list = recipient_list + self.template_name = template_name + self.context = context + threading.Thread.__init__(self) + + def run(self): + send_html_email( + subject=self.subject, + recipient_list=self.recipient_list, + template=self.template_name, + context=self.context, + ) + + +def send_new_account_email(user, password): + if user.is_student: + template_name = "accounts/email/new_student_account_confirmation.html" + else: + template_name = "accounts/email/new_lecturer_account_confirmation.html" + email = { + "subject": "Your Dj LMS account confirmation and credentials", + "recipient_list": [user.email], + "template_name": template_name, + "context": {"user": user, "password": password}, + } + EmailThread(**email).start() diff --git a/accounts/views.py b/accounts/views.py index ac60678..3168d5a 100644 --- a/accounts/views.py +++ b/accounts/views.py @@ -283,6 +283,7 @@ def staff_add_view(request): form = StaffAddForm(request.POST) first_name = request.POST.get("first_name") last_name = request.POST.get("last_name") + email = request.POST.get("email") if form.is_valid(): @@ -293,7 +294,9 @@ def staff_add_view(request): + first_name + " " + last_name - + " has been created.", + + " has been created. An email with account credentials will be sent to " + + email + + " within a minute", ) return redirect("lecturer_list") else: diff --git a/config/__init__.py b/config/__init__.py index 5568b6d..e69de29 100644 --- a/config/__init__.py +++ b/config/__init__.py @@ -1,5 +0,0 @@ -# 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",) diff --git a/config/celery.py b/config/celery.py deleted file mode 100644 index f03d052..0000000 --- a/config/celery.py +++ /dev/null @@ -1,22 +0,0 @@ -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}") diff --git a/config/settings.py b/config/settings.py index ab4c493..ba01bc2 100644 --- a/config/settings.py +++ b/config/settings.py @@ -237,15 +237,3 @@ 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 diff --git a/requirements/base.txt b/requirements/base.txt index 39716a3..4aec2f0 100644 --- a/requirements/base.txt +++ b/requirements/base.txt @@ -24,11 +24,6 @@ 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 diff --git a/templates/core/index.html b/templates/core/index.html index 7b3968e..6101aee 100644 --- a/templates/core/index.html +++ b/templates/core/index.html @@ -40,19 +40,7 @@ {% endif %} -{% if messages %} -{% for message in messages %} -{% if message.tags == 'error' %} -
- {{ message }} -
-{% else %} -
- {{ message }} -
-{% endif %} -{% endfor %} -{% endif %} +{% include 'snippets/messages.html' %}
News & Events
diff --git a/templates/snippets/messages.html b/templates/snippets/messages.html index 4b422ab..51fae2b 100644 --- a/templates/snippets/messages.html +++ b/templates/snippets/messages.html @@ -2,17 +2,17 @@ {% if messages %} {% for message in messages %} {% if message.tags == 'error' %} -
- {{ message }} +
+ {{ message }}
{% elif message.tags == 'warning' %} -
- {{ message }} +
+ {{ message }}
{% else %} -
- {{ message }} +
+ {{ message }}
{% endif %} {% endfor %} -{% endif %} +{% endif %} \ No newline at end of file