mergehg sthg st
authornishanth
Mon, 01 Feb 2010 15:11:50 +0530
changeset 19 2cff66e3386a
parent 18 293692eb8f06 (current diff)
parent 17 9ca9f98af0eb (diff)
child 20 c5a282b84eb8
mergehg sthg st
pytask/urls.py
--- a/pytask/taskapp/forms/user.py	Mon Feb 01 15:00:40 2010 +0530
+++ b/pytask/taskapp/forms/user.py	Mon Feb 01 15:11:50 2010 +0530
@@ -1,7 +1,8 @@
 #!/usr/bin/python2.5
 
 from django import forms
-from pytask.taskapp.models import GENDER_CHOICES
+from pytask.taskapp.models import GENDER_CHOICES, Profile
+from django.forms import ModelForm
 
 class RegistrationForm(forms.Form):
     username = forms.CharField(max_length=30, required=True)
@@ -14,3 +15,29 @@
 class LoginForm(forms.Form):
     username = forms.CharField(max_length=30, required=True)
     password = forms.CharField(max_length=60, required=True, widget=forms.PasswordInput)
+
+class UserProfileForm(ModelForm):
+    class Meta:
+        model = Profile
+        exclude = ('user','rights')
+    def __init__(self, *args, **kwargs):
+        super(UserProfileForm, self).__init__(*args, **kwargs)
+        instance = getattr(self, 'instance', None)
+        if instance and instance.id:
+            self.fields['dob'].widget.attrs['readonly'] = True
+            self.fields['gender'].widget.attrs['readonly'] = True
+            self.fields['credits'].widget.attrs['readonly'] = True
+            self.fields['aboutme'].widget.attrs['readonly'] = True
+            self.fields['foss_comm'].widget.attrs['readonly'] = True
+            self.fields['phonenum'].widget.attrs['readonly'] = True
+            self.fields['homepage'].widget.attrs['readonly'] = True
+            self.fields['street'].widget.attrs['readonly'] = True
+            self.fields['city'].widget.attrs['readonly'] = True
+            self.fields['country'].widget.attrs['readonly'] = True
+            self.fields['nick'].widget.attrs['readonly'] = True
+        #fields = ['dob','gender','credits','aboutme','foss_comm','phonenum','homepage','street','city','country','nick']
+
+class UserProfileEditForm(ModelForm):
+    class Meta:
+        model = Profile
+        exclude = ('user','rights','dob','credits')
--- a/pytask/taskapp/views/user.py	Mon Feb 01 15:00:40 2010 +0530
+++ b/pytask/taskapp/views/user.py	Mon Feb 01 15:11:50 2010 +0530
@@ -1,10 +1,11 @@
 from django.http import HttpResponse
 from django.shortcuts import redirect, render_to_response
 from pytask.taskapp.models import Task
-from pytask.taskapp.forms.user import RegistrationForm, LoginForm
-from pytask.taskapp.events.user import createUser
+from pytask.taskapp.forms.user import RegistrationForm, LoginForm, UserProfileForm, UserProfileEditForm
+from pytask.taskapp.events.user import createUser, updateProfile
 from django.contrib.auth import login, logout, authenticate
 from django.contrib.auth.models import User
+from pytask.taskapp.models import Profile
 
 def show_msg(message, redirect_url=None, url_desc=None):
     """ simply redirect to homepage """
@@ -97,3 +98,34 @@
 def user_logout(request):
     logout(request)
     return show_msg('You have logged off successfully!!!')
+
+def view_my_profile(request,uid):
+    """ allows the user to view the profiles of users """
+    edit_profile = True if request.user == User.objects.get(pk=uid) else False
+    try:
+        profile = Profile.objects.get(user = User.objects.get(pk=uid))
+        view_profile_form = UserProfileForm(instance = profile)
+    except Profile.DoesNotExist:
+        raise Http404
+    return render_to_response('user/my_profile.html', {'view_profile_form': view_profile_form,'edit_profile':edit_profile})
+
+def edit_my_profile(request):
+    """ enables the user to edit his/her user profile """
+    if str(request.user) == 'AnonymousUser':
+        return show_msg('Please register yourself to activate the functionality')
+    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']}
+            #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))
+    else:
+        profile = Profile.objects.get(user = request.user)
+        edit_profile_form = UserProfileEditForm(instance = profile)
+        return render_to_response('user/edit_profile.html',{'edit_profile_form' : edit_profile_form})
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/pytask/templates/user/edit_profile.html	Mon Feb 01 15:11:50 2010 +0530
@@ -0,0 +1,7 @@
+{% extends 'base.html' %}
+{% block content %}
+<form action="/user/edit/" method="post">
+{{ edit_profile_form.as_p }}
+<input type="submit" value="Apply Changes" />
+</form>
+{% endblock %}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/pytask/templates/user/my_profile.html	Mon Feb 01 15:11:50 2010 +0530
@@ -0,0 +1,11 @@
+{% extends 'base.html' %}
+{% block content %}
+
+{% if edit_profile %}
+    <a href="/user/edit/">edit my profile</a>
+{% endif %}
+
+<br>
+{{ view_profile_form.as_p }}
+
+{% endblock %}
--- a/pytask/urls.py	Mon Feb 01 15:00:40 2010 +0530
+++ b/pytask/urls.py	Mon Feb 01 15:11:50 2010 +0530
@@ -16,6 +16,7 @@
     # (r'^admin/doc/', include('django.contrib.admindocs.urls')),
     
     (r'^$', homepage),
+    
     (r'^task/browse/$', browse_tasks),
     (r'^task/view/tid=(\d+)$', view_task),
     (r'^task/create/$', create_task),
@@ -28,5 +29,9 @@
     
     (r'^accounts/register/$',register),
     (r'^accounts/login/$',user_login),
-    (r'^accounts/logout/$',user_logout)
+    (r'^accounts/logout/$',user_logout),
+    
+    (r'^user/view/uid=(\d+)$', view_my_profile),
+    (r'^user/edit/?$',edit_my_profile),
+
 )