Revision 49790d9d snf-astakos-app/astakos/im/forms.py

b/snf-astakos-app/astakos/im/forms.py
44 44
from django.utils.functional import lazy
45 45
from django.utils.safestring import mark_safe
46 46
from django.contrib import messages
47
from django.utils.encoding import smart_str
47 48

  
48
from astakos.im.models import AstakosUser, Invitation, get_latest_terms
49
from astakos.im.models import AstakosUser, Invitation, get_latest_terms, EmailChange
49 50
from astakos.im.settings import INVITATIONS_PER_LEVEL, DEFAULT_FROM_EMAIL, BASEURL, SITENAME, RECAPTCHA_PRIVATE_KEY, DEFAULT_CONTACT_EMAIL, RECAPTCHA_ENABLED
50 51
from astakos.im.widgets import DummyWidget, RecaptchaWidget
52
from astakos.im.functions import send_change_email
51 53

  
52 54
# since Django 1.4 use django.core.urlresolvers.reverse_lazy instead
53 55
from astakos.im.util import reverse_lazy, reserved_email, get_query
54 56

  
55 57
import logging
58
import hashlib
56 59
import recaptcha.client.captcha as captcha
60
from random import random
57 61

  
58 62
logger = logging.getLogger(__name__)
59 63

  
......
392 396
            send_mail(_("Password reset on %s alpha2 testing") % SITENAME,
393 397
                t.render(Context(c)), from_email, [user.email])
394 398

  
399
class EmailChangeForm(forms.ModelForm):
400
    class Meta:
401
        model = EmailChange
402
        fields = ('new_email_address',)
403
            
404
    def clean_new_email_address(self):
405
        addr = self.cleaned_data['new_email_address']
406
        if AstakosUser.objects.filter(email__iexact=addr):
407
            raise forms.ValidationError(_(u'This email address is already in use. Please supply a different email address.'))
408
        return addr
409
    
410
    def save(self, email_template_name, request, commit=True):
411
        ec = super(EmailChangeForm, self).save(commit=False)
412
        ec.user = request.user
413
        activation_key = hashlib.sha1(str(random()) + smart_str(ec.new_email_address))
414
        ec.activation_key=activation_key.hexdigest()
415
        if commit:
416
            ec.save()
417
        send_change_email(ec, request, email_template_name=email_template_name)
418

  
395 419
class SignApprovalTermsForm(forms.ModelForm):
396 420
    class Meta:
397 421
        model = AstakosUser

Also available in: Unified diff