diff --git a/templates/base.html b/templates/base.html index 4540b8f..56232cb 100644 --- a/templates/base.html +++ b/templates/base.html @@ -23,6 +23,7 @@
  • Home
  • New Paste
  • {% if user.is_authenticated %} +
  • Account
  • Logout
  • {% else %}
  • Login
  • diff --git a/users/forms.py b/users/forms.py index 0f7059a..8fff434 100644 --- a/users/forms.py +++ b/users/forms.py @@ -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 \ No newline at end of file diff --git a/users/templates/users/password_change.html b/users/templates/users/user_update.html similarity index 93% rename from users/templates/users/password_change.html rename to users/templates/users/user_update.html index ff86df9..2753104 100644 --- a/users/templates/users/password_change.html +++ b/users/templates/users/user_update.html @@ -16,7 +16,7 @@ -

    Change password

    +

    Edit your account

    diff --git a/users/urls.py b/users/urls.py index 226883b..e425493 100644 --- a/users/urls.py +++ b/users/urls.py @@ -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\d+)/$', SendConfirmationView.as_view(), name='send_confirmation'), url(r'^confirm/(?P\d+)/(?P.+)/$', CheckConfirmationView.as_view(), name='check_confirmation'), + url(r'^update/(?P\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'), + ) \ No newline at end of file diff --git a/users/views.py b/users/views.py index 435f7cc..07dea10 100644 --- a/users/views.py +++ b/users/views.py @@ -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'