now user profile editing is done thro forms in a more elegant way.
authornishanth
Fri, 05 Mar 2010 15:13:37 +0530
changeset 206 85660d75683d
parent 205 0c317f68df49
child 207 2de52334fe6c
now user profile editing is done thro forms in a more elegant way.
taskapp/forms/user.py
taskapp/views/user.py
--- a/taskapp/forms/user.py	Thu Mar 04 19:08:47 2010 +0530
+++ b/taskapp/forms/user.py	Fri Mar 05 15:13:37 2010 +0530
@@ -1,5 +1,10 @@
 #!/usr/bin/python2.5
 
+import os
+import PIL
+
+from pytask.taskapp.utilities.helper import get_key
+
 from django import forms
 from pytask.taskapp.models import GENDER_CHOICES, Profile
 from registration.forms import RegistrationFormUniqueEmail
@@ -12,6 +17,28 @@
         model = Profile
         exclude = ('user','rights','dob','credits')
 
+    def clean_photo(self):
+        uploaded_photo = self.data.get('photo', None)
+        prev_photo = self.instance.photo
+        if uploaded_photo:
+            if uploaded_photo.size > 1048576:
+                raise forms.ValidationError('Images only smaller than 1MB allowed')
+            tmp_im_path = '/tmp/'+get_key()
+            tmp_file = open(tmp_im_path, 'w')
+            tmp_file.write(uploaded_photo.read())
+            tmp_file.close()
+            try:
+                PIL.Image.open(tmp_im_path)
+            except IOError:
+                raise forms.ValidationError('Image format unknown')
+            os.remove(tmp_im_path)
+
+            if prev_photo: os.remove(prev_photo.path)
+            return uploaded_photo
+        else:
+            return prev_photo
+
+
 class RegistrationFormCustom(RegistrationFormUniqueEmail):
     """Used instead of RegistrationForm used by default django-registration backend, this adds date of birth and gender to the default django-registration RegistrationForm"""
     
--- a/taskapp/views/user.py	Thu Mar 04 19:08:47 2010 +0530
+++ b/taskapp/views/user.py	Fri Mar 05 15:13:37 2010 +0530
@@ -7,7 +7,7 @@
 
 from pytask.taskapp.models import Task, Profile, Request
 
-from pytask.taskapp.events.user import createUser, updateProfile
+from pytask.taskapp.events.user import createUser
 from pytask.taskapp.events.request import reply_to_request
 
 from pytask.taskapp.forms.user import UserProfileEditForm, UserChoiceForm
@@ -101,33 +101,23 @@
     """ enables the user to edit his/her user profile """
 
     user = get_user(request.user)
+    user_profile = user.get_profile()
+
+    edit_profile_form = UserProfileEditForm(instance = user_profile)
     if request.method == 'POST':
-        form = UserProfileEditForm(request.POST)
-#        if not form.is_valid():
-#            edit_profile_form = UserProfileEditForm(instance = form)
-#            return render_to_response('user/edit_profile.html',{'edit_profile_form' : edit_profile_form})
-        if request.user.is_authenticated() == True:
-            profile = Profile.objects.get(user = request.user)
-            data = request.POST#form.cleaned_data
-            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']}
-            uploaded_photo = request.FILES.get('photo',None)
-            prev_photo = profile.photo
-            if uploaded_photo:
-                if prev_photo:
-                    os.remove(prev_photo.path)
-                properties['photo'] = uploaded_photo
-            #fields = ['dob','gender','credits','aboutme','foss_comm','phonenum','homepage','street','city','country','nick']
-            updateProfile(profile,properties)
-            return redirect('/user/view/uid='+str(profile.user_id))
+
+        data = request.POST.copy()
+        uploaded_photo = request.FILES.get('photo', None)
+        data.__setitem__('photo', uploaded_photo)
+
+        edit_profile_form = UserProfileEditForm(data, instance=user_profile)
+        if edit_profile_form.is_valid():
+            edit_profile_form.save()
+            return redirect('/user/view/uid='+str(user.id))
+        else:
+            return render_to_response('user/edit_profile.html',{'user':user, 'edit_profile_form' : edit_profile_form})
     else:
-        profile = Profile.objects.get(user = request.user)
+        profile = user.get_profile()
         edit_profile_form = UserProfileEditForm(instance = profile)
         return render_to_response('user/edit_profile.html',{'edit_profile_form' : edit_profile_form, 'user':user})