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