diff options
author | hardythe1 | 2015-07-09 19:00:34 +0530 |
---|---|---|
committer | hardythe1 | 2015-07-09 19:00:34 +0530 |
commit | 7fdc127e4f823508616676bf67265871b622d5e6 (patch) | |
tree | d3df5185135399b5cf6fb5b45dd692e261600e71 /website/views.py | |
parent | 1abc6a736f5a8a91ff45da18fb0f37adb1ce1ebb (diff) | |
download | SciPy2015-7fdc127e4f823508616676bf67265871b622d5e6.tar.gz SciPy2015-7fdc127e4f823508616676bf67265871b622d5e6.tar.bz2 SciPy2015-7fdc127e4f823508616676bf67265871b622d5e6.zip |
add forgot password facility
Diffstat (limited to 'website/views.py')
-rw-r--r-- | website/views.py | 91 |
1 files changed, 76 insertions, 15 deletions
diff --git a/website/views.py b/website/views.py index fdf019b..b32f99a 100644 --- a/website/views.py +++ b/website/views.py @@ -16,17 +16,30 @@ from django.core.mail import send_mail from website.forms import ProposalForm, UserRegisterForm, UserLoginForm from website.models import Proposal, Comments from social.apps.django_app.default.models import UserSocialAuth +import random +import string def userregister(request): context = {} context.update(csrf(request)) + registered_emails = [] + users = User.objects.all() + for user in users: + registered_emails.append(user.email) if request.user.is_anonymous(): if request.method == 'POST': form = UserRegisterForm(request.POST) if form.is_valid(): - form.save() - return HttpResponseRedirect('/2015/cfp') + data = form.cleaned_data + if data['email'] in registered_emails: + context['form'] = form + context['email_registered'] = True + return render_to_response('user-register.html', context) + else: + form.save() + context['registration_complete'] = True + return render_to_response('cfp.html', context) else: context.update(csrf(request)) context['form'] = form @@ -41,25 +54,70 @@ def userregister(request): return HttpResponseRedirect('/2015/cfp') -def userlogin(request): +def forgotpassword(request): context = {} + user_emails = [] context.update(csrf(request)) - if request.method == "POST": - username = request.POST['username'] - password = request.POST['password'] - user = authenticate(username=username, password=password) - if user is not None: - login(request, user) - context['user'] = user - return render_to_response('cfp.html', context) + if request.method == 'POST': + users = User.objects.all() + for user in users: + user_emails.append(user.email) + email = request.POST['email'] + if email == "": + context['invalid_email'] = True + return render_to_response("forgot-password.html", context) + if email in user_emails: + user = User.objects.get(email=email) + password = ''.join(random.choice(string.ascii_uppercase + string.digits) for _ in range(8)) + user.set_password(password) + user.save() + sender_name = "SciPy India 2015" + sender_email = "scipy@fossee.in" + subject = "SciPy India - Password Reset" + to = (user.email, ) + message = """Dear """+user.first_name+""",\nYour password for SciPy India 2015 been reset. Your credentials are:\nUsername: """+user.username+"""\nPassword: """+password+"""\n\nWe recommend you to login with the given credentials & update your password immediately.\nLink to set new password: http://scipy.in/2015/update-password\n\nThank You !\n\nRegards,\n SciPy India,\nFOSSEE - IIT Bombay.""" + send_mail(subject, message, sender_email, to) + form = UserLoginForm() + context['form'] = form + context['password_reset'] = True + return render_to_response("cfp.html", context) else: - context['invalid'] = True - context['form'] = UserLoginForm - return render_to_response('user-login.html', context) + context['invalid_email'] = True + return render_to_response("forgot-password.html", context) + else: + return render_to_response('forgot-password.html', context) + + +def updatepassword(request): + context = {} + user = request.user + context.update(csrf(request)) + if user.is_authenticated(): + if request.method == 'POST': + new_password = request.POST['new_password'] + confirm = request.POST['confirm_new_password'] + if new_password == "" or confirm == "": + context['empty'] = True + return render_to_response("update-password.html", context) + if new_password == confirm: + user.set_password(new_password) + user.save() + context['password_updated'] = True + logout(request) + form = UserLoginForm() + context['form'] = form + return render_to_response("cfp.html", context) + else: + context['no_match'] = True + return render_to_response("update-password.html", context) + else: + return render_to_response("update-password.html", context) else: form = UserLoginForm() context['form'] = form - return render_to_response('user-login.html', context) + context['for_update_password'] = True + return render_to_response('cfp.html', context) + def home(request): @@ -88,6 +146,9 @@ def cfp(request): user = authenticate(username=username, password=password) if user is not None: login(request, user) + if 'next' in request.GET: + next = request.GET['next'] + return HttpResponseRedirect(next) context['user'] = user return render_to_response('cfp.html', context) else: |