diff --git a/src/postorius/forms.py b/src/postorius/forms.py index 8e1203c..b25458e 100644 --- a/src/postorius/forms.py +++ b/src/postorius/forms.py @@ -772,17 +772,7 @@ raise forms.ValidationError(_('This email is in use.' 'Please choose another or contact the administrator'), 'error') - # Check if there is a pending ConfirmationProfile - found_confirmation_profile = False - for profile in AddressConfirmationProfile.objects.filter(email=email): - if profile.is_expired: - profile.delete() - else: - found_confirmation_profile = True - if found_confirmation_profile: - raise forms.ValidationError(_('An Activation email has been sent to that address,' - ' use this email or contact the administrator'), 'error') # Check if the email is attached to a user in Mailman try: utils.get_client().get_user(email) diff --git a/src/postorius/models.py b/src/postorius/models.py index 043c2bc..a974e43 100644 --- a/src/postorius/models.py +++ b/src/postorius/models.py @@ -257,7 +257,7 @@ """ email = models.EmailField() activation_key = models.CharField(max_length=40) - created = models.DateTimeField() + created = models.DateTimeField(auto_now=True) user = models.ForeignKey(User) objects = AddressConfirmationProfileManager() diff --git a/src/postorius/tests/mailman_api_tests/test_address_activation.py b/src/postorius/tests/mailman_api_tests/test_address_activation.py index 0790ab8..397bc74 100644 --- a/src/postorius/tests/mailman_api_tests/test_address_activation.py +++ b/src/postorius/tests/mailman_api_tests/test_address_activation.py @@ -56,26 +56,15 @@ self.assertTrue(form.is_valid()) def test_email_used_by_django_auth_is_invalid(self): - """ - No need for cassette becuase we should check mailman last since it's the most expensive - """ + # No need for cassette becuase we should check mailman last since it's the most expensive form = AddressActivationForm({'email': 'les@example.org',}) self.assertFalse(form.is_valid()) def test_invalid_email_is_not_valid(self): - """ - No need for cassette becuase we should check mailman last since it's the most expensive - """ + # No need for cassette becuase we should check mailman last since it's the most expensive form = AddressActivationForm({'email': 'les@example',}) self.assertFalse(form.is_valid()) - def test_email_used_by_confirmation_profile_is_invalid(self): - """ - No need for cassette becuase we should check mailman last since it's the most expensive - """ - form = AddressActivationForm({'email': 'les2@example.org',}) - self.assertFalse(form.is_valid()) - @MM_VCR.use_cassette('test_address_activation.yaml') def test_email_used_by_expired_confirmation_profile_is_valid(self): form = AddressActivationForm({'email': 'expired@example.org',}) @@ -115,12 +104,15 @@ self.assertTrue(type(self.profile.created), datetime) def test_no_duplicate_profiles(self): - # Creating a new profile returns an existing record + # Creating a new profile returns an existing updated record # (if one exists), instead of creating a new one. new_profile = AddressConfirmationProfile.objects.create_profile( u'les@example.org', User.objects.create(email=u'ler@mailman.mostdesirable.org')) - self.assertEqual(self.profile, new_profile) + self.assertEqual(new_profile.user, self.profile.user) + self.assertEqual(new_profile.email, self.profile.email) + self.assertNotEqual(new_profile.created, self.profile.created) + self.assertNotEqual(new_profile.activation_key, self.profile.activation_key) def test_unicode_representation(self): # Correct unicode representation? diff --git a/src/postorius/views/user.py b/src/postorius/views/user.py index 7676fd5..4f43e42 100644 --- a/src/postorius/views/user.py +++ b/src/postorius/views/user.py @@ -38,8 +38,10 @@ from postorius.auth.decorators import * from postorius.views.generic import MailmanUserView from smtplib import SMTPException -import errno from socket import error as socket_error +import errno +import hashlib +import random class UserMailmanSettingsView(MailmanUserView): @@ -233,8 +235,11 @@ if request.method == 'POST': form = AddressActivationForm(request.POST) if form.is_valid(): - profile = AddressConfirmationProfile.objects.create_profile( - email=form.cleaned_data['email'], user=request.user) + email_str = form.cleaned_data['email'].encode('utf-8') + activation_key = hashlib.sha1(str(random.random())+email_str).hexdigest() + defaults = {'activation_key': activation_key,} + profile, created = AddressConfirmationProfile.objects.update_or_create( + email=form.cleaned_data['email'], user=request.user, defaults=defaults) try: profile.send_confirmation_link(request) messages.success(request,