Basic user profile edit form

This commit is contained in:
Skia 2015-11-24 15:52:27 +01:00
parent 259182c1c4
commit ebcdcf4245
4 changed files with 36 additions and 5 deletions

View File

@ -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):

View File

@ -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.

View File

@ -17,6 +17,7 @@ User list
<p><a href="{% url 'core:user_edit' profile.id %}">Edit</a></p>
{% endif %}
<p>You're seeing the profile of <strong>{{ profile.get_display_name }}</strong></p>
<p>{{ profile.nick_name }}</p>
{% endif %}
{% if user_list %}

View File

@ -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)