Skip to content

Commit

Permalink
get_user_model is the one-true-way now
Browse files Browse the repository at this point in the history
  • Loading branch information
mjschultz committed Apr 15, 2016
1 parent d4b7914 commit c373b4f
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 48 deletions.
10 changes: 4 additions & 6 deletions inviter2/tests.py
Expand Up @@ -10,12 +10,7 @@

from six.moves.urllib.parse import urlparse

try:
from django.contrib.auth import get_user_model
except ImportError:
from django.contrib.auth.models import User
else:
User = get_user_model()
from django.contrib.auth import get_user_model
from django.core import mail
from django.core.urlresolvers import reverse
from django.http import Http404
Expand All @@ -27,6 +22,9 @@
from .views import UserMixin


User = get_user_model()


class InviteTest(TestCase):
def setUp(self):
self.required_fields = getattr(User, 'REQUIRED_FIELDS', [])
Expand Down
27 changes: 12 additions & 15 deletions inviter2/utils.py
Expand Up @@ -4,12 +4,7 @@

from django import template
from django.conf import settings
try:
from django.contrib.auth import get_user_model
except ImportError:
from django.contrib.auth.models import User
else:
User = get_user_model()
from django.contrib.auth import get_user_model
from django.core.mail import send_mail
from django.core.urlresolvers import reverse
from django.utils.http import int_to_base36
Expand All @@ -18,27 +13,28 @@
from .views import import_attribute, TOKEN_GENERATOR


User = get_user_model()
FROM_EMAIL = getattr(settings, 'INVITER_FROM_EMAIL',
settings.DEFAULT_FROM_EMAIL)

token_generator = import_attribute(TOKEN_GENERATOR)


def create_inactive_user(**initials):
"""
Create user with ``is_active`` se to ``False`` and a random password
"""
"""Create user with ``is_active`` se to ``False`` and a random password."""
initials.update(is_active=False)
user = User.objects.create(**initials)
user.set_unusable_password()
user.save()
return user


def send_invite(invitee, inviter, url=None, opt_out_url=None, **kwargs):
"""
Send the default invitation email assembled from
``inviter2/email/subject.txt`` and ``inviter2/email/body.txt``
Send the default invitation email.
Assembled from ``inviter2/email/subject.txt`` and
``inviter2/email/body.txt``
Both templates will receive all the ``kwargs``.
Expand Down Expand Up @@ -75,8 +71,10 @@ def send_invite(invitee, inviter, url=None, opt_out_url=None, **kwargs):
def invite(email, inviter, user=None, sendfn=send_invite, resend=True,
**kwargs):
"""
Invite a given email address and return a ``(User, sent)`` tuple similar
to the Django :meth:`django.db.models.Manager.get_or_create` method.
Invite a given email address.
Returns a ``(User, sent)`` tuple similar to the Django
:meth:`django.db.models.Manager.get_or_create` method.
If a user is passed in, reinvite the user. For projects that support
multiple users with the same email address, it is necessary to pass in the
Expand Down Expand Up @@ -122,7 +120,6 @@ def invite(email, inviter, user=None, sendfn=send_invite, resend=True,
:attr:`inviter2.utils.send_invite`
:param resend: Resend email to users that are not registered yet
"""

if OptOut.objects.is_blocked(email):
return None, False
try:
Expand Down
45 changes: 18 additions & 27 deletions inviter2/views.py
Expand Up @@ -3,12 +3,7 @@
import importlib

from django.conf import settings
try:
from django.contrib.auth import get_user_model
except ImportError:
from django.contrib.auth.models import User
else:
User = get_user_model()
from django.contrib.auth import get_user_model
from django.core.urlresolvers import reverse
from django.http import Http404, HttpResponseRedirect, HttpResponseForbidden
from django.utils.http import base36_to_int
Expand All @@ -17,6 +12,7 @@
from .forms import OptOutForm


User = get_user_model()
FORM = getattr(settings, 'INVITER_FORM', 'inviter2.forms.RegistrationForm')
INVITER_FORM_USER_KWARG = getattr(
settings, 'INVITER_FORM_USER_KWARG', 'instance'
Expand All @@ -34,9 +30,7 @@


def import_attribute(path):
"""
Import an attribute from a module.
"""
"""Import an attribute from a module."""
module = '.'.join(path.split('.')[:-1])
function = path.split('.')[-1]

Expand All @@ -45,8 +39,7 @@ def import_attribute(path):


class UserMixin(object):
""" Handles retrieval of users from the token and does a bit of access
management. """
"""Handle retrieval of users from the token."""

token_generator = import_attribute(TOKEN_GENERATOR)

Expand All @@ -60,8 +53,9 @@ def get_user(self, uidb36):

def dispatch(self, request, uidb36, token, *args, **kwargs):
"""
Overriding the default dispatch method on Django's views to do
some token validation and if necessary deny access to the resource.
Override the dispatch method to do token validation.
If necessary this will deny access to the resource.
Also passes the user as first argument after the request argument
to the handler method.
Expand All @@ -77,11 +71,13 @@ def dispatch(self, request, uidb36, token, *args, **kwargs):

class Register(UserMixin, TemplateView):
"""
A registration view for invited users. The user model already exists - this
view just takes care of setting a password and username, and maybe update
the email address. Anywho - one can customize the form that is used.
A registration view for invited users.
The user model already exists - this view just takes care of setting a
password and username, and maybe update the email address. Anywho - one
can customize the form that is used.
"""

template_name = INVITER_FORM_TEMPLATE
form = import_attribute(FORM)

Expand All @@ -90,21 +86,13 @@ def redirect_url(self):
return getattr(settings, 'INVITER_REDIRECT', 'inviter2:done')

def get(self, request, user):
"""
Unfortunately just a copy of
:attr:`django.contrib.auth.views.password_reset_confirm`
"""
context = {
'invitee': user,
'form': self.form(**{INVITER_FORM_USER_KWARG: user})
}
return self.render_to_response(context)

def post(self, request, user):
"""
Unfortunately just a copy of
:attr:`django.contrib.auth.views.password_reset_confirm`
"""
form = self.form(**{
INVITER_FORM_USER_KWARG: user,
'data': request.POST
Expand All @@ -127,9 +115,12 @@ def get(self, request):


class OptOut(UserMixin, TemplateView):
""" We want to give the user also the option to *not* receive any
invitations anymore, which is happening in this view and
:class:`inviter2.forms.OptOutForm`. """
"""
Give the user also the option to *not* receive any invitations anymore.
Which is happening in this view and :class:`inviter2.forms.OptOutForm`.
"""

template_name = INVITER_OPTOUT_TEMPLATE

def get(self, request, user):
Expand Down

0 comments on commit c373b4f

Please sign in to comment.