From 15054526f6fd869a2891d671ff1b01f761cee17f Mon Sep 17 00:00:00 2001 From: Rowan Seymour Date: Fri, 14 Oct 2016 15:02:02 +0200 Subject: [PATCH 1/2] Fix use of unicode(..) and add test for ImageThumbnailWidget --- smartmin/widgets.py | 8 +++++--- test_runner/blog/tests.py | 17 +++++++++++++++++ 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/smartmin/widgets.py b/smartmin/widgets.py index 24fa5ed..4167e1a 100644 --- a/smartmin/widgets.py +++ b/smartmin/widgets.py @@ -1,5 +1,7 @@ from __future__ import unicode_literals +import six + from datetime import datetime from django.forms import widgets from django.utils.html import escape @@ -50,8 +52,8 @@ def value_from_datadict(self, data, files, name): return None class Media: - js = ('js/datepicker.js',) - css = {'all': ('css/datepicker.css',)} + js = ('js/datepicker.js',) + css = {'all': ('css/datepicker.css',)} class ImageThumbnailWidget(widgets.ClearableFileInput): @@ -70,4 +72,4 @@ def render(self, name, value, attrs=None): thumb_html += '' % name thumb_html += '' - return mark_safe(unicode('
%s
' % thumb_html)) + return mark_safe(six.text_type('
%s
' % thumb_html)) diff --git a/test_runner/blog/tests.py b/test_runner/blog/tests.py index b3dd698..c34f248 100644 --- a/test_runner/blog/tests.py +++ b/test_runner/blog/tests.py @@ -6,6 +6,7 @@ from django.conf import settings from django.contrib.auth.models import User, Group from django.core import mail +from django.core.files.uploadedfile import SimpleUploadedFile from django.core.urlresolvers import reverse from django.test import TestCase from django.test.client import Client @@ -20,6 +21,7 @@ from smartmin.tests import SmartminTest from smartmin.users.models import FailedLogin, RecoveryToken, PasswordHistory from smartmin.views import smart_url +from smartmin.widgets import ImageThumbnailWidget from test_runner.blog.models import Post, Category from .views import PostCRUDL @@ -1150,3 +1152,18 @@ def test_expiration(self): self.assertEquals(302, response.status_code) self.assertIn(reverse('blog.post_list'), response['location']) + + +class WidgetsTest(SmartminTest): + def test_image_thumbnail(self): + widget = ImageThumbnailWidget(320, 240) + img = SimpleUploadedFile('test_image.jpg', [], content_type='image/jpeg') + + html = widget.render('logo', img) + + self.assertNotIn('img', html) + + img.url = '/media/2423.jpg' + html = widget.render('logo', img) + + self.assertIn('', html) From 0d32b1792d5b907dfb40eeb95370e83753c1e460 Mon Sep 17 00:00:00 2001 From: Rowan Seymour Date: Fri, 14 Oct 2016 15:22:59 +0200 Subject: [PATCH 2/2] Fix modifying dict during iteration --- smartmin/views.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/smartmin/views.py b/smartmin/views.py index a905504..cebe87a 100644 --- a/smartmin/views.py +++ b/smartmin/views.py @@ -882,9 +882,9 @@ def get_form(self, form_class=None): if fields is not None: # filter out our form fields - for name, field in self.form.fields.items(): - if not name in fields: - del self.form.fields[name] + remove = [name for name in self.form.fields.keys() if name not in fields] + for name in remove: + del self.form.fields[name] # stuff in our referer as the default location for where to return location = forms.CharField(widget=forms.widgets.HiddenInput(), required=False)