users can view and edit profile .
authornishanth
Mon, 12 Apr 2010 16:17:53 +0530
changeset 17 125b6fc8f20b
parent 16 bef53aaf9085
child 18 7dae32a2439b
users can view and edit profile .
reg/events.py
reg/forms.py
reg/site/urls.py
reg/views.py
templates/base.html
templates/edit_profile.html
templates/view_profile.html
--- a/reg/events.py	Mon Apr 12 15:14:03 2010 +0530
+++ b/reg/events.py	Mon Apr 12 16:17:53 2010 +0530
@@ -7,7 +7,7 @@
 from workshop.reg.models import Profile, Event
 from workshop.reg.utils import gen_key
 
-def create_user(email, password, firstname="", lastname="", gender="M", profession="S", affiliated_to="", interests="" ):
+def create_user(email, password, first_name="", last_name="", gender="M", profession="S", affiliated_to="", interests="" ):
     """ create a user with random username and set the password.
     """
 
@@ -18,8 +18,8 @@
         except User.DoesNotExist:
             break
     new_user = User.objects.create_user(username, email, password)
-    new_user.firstname = firstname
-    new_user.lastname = lastname
+    new_user.first_name = first_name
+    new_user.last_name = last_name
     new_user.is_active = False
     new_user.save()
 
@@ -86,4 +86,22 @@
     user.set_password(new_password)
     user.save()
 
+def update_profile(user, properties):
+    """ properties must be a dictionary that contains all the attributes that
+    we take while a new user registers.
+    """
 
+    profile = user.get_profile()
+
+    user.first_name = properties['first_name']
+    user.last_name = properties['last_name']
+    user.save()
+
+    profile.gender = properties['gender']
+    profile.profession = properties['profession']
+    profile.affiliated_to = properties['affiliated_to']
+    profile.interests = properties['interests']
+    profile.save()
+
+
+
--- a/reg/forms.py	Mon Apr 12 15:14:03 2010 +0530
+++ b/reg/forms.py	Mon Apr 12 16:17:53 2010 +0530
@@ -5,7 +5,7 @@
 from django.contrib.auth.models import User
 from django.contrib.auth import authenticate
 
-from workshop.reg.models import Profile, Event
+from workshop.reg.models import Profile, Event, GENDER_CHOICES, PROFESSION_CHOICES
 
 class LoginForm(forms.Form):
     """ a form to handle login.
@@ -157,3 +157,15 @@
         if not new_password == self.data['confirm_password']:
             raise forms.ValidationError("Passwords do not match")
 
+class EditProfileForm(forms.Form):
+    """ form for editing the profile.
+    """
+
+    first_name = forms.CharField(required=True)
+    last_name = forms.CharField(required=True)
+    gender = forms.ChoiceField(choices=GENDER_CHOICES)
+    profession = forms.ChoiceField(choices=PROFESSION_CHOICES)
+    affiliated_to = forms.CharField(max_length=100, required=True, label="College/Company")
+    interests = forms.CharField(max_length=100, label="Fields of interest")
+
+
--- a/reg/site/urls.py	Mon Apr 12 15:14:03 2010 +0530
+++ b/reg/site/urls.py	Mon Apr 12 16:17:53 2010 +0530
@@ -7,11 +7,13 @@
     (r'^login/$', reg_views.user_login),
     (r'^logout/$', reg_views.user_logout),
     (r'^register/$', reg_views.user_register),
+    (r'^profile/view/$', reg_views.view_profile),
+    (r'^profile/edit/$', reg_views.edit_profile),                       
     (r'^password_reset/$', reg_views.reset_password),
     (r'^password_change/$', reg_views.change_password),
     (r'^event/create/$', reg_views.create_event),
     (r'^event/view/(\w+)/$', reg_views.view_event),
-    (r'^event/register/(\w+)/$', reg_views.user_register),
+    (r'^event/register/(\w+)/$', reg_views.register_for_event),
     (r'^event/feedback/open/(\w+)/$', reg_views.open_feedback),
     (r'^event/feedback/close/(\w+)/$', reg_views.close_feedback),
     (r'^event/registration/open/(\w+)/$', reg_views.open_registration),
--- a/reg/views.py	Mon Apr 12 15:14:03 2010 +0530
+++ b/reg/views.py	Mon Apr 12 16:17:53 2010 +0530
@@ -21,7 +21,7 @@
     user = request.user
     events = Event.objects.all()[:10]
 
-    return render_to_response('index.html', {'user':user})
+    return render_to_response('index.html', {'user':user, 'events':events})
 
 def user_login(request):
     """ get the user object from e-mail and then check for password.
