taskapp/views/user.py
author anoop
Mon, 15 Feb 2010 15:14:27 +0530
changeset 39 476613c3ab0a
parent 28 e137b605b888
child 40 43853e376bb8
permissions -rw-r--r--
Only logged in users can view user profile.
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
27
8d52492d3c14 changed the view my profile template.
anoop
parents: 23
diff changeset
     4
from pytask.taskapp.forms.user import RegistrationForm, LoginForm, UserProfileEditForm
23
f33084ea1361 added view profile and edit profile functionalities.
anoop
parents: 21
diff changeset
     5
from pytask.taskapp.events.user import createUser, updateProfile
17
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
23
f33084ea1361 added view profile and edit profile functionalities.
anoop
parents: 21
diff changeset
     8
from pytask.taskapp.models import Profile
39
476613c3ab0a Only logged in users can view user profile.
anoop
parents: 28
diff changeset
     9
from django.contrib.auth.decorators import login_required
476613c3ab0a Only logged in users can view user profile.
anoop
parents: 28
diff changeset
    10
17
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
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
    13
    """ 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
    14
    
21
c28774fe7ffd implemented "add another mentor" functionality to a task.
nishanth
parents: 20
diff changeset
    15
    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
    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
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
    18
    """ 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
    19
    
aa45fec40e7e renamed users.py to user.py and tasks to task in views folder and updated urls.py accordingly .
nishanth
parents:
diff changeset
    20
    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
    21
    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
    22
    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
    23
    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
    24
    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
    25
    
aa45fec40e7e renamed users.py to user.py and tasks to task in views folder and updated urls.py accordingly .
nishanth
parents:
diff changeset
    26
    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
    27
        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
    28
        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
    29
        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
    30
        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
    31
            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
    32
        else:
aa45fec40e7e renamed users.py to user.py and tasks to task in views folder and updated urls.py accordingly .
nishanth
parents:
diff changeset
    33
            task_list = Task.objects.order_by('id').reverse()[:10]
21
c28774fe7ffd implemented "add another mentor" functionality to a task.
nishanth
parents: 20
diff changeset
    34
            
c28774fe7ffd implemented "add another mentor" functionality to a task.
nishanth
parents: 20
diff changeset
    35
        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
    36
        
17
aa45fec40e7e renamed users.py to user.py and tasks to task in views folder and updated urls.py accordingly .
nishanth
parents:
diff changeset
    37
    else:
aa45fec40e7e renamed users.py to user.py and tasks to task in views folder and updated urls.py accordingly .
nishanth
parents:
diff changeset
    38
        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
    39
        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
    40
        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
    41
        
21
c28774fe7ffd implemented "add another mentor" functionality to a task.
nishanth
parents: 20
diff changeset
    42
        context = {'user':user,
c28774fe7ffd implemented "add another mentor" functionality to a task.
nishanth
parents: 20
diff changeset
    43
                   'is_guest':is_guest,
c28774fe7ffd implemented "add another mentor" functionality to a task.
nishanth
parents: 20
diff changeset
    44
                   'is_mentor':is_mentor,
c28774fe7ffd implemented "add another mentor" functionality to a task.
nishanth
parents: 20
diff changeset
    45
                   'task_list':task_list,
c28774fe7ffd implemented "add another mentor" functionality to a task.
nishanth
parents: 20
diff changeset
    46
                   'can_create_task':can_create_task,
c28774fe7ffd implemented "add another mentor" functionality to a task.
nishanth
parents: 20
diff changeset
    47
                   }
c28774fe7ffd implemented "add another mentor" functionality to a task.
nishanth
parents: 20
diff changeset
    48
                   
c28774fe7ffd implemented "add another mentor" functionality to a task.
nishanth
parents: 20
diff changeset
    49
        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
    50
aa45fec40e7e renamed users.py to user.py and tasks to task in views folder and updated urls.py accordingly .
nishanth
parents:
diff changeset
    51
