Package: garpix-notify
Garpix NotifyQuickstartInstall with pip:pipinstallgarpix_notifyAdd thegarpix_notifyand dependencies to yourINSTALLED_APPS:# settings.pyINSTALLED_APPS=[# ...'fcm_django','garpix_notify',]FCM_DJANGO_SETTINGS={"APP_VERBOSE_NAME":"Firebase Cloud Messaging","FCM_SERVER_KEY":"[your api key]","ONE_DEVICE_PER_USER":False,"DELETE_INACTIVE_DEVICES":False,}Package not included migrations, set path to migration directory. Don't forget create this directory (app/migrations/garpix_notify/) and place empty__init__.py:app/migrations/
app/migrations/__init__.py  # empty file
app/migrations/garpix_notify/__init__.py  # empty fileAdd path to settings:# settings.pyMIGRATION_MODULES={'garpix_notify':'app.migrations.garpix_notify',}Add mixins to settings if you need to add extra functionality to Notify models:# settings.pyGARPIX_NOTIFY_MIXIN='app.models.notify_mixin.NotifyMixin'GARPIX_SYSTEM_NOTIFY_MIXIN='app.models.notify_mixin.SystemNotifyMixin'Create your custom user model and addAUTH_USER_MODELtoapp/settings.py:AUTH_USER_MODEL = 'user.User'Run make migrations:pythonmanage.pymakemigrationsMigrate:pythonmanage.pymigrateExampleStep 1. Set notify types inapp/settings.py, for example:REGISTRATION_EVENT=1FEEDBACK_EVENT=2EXAMPLE_EVENT_1=3EXAMPLE_EVENT_2=4NOTIFY_EVENTS={REGISTRATION_EVENT:{'title':'Register',},FEEDBACK_EVENT:{'title':'Feeback',},EXAMPLE_EVENT_1:{'title':'Example 1',},EXAMPLE_EVENT_2:{'title':'Example 2',},}CHOICES_NOTIFY_EVENT=[(k,v['title'])fork,vinNOTIFY_EVENTS.items()]Step 2. Import default settings in yourapp/settings.pyfromgarpix_notify.settingsimport*or copy from here if you want more customization# notify configPERIODIC_SENDING=60EMAIL_MAX_DAY_LIMIT=240EMAIL_MAX_HOUR_LIMIT=240# SMSSMS_URL_TYPE=0SMS_API_ID=1234567890SMS_LOGIN=''SMS_PASSWORD=''SMS_FROM=''# CALLCALL_URL_TYPE=0CALL_API_ID=1234567890CALL_LOGIN=''CALL_PASSWORD=''# TELEGRAMTELEGRAM_API_KEY='000000000:AAAAAAAAAA-AAAAAAAA-_AAAAAAAAAAAAAA'TELEGRAM_BOT_NAME='MySuperBot'TELEGRAM_WELCOME_TEXT='Hello'TELEGRAM_HELP_TEXT='/set !help for HELP'TELEGRAM_BAD_COMMAND_TEXT='Incorrect command format'TELEGRAM_SUCCESS_ADDED_TEXT='Success'TELEGRAM_FAILED_ADDED_TEXT='Failed'TELEGRAM_PARSE_MODE=NoneTELEGRAM_DISABLE_NOTIFICATION=FalseTELEGRAM_DISABLE_PAGE_PREVIEW=FalseTELEGRAM_SENDING_WITHOUT_REPLY=FalseTELEGRAM_TIMEOUT=None# VIBERVIBER_API_KEY='000000000:AAAAAAAAAA-AAAAAAAA-_AAAAAAAAAAAAAA'VIBER_BOT_NAME='MySuperViberBot'VIBER_WELCOME_TEXT='Hello'VIBER_SUCCESS_ADDED_TEXT='Success'VIBER_FAILED_ADDED_TEXT='Failed'VIBER_TEXT_FOR_NEW_SUB='HI!'# WHATSAPPIS_WHATS_APP_ENABLED=TrueWHATS_APP_AUTH_TOKEN=NoneWHATS_APP_ACCOUNT_SID=NoneWHATS_APP_NUMBER_SENDER=None# SETTINGSEMAIL_MALLING=1GARPIX_NOTIFY_MIXIN='garpix_notify.mixins.notify_mixin.NotifyMixin'NOTIFY_USER_WANT_MESSAGE_CHECK=NoneNOTIFY_CALL_CODE_CHECK=NoneGARPIX_NOTIFY_CELERY_SETTINGS='app.celery.app'DEFAULT_SYSTEM_NOTIFY_TYPE='system'Step 3. Go to the admin panel and go to the "Notifications" section - "SMTP accounts"Add an SMTP account to send Email notifications. These will be the senders of Email notifications.Step 4. Also go to "Notifications" - "Categories"Create a category that will be used to send emails. Usually one category is enough. The ability to enter several categories
is necessary to divide them into informational and marketing notifications.Step 4. Go to "Notifications" - "Templates"Create a template for a specific event (when you added them tosettings.py).Step 5. Call Notify.send()In the code where it is necessary to work out sending a notification, we perform the following actions:fromdjango.confimportsettingsfromgarpix_notify.modelsimportNotify# Syntax# Notify.send(<event>, <context>[, <user=None>, <email=None>, <phone=None>, <files=None>, <data_json=None>])# That is, we specify the event ID as the first parameter,# create variables for the template,# third - the user to send it to (it is not necessary to specify his email, phone number, etc.,# because this will be determined automatically depending on the type of template)# Exampleuser=request.user# this will be the recipient of the notification.Notify.send(settings.REGISTRATION_EVENT,{'confirmation_code':'abcdef12345',},user=user)# If we do not have a user in the system, but we need to send an email, we can do the followingNotify.send(settings.EXAMPLE_EVENT_1,{'confirmation_code':'abcdef12345',},email='example@mail.ru')# If you need more detailed time settings, add send_atNotify.send(settings.EXAMPLE_EVENT_1,{'confirmation_code':'abcdef12345',},email='example@mail.ru',send_at=(datetime.datetime.now()+datetime.timedelta(days=1)))# If you need to send a code by phone callNotify.send(settings.EXAMPLE_EVENT_2,phone='79998881122',context={})# or if you need to get the code directlyNotify.call(phone=79998881122)Mass email and sms mailing:To perform a mass mailing, you need to add user lists to the template.
Or directly in the notification.New system Notifications:Now system notifications are placed in an independent model.
It is not necessary to create templates for them.
Mass mailing is also available.fromdjango.confimportsettingsfromgarpix_notify.modelsimportSystemNotifyfromdjango.contrib.authimportget_user_model# 1. Example of use without templatesUser=get_user_model()user=User.objects.filter().first()SystemNotify.send({'test':'data'},user)# 2. Example of using a templateSystemNotify.send({'test':'data'},event=settings.EXAMPLE_EVENT_2)Do not forget run celery:celery -A app worker --loglevel=info -BTelegram NotifyRegister you bothttps://t.me/BotFatherGo tohttp://localhost:8000/admin/garpix_notify/notifyconfig/and fill "Telegram" section (API keyandBot name).Run daemon:python3backend/manage.pygarpix_notify_telegramGo to your bot and send/startcommand.Also, seeuser/admin.pyfile (see instructions):fromdjango.contribimportadminfrom.modelsimportUserfromdjango.contrib.auth.adminimportUserAdmin@admin.register(User)classUserAdmin(UserAdmin):fieldsets=(('Telegram',{'fields':('telegram_chat_id','telegram_secret','get_telegram_connect_user_help'),}))+UserAdmin.fieldsetsreadonly_fields=['telegram_secret','get_telegram_connect_user_help']+list(UserAdmin.readonly_fields)System ws NotifyAdd thechannelsto yourINSTALLED_APPS:# settings.pyINSTALLED_APPS=[# ...'channels',]Add theREDIS_HOSTandREDIS_PORTvariables andasgiandchannelsconfigurations:# settings.pyREDIS_HOST=os.getenv('REDIS_HOST','localhost')REDIS_PORT=os.getenv('REDIS_PORT',6379)...ASGI_APPLICATION='app.asgi.application'CHANNEL_LAYERS={'default':{'BACKEND':'channels_redis.core.RedisChannelLayer','CONFIG':{"hosts":[(REDIS_HOST,REDIS_PORT)],},},}Edit yourasgi.pyfileimportosfromchannels.authimportAuthMiddlewareStackfromchannels.routingimportProtocolTypeRouter,URLRouterfromdjango.core.asgiimportget_asgi_applicationfromgarpix_notifyimportroutingos.environ.setdefault('DJANGO_SETTINGS_MODULE','app.settings')application=ProtocolTypeRouter({"http":get_asgi_application(),"websocket":AuthMiddlewareStack(URLRouter(routing.websocket_urlpatterns)),})Socket notification examplegroup_name=f'workflow-{user.pk}'Notify.send(settings.MY_NOTIFY,{'message':'my message',},room_name=group_name,user=user)For notifications of the SYSTEM type, a separate non-periodic task is used that works instantly, if room_name is missing system messages will be sent in'room_{id}'where'id'is user idYou can add notifies rotes to your project:urlpatterns=[# ...path('',include(('garpix_notify.urls','garpix_notify'),namespace='garpix_notify')),]ChangelogSeeCHANGELOG.md.ContributingSeeCONTRIBUTING.md.LicenseMIT