# HG changeset patch # User nishanth # Date 1271021245 -19800 # Node ID e29ecb7819e78c6058d05520ce609b383005efcd # Parent e2699e0421298c56692034198db6da67deeef5de password change and password reset have been done. diff -r e2699e042129 -r e29ecb7819e7 password_change.html --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/password_change.html Mon Apr 12 02:57:25 2010 +0530 @@ -0,0 +1,11 @@ +{% extends "base.html" %} +{% block content %} + {% if password_changed %} + Your password has been successfully changed. Click here to return to home page. + {% else %} +
+ {% endif %} +{% endblock %} diff -r e2699e042129 -r e29ecb7819e7 password_reset.html --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/password_reset.html Mon Apr 12 02:57:25 2010 +0530 @@ -0,0 +1,11 @@ +{% extends "base.html" %} +{% block content %} + {% if new_password %} + Your password has been reset to {{ new_password }}. Click here to return to home page. + {% else %} + + {% endif %} +{% endblock %} diff -r e2699e042129 -r e29ecb7819e7 reg/events.py --- a/reg/events.py Fri Apr 09 16:51:56 2010 +0530 +++ b/reg/events.py Mon Apr 12 02:57:25 2010 +0530 @@ -58,7 +58,6 @@ except IntegrityError: pass - def activate_user(user): """ mark the is_active flag as true. """ @@ -66,3 +65,25 @@ user.is_active = True user.save() return user + +def reset_password(user): + """ get a key and set it as password. + for now, print the key. + later on add the send mail function. + """ + + new_password = gen_key(10) + user.set_password(new_password) + user.save() + print "The new password is", new_password + return new_password + +def change_password(user, new_password): + """ for now just set the password and be done with it. + later on, if we want to do something else also then we can add them here. + """ + + user.set_password(new_password) + user.save() + + diff -r e2699e042129 -r e29ecb7819e7 reg/forms.py --- a/reg/forms.py Fri Apr 09 16:51:56 2010 +0530 +++ b/reg/forms.py Mon Apr 12 02:57:25 2010 +0530 @@ -105,3 +105,55 @@ if start_date > stop_date: raise forms.ValidationError('The event stops even before it starts. I am starting to wonder what kind of event is this.') return stop_date + +class PasswordResetForm(forms.Form): + """ check for the existance of user for the email. + Reset the password irrespective of active status. + """ + + email = forms.EmailField() + + def clean_email(self): + + email = self.cleaned_data['email'] + try: + user = User.objects.get(email__iexact=email) + return email + except User.DoesNotExist: + raise forms.ValidationError("This not a registered email. Please enter a valid email.") + +class PasswordChangeForm(forms.Form): + + old_password = forms.CharField(widget=forms.PasswordInput) + new_password = forms.CharField(widget=forms.PasswordInput) + confirm_password = forms.CharField(widget=forms.PasswordInput) + + def clean_old_password(self): + """ authenticate the given password against username. + """ + + username = self.data['username'] + old_password = self.cleaned_data['old_password'] + user = authenticate(username=username, password=old_password) + if not user: + raise forms.ValidationError("Incorrect password") + return old_password + + + def clean_new_password(self): + """ check if the password contains only alphabets or digits or punctuation. + then check if the size of the password is optimal. + then check if both the given passwords match. + """ + + new_password = self.cleaned_data['new_password'] + + if new_password.strip(string.ascii_letters+string.punctuation+string.digits): + raise forms.ValidationError("Only alphabets, digits and punctuation symbols are allowed in password") + + if not 8 <= len(new_password) <= 30: + raise forms.ValidationError("Password must be atleast 8 characters and at most 30 characters") + + if not new_password == self.data['confirm_password']: + raise forms.ValidationError("Passwords do not match") + diff -r e2699e042129 -r e29ecb7819e7 reg/site/urls.py --- a/reg/site/urls.py Fri Apr 09 16:51:56 2010 +0530 +++ b/reg/site/urls.py Mon Apr 12 02:57:25 2010 +0530 @@ -3,10 +3,12 @@ from workshop.reg import views as reg_views urlpatterns = patterns('', - ('^$', reg_views.homepage), - ('^login/$', reg_views.user_login), - ('^logout/$', reg_views.user_logout), - ('^register/$', reg_views.user_register), - ('^event/create/$', reg_views.create_event), - ('^event/view/(\w+)/$', reg_views.view_event), + (r'^$', reg_views.homepage), + (r'^login/$', reg_views.user_login), + (r'^logout/$', reg_views.user_logout), + (r'^register/$', reg_views.user_register), + (r'^password_reset/$', reg_views.reset_password), + (r'^password_change/$', reg_views.change_password), + (r'^event/create/$', reg_views.create_event), + (r'^event/view/(\w+)/$', reg_views.view_event), ) diff -r e2699e042129 -r e29ecb7819e7 reg/views.py --- a/reg/views.py Fri Apr 09 16:51:56 2010 +0530 +++ b/reg/views.py Mon Apr 12 02:57:25 2010 +0530 @@ -116,3 +116,53 @@ return HttpResponse(str(event)) +def reset_password(request): + """ check for the existance of e-mail. + Then call the event. + """ + + user = request.user + if user.is_authenticated(): + return redirect('/reg') + + if request.method == "POST": + form = reg_forms.PasswordResetForm(request.POST) + if form.is_valid(): + email = form.cleaned_data['email'] + user = User.objects.get(email__iexact=email) + new_password = reg_events.reset_password(user) + return render_to_response('password_reset.html', {'new_password':new_password}) + else: + return render_to_response('password_reset.html', {'form':form}) + else: + form = reg_forms.PasswordResetForm() + return render_to_response('password_reset.html', {'form':form}) + +def change_password(request): + """ check for the password and then set the new password. + """ + + user = request.user + if not user.is_authenticated(): + return redirect('/reg') + + if request.method == "POST": + data = request.POST.copy() + data.__setitem__('username', user.username) + form = reg_forms.PasswordChangeForm(data) + if form.is_valid(): + new_password = form.cleaned_data['new_password'] + reg_events.change_password(user, new_password) + return render_to_response('password_change.html', {'password_changed': True}) + else: + return render_to_response('password_change.html', {'form':form}) + else: + form = reg_forms.PasswordChangeForm() + return render_to_response('password_change.html', {'form':form}) + + + + + + +