from django.contrib.auth import login, logout, authenticate from django.contrib.auth.decorators import login_required from django.shortcuts import render_to_response, get_object_or_404 from django.shortcuts import render, redirect from django.template import RequestContext from django.http import Http404 from allotter.models import Profile, Option, Exam from allotter.forms import RegistrationForm, UserLoginForm from settings import URL_ROOT def index(request): """The start page. """ user = request.user if user.is_authenticated(): return redirect("/allotter/hello/") return redirect("/allotter/login/") def user_register(request): """ Register a new user. Create a user and corresponding profile and store roll_number also.""" user = request.user if user.is_authenticated(): return redirect("/allotter/hello/") if request.method == "POST": form = RegistrationForm(request.POST) if form.is_valid(): data = form.cleaned_data u_name, pwd = form.save_data() return redirect("/allotter/hello/") else: return render_to_response('allotter/register.html', {'form':form}, context_instance=RequestContext(request)) else: form = RegistrationForm() return render_to_response('allotter/register.html', {'form':form}, context_instance=RequestContext(request)) def user_login(request): """Take the credentials of the user and log the user in.""" user = request.user if user.is_authenticated(): return redirect("/allotter/hello/") if request.method == "POST": form = UserLoginForm(request.POST) if form.is_valid(): user = form.cleaned_data login(request, user) return redirect("/allotter/hello/") else: context = {"form": form} return render_to_response('allotter/login.html', context, context_instance=RequestContext(request)) else: form = UserLoginForm() context = {"form": form} return render_to_response('allotter/login.html', context, context_instance=RequestContext(request)) @login_required def hello(request): user = request.user context = {'user': user} ci = RequestContext(request) return render_to_response('allotter/hello.html', context, context_instance=ci) @login_required def apply(request): user = request.user if not(user.is_authenticated()): return redirect('/allotter/login/') user_profile = user.get_profile() subject = user_profile.exam_code options_available = Option.objects.filter(exam__exam_code=subject).distinct() context = {'user': user, 'subject': subject, 'options' : options_available} ci = RequestContext(request) return render_to_response('allotter/apply.html', context, context_instance=ci)