password change and password reset have been done.
from django.contrib.auth.models import User
from django.contrib.auth import authenticate, login, logout
from django.contrib.auth.decorators import login_required
from django.shortcuts import render_to_response, redirect
from workshop.reg.models import Event
from workshop.reg import forms as reg_forms
from workshop.reg import events as reg_events
from django.http import HttpResponse
def homepage(request):
""" see if the user is active.
If not, only show the re send activation email link.
else show all the options in homepage.
"""
user = request.user
events = Event.objects.all()[:10]
def user_login(request):
""" get the user object from e-mail and then check for password.
"""
user = request.user
if user.is_authenticated():
return redirect('/reg')
if request.method == "POST":
form = reg_forms.LoginForm(request.POST)
if form.is_valid():
email = form.cleaned_data['email']
password = form.cleaned_data['password']
username = User.objects.get(email__iexact=email).username
user = authenticate(username=username, password=password)
login(request, user)
return redirect('/reg')
else:
return render_to_response('login.html', {'form':form})
else:
form = reg_forms.LoginForm()
return render_to_response('login.html', {'form':form})
def user_logout(request):
""" simply logout the user and redirect to homepage.
"""
logout(request)
return redirect('/reg')
def user_register(request):
""" take the credentials like name, college and gender here itself.
"""
if request.method == "POST":
form = reg_forms.RegisterForm(request.POST)
if form.is_valid():
data = form.cleaned_data
reg_events.create_user(email=data['email'],
password=data['password'],
firstname=data['first_name'],
lastname=data['last_name'],
gender=data['gender'],
profession=data['profession'],
affiliated_to=data['affiliated_to'],
interests=data['interests']
)
return render_to_response('account_created.html')
else:
return render_to_response('register.html', {'form':form})
else:
form = reg_forms.RegisterForm()
return render_to_response('register.html', {'form':form})
def create_event(request):
""" see if the user is a staff and only then let him do it.
"""
user = request.user
if user.is_authenticated() and user.is_staff:
if request.method == "POST":
form = reg_forms.EventCreateForm(request.POST)
if form.is_valid():
data = form.cleaned_data
new_event = reg_events.create_event(title=data['title'],
description=data['description'],
start_date=data['start_date'],
stop_date=data['stop_date'],
created_by=user,
)
event_url = "/reg/event/view/%s"%(new_event.key)
return redirect(event_url)
else:
return render_to_response('event_create.html', {'form':form})
else:
form = reg_forms.EventCreateForm()
return render_to_response('event_create.html', {'form':form})
else:
return redirect('/reg')
def view_event(request, key):
""" get the event by its key and display it.
"""
user = request.user
is_guest = False if user.is_authenticated() else True
try:
event = Event.objects.get(key__iexact=key)
except Event.DoesNotExist:
return HttpResponse("F off")
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})