taskapp/views/user.py
changeset 23 f33084ea1361
parent 21 c28774fe7ffd
child 27 8d52492d3c14
equal deleted inserted replaced
22:36d3173ab7f9 23:f33084ea1361
     1 from django.http import HttpResponse
     1 from django.http import HttpResponse
     2 from django.shortcuts import redirect, render_to_response
     2 from django.shortcuts import redirect, render_to_response
     3 from pytask.taskapp.models import Task
     3 from pytask.taskapp.models import Task
     4 from pytask.taskapp.forms.user import RegistrationForm, LoginForm
     4 from pytask.taskapp.forms.user import RegistrationForm, LoginForm, UserProfileForm, UserProfileEditForm
     5 from pytask.taskapp.events.user import createUser
     5 from pytask.taskapp.events.user import createUser, updateProfile
     6 from django.contrib.auth import login, logout, authenticate
     6 from django.contrib.auth import login, logout, authenticate
     7 from django.contrib.auth.models import User
     7 from django.contrib.auth.models import User
       
     8 from pytask.taskapp.models import Profile
     8 
     9 
     9 def show_msg(message, redirect_url=None, url_desc=None):
    10 def show_msg(message, redirect_url=None, url_desc=None):
    10     """ simply redirect to homepage """
    11     """ simply redirect to homepage """
    11     
    12     
    12     return render_to_response('show_msg.html',{'message':message, 'redirect_url':redirect_url, 'url_desc':url_desc})
    13     return render_to_response('show_msg.html',{'message':message, 'redirect_url':redirect_url, 'url_desc':url_desc})
    95         return render_to_response('user/login.html',{'form': form})
    96         return render_to_response('user/login.html',{'form': form})
    96 
    97 
    97 def user_logout(request):
    98 def user_logout(request):
    98     logout(request)
    99     logout(request)
    99     return show_msg('You have logged off successfully!!!')
   100     return show_msg('You have logged off successfully!!!')
       
   101 
       
   102 def view_my_profile(request,uid):
       
   103     """ allows the user to view the profiles of users """
       
   104     edit_profile = True if request.user == User.objects.get(pk=uid) else False
       
   105     try:
       
   106         profile = Profile.objects.get(user = User.objects.get(pk=uid))
       
   107         view_profile_form = UserProfileForm(instance = profile)
       
   108     except Profile.DoesNotExist:
       
   109         raise Http404
       
   110     return render_to_response('user/my_profile.html', {'view_profile_form': view_profile_form,'edit_profile':edit_profile})
       
   111 
       
   112 def edit_my_profile(request):
       
   113     """ enables the user to edit his/her user profile """
       
   114     if str(request.user) == 'AnonymousUser':
       
   115         return show_msg('Please register yourself to activate the functionality')
       
   116     if request.method == 'POST':
       
   117         form = UserProfileEditForm(request.POST)
       
   118 #        if not form.is_valid():
       
   119 #            edit_profile_form = UserProfileEditForm(instance = form)
       
   120 #            return render_to_response('user/edit_profile.html',{'edit_profile_form' : edit_profile_form})
       
   121         if request.user.is_authenticated() == True:
       
   122             profile = Profile.objects.get(user = request.user)
       
   123             data = request.POST#form.cleaned_data
       
   124             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']}
       
   125             #fields = ['dob','gender','credits','aboutme','foss_comm','phonenum','homepage','street','city','country','nick']
       
   126             updateProfile(profile,properties)
       
   127             return redirect('/user/view/uid='+str(profile.user_id))
       
   128     else:
       
   129         profile = Profile.objects.get(user = request.user)
       
   130         edit_profile_form = UserProfileEditForm(instance = profile)
       
   131         return render_to_response('user/edit_profile.html',{'edit_profile_form' : edit_profile_form})