taskapp/views/user.py
author nishanth
Wed, 24 Feb 2010 20:13:20 +0530
changeset 86 fcbdf372857c
parent 83 fd2e1bd7af82
child 97 3699550991c6
permissions -rw-r--r--
modified requests to match the new model.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
61
708dd49d531b added custom image storage for profile photo.
nishanth
parents: 60
diff changeset
     1
import os
83
fd2e1bd7af82 added accept_assign_credits request
nishanth
parents: 69
diff changeset
     2
42
9b5b8c997598 started using django-registration default backend, removed browse users functionality.
anoop
parents: 40
diff changeset
     3
from django.http import HttpResponse, Http404
17
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 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
     5
from django.contrib.auth.models import User
39
476613c3ab0a Only logged in users can view user profile.
anoop
parents: 28
diff changeset
     6
from django.contrib.auth.decorators import login_required
476613c3ab0a Only logged in users can view user profile.
anoop
parents: 28
diff changeset
     7
83
fd2e1bd7af82 added accept_assign_credits request
nishanth
parents: 69
diff changeset
     8
from pytask.taskapp.models import Task, Profile, Request
fd2e1bd7af82 added accept_assign_credits request
nishanth
parents: 69
diff changeset
     9
from pytask.taskapp.events.user import createUser, updateProfile
fd2e1bd7af82 added accept_assign_credits request
nishanth
parents: 69
diff changeset
    10
from pytask.taskapp.forms.user import UserProfileEditForm
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
39
476613c3ab0a Only logged in users can view user profile.
anoop
parents: 28
diff changeset
    51
@login_required
42
9b5b8c997598 started using django-registration default backend, removed browse users functionality.
anoop
parents: 40
diff changeset
    52
def view_my_profile(request,uid=None):
23
f33084ea1361 added view profile and edit profile functionalities.
anoop
parents: 21
diff changeset
    53
    """ allows the user to view the profiles of users """
42
9b5b8c997598 started using django-registration default backend, removed browse users functionality.
anoop
parents: 40
diff changeset
    54
    if uid == None:
9b5b8c997598 started using django-registration default backend, removed browse users functionality.
anoop
parents: 40
diff changeset
    55
        edit_profile = True
9b5b8c997598 started using django-registration default backend, removed browse users functionality.
anoop
parents: 40
diff changeset
    56
        profile = Profile.objects.get(user = request.user)
69
49532a0f5071 added user context variable in view_profile and edit_profile.
nishanth
parents: 68
diff changeset
    57
        return render_to_response('user/my_profile.html', {'edit_profile':edit_profile,'profile':profile, 'user':request.user})
23
f33084ea1361 added view profile and edit profile functionalities.
anoop
parents: 21
diff changeset
    58
    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
    59
    try:
f33084ea1361 added view profile and edit profile functionalities.
anoop
parents: 21
diff changeset
    60
        profile = Profile.objects.get(user = User.objects.get(pk=uid))
f33084ea1361 added view profile and edit profile functionalities.
anoop
parents: 21
diff changeset
    61
    except Profile.DoesNotExist:
f33084ea1361 added view profile and edit profile functionalities.
anoop
parents: 21
diff changeset
    62
        raise Http404
69
49532a0f5071 added user context variable in view_profile and edit_profile.
nishanth
parents: 68
diff changeset
    63
    return render_to_response('user/my_profile.html', {'edit_profile':edit_profile,'profile':profile, 'user':request.user})
23
f33084ea1361 added view profile and edit profile functionalities.
anoop
parents: 21
diff changeset
    64
45
ab8918e654ae added forgot password in template.
anoop
parents: 42
diff changeset
    65
@login_required
23
f33084ea1361 added view profile and edit profile functionalities.
anoop
parents: 21
diff changeset
    66
def edit_my_profile(request):
f33084ea1361 added view profile and edit profile functionalities.
anoop
parents: 21
diff changeset
    67
    """ enables the user to edit his/her user profile """
f33084ea1361 added view profile and edit profile functionalities.
anoop
parents: 21
diff changeset
    68
    if request.method == 'POST':
f33084ea1361 added view profile and edit profile functionalities.
anoop
parents: 21
diff changeset
    69
        form = UserProfileEditForm(request.POST)
f33084ea1361 added view profile and edit profile functionalities.
anoop
parents: 21
diff changeset
    70
#        if not form.is_valid():
f33084ea1361 added view profile and edit profile functionalities.
anoop
parents: 21
diff changeset
    71
#            edit_profile_form = UserProfileEditForm(instance = form)
f33084ea1361 added view profile and edit profile functionalities.
anoop
parents: 21
diff changeset
    72
#            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
    73
        if request.user.is_authenticated() == True:
f33084ea1361 added view profile and edit profile functionalities.
anoop
parents: 21
diff changeset
    74
            profile = Profile.objects.get(user = request.user)
