Skip to content

Commit

Permalink
Implemented the edit_profile feature and associated templates.
Browse files Browse the repository at this point in the history
  • Loading branch information
divyekapoor committed Mar 9, 2011
1 parent e9421bc commit 6f6593f
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 18 deletions.
7 changes: 7 additions & 0 deletions exnihilo/contest/templates/edit_done.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{% extends "message.html" %}

{% block msg-title %}Edit Successful{% endblock %}
{% block msg-text %}You've successfully edited your profile information. Now,
why don't you see how it's looking at
<a href="{% url signup.views.profile user.username %}">{% url signup.views.profile user.username %}</a>
{% endblock %}
25 changes: 25 additions & 0 deletions exnihilo/contest/templates/edit_profile.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{% extends "generic-form.html" %}
{% block extra-css %}
.form_fields {
margin:5px 5px;
width:95%;
}
#id_about_me {
margin:0 0;
padding:0 0;
width:180px;
height:80px;
}

@media screen and (min-width:301px) {
.column {
width:100%;
}

}
{% endblock %}


{% block extra-content %}
<p>Change your password <a href="{% url signup.views.edit_password %}">here</a>.</p>
{% endblock %}
14 changes: 7 additions & 7 deletions exnihilo/contest/templates/layout.html
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,6 @@
<h1 class="logo">Lebensraum</h1>

{% block login-form %}
{% if form.errors %}
<script type="text/javascript">
alert("Your login failed. Please try again");
</script>
{% endif %}
{% if not user.is_authenticated %}
<form method="post" action="{% url django.contrib.auth.views.login %}">
{% csrf_token %}
Expand All @@ -61,7 +56,10 @@ <h1 class="logo">Lebensraum</h1>
<input type="hidden" name="next" value="{{ next }}" />
</form>
{% else %}
<div class="greeting">Hi {{ user.get_full_name }}!</div>
<div class="greeting">Hi {{ user.first_name }}!
<a href="{% url signup.views.edit_profile %}">Edit Your Profile</a>
or <a href="{% url django.contrib.auth.views.logout %}">Logout</a>
</div>
{% endif %}
{% endblock login-form %}

Expand All @@ -78,7 +76,9 @@ <h1 class="logo">Lebensraum</h1>

<div id="header">
{% block header %}
<a class="register" href="#">Register Now</a>
{% if not user.is_authenticated %}
<a class="register" href="{% url signup.views.signup %}">Register Now</a>
{% endif %}
{% endblock header %}
</div>
<div id="content">
Expand Down
10 changes: 8 additions & 2 deletions exnihilo/contest/templates/profile.html
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
{% extends "layout.html" %}
{% block post-css %}
<style type="text/css">

</style>
{% endblock %}

{% block main %}


<h1 class="name">{{ p_user.get_full_name }}</h1>
<h1 class="title">{{ p_user.get_full_name }}</h1>
<img class="profile_pic" src="{{ p_user.get_profile.pic.url }}" alt="{{ p_user.get_full_name }}" />
<div class="blurb">{{ p_user.get_profile.blurb }}</div>
<div class="about_me">{{ p_user.get_profile.about_me }}</div>
<a class="edit" href="{% url signup.views.edit_profile %}">Edit Profile</a>

{% endblock %}
28 changes: 27 additions & 1 deletion exnihilo/signup/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,34 @@ class Meta:
def save(self, *args, **kwargs):
new_user = super(UserProfileCreationForm, self).save(*args, **kwargs)
UserProfile.objects.create(user=new_user, pic=self.cleaned_data["pic"], about_me=self.cleaned_data["about_me"])
return new_user


class UserProfileModelForm(forms.ModelForm):

pic = forms.ImageField(label="Profile pic", required=False)
about_me = forms.CharField(max_length=1000, widget=forms.Textarea(), required=False)

class Meta:
model = UserProfile
model = User
fields = ('username', 'first_name', 'last_name', 'email',)

# def __init__(self, *args, **kwargs):
# instance = kwargs.get('instance', None)
# if instance is None:
# raise ValueError('This class requires an instance to be provided.')
# return super(UserProfileModelForm, self).__init__(self, *args, **kwargs)

def save(self, *args, **kwargs):
new_user = super(UserProfileModelForm, self).save(*args, **kwargs)
profile = new_user.get_profile()
if self.cleaned_data["pic"] is not None:
profile.pic = self.cleaned_data["pic"]

if self.cleaned_data["about_me"] is not None:
profile.about_me = self.cleaned_data["about_me"]

profile.save()
return new_user


33 changes: 26 additions & 7 deletions exnihilo/signup/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from django.template import RequestContext
from django.contrib.auth.decorators import login_required
from django.contrib.auth.models import User
from django.contrib.auth.forms import PasswordChangeForm

from exnihilo.signup.forms import UserProfileCreationForm, UserProfileModelForm

Expand Down Expand Up @@ -45,20 +46,38 @@ def profile(request, user_name):

# TODO
@login_required
def edit(request):
def edit_profile(request):
"""
Edit the profile of the currently logged in user
"""
ctx = {'title' : 'Edit Profile', 'form' : None, 'is_saved' : False}
ctx = {'title' : 'Edit Profile', 'form' : None}
if request.method == "POST":
form = UserProfileModelForm(request.POST, instance=request.user.get_profile())
form = UserProfileModelForm(request.POST, instance=request.user)
ctx["form"] = form
if form.is_valid():
form.save()
ctx["is_saved"] = True
changed_user = form.save()
return render_to_response('edit_done.html', ctx, context_instance=RequestContext(request))
else:
form = UserProfileModelForm(instance=request.user.get_profile())
form = UserProfileModelForm(instance=request.user)
ctx["form"] = form

return render_to_response('signup.html', ctx, context_instance=RequestContext(request))
# FIXME: Fix about_me not coming on the prompt to edit the profile
return render_to_response('edit_profile.html', ctx, context_instance=RequestContext(request))

@login_required
def edit_password(request):
"""
The user can change his/her password from this view.
"""
ctx = {'title':'Change Password', 'form': None}
form = None
if request.method == "POST":
form = PasswordChangeForm(request.user, request.POST)
if form.is_valid():
changed_user = form.save()
return render_to_response('edit_done.html', ctx, context_instance=RequestContext(request))
else:
form = PasswordChangeForm(request.user)

ctx['form'] = form
return render_to_response('generic-form.html', ctx, context_instance=RequestContext(request))
3 changes: 2 additions & 1 deletion exnihilo/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@

# Use django.contrib.auth for login management
(r'^accounts/signup/', include('exnihilo.signup.urls')),
(r'^accounts/edit/$', 'exnihilo.signup.views.edit'),
(r'^accounts/edit/$', 'exnihilo.signup.views.edit_profile'),
(r'^accounts/edit/password/$', 'exnihilo.signup.views.edit_password'),
(r'^accounts/profile/(?:([a-zA-Z0-9@\.+\-_]+)/)?$', 'exnihilo.signup.views.profile'),
(r'^accounts/', include('django.contrib.auth.urls')),
)

0 comments on commit 6f6593f

Please sign in to comment.