pytask/taskapp/views/user.py
author nishanth
Mon, 01 Feb 2010 11:10:29 +0530
changeset 14 f2623fb8041a
parent 13 4da58abdf6ff
child 16 d57e63325759
permissions -rw-r--r--
implemented add another mentor functionality to a task.
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
14
f2623fb8041a implemented add another mentor functionality to a task.
nishanth
parents: 13
diff changeset
     9
def show_msg(message, redirect_url=None, url_desc=None):
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
    
14
f2623fb8041a implemented add another mentor functionality to a task.
nishanth
parents: 13
diff changeset
    12
    return render_to_response('show_msg.html',{'message':message, 'redirect_url':redirect_url, 'url_desc':url_desc})
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]
14
f2623fb8041a implemented add another mentor functionality to a task.
nishanth
parents: 13
diff changeset
    31
            
f2623fb8041a implemented add another mentor functionality to a task.
nishanth
parents: 13
diff changeset
    32
        return render_to_response('index.html', {'is_guest':is_guest, 'task_list':task_list})
f2623fb8041a implemented add another mentor functionality to a task.
nishanth
parents: 13
diff changeset
    33
        
5
aea7e764c033 created views and templates for homepage,browse_task and added actions.
nishanth
parents: 2
diff changeset
    34
    else:
aea7e764c033 created views and templates for homepage,browse_task and added actions.
nishanth
parents: 2
diff changeset
    35
        user_profile = user.get_profile()
aea7e764c033 created views and templates for homepage,browse_task and added actions.
nishanth
parents: 2
diff changeset
    36
        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
    37
        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
    38
        
14
f2623fb8041a implemented add another mentor functionality to a task.
nishanth
parents: 13
diff changeset
    39
        context = {'user':user,
f2623fb8041a implemented add another mentor functionality to a task.
nishanth
parents: 13
diff changeset
    40
                   'is_guest':is_guest,
f2623fb8041a implemented add another mentor functionality to a task.
nishanth
parents: 13
diff changeset
    41
                   'is_mentor':is_mentor,
f2623fb8041a implemented add another mentor functionality to a task.
nishanth
parents: 13
diff changeset
    42
                   'task_list':task_list,
f2623fb8041a implemented add another mentor functionality to a task.
nishanth
parents: 13
diff changeset
    43
                   'can_create_task':can_create_task,
f2623fb8041a implemented add another mentor functionality to a task.
nishanth
parents: 13
diff changeset
    44
                   }
f2623fb8041a implemented add another mentor functionality to a task.
nishanth
parents: 13
diff changeset
    45
                   
f2623fb8041a implemented add another mentor functionality to a task.
nishanth
parents: 13
diff changeset
    46
        return render_to_response('index.html', context)
6
94136f9a48bc added login, logout, and register.
anoop
parents: 5
diff changeset
    47
5
aea7e764c033 created views and templates for homepage,browse_task and added actions.
nishanth
parents: 2
diff changeset
    48
6
94136f9a48bc added login, logout, and register.
anoop
parents: 5
diff changeset
    49
