Skip to content

Commit

Permalink
Cleaned up formatting.
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenmcd committed Sep 8, 2011
1 parent 0a9015e commit 6adaa7c
Show file tree
Hide file tree
Showing 4 changed files with 207 additions and 167 deletions.
94 changes: 66 additions & 28 deletions README.rst
@@ -1,56 +1,94 @@
Introduction
------------
============

A Django reusable app providing the ability to send PGP encrypted and multipart emails using the Django templating system. These features can be used together or separately. When configured to send PGP encrypted email, the ability for Admin users to manage PGP keys is also provided.
django-email-extras is a Django reusable app providing the
ability to send PGP encrypted and multipart emails using
Django templates. These features can be used together or
separately. When configured to send PGP encrypted email,
the ability for Admin users to manage PGP keys is also
provided.

Dependencies
------------
============

* `python-gnupg <http://code.google.com/p/python-gnupg/>`_ is required for sending PGP encrypted email.
* `python-gnupg <http://code.google.com/p/python-gnupg/>`_ is
required for sending PGP encrypted email.

Installation
------------
============

Checkout the source and run ``python setup.py install``. You can then add ``email_extras`` to your ``INSTALLED_APPS``.
Checkout the source and run ``python setup.py install``. You can
then add ``email_extras`` to your ``INSTALLED_APPS``.

How It Works
------------
============

There are two functions for sending email in the ``email_extras.utils`` module:
There are two functions for sending email in the ``email_extras.utils``
module:

* ``send_mail``
* ``send_mail``
* ``send_mail_template``

* ``send_mail_template``
The former mimics the signature of ``django.core.mail.send_mail``
while the latter provides the ability to send multipart emails
using the Django templating system. If configured correctly, both
these functions will PGP encrypt emails as described below.

The former mimics the signature of ``django.core.mail.send_mail`` while the latter provides the ability to send multipart emails using the Django templating system. If configured correctly, both these functions will PGP encrypt emails as described below.

Sending PGP encrypted email
^^^^^^^^^^^^^^^^^^^^^^^^^^^
Sending PGP Encrypted Email
---------------------------

`PGP explanation <http://en.wikipedia.org/wiki/Pretty_Good_Privacy>`_

Using `python-gnupg <http://code.google.com/p/python-gnupg/>`_, two
models are defined in ``email_extras.models`` - ``Key`` and ``Address``
which represent a PGP key and an email address for a successfully
imported key. These models exist purely for the sake of importing
keys and removing keys for a particular address via the Django
Admin.

Using `python-gnupg <http://code.google.com/p/python-gnupg/>`_, two models are defined in ``email_extras.models`` - ``Key`` and ``Address`` which represent a PGP key and an email address for a successfully import key. These models exist purely for the sake of importing keys and removing keys for a particular address via the Django Admin. When adding a key, the key is imported into the key ring on the server and the instance of the ``Key`` model is not saved. The email address for the key is also extracted and saved as an ``Address`` instance. The ``Address`` model is then used when sending email to check for an existing key to determine whether an email should be encrypted. When an ``Address`` is deleted via the Django Admin, the key is removed from the key ring on the server.
When adding a key, the key is imported into the key ring on
the server and the instance of the ``Key`` model is not saved. The
email address for the key is also extracted and saved as an
``Address`` instance.

Sending multipart email with the Django templating system
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
The ``Address`` model is then used when sending email to check for
an existing key to determine whether an email should be encrypted.
When an ``Address`` is deleted via the Django Admin, the key is
removed from the key ring on the server.

As mentioned above, the following function is provided in the ``email_extras.utils`` module:
Sending Multipart Email with Django Templates
---------------------------------------------

``send_mail_template(subject, template, addr_from, addr_to, fail_silently=False, attachments=None, context=None)``
As mentioned above, the following function is provided in
the ``email_extras.utils`` module::

The arguments that differ from ``django.core.mail.send_mail`` are ``template`` and ``context``. The ``template`` argument is simply the name of the template to be used for rendering the email contents. A template consists of both a HTML file and a TXT file each responsible for their respective versions of the email and should be stored in the ``email_extras`` directory where your templates are stored, therefore if the name ``contact_form`` was given for the ``template`` argument, the two template files for the email would be:
send_mail_template(subject, template, addr_from, addr_to, fail_silently=False, attachments=None, context=None)

``templates/email_extras/contact_form.html``
``templates/email_extras/contact_form.txt``
The arguments that differ from ``django.core.mail.send_mail`` are
``template`` and ``context``. The ``template`` argument is simply
the name of the template to be used for rendering the email contents.

The ``context`` argument is simply a dictionary that is used to populate the email templates, much like a normal request context would be used for a regular Django template.
A template consists of both a HTML file and a TXT file each responsible
for their respective versions of the email and should be stored in
the ``email_extras`` directory where your templates are stored,
therefore if the name ``contact_form`` was given for the ``template``
argument, the two template files for the email would be:

