reg/views.py
author nishanth
Fri, 09 Apr 2010 12:28:58 +0530
changeset 4 ededea9ad08b
parent 3 182f216da4a8
child 5 37e4027fba48
permissions -rw-r--r--
login and logout works .
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
4
ededea9ad08b login and logout works .
nishanth
parents: 3
diff changeset
     1
from django.contrib.auth.models import User
3
182f216da4a8 made the login view. have to write templates and check it.
nishanth
parents: 2
diff changeset
     2
from django.contrib.auth import authenticate, login, logout
182f216da4a8 made the login view. have to write templates and check it.
nishanth
parents: 2
diff changeset
     3
from django.contrib.auth.decorators import login_required
2
c11afa8623f7 incorporated gen_key .
nishanth
parents: 0
diff changeset
     4
4
ededea9ad08b login and logout works .
nishanth
parents: 3
diff changeset
     5
from django.shortcuts import render_to_response, redirect
ededea9ad08b login and logout works .
nishanth
parents: 3
diff changeset
     6
3
182f216da4a8 made the login view. have to write templates and check it.
nishanth
parents: 2
diff changeset
     7
from workshop.reg import forms as reg_forms
182f216da4a8 made the login view. have to write templates and check it.
nishanth
parents: 2
diff changeset
     8
4
ededea9ad08b login and logout works .
nishanth
parents: 3
diff changeset
     9
from django.http import HttpResponse
ededea9ad08b login and logout works .
nishanth
parents: 3
diff changeset
    10
ededea9ad08b login and logout works .
nishanth
parents: 3
diff changeset
    11
def homepage(request):
ededea9ad08b login and logout works .
nishanth
parents: 3
diff changeset
    12
    """ see if the user is active.
ededea9ad08b login and logout works .
nishanth
parents: 3
diff changeset
    13
    If not, only show the re send activation email link.
ededea9ad08b login and logout works .
nishanth
parents: 3
diff changeset
    14
    else show all the options in homepage.
ededea9ad08b login and logout works .
nishanth
parents: 3
diff changeset
    15
    """
ededea9ad08b login and logout works .
nishanth
parents: 3
diff changeset
    16
ededea9ad08b login and logout works .
nishanth
parents: 3
diff changeset
    17
    user = request.user
ededea9ad08b login and logout works .
nishanth
parents: 3
diff changeset
    18
ededea9ad08b login and logout works .
nishanth
parents: 3
diff changeset
    19
    if not user.is_authenticated():
ededea9ad08b login and logout works .
nishanth
parents: 3
diff changeset
    20
        return HttpResponse('not logged in<br><a href="/reg/login">login</a>')
ededea9ad08b login and logout works .
nishanth
parents: 3
diff changeset
    21
    elif not user.is_active:
ededea9ad08b login and logout works .
nishanth
parents: 3
diff changeset
    22
        return HttpResponse('not active<br /><a href="/reg/logout">logout</a>')
ededea9ad08b login and logout works .
nishanth
parents: 3
diff changeset
    23
    else:
ededea9ad08b login and logout works .
nishanth
parents: 3
diff changeset
    24
        return HttpResponse('you are ogey<br /><a href="/reg/logout">logout</a>')
ededea9ad08b login and logout works .
nishanth
parents: 3
diff changeset
    25
ededea9ad08b login and logout works .
nishanth
parents: 3
diff changeset
    26
def user_login(request):
3
182f216da4a8 made the login view. have to write templates and check it.
nishanth
parents: 2
diff changeset
    27
    """ get the user object from e-mail and then check for password.
2
c11afa8623f7 incorporated gen_key .
nishanth
parents: 0
diff changeset
    28
    """
c11afa8623f7 incorporated gen_key .
nishanth
parents: 0
diff changeset
    29
3
182f216da4a8 made the login view. have to write templates and check it.
nishanth
parents: 2
diff changeset
    30
    user = request.user
182f216da4a8 made the login view. have to write templates and check it.
nishanth
parents: 2
diff changeset
    31
    if user.is_authenticated():
182f216da4a8 made the login view. have to write templates and check it.
nishanth
parents: 2
diff changeset
    32
        return redirect('/reg')
182f216da4a8 made the login view. have to write templates and check it.
nishanth
parents: 2
diff changeset
    33
182f216da4a8 made the login view. have to write templates and check it.
nishanth
parents: 2
diff changeset
    34
    if request.method == "POST":
182f216da4a8 made the login view. have to write templates and check it.
nishanth
parents: 2
diff changeset
    35
        form = reg_forms.LoginForm(request.POST)
182f216da4a8 made the login view. have to write templates and check it.
nishanth
parents: 2
diff changeset
    36
        if form.is_valid():
182f216da4a8 made the login view. have to write templates and check it.
nishanth
parents: 2
diff changeset
    37
            email = form.cleaned_data['email']
182f216da4a8 made the login view. have to write templates and check it.
nishanth
parents: 2
diff changeset
    38
            password = form.cleaned_data['password']
4
ededea9ad08b login and logout works .
nishanth
parents: 3
diff changeset
    39
            username = User.objects.get(email__iexact=email).username
3
182f216da4a8 made the login view. have to write templates and check it.
nishanth
parents: 2
diff changeset
    40
4
ededea9ad08b login and logout works .
nishanth
parents: 3
diff changeset
    41
            user = authenticate(username=username, password=password)
3
182f216da4a8 made the login view. have to write templates and check it.
nishanth
parents: 2
diff changeset
    42
            login(request, user)
182f216da4a8 made the login view. have to write templates and check it.
nishanth
parents: 2
diff changeset
    43
            return redirect('/reg')
182f216da4a8 made the login view. have to write templates and check it.
nishanth
parents: 2
diff changeset
    44
        else:
182f216da4a8 made the login view. have to write templates and check it.
nishanth
parents: 2
diff changeset
    45
            return render_to_response('login.html', {'form':form})
182f216da4a8 made the login view. have to write templates and check it.
nishanth
parents: 2
diff changeset
    46
    else:
4
ededea9ad08b login and logout works .
nishanth
parents: 3
diff changeset
    47
        form = reg_forms.LoginForm()
3
182f216da4a8 made the login view. have to write templates and check it.
nishanth
parents: 2
diff changeset
    48
        return render_to_response('login.html', {'form':form})
4
ededea9ad08b login and logout works .
nishanth
parents: 3
diff changeset
    49
ededea9ad08b login and logout works .
nishanth
parents: 3
diff changeset
    50
def user_logout(request):
ededea9ad08b login and logout works .
nishanth
parents: 3
diff changeset
    51
    """ simply logout the user and redirect to homepage.
ededea9ad08b login and logout works .
nishanth
parents: 3
diff changeset
    52
    """
ededea9ad08b login and logout works .
nishanth
parents: 3
diff changeset
    53
ededea9ad08b login and logout works .
nishanth
parents: 3
diff changeset
    54
    logout(request)
ededea9ad08b login and logout works .
nishanth
parents: 3
diff changeset
    55
    print "logged out"
ededea9ad08b login and logout works .
nishanth
parents: 3
diff changeset
    56
    return redirect('/reg')
ededea9ad08b login and logout works .
nishanth
parents: 3
diff changeset
    57
ededea9ad08b login and logout works .
nishanth
parents: 3
diff changeset
    58