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

Add compatibility with newer Django versions #2

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions inviter/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from inviter.models import OptOut
from inviter.utils import invite, token_generator
import shortuuid
import urlparse
import urllib.parse



Expand Down Expand Up @@ -95,7 +95,7 @@ def test_opt_out(self):

resp = self.client.post(url, {})
self.assertEqual(302, resp.status_code, resp.status_code)
self.assertEqual(reverse('inviter:opt-out-done'), urlparse.urlparse(resp['Location']).path)
self.assertEqual(reverse('inviter:opt-out-done'), urllib.parse.urlparse(resp['Location']).path)
self.assertEqual(2, User.objects.count())

user, sent = invite("[email protected]", self.inviter)
Expand Down
109 changes: 109 additions & 0 deletions inviter/tests.py.bak
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
"""
This file demonstrates writing tests using the unittest module. These will pass
when you run "manage.py test".

Replace this with more appropriate tests for your application.
"""
from django.conf import settings
from django.contrib.auth import tests
from django.contrib.auth.models import User
from django.core import mail
from django.core.urlresolvers import reverse
from django.test import TestCase
from django.utils.http import int_to_base36
from inviter.models import OptOut
from inviter.utils import invite, token_generator
import shortuuid
import urlparse



class InviteTest(TestCase):
def setUp(self):
self.original_email_backend = settings.EMAIL_BACKEND
settings.EMAIL_BACKEND = 'django.core.mail.backends.locmem.EmailBackend'
self.inviter = User.objects.create(username=shortuuid.uuid())
self.existing = User.objects.create(username=shortuuid.uuid(),
email='[email protected]')

def tearDown(self):
settings.EMAIL_BACKEND = self.original_email_backend

def test_inviting(self):
user, sent = invite("[email protected]", self.inviter)
self.assertTrue(sent)
self.assertFalse(user.is_active)
self.assertEqual(1, len(mail.outbox))
self.assertEqual(3, User.objects.count())

# Resend the mail
user, sent = invite("[email protected]", self.inviter)
self.assertTrue(sent)
self.assertFalse(user.is_active)
self.assertEqual(2, len(mail.outbox))
self.assertEqual(3, User.objects.count())

# Don't resend the mail
user, sent = invite("[email protected]", self.inviter, resend = False)
self.assertFalse(sent)
self.assertFalse(user.is_active)
self.assertEqual(2, len(mail.outbox))
self.assertEqual(3, User.objects.count())

# Don't send the email to active users
user, sent = invite("[email protected]", self.inviter)
self.assertFalse(sent)
self.assertTrue(user.is_active)
self.assertEqual(2, len(mail.outbox))
self.assertEqual(3, User.objects.count())

def test_views(self):
user, sent = invite("[email protected]", self.inviter)
self.assertTrue(sent)
url_parts = int_to_base36(user.id), token_generator.make_token(user)

url = reverse('inviter:register', args=url_parts)

resp = self.client.get(url)

self.assertEqual(200, resp.status_code, resp.status_code)

resp = self.client.post(url, {'username': 'testuser', 'email': '[email protected]',
'new_password1': 'test-1234', 'new_password2': 'test-1234'})

self.assertEqual(302, resp.status_code, resp.content)

self.client.login(username='testuser', password='test-1234')

resp = self.client.get(reverse('inviter:done'))

self.assertEqual(200, resp.status_code, resp.status_code)

def test_opt_out(self):
self.assertEqual(2, User.objects.count())

user, sent = invite("[email protected]", self.inviter)
self.assertTrue(sent)
self.assertEqual(1, len(mail.outbox))
self.assertEqual(3, User.objects.count())

url_parts = int_to_base36(user.id), token_generator.make_token(user)
url = reverse('inviter:opt-out', args=url_parts)

resp = self.client.get(url)
self.assertEqual(200, resp.status_code, resp.status_code)

resp = self.client.post(url, {})
self.assertEqual(302, resp.status_code, resp.status_code)
self.assertEqual(reverse('inviter:opt-out-done'), urlparse.urlparse(resp['Location']).path)
self.assertEqual(2, User.objects.count())

user, sent = invite("[email protected]", self.inviter)
self.assertFalse(sent)
self.assertEqual(1, len(mail.outbox))
self.assertEqual(1, OptOut.objects.count())
self.assertTrue(OptOut.objects.is_blocked("[email protected]"))
self.assertIsNone(user)