Configuration
-------------
* ``templates/email_extras/contact_form.html``
* ``templates/email_extras/contact_form.txt``

There are two settings you can configure in your project's ``settings`` module:
The ``context`` argument is simply a dictionary that is used to
populate the email templates, much like a normal request context
would be used for a regular Django template.

Configuration
=============

* ``EMAIL_EXTRAS_USE_GNUPG`` - Boolean that controls whether the PGP encryption features are used. Defaults to True if ``EMAIL_EXTRAS_GNUPG_HOME`` is specified, otherwise False.
* ``EMAIL_EXTRAS_GNUPG_HOME`` - String representing a custom location for the GNUPG keyring.
There are two settings you can configure in your project's
``settings`` module:

* ``EMAIL_EXTRAS_USE_GNUPG`` - Boolean that controls whether the PGP
encryption features are used. Defaults to ``True`` if
``EMAIL_EXTRAS_GNUPG_HOME`` is specified, otherwise ``False``.
* ``EMAIL_EXTRAS_GNUPG_HOME`` - String representing a custom location
for the GNUPG keyring.
53 changes: 26 additions & 27 deletions email_extras/admin.py
Expand Up @@ -6,30 +6,29 @@


if USE_GNUPG:
from gnupg import GPG
from email_extras.models import Key, Address
from email_extras.utils import addresses_for_key

class KeyAdmin(admin.ModelAdmin):

def save_model(self, request, obj, form, change):
"""
import the key and parse the addresses from it and save them
and omit the super save_model call so as to never save the key instance
"""
gpg = GPG(gnupghome=GNUPG_HOME)
result = gpg.import_keys(obj.key)
if result.count == 0:
raise forms.ValidationError("Invalid Key")
else:
addresses = []
for key in result.results:
addresses.extend(addresses_for_key(gpg, key))
obj.addresses = ",".join(addresses)
for address in addresses:
Address.objects.get_or_create(address=address)

admin.site.register(Key, KeyAdmin)
admin.site.register(Address)


from gnupg import GPG
from email_extras.models import Key, Address
from email_extras.utils import addresses_for_key

class KeyAdmin(admin.ModelAdmin):

def save_model(self, request, obj, form, change):
"""
Import the key and parse the addresses from it and save
them, and omit the super save_model call so as to never
save the key instance.
"""
gpg = GPG(gnupghome=GNUPG_HOME)
result = gpg.import_keys(obj.key)
if result.count == 0:
raise forms.ValidationError("Invalid Key")
else:
addresses = []
for key in result.results:
addresses.extend(addresses_for_key(gpg, key))
obj.addresses = ",".join(addresses)
for address in addresses:
Address.objects.get_or_create(address=address)

admin.site.register(Key, KeyAdmin)
admin.site.register(Address)
78 changes: 40 additions & 38 deletions email_extras/models.py
Expand Up @@ -5,41 +5,43 @@


if USE_GNUPG:
from gnupg import GPG

class Key(models.Model):
"""
accepts a key and imports it via admin's save_model which omits saving
"""

key = models.TextField()
addresses = models.TextField(editable=False)

def __unicode__(self):
return self.addresses


class Address(models.Model):
"""
stores the address for a successfully imported key and allows deletion
"""

class Meta:
verbose_name_plural = "Addresses"

address = models.CharField(max_length=200)

def __unicode__(self):
return self.address

def delete(self):
"""
remove any keys for this address
"""
from email_extras.utils import addresses_for_key
gpg = GPG(gnupghome=GNUPG_HOME)
for key in gpg.list_keys():
if self.address in addresses_for_key(gpg, key):
gpg.delete_keys(key["fingerprint"], True)
gpg.delete_keys(key["fingerprint"])
super(Address, self).delete()
from gnupg import GPG

class Key(models.Model):
"""
Accepts a key and imports it via admin's save_model which
omits saving.
"""

key = models.TextField()
addresses = models.TextField(editable=False)

def __unicode__(self):
return self.addresses


class Address(models.Model):
"""
Stores the address for a successfully imported key and allows
deletion.
"""

class Meta:
verbose_name_plural = "Addresses"

address = models.CharField(max_length=200)

def __unicode__(self):
return self.address

def delete(self):
"""
Remove any keys for this address.
"""
from email_extras.utils import addresses_for_key
gpg = GPG(gnupghome=GNUPG_HOME)
for key in gpg.list_keys():
if self.address in addresses_for_key(gpg, key):
gpg.delete_keys(key["fingerprint"], True)
gpg.delete_keys(key["fingerprint"])
super(Address, self).delete()

0 comments on commit 6adaa7c

Please sign in to comment.