Coder Social home page Coder Social logo

django-email-confirm-la's Introduction

django-email-confirm-la

Build Badge

Coverage Badge

Version Badge

Django email confirmation for any Model and any Field.

Requirements

  • Python (2.6, 2.7, 3.3, 3.4)
  • Django (1.4, 1.5, 1.6, 1.7)

Installation

$ pip install django-email-confirm-la

In your settings.py:

Add the email_confirm_la app (put it after your apps) and set the required settings:

INSTALLED_APPS = (
    ...
    'email_confirm_la',
    ...
)

DEFAULT_FROM_EMAIL = '[email protected]'
EMAIL_CONFIRM_LA_HTTP_PROTOCOL = 'http'
EMAIL_CONFIRM_LA_DOMAIN = 'your-domain.com'

If you are using the sites framework, then EMAIL_CONFIRM_LA_DOMAIN can be omitted and Site.objects.get_current().domain will be used.

In your urls.py:

urlpatterns = patterns(
    '',
    url(r'^email_confirmation/', include('email_confirm_la.urls')),
    ...
)

then run

$ python manage.py syncdb
$ python manage.py migrate

Models

For User Model

from django.contrib.auth.models import User
from email_confirm_la.models import EmailConfirmation

user = User.objects.get(username='vinta')
unconfirmed_email = '[email protected]'

email_confirmation = EmailConfirmation.objects.set_email_for_object(
    email=unconfirmed_email,
    content_object=user,
)

For Any Model And Any Field

Assumed you have a model:

from django.db import models
from django.contrib.contenttypes.fields import GenericRelation  # Django 1.7+
from django.contrib.contenttypes.generic import GenericRelation

class YourModel(models.Model):
    ...
    customer_support_email = models.EmailField(max_length=255, null=True, blank=True)
    marketing_email = models.EmailField(max_length=255, null=True, blank=True)
    ...

    # optional, but recommended when you want to perform cascade-deletions
    email_confirmations = GenericRelation('email_confirm_la.EmailConfirmation', content_type_field='content_type', object_id_field='object_id')

And you want to confirm some emails:

from your_app.models import YourModel
from email_confirm_la.models import EmailConfirmation

some_model_instance = YourModel.objects.get(id=42)

email_confirmation = EmailConfirmation.objects.set_email_for_object(
    email='[email protected]',
    content_object=some_model_instance,
    email_field_name='customer_support_email'
)

email_confirmation = EmailConfirmation.objects.set_email_for_object(
    email='[email protected]',
    content_object=some_model_instance,
    email_field_name='marketing_email'
)

Signals

  • post_email_confirmation_send
  • post_email_confirm
  • post_email_save

In your models.py:

from django.dispatch import receiver
from email_confirm_la.signals import post_email_confirm

@receiver(post_email_confirm)
def post_email_confirm_callback(sender, confirmation, **kwargs):
    model_instace = confirmation.content_object
    email = confirmation.email

    do_your_stuff()

Commands

$ python manage.py clear_expired_email_confirmations

Templates

You will want to override the project's email text and confirmation page.

Ensure the email_confirm_la app in INSTALLED_APPS is after the app that you will place the customized templates in so that the django.template.loaders.app_directories.Loader finds your templates before the default templates.

Then copy the templates into your app:

$ cp -R django-email-confirm-la/email_confirm_la/templates/email_confirm_la your_app/templates/email_confirm_la

Finally, modify them:

  • email/email_confirmation_subject.txt: Produces the subject line of the email.
  • email/email_confirmation_message.html: The HTML body of the email.
  • email_confirm_success.html: What the user sees after clicking a confirmation link (on success).
  • email_confirm_fail.html: What the user sees after clicking a confirmation link that has expired or is invalid.

Settings

Default values of app settings:

EMAIL_CONFIRM_LA_EMAIL_BACKEND = settings.EMAIL_BACKEND
EMAIL_CONFIRM_LA_HTTP_PROTOCOL = 'http'
EMAIL_CONFIRM_LA_DOMAIN = ''  # remember to override this setting!
EMAIL_CONFIRM_LA_CONFIRM_EXPIRE_SEC = 60 * 60 * 24 * 1  # 1 day
EMAIL_CONFIRM_LA_CONFIRM_URL_REVERSE_NAME = 'confirm_email'
EMAIL_CONFIRM_LA_SAVE_EMAIL_TO_INSTANCE = True

Run Tests

$ pip install -r requirements_test.txt
$ python setup.py test

# or

$ docker build --rm=true -t djecl .
$ docker run --rm=true djecl

django-email-confirm-la's People

Contributors

joshdata avatar odyx avatar vinta avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.