Skip to content

Commit

Permalink
refactored messages
Browse files Browse the repository at this point in the history
  • Loading branch information
whiskybar committed Apr 21, 2011
1 parent ca2c9be commit 723fd09
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 23 deletions.
2 changes: 1 addition & 1 deletion enroll/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def __unicode__(self):
return self.key

def activate_user(self):
self.user.is_active = True;
self.user.is_active = True
self.user.save()

def notify_user(self, subject, mail_template):
Expand Down
12 changes: 6 additions & 6 deletions enroll/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,21 +61,21 @@ class TooSimplePasswordValidator(object):
LOWERCASE_SEQUENCE = 2 * string.lowercase
DIGIT_SEQUENCE = 2 * string.digits

def validate_sequnce(self, password, sequence, message):
def validate_sequence(self, password, sequence, message):
if password in sequence or password[::-1] in sequence:
raise ValidationError(message)
raise ValidationError(message, code=self.__class__)

def __init__(self, min_unique_chars=3):
self.min_unique_chars = min_unique_chars

def __call__(self, password):
#Case sensitive validations
if len(set(password)) < self.min_unique_chars:
raise ValidationError(_(u'Password must contains at least %d unique characters') % self.min_unique_chars)
raise ValidationError(_(u'Password must contains at least %d unique characters') % self.min_unique_chars, code=self.__class__)

#Case insensitive validations
password = password.lower()
self.validate_sequnce(password, self.LOWERCASE_SEQUENCE, _(u'Password cannot be ascending or descending sequence.'))
self.validate_sequnce(password, self.DIGIT_SEQUENCE, _(u'Password cannot be ascending or descending sequence.'))
self.validate_sequnce(password, getattr(settings , 'ENROLL_FORBIDDEN_PASSWORDS', []), _(u'Password is not allowed.'))
self.validate_sequence(password, self.LOWERCASE_SEQUENCE, _(u'Password cannot be ascending or descending sequence.'))
self.validate_sequence(password, self.DIGIT_SEQUENCE, _(u'Password cannot be ascending or descending sequence.'))
self.validate_sequence(password, getattr(settings, 'ENROLL_FORBIDDEN_PASSWORDS', []), _(u'This password is not allowed.'))

40 changes: 24 additions & 16 deletions enroll/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from django.utils.decorators import method_decorator
from django.views.decorators.cache import never_cache
from django.http import HttpResponseNotFound
#from django.utils.translation import ugettext_lazy as _
from django.utils.translation import ugettext_lazy as _

from enroll.forms import SignUpForm, RequestPassingAuthenticationForm,\
PasswordResetStepOneForm, PasswordResetStepTwoForm, ChangeEmailForm
Expand Down Expand Up @@ -118,10 +118,10 @@ def get(self, request, verification_key):
verification_type=VerificationToken.TYPE_SIGN_UP
)
except VerificationToken.DoesNotExist:
return self.on_failure(verification_key)
return self.on_success(token)
return self.on_failure(request, verification_key)
return self.on_success(request, token)

def on_success(self, token):
def on_success(self, request, token):
user = token.user

token.activate_user()
Expand All @@ -133,16 +133,16 @@ def on_success(self, token):
self.send_success_message()
return http.HttpResponseRedirect(self.success_url)

def on_failure(self, verification_key):
def on_failure(self, request, verification_key):
self.send_failure_message()
return http.HttpResponseRedirect(self.failure_url)


class PasswordResetView(SuccessMessageFormView):
"""Redirect to given URL or render page from template on success. Optionally send message using djnago.contrib.messages"""
template_name = 'registration/password_reset_form.html'
form_class = PasswordResetStepOneForm
success_url = '/'
success_message = _('An verification link has been sent to the your e-mail address.')

def get_form_kwargs(self):
kwargs = dict(request=self.request)
Expand All @@ -154,26 +154,27 @@ def form_valid(self, form):
return super(PasswordResetView, self).form_valid(form)


class VerifyPasswordResetView(AutoLoginMixin, SuccessMessageFormView):
class VerifyPasswordResetView(AutoLoginMixin, SuccessMessageFormView, FailureMessageMixin):
template_name ='registration/password_reset_confirm.html'
form_class = PasswordResetStepTwoForm
success_url = '/'
success_message = _('Your password has been updated.')
login_on_success = getattr(settings, 'ENROLL_AUTO_LOGIN', True)
failure_url = '/'
failure_message = _('Invalid verification link.')

def get_form_kwargs(self):
kwargs = dict(request=self.request, user=self.token.user)
kwargs.update(super(VerifyPasswordResetView, self).get_form_kwargs())
return kwargs

def token_does_not_exist(self, request, verification_key):
return HttpResponseNotFound()

@method_decorator(never_cache)
def dispatch(self, request, verification_key, *args, **kwargs):
self.request = request
try:
self.token = VerificationToken.objects.get(key=verification_key)
except VerificationToken.DoesNotExist:
return self.token_does_not_exist(request, verification_key)
return self.on_failure(request, verification_key)
return super(VerifyPasswordResetView, self).dispatch(request, *args, **kwargs)

def form_valid(self, form):
Expand All @@ -183,11 +184,16 @@ def form_valid(self, form):
self.token.delete()
return super(VerifyPasswordResetView, self).form_valid(form)

def on_failure(self, request, verification_key):
self.send_failure_message()
return http.HttpResponseRedirect(self.failure_url)


class ChangeEmailView(SuccessMessageFormView):
form_class = ChangeEmailForm
template_name = 'registration/change_email.html'
success_url = '/'
success_message = _('An activation link has been sent to your new address.')

def get_form_kwargs(self):
kwargs = dict(request=self.request)
Expand All @@ -201,7 +207,9 @@ def form_valid(self, form):

class VerifyEmailChangeView(SuccessMessageMixin, FailureMessageMixin, View):
success_url = '/'
success_message = _('Your e-mail address has been updated.')
failure_url = '/'
failure_message = _('Invalid activation link.')

def get(self, request, verification_key):
try:
Expand All @@ -211,10 +219,10 @@ def get(self, request, verification_key):
verification_type=VerificationToken.TYPE_EMAIL_CHANGE
)
except VerificationToken.DoesNotExist:
return self.on_failure(verification_key)
return self.on_success(token)
return self.on_failure(request, verification_key)
return self.on_success(request, token)

def on_success(self, token):
def on_success(self, request, token):
user = token.user
user.email = token.email
user.save()
Expand All @@ -223,7 +231,7 @@ def on_success(self, token):
self.send_success_message()
return http.HttpResponseRedirect(self.success_url)

def on_failure(self, verification_key):
def on_failure(self, request, verification_key):
self.send_failure_message()
return http.HttpResponseRedirect(self.failure_url)

Expand Down Expand Up @@ -301,4 +309,4 @@ def get(self, request, *args, **kwargs):
url = self.get_redirect_url()
if url:
return http.HttpResponseRedirect(url)
return super(LogoutView, self).get(self, request, *args, **kwargs)
return super(LogoutView, self).get(self, request, *args, **kwargs)

0 comments on commit 723fd09

Please sign in to comment.