app/projrev/views/login.py
author Santosh G. Vattam <vattam.santosh@gmail.com>
Mon, 10 Aug 2009 16:58:36 +0530
changeset 36 a7c525ec281a
parent 18 05b9e60e6a10
permissions -rw-r--r--
Added the nine attributes
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
8
294ff7ac9cb6 Added new set of files.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 1
diff changeset
    12
from django.contrib.auth import logout
1
324233b04d76 Login mechanism.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
    13
from django.contrib.auth.forms import AuthenticationForm
324233b04d76 Login mechanism.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
    14
from django.contrib.auth.models import User
8
294ff7ac9cb6 Added new set of files.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 1
diff changeset
    15
from django.core.urlresolvers import reverse
1
324233b04d76 Login mechanism.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
    16
from django.db import IntegrityError
324233b04d76 Login mechanism.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
    17
from django.http import HttpResponseRedirect
324233b04d76 Login mechanism.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
    18
from django.shortcuts import render_to_response
324233b04d76 Login mechanism.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
    19
from django.shortcuts import get_object_or_404
0
c94bd9ae70d2 First commit.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    20
c94bd9ae70d2 First commit.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    21
c94bd9ae70d2 First commit.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    22
def login_validate(request):
c94bd9ae70d2 First commit.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    23
  """Validate the user and log him in.
c94bd9ae70d2 First commit.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    24
  """
c94bd9ae70d2 First commit.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    25
8
294ff7ac9cb6 Added new set of files.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 1
diff changeset
    26
  template = 'projrev/auth/login.html'
294ff7ac9cb6 Added new set of files.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 1
diff changeset
    27
  context = {}
294ff7ac9cb6 Added new set of files.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 1
diff changeset
    28
1
324233b04d76 Login mechanism.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
    29
  if request.POST:
324233b04d76 Login mechanism.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
    30
    username = request.POST['username']
324233b04d76 Login mechanism.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
    31
    password = request.POST['password']
324233b04d76 Login mechanism.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
    32
    user = authenticate(username=username, password=password)
324233b04d76 Login mechanism.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
    33
    if user is not None:
324233b04d76 Login mechanism.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
    34
      if user.is_active:
324233b04d76 Login mechanism.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
    35
        login(request, user)
18
05b9e60e6a10 Changed CSS and removed Withdraw proposal.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 17
diff changeset
    36
        if user.is_staff:
05b9e60e6a10 Changed CSS and removed Withdraw proposal.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 17
diff changeset
    37
          return HttpResponseRedirect('/proposal/review/')
05b9e60e6a10 Changed CSS and removed Withdraw proposal.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 17
diff changeset
    38
        else:
05b9e60e6a10 Changed CSS and removed Withdraw proposal.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 17
diff changeset
    39
          return HttpResponseRedirect('/proposal/submit/')
1
324233b04d76 Login mechanism.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
    40
      else:
324233b04d76 Login mechanism.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
    41
        pass
324233b04d76 Login mechanism.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
    42
        # Return a 'disabled account' error message
0
c94bd9ae70d2 First commit.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    43
    else:
1
324233b04d76 Login mechanism.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
    44
      # Return an 'invalid login' error message.
8
294ff7ac9cb6 Added new set of files.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 1
diff changeset
    45
      context['error'] = True
1
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
  return render_to_response(template, context)
324233b04d76 Login mechanism.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
    48
324233b04d76 Login mechanism.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
    49
def create_account(request):
324233b04d76 Login mechanism.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
    50
  """Create an account for a user.
324233b04d76 Login mechanism.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
    51
  """
324233b04d76 Login mechanism.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
    52
324233b04d76 Login mechanism.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
    53
  if request.POST:
324233b04d76 Login mechanism.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
    54
    username = request.POST['username']
324233b04d76 Login mechanism.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
    55
    password = request.POST['password']
324233b04d76 Login mechanism.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
    56
    confirm_pasword = request.POST['confirmpassword']
324233b04d76 Login mechanism.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
    57
    if password == confirm_pasword:
324233b04d76 Login mechanism.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
    58
      try:
324233b04d76 Login mechanism.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
    59
        user = User.objects.create_user(username, username, password)
324233b04d76 Login mechanism.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
    60
        user.save()
17
5c1e26fa0fc4 Sending mails.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 9
diff changeset
    61
        subject = "[Sakshath] Registration at Saskshath portal"
