djangoflow / django-df-notifications Goto Github PK
View Code? Open in Web Editor NEWOpinionated DjangoFlow Omni-channel Notifications
License: MIT License
Opinionated DjangoFlow Omni-channel Notifications
License: MIT License
CustomPushNotification
title
body
image
audience = many2many to users all if none
created, modified, sent
and a send action in the admin
I am thinking about limiting notification rate per channel with throttling class similar to DRF.
Imagine the system generates 1000 push notifications for a user.
We want to send the first notification immediately, then start exponentially backing them off (either using django cache or database backend) and then send them a minute later, then 5 minutes later and so on.
Some channels and project specific notifications can be aggregated. Imagine social network, when you receive 1 like, then 150 in the next 5 minutes, you would not want to get 150 notifications but rather just two, second one saying you have received 150 likes (or some other logic like user a, user b and 148 other users have liked your story).
This won't be necessary once we move to notification templates via path names & prefixes
For now we need to be able to create multiple notifications templtes with the same slug so we can have different templates for different channels.
Migrations for 'df_notifications':
venv/lib/python3.9/site-packages/df_notifications/migrations/0002_alter_notificationtemplate_id_alter_userdevice_user.py
- Alter field id on notificationtemplate
- Alter field user on userdevice
Running migrations:
No migrations to apply.
Your models in app(s): 'df_notifications' have changes that are not yet reflected in a migration, and so won't be applied.
Run 'manage.py makemigrations' to make new migrations, and then re-run 'manage.py migrate' to apply them.
django@django-web-5c84cd796-vnb7b:/app$ ./manage.py makemigrations
Migrations for 'df_notifications':
/usr/local/lib/python3.9/site-packages/df_notifications/migrations/0002_alter_notificationhistory_id_alter_userdevice_user.py
- Alter field id on notificationhistory
- Alter field user on userdevice
Traceback (most recent call last):
File "/app/./manage.py", line 29, in <module>
execute_from_command_line(sys.argv)
File "/usr/local/lib/python3.9/site-packages/django/core/management/__init__.py", line 425, in execute_from_command_line
utility.execute()
File "/usr/local/lib/python3.9/site-packages/django/core/management/__init__.py", line 419, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/usr/local/lib/python3.9/site-packages/django/core/management/base.py", line 373, in run_from_argv
self.execute(*args, **cmd_options)
File "/usr/local/lib/python3.9/site-packages/django/core/management/base.py", line 417, in execute
output = self.handle(*args, **options)
File "/usr/local/lib/python3.9/site-packages/django/core/management/base.py", line 90, in wrapped
res = handle_func(*args, **kwargs)
File "/usr/local/lib/python3.9/site-packages/django/core/management/commands/makemigrations.py", line 190, in handle
self.write_migration_files(changes)
File "/usr/local/lib/python3.9/site-packages/django/core/management/commands/makemigrations.py", line 228, in write_migration_files
with open(writer.path, "w", encoding='utf-8') as fh:
PermissionError: [Errno 13] Permission denied: '/usr/local/lib/python3.9/site-packages/df_notifications/migrations/0002_alter_notificationhistory_id_alter_userdevice_user.py'
Sentry is attempting to send 2 pending error messages
Waiting up to 2 seconds
The idea is to move away from notifications models scattered across different apps and replace/rename AbstractNotification
that now is being inherited by applications specific models with NotificationTemplate
and extend it with slug
field, so other apps can use it like follows:
from df_notifications.signals import register_signal ? register_model? register_notification
@register_notification(signal=pre_save, model=User, slug='user_created'):
def user_created_notification(prev: instance, next: instance, model: models.Model, template: NotificationTemplate):
context = {
'prev': prev,
'instance': next,
}
if condition:
template.send_all(users=[next.user], context=context)
In the current design, we handle all notification channels via NotificationTemplate class.
It's bad because different channels require different fields: email needs a subject, body, body_html; push needs a subject, body, data.
Also, we handle sending notifications to the different channels in one place.
We need to develop a design, that allows us to write separate handlers (classes I think) for different notification channels.
Something like this, but you can propose your own solution for these problems:
class BaseNotificationChannel:
parts = []
def render_parts(...):
return {p: ... for p in self.parts}
class EmailNotificationChannel(BaseNotificationChannel):
parts = ['subject', 'body', 'body_html']
def send(...):
parts = self.render_parts(...)
send_email(...)
To store template parts in the DB, we can use https://github.com/jazzband/django-dbtemplates. Also, we need to lookup up the templates in the template
folder if we find none in the DB.
NotificationTemplate instances will store links or paths to these parts in this case.
Add a test to the tests.py for the JSONPostWebhookChannel.send method.
The goal is to check that we send correct http payload using JSONPostWebhookChannel
channel.
The goal is to use django-df-model-actions
package instead of using BaseModelRule
class from the library.
When we will have asynchronous actions djangoflow/django-df-model-actions#4.
So, we need to make sure that we can use two libs together.
You need to write some tests to prove it (and test manually), make changes if needed, and describe in readme how to use them (maybe using test app as an example).
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.