aa45fec40e7e renamed users.py to user.py and tasks to task in views folder and updated urls.py accordingly .
nishanth
parents:
diff changeset
    52
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
    53
    """ 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
    54
    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
    55
        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
    56
        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
    57
            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
    58
            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
    59
                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
    60
                    try:
aa45fec40e7e renamed users.py to user.py and tasks to task in views folder and updated urls.py accordingly .
nishanth
parents:
diff changeset
    61
                        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
    62
                            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
    63
                            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
    64
                    except:
aa45fec40e7e renamed users.py to user.py and tasks to task in views folder and updated urls.py accordingly .
nishanth
parents:
diff changeset
    65
                         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
    66
                    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
    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 = ['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
    69
                    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
    70
            else:
aa45fec40e7e renamed users.py to user.py and tasks to task in views folder and updated urls.py accordingly .
nishanth
parents:
diff changeset
    71
                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
    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
                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
    74
        else:
aa45fec40e7e renamed users.py to user.py and tasks to task in views folder and updated urls.py accordingly .
nishanth
parents:
diff changeset
    75
            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
    76
    else:
aa45fec40e7e renamed users.py to user.py and tasks to task in views folder and updated urls.py accordingly .
nishanth
parents:
diff changeset
    77
        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
    78
    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
    79
aa45fec40e7e renamed users.py to user.py and tasks to task in views folder and updated urls.py accordingly .
nishanth
parents:
diff changeset
    80
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
    81
    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
    82
        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
    83
        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
    84
        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
    85
        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
    86
            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
    87
                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
    88
                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
    89
            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
    90
                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
    91
        else:
aa45fec40e7e renamed users.py to user.py and tasks to task in views folder and updated urls.py accordingly .
nishanth
parents:
diff changeset
    92
            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
    93
            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
    94
            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
    95
        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
    96
    else:
aa45fec40e7e renamed users.py to user.py and tasks to task in views folder and updated urls.py accordingly .
nishanth
parents:
diff changeset
    97
        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
    98
        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
    99
aa45fec40e7e renamed users.py to user.py and tasks to task in views folder and updated urls.py accordingly .
nishanth
parents:
diff changeset
   100
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
   101
    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
   102
    return show_msg('You have logged off successfully!!!')
23
f33084ea1361 added view profile and edit profile functionalities.
anoop
parents: 21
diff changeset
   103
39
476613c3ab0a Only logged in users can view user profile.
anoop
parents: 28
diff changeset
   104
@login_required
23
f33084ea1361 added view profile and edit profile functionalities.
anoop
parents: 21
diff changeset
   105
def view_my_profile(request,uid):
f33084ea1361 added view profile and edit profile functionalities.
anoop
parents: 21
diff changeset
   106
    """ allows the user to view the profiles of users """
f33084ea1361 added view profile and edit profile functionalities.
anoop
parents: 21
diff changeset
   107
    edit_profile = True if request.user == User.objects.get(pk=uid) else False
f33084ea1361 added view profile and edit profile functionalities.
anoop
parents: 21
diff changeset
   108
    try:
f33084ea1361 added view profile and edit profile functionalities.
anoop
parents: 21
diff changeset
   109
        profile = Profile.objects.get(user = User.objects.get(pk=uid))
f33084ea1361 added view profile and edit profile functionalities.
anoop
parents: 21
diff changeset
   110
    except Profile.DoesNotExist:
f33084ea1361 added view profile and edit profile functionalities.
anoop
parents: 21
diff changeset
   111
        raise Http404
27
8d52492d3c14 changed the view my profile template.
anoop
parents: 23
diff changeset
   112
    return render_to_response('user/my_profile.html', {'edit_profile':edit_profile,'profile':profile})
23
f33084ea1361 added view profile and edit profile functionalities.
anoop
parents: 21
diff changeset
   113
f33084ea1361 added view profile and edit profile functionalities.
anoop
parents: 21
diff changeset
   114
def edit_my_profile(request):
f33084ea1361 added view profile and edit profile functionalities.
anoop
parents: 21
diff changeset
   115
    """ enables the user to edit his/her user profile """
f33084ea1361 added view profile and edit profile functionalities.
anoop
parents: 21
diff changeset
   116
    if str(request.user) == 'AnonymousUser':
f33084ea1361 added view profile and edit profile functionalities.
anoop
parents: 21
diff changeset
   117
        return show_msg('Please register yourself to activate the functionality')
f33084ea1361 added view profile and edit profile functionalities.
anoop
parents: 21
diff changeset
   118
    if request.method == 'POST':
f33084ea1361 added view profile and edit profile functionalities.
anoop
parents: 21
diff changeset
   119
        form = UserProfileEditForm(request.POST)
f33084ea1361 added view profile and edit profile functionalities.
anoop
parents: 21
diff changeset
   120
#        if not form.is_valid():
f33084ea1361 added view profile and edit profile functionalities.
anoop
parents: 21
diff changeset
   121
#            edit_profile_form = UserProfileEditForm(instance = form)
f33084ea1361 added view profile and edit profile functionalities.
anoop
parents: 21
diff changeset
   122
#            return render_to_response('user/edit_profile.html',{'edit_profile_form' : edit_profile_form})
f33084ea1361 added view profile and edit profile functionalities.
anoop
parents: 21
diff changeset
   123
        if request.user.is_authenticated() == True:
f33084ea1361 added view profile and edit profile functionalities.
anoop
parents: 21
diff changeset
   124
            profile = Profile.objects.get(user = request.user)
f33084ea1361 added view profile and edit profile functionalities.
anoop
parents: 21
diff changeset
   125
            data = request.POST#form.cleaned_data
f33084ea1361 added view profile and edit profile functionalities.
anoop
parents: 21
diff changeset
   126
            properties = {'aboutme':data['aboutme'], 'foss_comm':data['foss_comm'], 'phonenum':data['phonenum'], 'homepage':data['homepage'], 'street':data['street'], 'city':data['city'], 'country':data['country'], 'nick':data['nick']}
f33084ea1361 added view profile and edit profile functionalities.
anoop
parents: 21
diff changeset
   127
            #fields = ['dob','gender','credits','aboutme','foss_comm','phonenum','homepage','street','city','country','nick']
f33084ea1361 added view profile and edit profile functionalities.
anoop
parents: 21
diff changeset
   128
            updateProfile(profile,properties)
f33084ea1361 added view profile and edit profile functionalities.
anoop
parents: 21
diff changeset
   129
            return redirect('/user/view/uid='+str(profile.user_id))
f33084ea1361 added view profile and edit profile functionalities.
anoop
parents: 21
diff changeset
   130
    else:
f33084ea1361 added view profile and edit profile functionalities.
anoop
parents: 21
diff changeset
   131
        profile = Profile.objects.get(user = request.user)
f33084ea1361 added view profile and edit profile functionalities.
anoop
parents: 21
diff changeset
   132
        edit_profile_form = UserProfileEditForm(instance = profile)
f33084ea1361 added view profile and edit profile functionalities.
anoop
parents: 21
diff changeset
   133
        return render_to_response('user/edit_profile.html',{'edit_profile_form' : edit_profile_form})
28
e137b605b888 added browse users functionality, added user/browse.html, fixed view my profile template.
anoop
parents: 27
diff changeset
   134
e137b605b888 added browse users functionality, added user/browse.html, fixed view my profile template.
anoop
parents: 27
diff changeset
   135
def browse_users(request):
e137b605b888 added browse users functionality, added user/browse.html, fixed view my profile template.
anoop
parents: 27
diff changeset
   136
    userlist = User.objects.order_by('username')
e137b605b888 added browse users functionality, added user/browse.html, fixed view my profile template.
anoop
parents: 27
diff changeset
   137
    return render_to_response('user/browse.html',{'userlist':userlist})