taskapp/views/users.py
author anoop
Thu, 04 Feb 2010 22:17:36 +0530
changeset 15 4e29ed911123
parent 14 54694601c5e3
permissions -rw-r--r--
corrected views.user.register method .
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
12
a93eebabfeb1 created forms, views, templates, actions for home_page, browse_task.
nishanth
parents: 10
diff changeset
     1
from django.http import HttpResponse
a93eebabfeb1 created forms, views, templates, actions for home_page, browse_task.
nishanth
parents: 10
diff changeset
     2
from django.shortcuts import redirect, render_to_response
a93eebabfeb1 created forms, views, templates, actions for home_page, browse_task.
nishanth
parents: 10
diff changeset
     3
from pytask.taskapp.models import Task
13
0fb64b24a1c9 added views, templates for register, login, logout user.
anoop
parents: 12
diff changeset
     4
from pytask.taskapp.forms.user import RegistrationForm, LoginForm
0fb64b24a1c9 added views, templates for register, login, logout user.
anoop
parents: 12
diff changeset
     5
from pytask.taskapp.events.user import createUser
0fb64b24a1c9 added views, templates for register, login, logout user.
anoop
parents: 12
diff changeset
     6
from django.contrib.auth import login, logout, authenticate
0fb64b24a1c9 added views, templates for register, login, logout user.
anoop
parents: 12
diff changeset
     7
from django.contrib.auth.models import User
10
bf0cbea1bd12 removed views.py and made it a package. added auth_profile to settings.py
nishanth
parents:
diff changeset
     8
12
a93eebabfeb1 created forms, views, templates, actions for home_page, browse_task.
nishanth
parents: 10
diff changeset
     9
def redirect_to_homepage(request):
a93eebabfeb1 created forms, views, templates, actions for home_page, browse_task.
nishanth
parents: 10
diff changeset
    10
    """ simply redirect to homepage """
a93eebabfeb1 created forms, views, templates, actions for home_page, browse_task.
nishanth
parents: 10
diff changeset
    11
    
a93eebabfeb1 created forms, views, templates, actions for home_page, browse_task.
nishanth
parents: 10
diff changeset
    12
    return redirect('/')
a93eebabfeb1 created forms, views, templates, actions for home_page, browse_task.
nishanth
parents: 10
diff changeset
    13
a93eebabfeb1 created forms, views, templates, actions for home_page, browse_task.
nishanth
parents: 10
diff changeset
    14
def homepage(request):
a93eebabfeb1 created forms, views, templates, actions for home_page, browse_task.
nishanth
parents: 10
diff changeset
    15
    """ check for authentication and display accordingly. """
a93eebabfeb1 created forms, views, templates, actions for home_page, browse_task.
nishanth
parents: 10
diff changeset
    16
    
a93eebabfeb1 created forms, views, templates, actions for home_page, browse_task.
nishanth
parents: 10
diff changeset
    17
    user = request.user
a93eebabfeb1 created forms, views, templates, actions for home_page, browse_task.
nishanth
parents: 10
diff changeset
    18
    is_guest = False
a93eebabfeb1 created forms, views, templates, actions for home_page, browse_task.
nishanth
parents: 10
diff changeset
    19
    is_mentor = False
a93eebabfeb1 created forms, views, templates, actions for home_page, browse_task.
nishanth
parents: 10
diff changeset
    20
    can_create_task = False
a93eebabfeb1 created forms, views, templates, actions for home_page, browse_task.
nishanth
parents: 10
diff changeset
    21
    task_list = []
a93eebabfeb1 created forms, views, templates, actions for home_page, browse_task.
nishanth
parents: 10
diff changeset
    22
    
a93eebabfeb1 created forms, views, templates, actions for home_page, browse_task.
nishanth
parents: 10
diff changeset
    23
    if not user.is_authenticated():
a93eebabfeb1 created forms, views, templates, actions for home_page, browse_task.
nishanth
parents: 10
diff changeset
    24
        is_guest = True
a93eebabfeb1 created forms, views, templates, actions for home_page, browse_task.
nishanth
parents: 10
diff changeset
    25
        disp_num = 10
a93eebabfeb1 created forms, views, templates, actions for home_page, browse_task.
nishanth
parents: 10
diff changeset
    26
        tasks_count = Task.objects.count()
a93eebabfeb1 created forms, views, templates, actions for home_page, browse_task.
nishanth
parents: 10
diff changeset
    27
        if tasks_count <= disp_num:
a93eebabfeb1 created forms, views, templates, actions for home_page, browse_task.
nishanth
parents: 10
diff changeset
    28
            task_list = Task.objects.order_by('id').reverse()
a93eebabfeb1 created forms, views, templates, actions for home_page, browse_task.
nishanth
parents: 10
diff changeset
    29
        else:
a93eebabfeb1 created forms, views, templates, actions for home_page, browse_task.
nishanth
parents: 10
diff changeset
    30
            task_list = Task.objects.order_by('id').reverse()[:10]
