Added account update view

This commit is contained in:
sebastian 2013-11-03 20:08:24 +01:00
parent 93fe541717
commit 6cdf84b71c
5 changed files with 81 additions and 10 deletions

View File

@ -23,6 +23,7 @@
<li ><a href="#">Home</a></li>
<li><a href="{% url 'geometry_create' %}">New Paste</a></li>
{% if user.is_authenticated %}
<li><a href="{% url 'user_update' user_id=user.pk %}">Account</a></li>
<li><a href="{% url 'logout' %}">Logout</a></li>
{% else %}
<li><a href="{% url 'login' %}">Login</a></li>

View File

@ -22,6 +22,7 @@ class UserCreateForm(ModelForm):
def clean_email(self):
#Make sure Email is unique
email = self.cleaned_data.get("email")
if User.objects.filter(email = email):
raise forms.ValidationError("Email already in use.")
@ -33,6 +34,61 @@ class UserCreateForm(ModelForm):
user = super(UserCreateForm, self).save(commit=False)
user.set_password(self.cleaned_data["password1"])
user.is_active = False
if commit:
user.save()
return user
class UserUpdateForm(ModelForm):
new_password1 = forms.CharField(label='New Password',
widget=forms.PasswordInput,
required=False)
new_password2 = forms.CharField(label='New Password confirmation',
widget=forms.PasswordInput,
required=False)
current_password = forms.CharField(label='Current Password', widget=forms.PasswordInput)
class Meta:
model = User
fields = ['email']
def clean_current_password(self):
current_password = self.cleaned_data.get("current_password")
if not self.instance.check_password(current_password):
raise forms.ValidationError("Password incorrect")
return current_password
def clean_new_password2(self):
password1 = self.cleaned_data.get("new_password1")
password2 = self.cleaned_data.get("new_password2")
if password1 and password2 and password1 != password2:
raise forms.ValidationError("Passwords don't match")
return password1
def clean_email(self):
#Make sure Email is still unique
email = self.cleaned_data.get("email")
if User.objects.filter(email = email).exclude(id=self.instance.id):
raise forms.ValidationError("Email already in use.")
return email
def save(self, commit=True):
user = super(UserUpdateForm, self).save(commit=False)
password = self.cleaned_data.get("new_password1")
if password:
user.set_password(password)
if commit:
user.save()
return user

View File

@ -16,7 +16,7 @@
<!-- if IE without GCF, prompt goes here -->
</div>
<h1>Change password</h1>
<h1>Edit your account</h1>
<div class="pure-g-r">
<div class="pure-u-2-3">
<div class="textcontainer">

View File

@ -3,13 +3,15 @@ from django.core.urlresolvers import reverse_lazy
from pastebin.models import Geometry
from views import UserCreate, SendConfirmationView, CheckConfirmationView
from views import UserCreateView, UserUpdateView, SendConfirmationView, CheckConfirmationView
urlpatterns = patterns('',
url(r'^signup/$', UserCreate.as_view(), name='signup'),
url(r'^signup/$', UserCreateView.as_view(), name='signup'),
url(r'^confirm/(?P<user_id>\d+)/$', SendConfirmationView.as_view(), name='send_confirmation'),
url(r'^confirm/(?P<user_id>\d+)/(?P<token>.+)/$', CheckConfirmationView.as_view(), name='check_confirmation'),
url(r'^update/(?P<user_id>\d+)/$', UserUpdateView.as_view(), name='user_update'),
url(r'^login/$', 'django.contrib.auth.views.login', {'extra_context' : {'latest_geometries' : Geometry.get_latest()},
'template_name' : 'users/login.html'},
name='login'),
@ -17,8 +19,5 @@ urlpatterns = patterns('',
'next_page' : reverse_lazy('login')},
name='logout'),
url(r'^password/change/$', 'django.contrib.auth.views.password_change',{'extra_context' : {'latest_geometries' : Geometry.get_latest()},
'template_name' : 'users/password_change.html',
'post_change_redirect' : reverse_lazy('login')},
name='password_change'),
)

View File

@ -1,4 +1,4 @@
from django.views.generic.edit import CreateView
from django.views.generic.edit import CreateView, UpdateView
from django.views.generic.base import TemplateView
from django.contrib.auth.models import User
from django.core.urlresolvers import reverse
@ -7,20 +7,35 @@ from django.contrib.auth.tokens import default_token_generator
from django.template import loader
from django.utils.http import int_to_base36
from django.http import Http404
from django.core.exceptions import PermissionDenied
from pastebin.views import LastesGeometriesMixin
from forms import UserCreateForm
from forms import UserCreateForm, UserUpdateForm
class UserCreate(CreateView, LastesGeometriesMixin):
class UserCreateView(CreateView, LastesGeometriesMixin):
model = User
form_class = UserCreateForm
template_name = 'users/signup.html'
def get_success_url(self):
return reverse('send_confirmation', kwargs={'user_id' : self.object.pk})
class UserUpdateView(UpdateView, LastesGeometriesMixin):
model = User
form_class = UserUpdateForm
template_name = 'users/user_update.html'
pk_url_kwarg = 'user_id'
def get_queryset(self):
return self.model.objects.filter(id=self.request.user.id)
def get_success_url(self):
return reverse('geometry_create')
class SendConfirmationView(TemplateView, LastesGeometriesMixin):
template_name = 'users/send_confirmation.html'