12 changes: 7 additions & 5 deletions inviter/tokens.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
from django.conf import settings
from django.utils.crypto import salted_hmac, constant_time_compare


class TokenGenerator(object):
def make_token(self, user):
return self._make_token(user)

def _make_token(self, user):
value = u'-'.join([unicode(user.id), unicode(user.last_login),
value = '-'.join([str(user.id), str(user.last_login),
user.password])
return salted_hmac(settings.SECRET_KEY, value).hexdigest()[::2]

def check_token(self, user, token):
return constant_time_compare(token, self._make_token(user))

generator = TokenGenerator()

generator = TokenGenerator()

16 changes: 16 additions & 0 deletions inviter/tokens.py.bak
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from django.conf import settings
from django.utils.crypto import salted_hmac, constant_time_compare

class TokenGenerator(object):
def make_token(self, user):
return self._make_token(user)

def _make_token(self, user):
value = u'-'.join([unicode(user.id), unicode(user.last_login),
user.password])
return salted_hmac(settings.SECRET_KEY, value).hexdigest()[::2]

def check_token(self, user, token):
return constant_time_compare(token, self._make_token(user))

generator = TokenGenerator()
2 changes: 1 addition & 1 deletion inviter/urls.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from django.conf.urls.defaults import patterns, url
from django.conf.urls import patterns, url
from inviter.views import Register, Done, OptOut, OptOutDone

urlpatterns = patterns('',
Expand Down
63 changes: 32 additions & 31 deletions inviter/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@

def send_invite(invitee, inviter, url=None, opt_out_url=None, **kwargs):
"""
Send the default invitation email assembled from
Send the default invitation email assembled from
``inviter/email/subject.txt`` and ``inviter/email/body.txt``

Both templates will receive all the ``kwargs``.

:param invitee: The invited user
:param inviter: The inviting user
:param url: The invite URL
Expand All @@ -32,68 +32,69 @@ def send_invite(invitee, inviter, url=None, opt_out_url=None, **kwargs):
ctx.update(kwargs)
ctx.update(site=Site.objects.get_current(), url=url)
ctx = template.Context(ctx)

subject_template = kwargs.pop('subject_template', 'inviter/email/subject.txt')
body_template = kwargs.pop('body_template', 'inviter/email/body.txt')

subject = template.loader.get_template(subject_template)
body = template.loader.get_template(body_template)

subject = subject.render(ctx)
body = body.render(ctx)

subject = ' '.join(subject.split('\n')) # No newlines in subject lines allowed

send_mail(subject, body, FROM_EMAIL, [invitee.email])

def invite(email, inviter, 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.

If a user with ``email`` address does not exist:
* Creates a :class:`django.contrib.auth.models.User` object

* Creates a :class:`django.contrib.auth.models.User` object
* Set ``user.email = email``
* Set ``user.is_active = False``
* Set a random password
* Send the invitation email
* Return ``(user, True)``

If a user with ``email`` address exists and ``user.is_active == False``:
* Re-send the invitation

* Re-send the invitation
* Return ``(user, True)``

If a user with ``email`` address exists:

* Don't send the invitation
* Return ``(user, False)``

If the email address is blocked:

* Don't send the invitation
* Return ``(None, False)``

To customize sending, pass in a new ``sendfn`` function as documented by
:attr:`inviter.utils.send_invite`:

::

sendfn = lambda invitee, inviter, **kwargs: 1
invite("[email protected]", request.user, sendfn = sendfn)
invite("[email protected]", request.user, sendfn = sendfn)



:param email: The email address
:param inviter: The user inviting the email address
:param sendfn: An email sending function. Defaults to :attr:`inviter.utils.send_invite`
:param resend: Resend email to users that are not registered yet
:param resend: Resend email to users that are not registered yet
"""
if OptOut.objects.is_blocked(email):

if OptOut.objects.is_blocked(email):
return None, False
try:
user = User.objects.get(email=email)
# Por si existen muchos usuarios con el mismo email
user = User.objects.filter(email=email).first()
if user.is_active:
return user, False
if not resend:
Expand All @@ -106,13 +107,13 @@ def invite(email, inviter, sendfn=send_invite, resend=True, **kwargs):
)
user.set_unusable_password()
user.save()

url_parts = int_to_base36(user.id), token_generator.make_token(user)
url = reverse('inviter:register', args=url_parts)

opt_out_url = reverse('inviter:opt-out', args=url_parts)
kwargs.update(opt_out_url=opt_out_url)

sendfn(user, inviter, url=url, **kwargs)
return user, True