a93eebabfeb1 created forms, views, templates, actions for home_page, browse_task.
nishanth
parents: 10
diff changeset
    31
    else:
a93eebabfeb1 created forms, views, templates, actions for home_page, browse_task.
nishanth
parents: 10
diff changeset
    32
        user_profile = user.get_profile()
a93eebabfeb1 created forms, views, templates, actions for home_page, browse_task.
nishanth
parents: 10
diff changeset
    33
        is_mentor = True if user.task_mentors.all() else False
a93eebabfeb1 created forms, views, templates, actions for home_page, browse_task.
nishanth
parents: 10
diff changeset
    34
        can_create_task = False if user_profile.rights == u"CT" else True
a93eebabfeb1 created forms, views, templates, actions for home_page, browse_task.
nishanth
parents: 10
diff changeset
    35
        
a93eebabfeb1 created forms, views, templates, actions for home_page, browse_task.
nishanth
parents: 10
diff changeset
    36
    context = {'user':user,
a93eebabfeb1 created forms, views, templates, actions for home_page, browse_task.
nishanth
parents: 10
diff changeset
    37
               'is_guest':is_guest,
a93eebabfeb1 created forms, views, templates, actions for home_page, browse_task.
nishanth
parents: 10
diff changeset
    38
               'is_mentor':is_mentor,
a93eebabfeb1 created forms, views, templates, actions for home_page, browse_task.
nishanth
parents: 10
diff changeset
    39
               'task_list':task_list,
a93eebabfeb1 created forms, views, templates, actions for home_page, browse_task.
nishanth
parents: 10
diff changeset
    40
               'can_create_task':can_create_task,
a93eebabfeb1 created forms, views, templates, actions for home_page, browse_task.
nishanth
parents: 10
diff changeset
    41
               }
a93eebabfeb1 created forms, views, templates, actions for home_page, browse_task.
nishanth
parents: 10
diff changeset
    42
               
a93eebabfeb1 created forms, views, templates, actions for home_page, browse_task.
nishanth
parents: 10
diff changeset
    43
    return render_to_response('index.html', context)
13
0fb64b24a1c9 added views, templates for register, login, logout user.
anoop
parents: 12
diff changeset
    44
12
a93eebabfeb1 created forms, views, templates, actions for home_page, browse_task.
nishanth
parents: 10
diff changeset
    45
13
0fb64b24a1c9 added views, templates for register, login, logout user.
anoop
parents: 12
diff changeset
    46
def register(request):
0fb64b24a1c9 added views, templates for register, login, logout user.
anoop
parents: 12
diff changeset
    47
    """ user registration: gets the user details and create the user and userprofile if data entered is valid"""
0fb64b24a1c9 added views, templates for register, login, logout user.
anoop
parents: 12
diff changeset
    48
    if request.method == 'POST':
0fb64b24a1c9 added views, templates for register, login, logout user.
anoop
parents: 12
diff changeset
    49
        form = RegistrationForm(request.POST)
0fb64b24a1c9 added views, templates for register, login, logout user.
anoop
parents: 12
diff changeset
    50
        if form.is_valid():
0fb64b24a1c9 added views, templates for register, login, logout user.
anoop
parents: 12
diff changeset
    51
            data = form.cleaned_data
0fb64b24a1c9 added views, templates for register, login, logout user.
anoop
parents: 12
diff changeset
    52
            if data['password'] == data['repeat_password']:
14
54694601c5e3 imposed a filter on username that it contains only alphabets and numbers.
anoop
parents: 13
diff changeset
    53
                if data['username'].isalnum():
54694601c5e3 imposed a filter on username that it contains only alphabets and numbers.
anoop
parents: 13
diff changeset
    54
                    try:
54694601c5e3 imposed a filter on username that it contains only alphabets and numbers.
anoop
parents: 13
diff changeset
    55
                        if User.objects.get(username__exact = data['username']):
54694601c5e3 imposed a filter on username that it contains only alphabets and numbers.
anoop
parents: 13
diff changeset
    56
                            errors=['Choose some other username']
54694601c5e3 imposed a filter on username that it contains only alphabets and numbers.
anoop
parents: 13
diff changeset
    57
                            return render_to_response('user/register.html',{'form':form,'errors':errors})
54694601c5e3 imposed a filter on username that it contains only alphabets and numbers.
anoop
parents: 13
diff changeset
    58
                    except:
54694601c5e3 imposed a filter on username that it contains only alphabets and numbers.
anoop
parents: 13
diff changeset
    59
                         u = createUser(username=data['username'], email=data['email'], password=data['password'],dob = data['dob'],gender = data['gender'])
54694601c5e3 imposed a filter on username that it contains only alphabets and numbers.
anoop
parents: 13
diff changeset
    60
                    return redirect('/accounts/login/')
54694601c5e3 imposed a filter on username that it contains only alphabets and numbers.
anoop
parents: 13
diff changeset
    61
                else:
54694601c5e3 imposed a filter on username that it contains only alphabets and numbers.
anoop
parents: 13
diff changeset
    62
                    errors = ['Username can contain only alphabets and numbers!']
