Added file handling to support custom user add/edit forms.

pull/2851/merge
Eraldo Energy 2016-06-28 13:55:09 +02:00 zatwierdzone przez Matt Westcott
rodzic cceb216536
commit 1a956be2f2
10 zmienionych plików z 36 dodań i 6 usunięć

Wyświetl plik

@ -40,6 +40,7 @@ Changelog
* Fix: Saving a page by pressing enter key no longer triggers a "Changes may not be saved message" (Sean Muck, Matt Westcott)
* Fix: RoutablePageMixin no longer breaks in the presence of instance-only attributes such as those generated by FileFields (Fábio Macêdo Mendes)
* Fix: The `--schema-only` flag on update_index no longer expects an argument (Karl Hobley)
* Fix: Added file handling to support custom user add/edit forms with images/files (Eraldo Energy)
1.5.3 (18.07.2016)

Wyświetl plik

@ -159,6 +159,7 @@ Contributors
* sebworks
* Sean Muck
* Fábio Macêdo Mendes
* Eraldo Energy
Translators
===========

Wyświetl plik

@ -70,6 +70,7 @@ Bug fixes
* Saving a page by pressing enter key no longer triggers a "Changes may not be saved message" (Sean Muck, Matt Westcott)
* RoutablePageMixin no longer breaks in the presence of instance-only attributes such as those generated by FileFields (Fábio Macêdo Mendes)
* The ``--schema-only`` flag on update_index no longer expects an argument (Karl Hobley)
* Added file handling to support custom user add/edit forms with images/files (Eraldo Energy)
Upgrade considerations

Wyświetl plik

@ -0,0 +1,19 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('customuser', '0001_initial'),
]
operations = [
migrations.AddField(
model_name='CustomUser',
name='attachment',
field=models.FileField(blank=True),
),
]

Wyświetl plik

@ -41,6 +41,7 @@ class CustomUser(AbstractBaseUser, PermissionsMixin):
first_name = models.CharField(max_length=50, blank=True)
last_name = models.CharField(max_length=50, blank=True)
country = models.CharField(max_length=100, blank=True)
attachment = models.FileField(blank=True)
USERNAME_FIELD = 'username'
REQUIRED_FIELDS = ['email']

Wyświetl plik

@ -168,7 +168,7 @@ WAGTAIL_SITE_NAME = "Test Site"
# needs to here because it is used at the module level of wagtailusers.forms
# when the module gets loaded. The decorator 'override_settings' does not work
# in this scenario.
WAGTAIL_USER_CUSTOM_FIELDS = ['country']
WAGTAIL_USER_CUSTOM_FIELDS = ['country', 'attachment']
WAGTAILADMIN_RICH_TEXT_EDITORS = {
'default': {

Wyświetl plik

@ -12,7 +12,7 @@
<li><a href="#roles">{% trans "Roles" %}</a></li>
</ul>
<form action="{% url 'wagtailusers_users:add' %}" method="POST">
<form action="{% url 'wagtailusers_users:add' %}" method="POST"{% if form.is_multipart %} enctype="multipart/form-data"{% endif %}>
<div class="tab-content">
{% csrf_token %}
<section id="account" class="active nice-padding">

Wyświetl plik

@ -12,7 +12,7 @@
<li><a href="#roles">{% trans "Roles" %}</a></li>
</ul>
<form action="{% url 'wagtailusers_users:edit' user.pk %}" method="POST">
<form action="{% url 'wagtailusers_users:edit' user.pk %}" method="POST"{% if form.is_multipart %} enctype="multipart/form-data"{% endif %}>
<div class="tab-content">
{% csrf_token %}

Wyświetl plik

@ -4,6 +4,7 @@ from django import forms
from django.contrib.auth import get_user_model
from django.contrib.auth.models import Group, Permission
from django.core.exceptions import ImproperlyConfigured
from django.core.files.uploadedfile import SimpleUploadedFile
from django.core.urlresolvers import reverse
from django.test import TestCase, override_settings
from django.utils import six
@ -23,10 +24,12 @@ delete_user_perm_codename = "delete_{0}".format(AUTH_USER_MODEL_NAME.lower())
class CustomUserCreationForm(UserCreationForm):
country = forms.CharField(required=True, label="Country")
attachment = forms.FileField(required=True, label="Attachment")
class CustomUserEditForm(UserEditForm):
country = forms.CharField(required=True, label="Country")
attachment = forms.FileField(required=True, label="Attachment")
class TestUserFormHelpers(TestCase):
@ -140,7 +143,7 @@ class TestUserCreateView(TestCase, WagtailTestUtils):
@override_settings(
WAGTAIL_USER_CREATION_FORM='wagtail.wagtailusers.tests.CustomUserCreationForm',
WAGTAIL_USER_CUSTOM_FIELDS=['country'],
WAGTAIL_USER_CUSTOM_FIELDS=['country', 'document'],
)
def test_create_with_custom_form(self):
response = self.post({
@ -151,6 +154,7 @@ class TestUserCreateView(TestCase, WagtailTestUtils):
'password1': "password",
'password2': "password",
'country': "testcountry",
'attachment': SimpleUploadedFile('test.txt', b"Uploaded file"),
})
# Should redirect back to index
@ -161,6 +165,7 @@ class TestUserCreateView(TestCase, WagtailTestUtils):
self.assertEqual(users.count(), 1)
self.assertEqual(users.first().email, 'test@user.com')
self.assertEqual(users.first().country, 'testcountry')
self.assertEqual(users.first().attachment.read(), b"Uploaded file")
def test_create_with_password_mismatch(self):
response = self.post({
@ -354,6 +359,7 @@ class TestUserEditView(TestCase, WagtailTestUtils):
'password1': "password",
'password2': "password",
'country': "testcountry",
'attachment': SimpleUploadedFile('test.txt', b"Uploaded file"),
})
# Should redirect back to index
@ -363,6 +369,7 @@ class TestUserEditView(TestCase, WagtailTestUtils):
user = get_user_model().objects.get(pk=self.test_user.pk)
self.assertEqual(user.first_name, 'Edited')
self.assertEqual(user.country, 'testcountry')
self.assertEqual(user.attachment.read(), b"Uploaded file")
def test_edit_validation_error(self):
# Leave "username" field blank. This should give a validation error

Wyświetl plik

@ -122,7 +122,7 @@ def index(request):
@permission_required(add_user_perm)
def create(request):
if request.method == 'POST':
form = get_user_creation_form()(request.POST)
form = get_user_creation_form()(request.POST, request.FILES)
if form.is_valid():
user = form.save()
messages.success(request, _("User '{0}' created.").format(user), buttons=[
@ -145,7 +145,7 @@ def edit(request, user_id):
can_delete = user_can_delete_user(request.user, user)
if request.method == 'POST':
form = get_user_edit_form()(request.POST, instance=user)
form = get_user_edit_form()(request.POST, request.FILES, instance=user)
if form.is_valid():
user = form.save()
messages.success(request, _("User '{0}' updated.").format(user), buttons=[