From ebcdcf4245fb2aab211656949c82de31d1783d5f Mon Sep 17 00:00:00 2001 From: Skia Date: Tue, 24 Nov 2015 15:52:27 +0100 Subject: [PATCH] Basic user profile edit form --- core/forms.py | 11 +++++++++-- core/models.py | 11 +++++++++++ core/templates/core/user.html | 1 + core/views.py | 18 +++++++++++++++--- 4 files changed, 36 insertions(+), 5 deletions(-) diff --git a/core/forms.py b/core/forms.py index 08d59629..9d59091d 100644 --- a/core/forms.py +++ b/core/forms.py @@ -42,12 +42,19 @@ class LoginForm(AuthenticationForm): params={'username': self.username_field.verbose_name}, ) -class EditUserForm(UserChangeForm): +class UserEditForm(UserChangeForm): error_css_class = 'error' required_css_class = 'required' class Meta: model = User - fields = ('first_name', 'last_name', 'email', 'date_of_birth', 'groups', 'user_permissions') + fields = ('first_name', 'last_name', 'nick_name', 'email', 'date_of_birth', 'groups', 'user_permissions',) + + def __init__(self, *args, **kwargs): + super(UserEditForm, self).__init__(*args, **kwargs) + + def clean_password(self): + """We never handle password in this form""" + return class PagePropForm(forms.ModelForm): diff --git a/core/models.py b/core/models.py index 975121db..49a19960 100644 --- a/core/models.py +++ b/core/models.py @@ -65,6 +65,17 @@ class User(AbstractBaseUser, PermissionsMixin): def __str__(self): return self.username + def to_dict(self): + return self.__dict__ + + def get_profile(self): + return { + "last_name": self.last_name, + "first_name": self.first_name, + "nick_name": self.nick_name, + "date_of_birth": self.date_of_birth, + } + def get_full_name(self): """ Returns the first_name plus the last_name, with a space in between. diff --git a/core/templates/core/user.html b/core/templates/core/user.html index b808b2c4..707e9b75 100644 --- a/core/templates/core/user.html +++ b/core/templates/core/user.html @@ -17,6 +17,7 @@ User list

Edit

{% endif %}

You're seeing the profile of {{ profile.get_display_name }}

+

{{ profile.nick_name }}

{% endif %} {% if user_list %} diff --git a/core/views.py b/core/views.py index 0dba995b..ef64bcf9 100644 --- a/core/views.py +++ b/core/views.py @@ -2,9 +2,10 @@ from django.shortcuts import render, redirect, get_object_or_404 from django.http import HttpResponse from django.contrib.auth import logout as auth_logout from django.db import models +from django.contrib.auth.forms import PasswordChangeForm from .models import User, Page -from .forms import RegisteringForm, LoginForm, EditUserForm, PageEditForm, PagePropForm +from .forms import RegisteringForm, LoginForm, UserEditForm, PageEditForm, PagePropForm import logging @@ -89,8 +90,19 @@ def user_edit(request, user_id=None): if user_id is not None: user_id = int(user_id) if request.user.is_authenticated() and (request.user.pk == user_id or request.user.is_superuser): - context['profile'] = get_object_or_404(User, pk=user_id) - context['user_form'] = EditUserForm(instance=context['profile']).as_p() + p = get_object_or_404(User, pk=user_id) + if request.method == 'POST': + f = UserEditForm(request.POST, instance=p) + # Saving user + if f.is_valid(): + f.save() + context['tests'] = "USER_SAVED" + else: + context['tests'] = "USER_NOT_SAVED" + else: + f = UserEditForm(instance=p) + context['profile'] = p + context['user_form'] = f.as_p() return render(request, "core/edit_user.html", context) return user(request, user_id)