def register(request):
94136f9a48bc added login, logout, and register.
anoop
parents: 5
diff changeset
    50
    """ 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
    51
    if request.method == 'POST':
94136f9a48bc added login, logout, and register.
anoop
parents: 5
diff changeset
    52
        form = RegistrationForm(request.POST)
94136f9a48bc added login, logout, and register.
anoop
parents: 5
diff changeset
    53
        if form.is_valid():
94136f9a48bc added login, logout, and register.
anoop
parents: 5
diff changeset
    54
            data = form.cleaned_data
94136f9a48bc added login, logout, and register.
anoop
parents: 5
diff changeset
    55
            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
    56
                if data['username'].isalnum():
232d40a28118 imposed a filter on username that it contains only alphabets and numbers.
anoop
parents: 6
diff changeset
    57
                    try:
232d40a28118 imposed a filter on username that it contains only alphabets and numbers.
anoop
parents: 6
diff changeset
    58
                        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
    59
                            errors=['Choose some other username']
232d40a28118 imposed a filter on username that it contains only alphabets and numbers.
anoop
parents: 6
diff changeset
    60
                            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
    61
                    except:
232d40a28118 imposed a filter on username that it contains only alphabets and numbers.
anoop
parents: 6
diff changeset
    62
                         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
    63
                    return redirect('/accounts/login/')
232d40a28118 imposed a filter on username that it contains only alphabets and numbers.
anoop
parents: 6
diff changeset
    64
                else:
232d40a28118 imposed a filter on username that it contains only alphabets and numbers.
anoop
parents: 6
diff changeset
    65
                    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
    66
                    return render_to_response('user/register.html',{'form':form,'errors':errors})
6
94136f9a48bc added login, logout, and register.
anoop
parents: 5
diff changeset
    67
            else:
94136f9a48bc added login, logout, and register.
anoop
parents: 5
diff changeset
    68
                errors=['Password do not match']
94136f9a48bc added login, logout, and register.
anoop
parents: 5
diff changeset
    69
                form = RegistrationForm(request.POST)
94136f9a48bc added login, logout, and register.
anoop
parents: 5
diff changeset
    70
                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
    71
        else:
8
825a497d95b0 corrected views/user/register method
anoop
parents: 7
diff changeset
    72
            form = RegistrationForm(request.POST)
6
94136f9a48bc added login, logout, and register.
anoop
parents: 5
diff changeset
    73
    else:
94136f9a48bc added login, logout, and register.
anoop
parents: 5
diff changeset
    74
        form = RegistrationForm()
94136f9a48bc added login, logout, and register.
anoop
parents: 5
diff changeset
    75
    return render_to_response('user/register.html', {'form': form})
94136f9a48bc added login, logout, and register.
anoop
parents: 5
diff changeset
    76
94136f9a48bc added login, logout, and register.
anoop
parents: 5
diff changeset
    77
def user_login(request):
94136f9a48bc added login, logout, and register.
anoop
parents: 5
diff changeset
    78
    if request.method == 'POST':
94136f9a48bc added login, logout, and register.
anoop
parents: 5
diff changeset
    79
        username = request.POST['username']
94136f9a48bc added login, logout, and register.
anoop
parents: 5
diff changeset
    80
        password = request.POST['password']
94136f9a48bc added login, logout, and register.
anoop
parents: 5
diff changeset
    81
        user = authenticate(username=username, password=password)
94136f9a48bc added login, logout, and register.
anoop
parents: 5
diff changeset
    82
        if user is not None:
94136f9a48bc added login, logout, and register.
anoop
parents: 5
diff changeset
    83
            if user.is_active:
94136f9a48bc added login, logout, and register.
anoop
parents: 5
diff changeset
    84
                login(request, user)
94136f9a48bc added login, logout, and register.
anoop
parents: 5
diff changeset
    85
                return redirect('/')# Redirect to a success page.
94136f9a48bc added login, logout, and register.
anoop
parents: 5
diff changeset
    86
            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
    87
                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
    88
        else:
94136f9a48bc added login, logout, and register.
anoop
parents: 5
diff changeset
    89
            errors = ['Please check your username and password']
94136f9a48bc added login, logout, and register.
anoop
parents: 5
diff changeset
    90
            form = LoginForm()
94136f9a48bc added login, logout, and register.
anoop
parents: 5
diff changeset
    91
            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
    92
        return redirect('/')
94136f9a48bc added login, logout, and register.
anoop
parents: 5
diff changeset
    93
    else:
94136f9a48bc added login, logout, and register.
anoop
parents: 5
diff changeset
    94
        form = LoginForm()
94136f9a48bc added login, logout, and register.
anoop
parents: 5
diff changeset
    95
        return render_to_response('user/login.html',{'form': form})
94136f9a48bc added login, logout, and register.
anoop
parents: 5
diff changeset
    96
94136f9a48bc added login, logout, and register.
anoop
parents: 5
diff changeset
    97
def user_logout(request):
94136f9a48bc added login, logout, and register.
anoop
parents: 5
diff changeset
    98
    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
    99
    return show_msg('You have logged off successfully!!!')