diff --git a/simple_email_confirmation/__init__.py b/simple_email_confirmation/__init__.py index 0bc25cd..14aecd3 100644 --- a/simple_email_confirmation/__init__.py +++ b/simple_email_confirmation/__init__.py @@ -1,13 +1,26 @@ -__version__ = '0.12' +__version__ = '0.13' __all__ = [ - 'SimpleEmailConfirmationUserMixin', - 'EmailAddress', 'email_confirmed', 'unconfirmed_email_created', 'primary_email_changed', ] -from .models import SimpleEmailConfirmationUserMixin, EmailAddress +import django +from django.apps import apps +from django.conf import settings + from .signals import ( email_confirmed, unconfirmed_email_created, primary_email_changed, ) + +if django.get_version() < '4': + default_app_config = 'simple_email_confirmation.apps.EmailAddressConfig' + + +def get_email_address_model(): + """Convenience method to return the email model being used.""" + return apps.get_model(getattr( + settings, + 'SIMPLE_EMAIL_CONFIRMATION_EMAIL_ADDRESS_MODEL', + 'simple_email_confirmation.EmailAddress' + )) diff --git a/simple_email_confirmation/admin.py b/simple_email_confirmation/admin.py index 7df7926..529671a 100644 --- a/simple_email_confirmation/admin.py +++ b/simple_email_confirmation/admin.py @@ -7,4 +7,5 @@ class EmailAddressAdmin(admin.ModelAdmin): list_display = ('user', 'email', 'key', 'set_at', 'confirmed_at') search_fields = ('email', 'key') + admin.site.register((EmailAddress,), EmailAddressAdmin) diff --git a/simple_email_confirmation/apps.py b/simple_email_confirmation/apps.py new file mode 100644 index 0000000..4939e75 --- /dev/null +++ b/simple_email_confirmation/apps.py @@ -0,0 +1,15 @@ +"""Application configuration.""" + +from django.apps import AppConfig +from django.utils.translation import gettext_lazy as _ + +__all__ = ['EmailAddressConfig'] + + +class EmailAddressConfig(AppConfig): + """Default configuration for the simple_email_confirmation app.""" + + name = 'simple_email_confirmation' + label = 'simple_email_confirmation' + verbose_name = _('EmailAddress') + default_auto_field = 'django.db.models.AutoField' diff --git a/simple_email_confirmation/migrations/0001_initial.py b/simple_email_confirmation/migrations/0001_initial.py index 8b9d130..9b99c82 100644 --- a/simple_email_confirmation/migrations/0001_initial.py +++ b/simple_email_confirmation/migrations/0001_initial.py @@ -21,7 +21,7 @@ class Migration(migrations.Migration): ('key', models.CharField(unique=True, max_length=40)), ('set_at', models.DateTimeField(default=django.utils.timezone.now, help_text='When the confirmation key expiration was set')), ('confirmed_at', models.DateTimeField(help_text='First time this email was confirmed', null=True, blank=True)), - ('user', models.ForeignKey(related_name='email_address_set', to=settings.AUTH_USER_MODEL)), + ('user', models.ForeignKey(related_name='email_address_set', to=settings.AUTH_USER_MODEL, on_delete=models.CASCADE)), ], options={ 'verbose_name_plural': 'email addresses', diff --git a/simple_email_confirmation/models.py b/simple_email_confirmation/models.py index 5501162..f35c20f 100644 --- a/simple_email_confirmation/models.py +++ b/simple_email_confirmation/models.py @@ -1,12 +1,19 @@ from __future__ import unicode_literals +from random import randint + +import django from django.conf import settings from django.contrib.auth import get_user_model from django.db import models from django.db.models.signals import post_save -from django.utils.crypto import get_random_string from django.utils import timezone -from django.utils.translation import ugettext_lazy as _ +from django.utils.crypto import get_random_string + +if django.get_version() > '4': + from django.utils.translation import gettext_lazy as _ +else: + from django.utils.translation import ugettext_lazy as _ from .exceptions import ( EmailConfirmationExpired, EmailIsPrimary, EmailNotConfirmed, @@ -155,7 +162,7 @@ class EmailAddressManager(models.Manager): def generate_key(self): "Generate a new random key and return it" # sticking with the django defaults - return get_random_string() + return get_random_string(length=randint(15, 20)) def create_confirmed(self, email, user=None): "Create an email address in the confirmed state" @@ -204,7 +211,7 @@ class EmailAddress(models.Model): "An email address belonging to a User" user = models.ForeignKey( - settings.AUTH_USER_MODEL, related_name='email_address_set', + settings.AUTH_USER_MODEL, related_name='email_address_set', on_delete=models.CASCADE ) email = models.EmailField(max_length=255) key = models.CharField(max_length=40, unique=True) @@ -279,6 +286,7 @@ def auto_add(sender, **kwargs): else: user.email_address_set.create_unconfirmed(email) + # TODO: try to only connect this to the User model. We can't use # get_user_model() here - results in import loop. diff --git a/simple_email_confirmation/signals.py b/simple_email_confirmation/signals.py index f45ec28..354596c 100644 --- a/simple_email_confirmation/signals.py +++ b/simple_email_confirmation/signals.py @@ -1,7 +1,10 @@ from django.dispatch import Signal -email_confirmed = Signal(providing_args=['user', 'email']) -unconfirmed_email_created = Signal(providing_args=['user', 'email']) -primary_email_changed = Signal( - providing_args=['user', 'new_email', 'old_email'], -) +try: + email_confirmed = Signal(providing_args=['user', 'email']) + unconfirmed_email_created = Signal(providing_args=['user', 'email']) + primary_email_changed = Signal(providing_args=['user', 'new_email', 'old_email']) +except TypeError: + email_confirmed = Signal() + unconfirmed_email_created = Signal() + primary_email_changed = Signal()