app/projrev/views/login.py
author Madhusudan.C.S <madhusudancs@gmail.com>
Sat, 08 Aug 2009 23:54:36 +0530
changeset 18 05b9e60e6a10
parent 17 5c1e26fa0fc4
permissions -rw-r--r--
Changed CSS and removed Withdraw proposal.

"""This module contains the views for the login for the portal.
"""


__authors__ = [
  '"Madhusudan.C.S" <madhusudancs@gmail.com>',
]


from django.contrib.auth import authenticate
from django.contrib.auth import login
from django.contrib.auth import logout
from django.contrib.auth.forms import AuthenticationForm
from django.contrib.auth.models import User
from django.core.urlresolvers import reverse
from django.db import IntegrityError
from django.http import HttpResponseRedirect
from django.shortcuts import render_to_response
from django.shortcuts import get_object_or_404


def login_validate(request):
  """Validate the user and log him in.
  """

  template = 'projrev/auth/login.html'
  context = {}

  if request.POST:
    username = request.POST['username']
    password = request.POST['password']
    user = authenticate(username=username, password=password)
    if user is not None:
      if user.is_active:
        login(request, user)
        if user.is_staff:
          return HttpResponseRedirect('/proposal/review/')
        else:
          return HttpResponseRedirect('/proposal/submit/')
      else:
        pass
        # Return a 'disabled account' error message
    else:
      # Return an 'invalid login' error message.
      context['error'] = True

  return render_to_response(template, context)

def create_account(request):
  """Create an account for a user.
  """

  if request.POST:
    username = request.POST['username']
    password = request.POST['password']
    confirm_pasword = request.POST['confirmpassword']
    if password == confirm_pasword:
      try:
        user = User.objects.create_user(username, username, password)
        user.save()
        subject = "[Sakshath] Registration at Saskshath portal"
        message = """Hi,
   We have received a request for registration of your email address, 
"%s", to the http://sakshath.ac.in portal.
 
Your login credentials are:
  username: %s
  password: %s

--  
Regards,
Saksath admin""" % (username, username, password)

        user.email_user(subject=subject, message=message)

        context = {
            'created': True,
            'username': username,
            }
      except IntegrityError:
        context = {
            'exits': True,
            }
    else:
      context = {
          'password_err': True,
          }
  else:
    context = {}

  template = 'projrev/auth/create_account.html'

  return render_to_response(template, context)

def forgot_password(request):
  """Resend the password if forgotten.
  """

  template = 'projrev/auth/forgot_password.html'
  if request.POST:
    context = {
        'password_sent': True,
        }
  else:
    context = {}
    

  return render_to_response(template, context)

def logout_view(request):
  """Logout the user
  """

  logout(request)
  return HttpResponseRedirect(reverse('app.projrev.views.base.home'))