5c1e26fa0fc4 Sending mails.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 9
diff changeset
    62
        message = """Hi,
18
05b9e60e6a10 Changed CSS and removed Withdraw proposal.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 17
diff changeset
    63
   We have received a request for registration of your email address, 
05b9e60e6a10 Changed CSS and removed Withdraw proposal.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 17
diff changeset
    64
"%s", to the http://sakshath.ac.in portal.
05b9e60e6a10 Changed CSS and removed Withdraw proposal.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 17
diff changeset
    65
 
05b9e60e6a10 Changed CSS and removed Withdraw proposal.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 17
diff changeset
    66
Your login credentials are:
05b9e60e6a10 Changed CSS and removed Withdraw proposal.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 17
diff changeset
    67
  username: %s
05b9e60e6a10 Changed CSS and removed Withdraw proposal.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 17
diff changeset
    68
  password: %s
05b9e60e6a10 Changed CSS and removed Withdraw proposal.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 17
diff changeset
    69
05b9e60e6a10 Changed CSS and removed Withdraw proposal.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 17
diff changeset
    70
--  
05b9e60e6a10 Changed CSS and removed Withdraw proposal.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 17
diff changeset
    71
Regards,
05b9e60e6a10 Changed CSS and removed Withdraw proposal.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 17
diff changeset
    72
Saksath admin""" % (username, username, password)
17
5c1e26fa0fc4 Sending mails.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 9
diff changeset
    73
5c1e26fa0fc4 Sending mails.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 9
diff changeset
    74
        user.email_user(subject=subject, message=message)
18
05b9e60e6a10 Changed CSS and removed Withdraw proposal.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 17
diff changeset
    75
1
324233b04d76 Login mechanism.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
    76
        context = {
324233b04d76 Login mechanism.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
    77
            'created': True,
324233b04d76 Login mechanism.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
    78
            'username': username,
324233b04d76 Login mechanism.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
    79
            }
324233b04d76 Login mechanism.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
    80
      except IntegrityError:
324233b04d76 Login mechanism.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
    81
        context = {
324233b04d76 Login mechanism.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
    82
            'exits': True,
324233b04d76 Login mechanism.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
    83
            }
324233b04d76 Login mechanism.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
    84
    else:
324233b04d76 Login mechanism.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
    85
      context = {
324233b04d76 Login mechanism.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
    86
          'password_err': True,
324233b04d76 Login mechanism.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
    87
          }
324233b04d76 Login mechanism.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
    88
  else:
324233b04d76 Login mechanism.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
    89
    context = {}
324233b04d76 Login mechanism.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
    90
324233b04d76 Login mechanism.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
    91
  template = 'projrev/auth/create_account.html'
324233b04d76 Login mechanism.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
    92
324233b04d76 Login mechanism.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
    93
  return render_to_response(template, context)
324233b04d76 Login mechanism.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
    94
324233b04d76 Login mechanism.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
    95
def forgot_password(request):
324233b04d76 Login mechanism.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
    96
  """Resend the password if forgotten.
324233b04d76 Login mechanism.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
    97
  """
324233b04d76 Login mechanism.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
    98
9
38727da8a948 Create Account
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 8
diff changeset
    99
  template = 'projrev/auth/forgot_password.html'
1
324233b04d76 Login mechanism.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
   100
  if request.POST:
9
38727da8a948 Create Account
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 8
diff changeset
   101
    context = {
38727da8a948 Create Account
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 8
diff changeset
   102
        'password_sent': True,
38727da8a948 Create Account
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 8
diff changeset
   103
        }
1
324233b04d76 Login mechanism.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
   104
  else:
324233b04d76 Login mechanism.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
   105
    context = {}
9
38727da8a948 Create Account
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 8
diff changeset
   106
    
1
324233b04d76 Login mechanism.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
   107
324233b04d76 Login mechanism.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
   108
  return render_to_response(template, context)
0
c94bd9ae70d2 First commit.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   109
c94bd9ae70d2 First commit.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   110
def logout_view(request):
c94bd9ae70d2 First commit.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   111
  """Logout the user
c94bd9ae70d2 First commit.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   112
  """
8
294ff7ac9cb6 Added new set of files.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 1
diff changeset
   113
294ff7ac9cb6 Added new set of files.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 1
diff changeset
   114
  logout(request)
294ff7ac9cb6 Added new set of files.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 1
diff changeset
   115
  return HttpResponseRedirect(reverse('app.projrev.views.base.home'))