Research
Security News
Quasar RAT Disguised as an npm Package for Detecting Vulnerabilities in Ethereum Smart Contracts
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
Email notification with celery and administrative view for send email with delay e.g daily
#. Install from pip
.. code:: bash
$ pip install async_notifications
#. Add required apps in your settings **
.. code:: python
INSTALLED_APPS = [
...
'async_notifications'
]
#. Add ajax_select urls in urls.py
.. code:: python
from django.conf.urls import url, include
urlpatterns = [
...
url(r'^async_notifications/', include('async_notifications.urls')),
]
#. It's really important set CELERY_MODULE pointing to your project celery file, because it's needed for assing task to the current project, and configure some default celery options
.. code:: python
# settings.py
CELERY_MODULE = "demo.celery"
CELERY_TIMEZONE = TIME_ZONE
CELERY_ACCEPT_CONTENT = ['json']
#. Configure your email settings, e.g for development
.. code:: python
DEFAULT_FROM_EMAIL="mail@example.com"
EMAIL_HOST="localhost"
EMAIL_PORT="1025"
.. code:: python
# celery.py
app.conf.CELERYBEAT_SCHEDULE = {
# execute 12:30 pm
'send_daily_emails': {
'task': 'async_notifications.tasks.send_daily',
'schedule': crontab(minute=30, hour=0),
},
}
Remember use demo/init.py to update your projectfolder/init.py.
#. Run migrations
.. code:: bash
$ python manage.py migrate
You need to run 3 subsystems for run this app so you need 3 xterm, for this explanation I will use the demo project
Run smtp debug client
.. code:: bash
$ python -m smtpd -n -c DebuggingServer localhost:1025
Run celery, if you aren't setup celery yet see celery documentation <http://docs.celeryproject.org/en/latest/django/first-steps-with-django.html>
_.
.. code:: bash
$ celery -A demo worker -l info -B
Run django
.. code:: bash
$ python manage.py runserver
Report your context template
.. code:: python
from async_notifications.register import update_template_context
context = [
('fieldname', 'Field description'),
('fieldname2', 'Field description'),
...
]
update_template_context("yourcode", 'your email subject', context )
This automátically create a Email template if not found.
Context is list of tuples with the fields available in the template context, this context is add in the same file
that have send_email_from_template
Using with django templates
.. code:: python
update_template_context("yourcode", 'your email subject', context, 'templatepath.html', as_template=True )
Send dict as context is now available, but remember that you can not repit keys in dict so use with precaution.
Send an email :)
.. code:: python
send_email_from_template(code, recipient,
context={},
enqueued=True,
user=None,
upfile=None)
Params description:
recipient
is a list of emailscode
is the same code register in update_template_contextenqueued
if False send the email immediately else enqueued to be sent when send email task run.user
user how send emailupfile
attached file in emailWhen you need to pass a default template message base on template, but you have not the template object and also you need to write the object with django template sintaxis you can use DummyContextObject that return always something like {{ myobj.attr1.objattr }}
.. code:: python
from async_notifications.register import update_template_context, DummyContextObject
context = [
('myobj', 'Field description'),
...
]
message = render_to_string('some/template.html',
context={
'myobj': DummyContextObject('myobj')
}
)
update_template_context("yourcode", 'your email subject', context, message=message )
As recomendation install django-markitup and markdown to generate preview templates using django template system and configure ASYNC_NEWSLETTER_WIDGET
to overwrite default text area editor in template newsletter.
If you want to incorporate custom email sender you can configure with ASYNC_NEWSLETTER_SEVER_CONFIGS
.. code:: python
ASYNC_NEWSLETTER_SEVER_CONFIGS={
'host': 'localhost',
'port': '1025',
'fail_silently': False,
'backend': None,
'from': 'From user <user@example.com>'
'username':'my_username',
'password':'my_password',
'use_tls': True
}
In your app edit at the end of admin.py to register your model
.. code:: python
register_model('app.model_label', model class, prefix='prefix used to include in template')
register_news_basemodel('app.model_label', Title, class manager)
To create a new manager you need to create a class like
.. code:: python
from async_notifications.interfaces import NewsLetterInterface
class MembershipManager(NewsLetterInterface):
name = name used to include in template
model = Model
form = Filter form class
Take a look to NewsLetterInterface to know what methods you need to overwrite
This configuration could help you to integrate with Django CMS.
include in your INSTALLED_APPS
:
.. code:: python
INSTALLED_APPS = [
...
'async_notifications',
'async_notifications.djcms_async_notifications',
]
Configure how models and field async_notifications will use, ej. aldryn_people
.. code:: python
ASYNC_NOTIFICATION_GROUP = 'aldryn_people.Group'
ASYNC_NOTIFICATION_GROUP_LOOKUP_FIELDS = {
'order_by': 'translations__name',
'email': 'email',
'group_lookup': 'translations__name',
'display': 'name',
'filter': ['translations__name__icontains']}
ASYNC_NOTIFICATION_USER = 'aldryn_people.Person'
ASYNC_NOTIFICATION_USER_LOOKUP_FIELDS = {
'order_by': 'translations__name',
'display': 'name',
'filter': [
'user__first_name__icontains',
'user__last_name__icontains',
'translations__name__icontains'],
'group_lookup': 'groups__translations__name'}
.. note:: Django auth is used by default
CONTACT_PLUS_SEND_METHOD = 'async_notifications.djcms_async_notifications.contact_plus.send_email' ASYNC_NOTIFICATION_CONTACT_PLUS_EMAIL = 'email'
.. note::
This requires special cmsplugin-contact-plus version, we send a PRs, but is not merged yet.
For example using ckeditor widget
ASYNC_NOTIFICATION_TEXT_AREA_WIDGET = 'ckeditor.widgets.CKEditorWidget'
.. note::
See how to configure CKEditor <https://github.com/django-ckeditor/django-ckeditor>
_ .
FAQs
Email async notifications with celery.
We found that async-notifications demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
Security News
Research
A supply chain attack on Rspack's npm packages injected cryptomining malware, potentially impacting thousands of developers.
Research
Security News
Socket researchers discovered a malware campaign on npm delivering the Skuld infostealer via typosquatted packages, exposing sensitive data.