@@ -64,8 +64,8 @@
             data = form.cleaned_data
             reg_events.create_user(email=data['email'], 
                                    password=data['password'],
-                                   firstname=data['first_name'],
-                                   lastname=data['last_name'], 
+                                   first_name=data['first_name'],
+                                   last_name=data['last_name'], 
                                    gender=data['gender'], 
                                    profession=data['profession'], 
                                    affiliated_to=data['affiliated_to'], 
@@ -284,5 +284,44 @@
     else:
         return redirect("/reg")
 
+def view_profile(request):
+    """ check if user is logged in.
+    then show the profile.
+    """
+
+    user = request.user
+    if not user.is_authenticated():
+        return redirect('/reg')
+
+    user_profile = user.get_profile()
+    return render_to_response('view_profile.html', {'user':user, 'user_profile':user_profile})
+
+def edit_profile(request):
+    """ check if user is logged in.
+    """
+
+    user = request.user
+    if not user.is_authenticated():
+        return redirect('/reg')
+
+    user_profile = user.get_profile()
+
+    if request.method == "POST":
+        form = reg_forms.EditProfileForm(request.POST)
+        if form.is_valid():
+            reg_events.update_profile(user, form.cleaned_data)
+            return redirect('/reg/profile/view')
+        else:
+            return render_to_response('edit_profile.html', {'user':user, 'form':form})
+    else:
+        old_info = {'first_name': user.first_name,
+                    'last_name': user.last_name,
+                    'gender':user_profile.gender,
+                    'profession': user_profile.profession,
+                    'affiliated_to': user_profile.affiliated_to,
+                    'interests': user_profile.interests,
+                   }
+        form = reg_forms.EditProfileForm(old_info)
+        return render_to_response('edit_profile.html', {'user':user, 'form':form})
 
 
--- a/templates/base.html	Mon Apr 12 15:14:03 2010 +0530
+++ b/templates/base.html	Mon Apr 12 16:17:53 2010 +0530
@@ -130,7 +130,7 @@
 	<li><a href="/reg">Home</a></li>
 	{% if user.is_authenticated %}
 <!--		Welcome {{user.get_full_name}} -->
-		<li><a href="/reg/profile/edit">My Profile</a></li>
+		<li><a href="/reg/profile/view">My Profile</a></li>
 		<li><a href="/reg/logout">Logout</a></li>
 	{% else %}
 		<li><a href="/reg/login/">Login</a></li>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/templates/edit_profile.html	Mon Apr 12 16:17:53 2010 +0530
@@ -0,0 +1,7 @@
+{% extends "base.html" %}
+{% block content %}
+<form action="" method="post">
+{{form.as_p}}
+<input type="submit" value="Update">
+</form>
+{% endblock %}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/templates/view_profile.html	Mon Apr 12 16:17:53 2010 +0530
@@ -0,0 +1,7 @@
+{% extends "base.html" %}
+{% block content %}
+<a href="/reg/profile/edit">Edit Profile</a><br />
+{{user.first_name}} {{user.last_name}}<br />
+{{user_profile.affiliated_to }}<br />
+{{user_profile.interests }}<br />
+{% endblock %}