54694601c5e3 imposed a filter on username that it contains only alphabets and numbers.
anoop
parents: 13
diff changeset
    63
                    return render_to_response('user/register.html',{'form':form,'errors':errors})
13
0fb64b24a1c9 added views, templates for register, login, logout user.
anoop
parents: 12
diff changeset
    64
            else:
0fb64b24a1c9 added views, templates for register, login, logout user.
anoop
parents: 12
diff changeset
    65
                errors=['Password do not match']
0fb64b24a1c9 added views, templates for register, login, logout user.
anoop
parents: 12
diff changeset
    66
                form = RegistrationForm(request.POST)
0fb64b24a1c9 added views, templates for register, login, logout user.
anoop
parents: 12
diff changeset
    67
                return render_to_response('user/register.html',{'form':form,'errors':errors})#HttpResponse('Password did not match')
0fb64b24a1c9 added views, templates for register, login, logout user.
anoop
parents: 12
diff changeset
    68
        else:
15
4e29ed911123 corrected views.user.register method .
anoop
parents: 14
diff changeset
    69
            form = RegistrationForm(request.POST)
13
0fb64b24a1c9 added views, templates for register, login, logout user.
anoop
parents: 12
diff changeset
    70
    else:
0fb64b24a1c9 added views, templates for register, login, logout user.
anoop
parents: 12
diff changeset
    71
        form = RegistrationForm()
0fb64b24a1c9 added views, templates for register, login, logout user.
anoop
parents: 12
diff changeset
    72
    return render_to_response('user/register.html', {'form': form})
0fb64b24a1c9 added views, templates for register, login, logout user.
anoop
parents: 12
diff changeset
    73
0fb64b24a1c9 added views, templates for register, login, logout user.
anoop
parents: 12
diff changeset
    74
def user_login(request):
0fb64b24a1c9 added views, templates for register, login, logout user.
anoop
parents: 12
diff changeset
    75
    if request.method == 'POST':
0fb64b24a1c9 added views, templates for register, login, logout user.
anoop
parents: 12
diff changeset
    76
        username = request.POST['username']
0fb64b24a1c9 added views, templates for register, login, logout user.
anoop
parents: 12
diff changeset
    77
        password = request.POST['password']
0fb64b24a1c9 added views, templates for register, login, logout user.
anoop
parents: 12
diff changeset
    78
        user = authenticate(username=username, password=password)
0fb64b24a1c9 added views, templates for register, login, logout user.
anoop
parents: 12
diff changeset
    79
        if user is not None:
0fb64b24a1c9 added views, templates for register, login, logout user.
anoop
parents: 12
diff changeset
    80
            if user.is_active:
0fb64b24a1c9 added views, templates for register, login, logout user.
anoop
parents: 12
diff changeset
    81
                login(request, user)
0fb64b24a1c9 added views, templates for register, login, logout user.
anoop
parents: 12
diff changeset
    82
                return redirect('/')# Redirect to a success page.
0fb64b24a1c9 added views, templates for register, login, logout user.
anoop
parents: 12
diff changeset
    83
            else:
0fb64b24a1c9 added views, templates for register, login, logout user.
anoop
parents: 12
diff changeset
    84
                return HttpResponse('username is not active, please contact the administrator')# Return a 'disabled account' error message
0fb64b24a1c9 added views, templates for register, login, logout user.
anoop
parents: 12
diff changeset
    85
        else:
0fb64b24a1c9 added views, templates for register, login, logout user.
anoop
parents: 12
diff changeset
    86
            errors = ['Please check your username and password']
0fb64b24a1c9 added views, templates for register, login, logout user.
anoop
parents: 12
diff changeset
    87
            form = LoginForm()
0fb64b24a1c9 added views, templates for register, login, logout user.
anoop
parents: 12
diff changeset
    88
            return render_to_response('user/login.html',{'form':form,'errors':errors})# Return an 'invalid login' error message.
0fb64b24a1c9 added views, templates for register, login, logout user.
anoop
parents: 12
diff changeset
    89
        return redirect('/')
0fb64b24a1c9 added views, templates for register, login, logout user.
anoop
parents: 12
diff changeset
    90
    else:
0fb64b24a1c9 added views, templates for register, login, logout user.
anoop
parents: 12
diff changeset
    91
        form = LoginForm()
0fb64b24a1c9 added views, templates for register, login, logout user.
anoop
parents: 12
diff changeset
    92
        return render_to_response('user/login.html',{'form': form})
0fb64b24a1c9 added views, templates for register, login, logout user.
anoop
parents: 12
diff changeset
    93
0fb64b24a1c9 added views, templates for register, login, logout user.
anoop
parents: 12
diff changeset
    94
def user_logout(request):
0fb64b24a1c9 added views, templates for register, login, logout user.
anoop
parents: 12
diff changeset
    95
    logout(request)
0fb64b24a1c9 added views, templates for register, login, logout user.
anoop
parents: 12
diff changeset
    96
    return HttpResponse('You have logged off successfully!!!')