f33084ea1361 added view profile and edit profile functionalities.
anoop
parents: 21
diff changeset
    75
            data = request.POST#form.cleaned_data
61
708dd49d531b added custom image storage for profile photo.
nishanth
parents: 60
diff changeset
    76
            properties = {'aboutme':data['aboutme'],
708dd49d531b added custom image storage for profile photo.
nishanth
parents: 60
diff changeset
    77
                          'foss_comm':data['foss_comm'],
708dd49d531b added custom image storage for profile photo.
nishanth
parents: 60
diff changeset
    78
                          'phonenum':data['phonenum'],
708dd49d531b added custom image storage for profile photo.
nishanth
parents: 60
diff changeset
    79
                          'homepage':data['homepage'],
708dd49d531b added custom image storage for profile photo.
nishanth
parents: 60
diff changeset
    80
                          'street':data['street'],
708dd49d531b added custom image storage for profile photo.
nishanth
parents: 60
diff changeset
    81
                          'city':data['city'],
708dd49d531b added custom image storage for profile photo.
nishanth
parents: 60
diff changeset
    82
                          'country':data['country'],
708dd49d531b added custom image storage for profile photo.
nishanth
parents: 60
diff changeset
    83
                          'nick':data['nick']}
708dd49d531b added custom image storage for profile photo.
nishanth
parents: 60
diff changeset
    84
            uploaded_photo = request.FILES.get('photo',None)
708dd49d531b added custom image storage for profile photo.
nishanth
parents: 60
diff changeset
    85
            prev_photo = profile.photo
708dd49d531b added custom image storage for profile photo.
nishanth
parents: 60
diff changeset
    86
            if uploaded_photo:
708dd49d531b added custom image storage for profile photo.
nishanth
parents: 60
diff changeset
    87
                if prev_photo:
708dd49d531b added custom image storage for profile photo.
nishanth
parents: 60
diff changeset
    88
                    os.remove(prev_photo.path)
708dd49d531b added custom image storage for profile photo.
nishanth
parents: 60
diff changeset
    89
                properties['photo'] = uploaded_photo
23
f33084ea1361 added view profile and edit profile functionalities.
anoop
parents: 21
diff changeset
    90
            #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
    91
            updateProfile(profile,properties)
f33084ea1361 added view profile and edit profile functionalities.
anoop
parents: 21
diff changeset
    92
            return redirect('/user/view/uid='+str(profile.user_id))
f33084ea1361 added view profile and edit profile functionalities.
anoop
parents: 21
diff changeset
    93
    else:
f33084ea1361 added view profile and edit profile functionalities.
anoop
parents: 21
diff changeset
    94
        profile = Profile.objects.get(user = request.user)
f33084ea1361 added view profile and edit profile functionalities.
anoop
parents: 21
diff changeset
    95
        edit_profile_form = UserProfileEditForm(instance = profile)
69
49532a0f5071 added user context variable in view_profile and edit_profile.
nishanth
parents: 68
diff changeset
    96
        return render_to_response('user/edit_profile.html',{'edit_profile_form' : edit_profile_form, 'user':request.user})
28
e137b605b888 added browse users functionality, added user/browse.html, fixed view my profile template.
anoop
parents: 27
diff changeset
    97
83
fd2e1bd7af82 added accept_assign_credits request
nishanth
parents: 69
diff changeset
    98
@login_required
fd2e1bd7af82 added accept_assign_credits request
nishanth
parents: 69
diff changeset
    99
def browse_requests(request):
fd2e1bd7af82 added accept_assign_credits request
nishanth
parents: 69
diff changeset
   100
    
fd2e1bd7af82 added accept_assign_credits request
nishanth
parents: 69
diff changeset
   101
    user = request.user
86
fcbdf372857c modified requests to match the new model.
nishanth
parents: 83
diff changeset
   102
    active_reqs = user.request_sent_to.filter(is_replied=False)
83
fd2e1bd7af82 added accept_assign_credits request
nishanth
parents: 69
diff changeset
   103
    reqs = active_reqs.order_by('creation_date').reverse()
fd2e1bd7af82 added accept_assign_credits request
nishanth
parents: 69
diff changeset
   104
    for pos, req in enumerate(reversed(reqs)):
fd2e1bd7af82 added accept_assign_credits request
nishanth
parents: 69
diff changeset
   105
        req.pos = pos
fd2e1bd7af82 added accept_assign_credits request
nishanth
parents: 69
diff changeset
   106
    context = {
fd2e1bd7af82 added accept_assign_credits request
nishanth
parents: 69
diff changeset
   107
        'user':user,
fd2e1bd7af82 added accept_assign_credits request
nishanth
parents: 69
diff changeset
   108
        'reqs':reqs,
fd2e1bd7af82 added accept_assign_credits request
nishanth
parents: 69
diff changeset
   109
    }
