taskapp/views/user.py
changeset 206 85660d75683d
parent 186 c083ebb6f8d7
child 208 ba839dd7c7d7
equal deleted inserted replaced
205:0c317f68df49 206:85660d75683d
     5 from django.contrib.auth.models import User
     5 from django.contrib.auth.models import User
     6 from django.contrib.auth.decorators import login_required
     6 from django.contrib.auth.decorators import login_required
     7 
     7 
     8 from pytask.taskapp.models import Task, Profile, Request
     8 from pytask.taskapp.models import Task, Profile, Request
     9 
     9 
    10 from pytask.taskapp.events.user import createUser, updateProfile
    10 from pytask.taskapp.events.user import createUser
    11 from pytask.taskapp.events.request import reply_to_request
    11 from pytask.taskapp.events.request import reply_to_request
    12 
    12 
    13 from pytask.taskapp.forms.user import UserProfileEditForm, UserChoiceForm
    13 from pytask.taskapp.forms.user import UserProfileEditForm, UserChoiceForm
    14 
    14 
    15 from pytask.taskapp.utilities.request import get_request, create_request
    15 from pytask.taskapp.utilities.request import get_request, create_request
    99 @login_required
    99 @login_required
   100 def edit_my_profile(request):
   100 def edit_my_profile(request):
   101     """ enables the user to edit his/her user profile """
   101     """ enables the user to edit his/her user profile """
   102 
   102 
   103     user = get_user(request.user)
   103     user = get_user(request.user)
       
   104     user_profile = user.get_profile()
       
   105 
       
   106     edit_profile_form = UserProfileEditForm(instance = user_profile)
   104     if request.method == 'POST':
   107     if request.method == 'POST':
   105         form = UserProfileEditForm(request.POST)
   108 
   106 #        if not form.is_valid():
   109         data = request.POST.copy()
   107 #            edit_profile_form = UserProfileEditForm(instance = form)
   110         uploaded_photo = request.FILES.get('photo', None)
   108 #            return render_to_response('user/edit_profile.html',{'edit_profile_form' : edit_profile_form})
   111         data.__setitem__('photo', uploaded_photo)
   109         if request.user.is_authenticated() == True:
   112 
   110             profile = Profile.objects.get(user = request.user)
   113         edit_profile_form = UserProfileEditForm(data, instance=user_profile)
   111             data = request.POST#form.cleaned_data
   114         if edit_profile_form.is_valid():
   112             properties = {'aboutme':data['aboutme'],
   115             edit_profile_form.save()
   113                           'foss_comm':data['foss_comm'],
   116             return redirect('/user/view/uid='+str(user.id))
   114                           'phonenum':data['phonenum'],
   117         else:
   115                           'homepage':data['homepage'],
   118             return render_to_response('user/edit_profile.html',{'user':user, 'edit_profile_form' : edit_profile_form})
   116                           'street':data['street'],
   119     else:
   117                           'city':data['city'],
   120         profile = user.get_profile()
   118                           'country':data['country'],
       
   119                           'nick':data['nick']}
       
   120             uploaded_photo = request.FILES.get('photo',None)
       
   121             prev_photo = profile.photo
       
   122             if uploaded_photo:
       
   123                 if prev_photo:
       
   124                     os.remove(prev_photo.path)
       
   125                 properties['photo'] = uploaded_photo
       
   126             #fields = ['dob','gender','credits','aboutme','foss_comm','phonenum','homepage','street','city','country','nick']
       
   127             updateProfile(profile,properties)
       
   128             return redirect('/user/view/uid='+str(profile.user_id))
       
   129     else:
       
   130         profile = Profile.objects.get(user = request.user)
       
   131         edit_profile_form = UserProfileEditForm(instance = profile)
   121         edit_profile_form = UserProfileEditForm(instance = profile)
   132         return render_to_response('user/edit_profile.html',{'edit_profile_form' : edit_profile_form, 'user':user})
   122         return render_to_response('user/edit_profile.html',{'edit_profile_form' : edit_profile_form, 'user':user})
   133 
   123 
   134 @login_required
   124 @login_required
   135 def browse_requests(request):
   125 def browse_requests(request):