Added email notification system on every image upload

This commit is contained in:
faridlu 2022-09-29 04:38:21 +06:00
parent ef12610d6c
commit c319277818
10 changed files with 161 additions and 0 deletions

View File

@ -4,3 +4,7 @@ from django.apps import AppConfig
class AlbumConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'album'
def ready(self) -> None:
from . import signals
return super().ready()

15
album/signals.py Normal file
View File

@ -0,0 +1,15 @@
from django.contrib.auth import get_user_model
from django.contrib.sites.shortcuts import get_current_site
from django.db.models import Sum
from django.db.models.signals import post_save, pre_save
from django.dispatch import receiver
from .models import Album
from .tasks import send_new_album_notification
@receiver(post_save, sender=Album)
def post_save_album_instance(sender, instance, created, **kwargs):
if created:
send_new_album_notification.delay(instance.id)

36
album/tasks.py Normal file
View File

@ -0,0 +1,36 @@
import datetime
from itertools import chain
import numpy as np
from celery import shared_task
from celery.utils.log import get_task_logger
from django.conf import settings
from django.contrib.auth import get_user_model
from django.core import mail
from django.core.mail import EmailMultiAlternatives
from django.core.mail.backends.smtp import EmailBackend
from django.db.models.expressions import F
from django.template.loader import render_to_string
from django.utils.html import strip_tags
from django.utils.timezone import make_aware
from .models import Album
@shared_task(name='send_new_album_notification')
def send_new_album_notification(album_id):
album = Album.objects.get(id=album_id)
subject = 'New Image added | Verification Required - ungleich'
context = {
'subject': subject,
'album': album
}
html_message = render_to_string('email/new_album_notification.html', context)
plain_message = strip_tags(html_message)
from_email = settings.EMAIL_HOST_USER
print(from_email)
se_mail = mail.send_mail(
subject, plain_message, from_email, [from_email], html_message=html_message
)

View File

@ -0,0 +1,4 @@
from __future__ import absolute_import
from .celery import app as celery_app

32
config/celery.py Normal file
View File

@ -0,0 +1,32 @@
from __future__ import absolute_import, unicode_literals
import os
from celery import Celery
from django.conf import settings
from . import celeryconfig
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'config.settings.development')
app = Celery('config',
broker=celeryconfig.broker_url,
backend=celeryconfig.result_backend)
app.config_from_object("config.celeryconfig")
app.autodiscover_tasks(settings.INSTALLED_APPS)
@app.task(bind=True)
def debug_task(self):
print('Request: {0!r}'.format(self.request))
from celery.schedules import crontab
app.conf.beat_schedule = {
# 'scan_images_to_convert_into_webp_format': {
# 'task': 'products.tasks.scan_images_to_convert_into_webp_format',
# 'schedule': crontab(minute='*/5'), # Executes every 5 minutes
# },
}

7
config/celeryconfig.py Normal file
View File

@ -0,0 +1,7 @@
import os
import tempfile
broker_url = "redis://localhost:6379/0"
result_backend = "redis://localhost:6379/0"
accept_content = ["pickle", "json", "msgpack", "yaml"]
task_ignore_result = False

View File

@ -115,4 +115,23 @@ MEDIA_ROOT = os.path.join(BASE_DIR, 'static/media')
# Default primary key field type
# https://docs.djangoproject.com/en/3.2/ref/settings/#default-auto-field
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
BROKER_URL = 'redis://localhost:6379'
CELERY_RESULT_BACKEND = 'redis://localhost:6379'
CELERY_ACCEPT_CONTENT = ['application/json']
CELERY_TASK_SERIALIZER = 'json'
CELERY_RESULT_SERIALIZER = 'json'
CELERY_TIMEZONE = 'UTC'
# Email Configuration
EMAIL_BACKEND = "django.core.mail.backends.smtp.EmailBackend"
EMAIL_HOST = "smtp.gmail.com" # This HOST will be used, if we use google account
EMAIL_PORT = 587
EMAIL_HOST_USER = 'fhdahmod@gmail.com'
EMAIL_HOST_PASSWORD = 'wtneyydywieelkyk'
DEFAULT_FROM_EMAIL = EMAIL_HOST_USER
EMAIL_USE_TLS = True

Binary file not shown.

View File

@ -1,17 +1,28 @@
amqp==2.6.1
asgiref==3.5.2
astroid==2.12.10
asttokens==2.0.8
async-timeout==4.0.2
autopep8==1.7.0
backcall==0.2.0
backports.zoneinfo==0.2.1
billiard==3.6.4.0
black==22.8.0
celery==4.4.1
certifi==2022.9.24
charset-normalizer==2.0.12
click==8.1.3
click-didyoumean==0.3.0
click-plugins==1.1.1
click-repl==0.2.0
decorator==5.1.1
Deprecated==1.2.13
dill==0.3.5.1
Django==3.2
django-celery-beat==2.2.0
django-extensions==3.2.1
django-filter==22.1
django-timezone-field==4.2.3
djangorestframework==3.14.0
executing==1.1.0
idna==3.4
@ -19,11 +30,14 @@ importlib-metadata==4.12.0
ipython==8.5.0
isort==5.10.1
jedi==0.18.1
kombu==4.6.11
lazy-object-proxy==1.7.1
Markdown==3.4.1
matplotlib-inline==0.1.6
mccabe==0.7.0
mypy-extensions==0.4.3
numpy==1.23.3
packaging==21.3
parso==0.8.3
pathspec==0.10.1
pexpect==4.8.0
@ -36,8 +50,12 @@ pure-eval==0.2.2
pycodestyle==2.9.1
Pygments==2.13.0
pylint==2.15.3
pyparsing==3.0.9
python-crontab==2.6.0
python-dateutil==2.8.2
python-decouple==3.6
pytz==2022.2.1
redis==4.3.4
requests==2.27.1
six==1.16.0
sqlparse==0.4.3
@ -48,8 +66,10 @@ tomlkit==0.11.4
tqdm==4.64.1
traitlets==5.4.0
typing-extensions==4.3.0
tzdata==2022.4
urllib3==1.26.12
validators==0.18.2
vine==1.3.0
wcwidth==0.2.5
weaviate-client==3.8.0
wrapt==1.14.1

View File

@ -0,0 +1,24 @@
{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="x-ua-compatible" content="ie=edge">
<title>{{ subject }}</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="">
<meta name="author" content="ungleich">
<link rel="icon" href="{% static 'images/favicons/favicon.ico' %}">
</head>
<body>
<div class="container">
<p>Hi there, there have a new image request added, which needs verification. Please follow the <a href="http://127.0.0.1:8000/admin/album/album/{{album.id}}/change/">link</a> to verify</p>
</div>
</body>
</html>