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