fd2e1bd7af82 added accept_assign_credits request
nishanth
parents: 69
diff changeset
   110
fd2e1bd7af82 added accept_assign_credits request
nishanth
parents: 69
diff changeset
   111
    return render_to_response('user/browse_requests.html', context)
fd2e1bd7af82 added accept_assign_credits request
nishanth
parents: 69
diff changeset
   112
fd2e1bd7af82 added accept_assign_credits request
nishanth
parents: 69
diff changeset
   113
@login_required
fd2e1bd7af82 added accept_assign_credits request
nishanth
parents: 69
diff changeset
   114
def view_request(request, rid):
fd2e1bd7af82 added accept_assign_credits request
nishanth
parents: 69
diff changeset
   115
    """ please note that request variable in this method is that of django.
fd2e1bd7af82 added accept_assign_credits request
nishanth
parents: 69
diff changeset
   116
    our app request is called user_request.
fd2e1bd7af82 added accept_assign_credits request
nishanth
parents: 69
diff changeset
   117
    """
fd2e1bd7af82 added accept_assign_credits request
nishanth
parents: 69
diff changeset
   118
fd2e1bd7af82 added accept_assign_credits request
nishanth
parents: 69
diff changeset
   119
    user = request.user
86
fcbdf372857c modified requests to match the new model.
nishanth
parents: 83
diff changeset
   120
    reqs = user.request_sent_to.filter(is_replied=False).order_by('creation_date')
83
fd2e1bd7af82 added accept_assign_credits request
nishanth
parents: 69
diff changeset
   121
    user_request = reqs[int(rid)]
fd2e1bd7af82 added accept_assign_credits request
nishanth
parents: 69
diff changeset
   122
fd2e1bd7af82 added accept_assign_credits request
nishanth
parents: 69
diff changeset
   123
    context = {
fd2e1bd7af82 added accept_assign_credits request
nishanth
parents: 69
diff changeset
   124
        'user':user,
fd2e1bd7af82 added accept_assign_credits request
nishanth
parents: 69
diff changeset
   125
        'req':user_request,
86
fcbdf372857c modified requests to match the new model.
nishanth
parents: 83
diff changeset
   126
        'sent_users':user_request.sent_to.all()
83
fd2e1bd7af82 added accept_assign_credits request
nishanth
parents: 69
diff changeset
   127
    }
fd2e1bd7af82 added accept_assign_credits request
nishanth
parents: 69
diff changeset
   128
fd2e1bd7af82 added accept_assign_credits request
nishanth
parents: 69
diff changeset
   129
    return render_to_response('user/view_request.html', context)
fd2e1bd7af82 added accept_assign_credits request
nishanth
parents: 69
diff changeset
   130
fd2e1bd7af82 added accept_assign_credits request
nishanth
parents: 69
diff changeset
   131
@login_required
fd2e1bd7af82 added accept_assign_credits request
nishanth
parents: 69
diff changeset
   132
def process_request(request, rid, reply):
fd2e1bd7af82 added accept_assign_credits request
nishanth
parents: 69
diff changeset
   133
    """ check if the method is post and then update the request.
fd2e1bd7af82 added accept_assign_credits request
nishanth
parents: 69
diff changeset
   134
    if it is get, display a 404 error.
fd2e1bd7af82 added accept_assign_credits request
nishanth
parents: 69
diff changeset
   135
    """
fd2e1bd7af82 added accept_assign_credits request
nishanth
parents: 69
diff changeset
   136
fd2e1bd7af82 added accept_assign_credits request
nishanth
parents: 69
diff changeset
   137
    if request.method=="POST":
fd2e1bd7af82 added accept_assign_credits request
nishanth
parents: 69
diff changeset
   138
        user = request.user
fd2e1bd7af82 added accept_assign_credits request
nishanth
parents: 69
diff changeset
   139
        browse_request_url= '/user/requests'
86
fcbdf372857c modified requests to match the new model.
nishanth
parents: 83
diff changeset
   140
        reqs = user.request_sent_to.filter(is_replied=False).order_by('creation_date')
83
fd2e1bd7af82 added accept_assign_credits request
nishanth
parents: 69
diff changeset
   141
        user_request = reqs[int(rid)]
86
fcbdf372857c modified requests to match the new model.
nishanth
parents: 83
diff changeset
   142
        
83
fd2e1bd7af82 added accept_assign_credits request
nishanth
parents: 69
diff changeset
   143
        return show_msg("Your reply has been processed", browse_request_url, "view other requests")
fd2e1bd7af82 added accept_assign_credits request
nishanth
parents: 69
diff changeset
   144
    else:
fd2e1bd7af82 added accept_assign_credits request
nishanth
parents: 69
diff changeset
   145
        return show_msg("You are not authorised to do this", browse_request_url, "view other requests")