Package: django-entity-emailer
Django Entity EmailerDo you:UseDjango-Entity-Event?Want to have emailing as another medium for entity events?Want a record of emails sent?Want automatic assurance that you don’t accidentally send hundreds
of emails over the course of a few minutes?Then use Django Entity Emailer!InstallationThis package can currently be installed by downloading and installing
from source:git clone
python setup.py installComing soon:pip install.Setup and ConfigurationIn order to use django-entity-emailer, you must be mirroring entities
using thedjango-entityframework.
Additionally, in order to send email to entities, those
entities must include a value for the key'email'in theirentity_metafield.If both of those conditions are true, setup is fairly straightforward:Addentity_emailertoINSTALLED_APPS.Either set a value forsettings.ENTITY_EMAILER_FROM_EMAIL, or be
sure that thesettings.DEFAULT_FROM_EMAILis set to an
appropriate value.Ensure that all the dependencies are installed and listed inINSTALLED_APPSpip:django-db-mutex, INSTALLED_APPS:db_mutexpip:django-entity-subscription, INSTALLED_APPS:entity_subscriptionAdd the scheduled email task to yourCELERYBEAT_SCHEDULE(see
configuring celery section).Runpython manage.py syncdbandpython manage.py migrateEnsure that a email medium is set up by runningpython manage.py add_email_medium.When sending an email, django-entity-emailer will first check if theENTITY_EMAILER_FROM_EMAILexists. If it does, it will use that value
in the email’s ‘from’ field, otherwise it will fall back to the value
set inDEFAULT_FROM_EMAIL.Finally, django-entity-emailer is an installable medium that is used withdjango-entity-event. This libary makes it easy for developers and
users to manage what sorts of notifications users recieve over various
mediums. However, it does require some configuration. For a simple emailer configuration,
see the ‘Basic entity-subscription configuration’ section.Getting'email'into'entity_meta'The requirement that entities be mirrored with an'email'field in
theirentity_metais not difficult.After installing django-entity, it is as simple as creating a model
inheriting fromentity.BaseEntityModel, with aget_entity_metathat returns the email along with any other data to be mirrored. A
simple example could be:fromdjango.dbimportmodelsfromentityimportBaseEntityModelclassAccount(BaseEntityModel)username=models.CharField(max_length=64)email=models.CharField(max_length=254)defget_entity_meta(self):return{'email':self.email,'username':self.username}Also note that it is not necessary for every mirrored entity to
include an email, only those entities that will actually be sent
emails need to have emails mirrored in theirentity_meta.For a more complete description of how entity mirroring works, see the
documentation for django-entity.Basic entity-event configurationIn order to ensure that users of your site will not recieve emails
that they don’t want to recieve, the entity-emailer application ties
in to theentity-eventframework. As a developer it is up to
you to expose the ability for users to subscribe and unsubscribe from
emails. Here, we will show the basic configuration required to start
sending emails.Runningmanage.py add_email_mediumwill add the medium that
entity-emailer relies on to send emails. We must also have a source of
emails, and a subscription to that combination of email and source.fromentity_emailerimportget_mediumfromentity_event.modelsimportSource,Subscriptionfromentity.modelsimportEntity,EntityKindsuper_entity=Entity.objects.get_for_obj(my_group_object)user_entity_kind=EntityKind.objects.get(name='myusermodel')email_medium=get_medium()admin_source=Source.objects.create(name='admin',display_name='Admin Notifications',description='Important notifications for the site Admin.',)Subscription.objects.create(source=admin_source,medium=email_medium,entity=super_entity,subentity_kind=user_entity_kind)Along with this, you will need to associate the email medium with aRenderingStyleobject in entity event so that it can perform email
rendering. More about this in the next section.Django Entity Emailer must know the email addresses of entities and assumes that an
email address has been mirrored by default in the entity metadata. By default, it
uses the “email” metadata key, but this can be overridden by setting aENTITY_EMAILER_EMAIL_KEYin the settings.Django Entity Emailer also has the ability to exclude certain entities from ever
being emailed. In order to do this, mirror metadata that whenNoneorFalsemeans that the entity should never be emailed. Then set theENTITY_EMAILER_EXCLUDE_KEYsetting to the key of this metadata.Sending an Email about an EventSending an email is as simple as saving an event to the database
and subscribing to the email medium after templates are defined for the
email. The entity emailer will go through
the events, send out emails to the subscribed targets, and mark the
events as seen so that duplicate emails are never sent.For example, let’s say that we wish to be notified via email when a user
logs into a site. Assuming that the email medium and admin sources are setup
from our previous examples, we can make an email template (login.html) that looks like the
following:{{user}}justloggedin!We then set up a rendering style and a context renderer for this template so that
emails can be rendered:fromentity_event.modelsimportRenderingStyle,ContextRendererstyle=RenderingStyle.objects.create(name='email')ContextRenderer.objects.create(rendering_style=style,source=admin_source,html_template_path='templates/login.html',)When the context renderer is in place, the email medium will need to be updated to point
to the appropriate rendering style we want to use. To continue our example:email_medium.rendering_style=styleemail_medium.save()Once we have the rendering style in place, assume an Event is created with the following context:{'user':'User name'}When this happens, an email will be sent to the subscribed user that says ‘User name just logged in!’.The subject line of this email will use the first 40 characters from the rendered email template. However,
if one specifies a <title> HTML tag in their template, the contents of the tag will be used as the
email subject.For more detailed information on event rendering, checkoutdjango-entity-event.UnsubscribingUsers may want to be able to unsubscribe from certain types of
emails. This is easy in django-entity-emailer. Emails can be
unsubscribed from by individual sources, by using the
entity-subscription framework.fromentity_emailerimportget_mediumfromentity_eventimportSource,Unsubscribeadmin_emails=Source.objects.get(name='admin')Unsubscribe.objects.create(entity=entity_of_user_to_unsub,source=admin_emailsmedium=get_medium())This user will be excluded both from receiving emails of this type
that were sent to them individually, or as part of a group email.Showing Emails in the BrowserUsers may view emails in a browser with this application. This is accomplished by including
theentity_emailerurls into the Django project and providing theview_uidof the email as the url argument.
The url view will use the text/html templates of the email to render it as a web page.Release Notes0.9.0Added Django 1.8 support and dropped 1.6 support0.8.4Added the abilty to override the email key in entity metadata.Added the ability to exlude entities from being emailed based on a metadata key.0.8.1Added Django 1.7 supportAdded Python 3.4 support0.7.1Squashed entity emailer migrations and removed entity subscription dependency.0.7Converted entity emailer to solely be a medium for entity event.0.6Added arecipientsfield to theEmailmodel and removed thesend_tofield. This allows the userto provide more than one receiver (or group of receivers) for the email.0.5Added acontext_loaderfield on theEmailTemplatemodel. This function allows a user to provide a functionpath that for fetching and returning data from the storedEmailcontext.Added a basicEmailViewand urls for rendering emails through a Django view.0.4Updated to useEntityKindmodels rather thanContentTypemodels for specifying entity groups.A schema migration to remove the oldsubentity_typefield while adding the newsubentity_kindfield were added so that users may make appropriate data migrations. Note that it is up to the
user to write the appropriate data migration for converting entity types to entity kinds.