From c31927781869fbc86be7bd1803f05f960992fee4 Mon Sep 17 00:00:00 2001 From: Farid Chowdhury Date: Thu, 29 Sep 2022 04:38:21 +0600 Subject: [PATCH] Added email notification system on every image upload --- album/apps.py | 4 +++ album/signals.py | 15 ++++++++ album/tasks.py | 36 ++++++++++++++++++++ config/__init__.py | 4 +++ config/celery.py | 32 +++++++++++++++++ config/celeryconfig.py | 7 ++++ config/settings/base.py | 19 +++++++++++ db.sqlite3 | Bin 159744 -> 159744 bytes requirements.txt | 20 +++++++++++ templates/email/new_album_notification.html | 24 +++++++++++++ 10 files changed, 161 insertions(+) create mode 100644 album/signals.py create mode 100644 album/tasks.py create mode 100644 config/celery.py create mode 100644 config/celeryconfig.py create mode 100644 templates/email/new_album_notification.html diff --git a/album/apps.py b/album/apps.py index 83b2048..818866b 100644 --- a/album/apps.py +++ b/album/apps.py @@ -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() diff --git a/album/signals.py b/album/signals.py new file mode 100644 index 0000000..17c1706 --- /dev/null +++ b/album/signals.py @@ -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) diff --git a/album/tasks.py b/album/tasks.py new file mode 100644 index 0000000..836ff96 --- /dev/null +++ b/album/tasks.py @@ -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 + ) diff --git a/config/__init__.py b/config/__init__.py index e69de29..365e955 100644 --- a/config/__init__.py +++ b/config/__init__.py @@ -0,0 +1,4 @@ + +from __future__ import absolute_import + +from .celery import app as celery_app diff --git a/config/celery.py b/config/celery.py new file mode 100644 index 0000000..fa89c9f --- /dev/null +++ b/config/celery.py @@ -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 + # }, +} diff --git a/config/celeryconfig.py b/config/celeryconfig.py new file mode 100644 index 0000000..168f102 --- /dev/null +++ b/config/celeryconfig.py @@ -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 diff --git a/config/settings/base.py b/config/settings/base.py index 661e36a..722a10c 100644 --- a/config/settings/base.py +++ b/config/settings/base.py @@ -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 diff --git a/db.sqlite3 b/db.sqlite3 index 68327a53cf1c46e5f3108f6b22abb515eeab422c..50b04cd1f93db411022720cd6e7e722b12ef0ea4 100644 GIT binary patch delta 1107 zcma))!A}!G7{zzmQoC4oK&_2|gp!ER1KXXM?rwM0#9E51(iYh^h?lzUuCxSbX-fny zQcbvu;m~+Nx_`H(NwU< zrvVyZ4&xC{8o>b%#xfRVPGJ26BSAt!a6YxX;K`-tjC9E}j4JsWV8xWR-7be@LS)3V z2w|S*5oy^al_hf-SSs&oz_qK^Ky~tZx1FKjD>%q}WR$M2T`SIS&gjMM&ZAC+-f+CX z@X@|+XR77>j?3jl_a$nE4hH)NhP-at>+W||-YAy7=SjSx#@9+Zn_rx50TE0;tVA%D z#1`cQ5la#Y1a-Y&6pJj|c)|&zMloH;=1bY!BFjG23R(Tmf|i|U)47GQ`YjSmMB_mv znpESl+fy^{W`fX!61l5H?pkT>aM?r4j=9<-$}8QN7_<_KU292q8^GNaoEWv3`*^#ZV@j u)@E}JWDbuT1*48w${3!1SlX0JoeYO6SMOVxVba3D3+;QgyP;UtZu|z%y)oth delta 168 zcmZp8z}fJCbAmLZ?L--8M%#@E9Qtfb;+)K;oYNVD8AT>H>1#6bG_TR$zDA!hVgd&f z|5pb7-~3-U3l_ZO7iVQ=WaMCCVP@py;9_84VB-JI!2gH;J5cg9zbFecCnFOxGbbl6 zSjvWhe;&Wh#zq5vrUa(x3*Iw|ZV!IXxG_M6hqH}=zn@=~Z#SPO?^E6ip65J%-1E4; Tb9r*MZEQ^A+}`HNRHX(0Arvet diff --git a/requirements.txt b/requirements.txt index abdd336..6762d0e 100644 --- a/requirements.txt +++ b/requirements.txt @@ -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 diff --git a/templates/email/new_album_notification.html b/templates/email/new_album_notification.html new file mode 100644 index 0000000..6ba64c8 --- /dev/null +++ b/templates/email/new_album_notification.html @@ -0,0 +1,24 @@ +{% load static %} + + + + + + + + {{ subject }} + + + + + + + + + +
+

Hi there, there have a new image request added, which needs verification. Please follow the link to verify

+
+ + + \ No newline at end of file