Package: django-ilmoitin
django-ilmoitinA templated Django messaging libraryInstallationpip install django-ilmoitinAdddjango_ilmoitintoINSTALLED_APPS.Run migrationspythonmanage.pymigrateilmoitinUse version 0.7.0 for a project based on Django >= 3.2 and Python >= 3.8For older Python versions, use version 0.6.0.Use version 0.5.x for a project based on Django 2.x.If you need to make changes todjango-ilmoitinand your project uses Django 2.x, add your changes to the branchstable/0.5.xand then make a new 0.5.x release from it.Usagedjango-ilmoitinusesdjango-mailerto send emails, so you need to configure theMAILER_EMAIL_BACKENDsetting to letdjango-mailerknow, how to actually
send the mail:MAILER_EMAIL_BACKEND="your.actual.EmailBackend"Define default from address in settingsDEFAULT_FROM_EMAIL="Ilmoitin <ilmoitin@example.com>"In case you need translated from addresses, those can be defined likeILMOITIN_TRANSLATED_FROM_EMAIL:{"fi":"Yrjö <ilmoitin@example.com>","en":"George <ilmoitin@example.com>",}The value fromDEFAULT_FROM_EMAILwill be used for languages not defined in that dict.Create anotifications.pyfile in django app and register your notification types:fromdjango_ilmoitin.registryimportnotificationsnotifications.register("event_created","Event created")notifications.register("event_deleted","Event deleted")Create adummy_context.pyfile in django app and add dummy context data.
Either use the codes of notifications that you registered in the previous step, or
use the constCOMMON_CONTEXTto make some variables available for all templates:fromdjango_ilmoitin.dummy_contextimportCOMMON_CONTEXT,dummy_contextfrom.modelsimportMyModelmy_object=MyModel(foo="bar")dummy_context.update({COMMON_CONTEXT:{"my_object":my_object},"event_created":{"foo":"bar"},"event_deleted":{"fizz":"buzz"}})Import notifications and dummy context in your apps.py:fromdjango.appsimportAppConfigclassExampleConfig(AppConfig):name="example"defready(self):importexample.notificationsimportexample.dummy_contextGo to django admin and add notification templates to your notificationsSend notifications. List of attachment files can be passed as last optional argument:fromdjango_ilmoitin.utilsimportsend_notificationcontext={"foo":"bar",}attachment="test.txt","foo bar","text/plain"send_notification("foo@bar.com","event_created",context,[attachment])By default, notifications will be sent immediately, if you only want to add notification to the message queue
and send it later, configureILMOITIN_QUEUE_NOTIFICATIONS:ILMOITIN_QUEUE_NOTIFICATIONS=TrueUsing the GraphQL APIThe package provides an optional GraphQL API that requires a workinggrapheneAPI
to work, and it needs additional dependencies.To install them, run:pip install django-ilmoitin[graphql_api]Add theQueryto the entrypoint where you build your schema:# my_app/schema.pyimportdjango_ilmoitin.api.schemaasdjango_ilmoitin_schemaclassQuery(# other extended classesdjango_ilmoitin_schema.Query,graphene.ObjectType,):passAdding authentication to the queriesAll the queries are public by default. The way to protect them is to override the resolvers on your app and call the "parent" query on the new resolver.An example of how to protect a query would be as follows:classQuery(# other extended classesdjango_ilmoitin_schema.Query,graphene.ObjectType,):@staticmethod@login_requireddefresolve_notification_templates(parent,info,**kwargs):returndjango_ilmoitin_schema.Query.resolve_notification_templates(parent,info,**kwargs)If you need more specific permission checking, you can also doclassQuery(# other extended classesdjango_ilmoitin_schema.Query,graphene.ObjectType,):@staticmethoddefresolve_notification_templates(parent,info,**kwargs):user=info.context.userifuser.has_perms(["very_specific_permission"]):returndjango_ilmoitin_schema.Query.resolve_notification_templates(parent,info,**kwargs)raisePermissionError("User not authorised")Code formatThis project usesblackfor Python code formatting.
We follow the basic config, without any modifications. Basicblackcommands:To letblackdo its magic:black .To see which filesblackwould change:black --check .Troubleshooting guideCannot receive email even though it was sent successfullySome strict spam filter might mark email as spam if its Message-ID header has suspicious domain name (e.g158431519447.10.15335486611387428798@qa-staging-i09m9b-staging-77bd999444-p2497)This is because Python tries to generate messsage id base on the FQDN of the local machine before sending email
. Fortunately most of Email Sending services (Mailgun, MailChimp, Sendgrid,..) have a way to generate a reliable
message-id that will likely pass spam filter, so we better let them do it.If you are usingdjango-anymailas the email backend, there is an easy way to remove the auto-generated Message
ID usingpre_sendsignalExample:fromanymail.signalsimportpre_send@receiver(pre_send)defremove_message_id(sender,message,**kwargs):message.extra_headers.pop("Message-ID",None)Note that it only works if you are usingdjango-anymailas your email backend