Support sqlite3, using thread to send emails, etc.
Lighten the project by supporting sqlite3 and also handling email using only python threads
This commit is contained in:
parent
ae979a2a89
commit
4dad1fc0ae
13
README.md
13
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
|
> The following programs are required to run the project
|
||||||
|
|
||||||
- [Python3.8+](https://www.python.org/downloads/)
|
- [Python3.8+](https://www.python.org/downloads/)
|
||||||
- [Redis](https://redis.io/docs/latest/operate/oss_and_stack/install/install-redis/)
|
|
||||||
|
|
||||||
# Installation
|
# Installation
|
||||||
|
|
||||||
@ -83,18 +82,6 @@ python manage.py createsuperuser
|
|||||||
python manage.py runserver
|
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
|
Last but not least, go to this address http://127.0.0.1:8000
|
||||||
|
|
||||||
# References
|
# References
|
||||||
|
|||||||
@ -1,5 +1,8 @@
|
|||||||
from .tasks import send_new_student_email, send_new_lecturer_email
|
from .utils import (
|
||||||
from .utils import generate_student_credentials, generate_lecturer_credentials
|
generate_student_credentials,
|
||||||
|
generate_lecturer_credentials,
|
||||||
|
send_new_account_email,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
def post_save_account_receiver(sender, instance=None, created=False, *args, **kwargs):
|
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.set_password(password)
|
||||||
instance.save()
|
instance.save()
|
||||||
# Send email with the generated credentials
|
# Send email with the generated credentials
|
||||||
send_new_student_email.delay(instance.pk, password)
|
send_new_account_email(instance, password)
|
||||||
|
|
||||||
if instance.is_lecturer:
|
if instance.is_lecturer:
|
||||||
username, password = generate_lecturer_credentials()
|
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.set_password(password)
|
||||||
instance.save()
|
instance.save()
|
||||||
# Send email with the generated credentials
|
# Send email with the generated credentials
|
||||||
send_new_lecturer_email.delay(instance.pk, password)
|
send_new_account_email(instance, password)
|
||||||
|
|||||||
@ -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},
|
|
||||||
)
|
|
||||||
@ -1,6 +1,8 @@
|
|||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from django.contrib.auth import get_user_model
|
from django.contrib.auth import get_user_model
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
|
import threading
|
||||||
|
from core.utils import send_html_email
|
||||||
|
|
||||||
|
|
||||||
def generate_password():
|
def generate_password():
|
||||||
@ -27,3 +29,34 @@ def generate_student_credentials():
|
|||||||
|
|
||||||
def generate_lecturer_credentials():
|
def generate_lecturer_credentials():
|
||||||
return generate_lecturer_id(), generate_password()
|
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()
|
||||||
|
|||||||
@ -283,6 +283,7 @@ def staff_add_view(request):
|
|||||||
form = StaffAddForm(request.POST)
|
form = StaffAddForm(request.POST)
|
||||||
first_name = request.POST.get("first_name")
|
first_name = request.POST.get("first_name")
|
||||||
last_name = request.POST.get("last_name")
|
last_name = request.POST.get("last_name")
|
||||||
|
email = request.POST.get("email")
|
||||||
|
|
||||||
if form.is_valid():
|
if form.is_valid():
|
||||||
|
|
||||||
@ -293,7 +294,9 @@ def staff_add_view(request):
|
|||||||
+ first_name
|
+ first_name
|
||||||
+ " "
|
+ " "
|
||||||
+ last_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")
|
return redirect("lecturer_list")
|
||||||
else:
|
else:
|
||||||
|
|||||||
@ -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",)
|
|
||||||
@ -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}")
|
|
||||||
@ -237,15 +237,3 @@ STATICFILES_STORAGE = "whitenoise.storage.CompressedManifestStaticFilesStorage"
|
|||||||
|
|
||||||
STUDENT_ID_PREFIX = config("STUDENT_ID_PREFIX", "ugr")
|
STUDENT_ID_PREFIX = config("STUDENT_ID_PREFIX", "ugr")
|
||||||
LECTURER_ID_PREFIX = config("LECTURER_ID_PREFIX", "lec")
|
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,11 +24,6 @@ django-jet-reboot==1.3.5
|
|||||||
# Environment variable
|
# Environment variable
|
||||||
python-decouple==3.8
|
python-decouple==3.8
|
||||||
|
|
||||||
# Celery/redis
|
|
||||||
celery==5.4.0
|
|
||||||
redis==5.0.4
|
|
||||||
hiredis==2.3.2
|
|
||||||
|
|
||||||
# Payments
|
# Payments
|
||||||
stripe==5.5.0
|
stripe==5.5.0
|
||||||
gopay==2.0.1
|
gopay==2.0.1
|
||||||
|
|||||||
@ -40,19 +40,7 @@
|
|||||||
</div>
|
</div>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
|
||||||
{% if messages %}
|
{% include 'snippets/messages.html' %}
|
||||||
{% for message in messages %}
|
|
||||||
{% if message.tags == 'error' %}
|
|
||||||
<div class="alert alert-danger">
|
|
||||||
<i class="fas fa-exclamation-circle"></i>{{ message }}
|
|
||||||
</div>
|
|
||||||
{% else %}
|
|
||||||
<div class="alert alert-success">
|
|
||||||
<i class="fas fa-check-circle"></i>{{ message }}
|
|
||||||
</div>
|
|
||||||
{% endif %}
|
|
||||||
{% endfor %}
|
|
||||||
{% endif %}
|
|
||||||
|
|
||||||
<div>
|
<div>
|
||||||
<div class="title-1">News & Events</div>
|
<div class="title-1">News & Events</div>
|
||||||
|
|||||||
@ -2,16 +2,16 @@
|
|||||||
{% if messages %}
|
{% if messages %}
|
||||||
{% for message in messages %}
|
{% for message in messages %}
|
||||||
{% if message.tags == 'error' %}
|
{% if message.tags == 'error' %}
|
||||||
<div class="alert py-2 alert-danger">
|
<div class="alert py-2 alert-danger d-flex align-items-center">
|
||||||
<i class="fas fa-exclamation-circle me-2"></i>{{ message }}
|
<i class="fas fa-exclamation-circle me-2" style="min-width: 35px;"></i><span>{{ message }}</span>
|
||||||
</div>
|
</div>
|
||||||
{% elif message.tags == 'warning' %}
|
{% elif message.tags == 'warning' %}
|
||||||
<div class="alert py-2 alert-warning">
|
<div class="alert py-2 alert-warning d-flex align-items-center">
|
||||||
<i class="fas fa-exclamation-circle me-2"></i>{{ message }}
|
<i class="fas fa-exclamation-circle me-2" style="min-width: 35px;"></i><span>{{ message }}</span>
|
||||||
</div>
|
</div>
|
||||||
{% else %}
|
{% else %}
|
||||||
<div class="alert py-2 alert-success">
|
<div class="alert py-2 alert-success d-flex align-items-center">
|
||||||
<i class="fas fa-check-circle me-2"></i>{{ message }}
|
<i class="fas fa-check-circle me-2" style="min-width: 35px;"></i><span>{{ message }}</span>
|
||||||
</div>
|
</div>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user