Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Django 4 compatible #74

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
21 changes: 17 additions & 4 deletions simple_email_confirmation/__init__.py
Original file line number Diff line number Diff line change
@@ -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'
))
1 change: 1 addition & 0 deletions simple_email_confirmation/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
15 changes: 15 additions & 0 deletions simple_email_confirmation/apps.py
Original file line number Diff line number Diff line change
@@ -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'
2 changes: 1 addition & 1 deletion simple_email_confirmation/migrations/0001_initial.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
16 changes: 12 additions & 4 deletions simple_email_confirmation/models.py
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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.

Expand Down
13 changes: 8 additions & 5 deletions simple_email_confirmation/signals.py
Original file line number Diff line number Diff line change
@@ -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()