app/projrev/views/login.py
author Madhusudan.C.S <madhusudancs@gmail.com>
Tue, 04 Aug 2009 19:42:47 +0530
changeset 1 324233b04d76
parent 0 c94bd9ae70d2
child 8 294ff7ac9cb6
permissions -rw-r--r--
Login mechanism.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
c94bd9ae70d2 First commit.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     1
"""This module contains the views for the login for the portal.
c94bd9ae70d2 First commit.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     2
"""
c94bd9ae70d2 First commit.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     3
c94bd9ae70d2 First commit.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     4
c94bd9ae70d2 First commit.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     5
__authors__ = [
c94bd9ae70d2 First commit.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     6
  '"Madhusudan.C.S" <madhusudancs@gmail.com>',
c94bd9ae70d2 First commit.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     7
]
c94bd9ae70d2 First commit.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     8
c94bd9ae70d2 First commit.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     9
1
324233b04d76 Login mechanism.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
    10
from django.contrib.auth import authenticate
324233b04d76 Login mechanism.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
    11
from django.contrib.auth import login
324233b04d76 Login mechanism.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
    12
from django.contrib.auth.forms import AuthenticationForm
324233b04d76 Login mechanism.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
    13
from django.contrib.auth.models import User
324233b04d76 Login mechanism.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
    14
from django.db import IntegrityError
324233b04d76 Login mechanism.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
    15
from django.http import HttpResponseRedirect
324233b04d76 Login mechanism.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
    16
from django.shortcuts import render_to_response
324233b04d76 Login mechanism.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
    17
from django.shortcuts import get_object_or_404
0
c94bd9ae70d2 First commit.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    18
c94bd9ae70d2 First commit.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    19
c94bd9ae70d2 First commit.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    20
def login_validate(request):
c94bd9ae70d2 First commit.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    21
  """Validate the user and log him in.
c94bd9ae70d2 First commit.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    22
  """
c94bd9ae70d2 First commit.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    23
1
324233b04d76 Login mechanism.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
    24
  if request.POST:
324233b04d76 Login mechanism.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
    25
    username = request.POST['username']
324233b04d76 Login mechanism.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
    26
    password = request.POST['password']
324233b04d76 Login mechanism.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
    27
    user = authenticate(username=username, password=password)
324233b04d76 Login mechanism.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
    28
    if user is not None:
324233b04d76 Login mechanism.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
    29
      if user.is_active:
324233b04d76 Login mechanism.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
    30
        login(request, user)
324233b04d76 Login mechanism.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
    31
        return HttpResponseRedirect('/proposal/submit/')
324233b04d76 Login mechanism.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
    32
      else:
324233b04d76 Login mechanism.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
    33
        pass
324233b04d76 Login mechanism.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
    34
        # Return a 'disabled account' error message
0
c94bd9ae70d2 First commit.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    35
    else:
1
324233b04d76 Login mechanism.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
    36
      # Return an 'invalid login' error message.
0
c94bd9ae70d2 First commit.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    37
      pass
c94bd9ae70d2 First commit.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    38
  else:
1
324233b04d76 Login mechanism.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
    39
    context = {}
324233b04d76 Login mechanism.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
    40
    template = 'projrev/auth/login.html'
324233b04d76 Login mechanism.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
    41
324233b04d76 Login mechanism.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
    42
  return render_to_response(template, context)
324233b04d76 Login mechanism.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
    43
324233b04d76 Login mechanism.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
    44
def create_account(request):
324233b04d76 Login mechanism.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
    45
  """Create an account for a user.
324233b04d76 Login mechanism.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
    46
  """
324233b04d76 Login mechanism.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
    47
324233b04d76 Login mechanism.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
    48
  if request.POST:
324233b04d76 Login mechanism.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
    49
    username = request.POST['username']
324233b04d76 Login mechanism.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
    50
    password = request.POST['password']
324233b04d76 Login mechanism.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
    51
    confirm_pasword = request.POST['confirmpassword']
324233b04d76 Login mechanism.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
    52
    if password == confirm_pasword:
324233b04d76 Login mechanism.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
    53
      try:
324233b04d76 Login mechanism.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
    54
        user = User.objects.create_user(username, username, password)
324233b04d76 Login mechanism.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
    55
        user.save()
324233b04d76 Login mechanism.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
    56
        context = {
324233b04d76 Login mechanism.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
    57
            'created': True,
324233b04d76 Login mechanism.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
    58
            'username': username,
324233b04d76 Login mechanism.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
    59
            }
324233b04d76 Login mechanism.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
    60
      except IntegrityError:
324233b04d76 Login mechanism.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
    61
        context = {
324233b04d76 Login mechanism.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
    62
            'exits': True,
324233b04d76 Login mechanism.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
    63
            }
324233b04d76 Login mechanism.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
    64
    else:
324233b04d76 Login mechanism.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
    65
      context = {
324233b04d76 Login mechanism.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
    66
          'password_err': True,
324233b04d76 Login mechanism.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
    67
          }
324233b04d76 Login mechanism.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
    68
  else:
324233b04d76 Login mechanism.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
    69
    context = {}
324233b04d76 Login mechanism.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
    70
324233b04d76 Login mechanism.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
    71
  template = 'projrev/auth/create_account.html'
324233b04d76 Login mechanism.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
    72
324233b04d76 Login mechanism.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
    73
  return render_to_response(template, context)
324233b04d76 Login mechanism.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
    74
324233b04d76 Login mechanism.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
    75
def forgot_password(request):
324233b04d76 Login mechanism.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
    76
  """Resend the password if forgotten.
324233b04d76 Login mechanism.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
    77
  """
324233b04d76 Login mechanism.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
    78
324233b04d76 Login mechanism.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
    79
  if request.POST:
0
c94bd9ae70d2 First commit.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    80
    pass
1
324233b04d76 Login mechanism.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
    81
  else:
324233b04d76 Login mechanism.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
    82
    context = {}
324233b04d76 Login mechanism.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
    83
    template = 'projrev/auth/forgot_password.html'
324233b04d76 Login mechanism.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
    84
324233b04d76 Login mechanism.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
    85
  return render_to_response(template, context)
0
c94bd9ae70d2 First commit.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    86
c94bd9ae70d2 First commit.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    87
def logout_view(request):
c94bd9ae70d2 First commit.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    88
  """Logout the user
c94bd9ae70d2 First commit.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    89
  """
c94bd9ae70d2 First commit.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    90