Skip to content

Commit

Permalink
Merge cd61760 into ab24c42
Browse files Browse the repository at this point in the history
  • Loading branch information
SebCorbin committed Jan 27, 2024
2 parents ab24c42 + cd61760 commit 3a913ea
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 18 deletions.
17 changes: 9 additions & 8 deletions jsignature/utils.py
Expand Up @@ -21,19 +21,20 @@ def _remove_empty_pts(pt):
'y': list(filter(lambda n: n is not None, pt['y']))
}

if type(data) is str:
if isinstance(data, str):
drawing = json.loads(data, object_hook=_remove_empty_pts)
elif type(data) is list:
elif isinstance(data, list):
drawing = data
else:
raise ValueError

# Compute box
min_width = int(round(min(chain(*[d['x'] for d in drawing])))) - 10
max_width = int(round(max(chain(*[d['x'] for d in drawing])))) + 10
padding = 10
min_width = int(round(min(chain(*[d['x'] for d in drawing])))) - padding
max_width = int(round(max(chain(*[d['x'] for d in drawing])))) + padding
width = max_width - min_width
min_height = int(round(min(chain(*[d['y'] for d in drawing])))) - 10
max_height = int(round(max(chain(*[d['y'] for d in drawing])))) + 10
min_height = int(round(min(chain(*[d['y'] for d in drawing])))) - padding
max_height = int(round(max(chain(*[d['y'] for d in drawing])))) + padding
height = max_height - min_height

# Draw image
Expand All @@ -55,10 +56,10 @@ def _remove_empty_pts(pt):
if bbox:
im.crop(bbox)

old_pil_version = int(PIL_VERSION.split('.')[0]) < 10
im.thumbnail(
(width, height),
Image.ANTIALIAS if old_pil_version else Image.LANCZOS
# Image.ANTIALIAS is replaced in PIL 10.0.0
Image.ANTIALIAS if int(PIL_VERSION.split('.')[0]) < 10 else Image.LANCZOS
)

if as_file:
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Expand Up @@ -14,7 +14,7 @@
long_description=open(os.path.join(here, 'README.rst')).read() + '\n\n' +
open(os.path.join(here, 'CHANGES')).read(),
license='LPGL, see LICENSE file.',
install_requires=['Django>=4.2', 'pillow<9.1.0', 'pyquery>=1.4.2'],
install_requires=['Django>=4.2', 'pillow', 'pyquery>=1.4.2'],
packages=find_packages(exclude=['example_project*', 'tests']),
include_package_data=True,
zip_safe=False,
Expand Down
23 changes: 14 additions & 9 deletions tests/test_fields.py
@@ -1,17 +1,12 @@
import json

from django import forms
from django.test import SimpleTestCase
from django.core.exceptions import ValidationError

from jsignature.fields import JSignatureField
from jsignature.forms import JSignatureField as JSignatureFormField

try:
from django.utils import six

string_types = six.string_types
except ImportError:
string_types = str
from tests.models import JSignatureTestModel


class JSignatureFieldTest(SimpleTestCase):
Expand Down Expand Up @@ -46,15 +41,15 @@ def test_get_prep_value_correct_values_python(self):
f = JSignatureField()
val = [{"x": [1, 2], "y": [3, 4]}]
val_prep = f.get_prep_value(val)
self.assertIsInstance(val_prep, string_types)
self.assertIsInstance(val_prep, str)
self.assertEqual(val, json.loads(val_prep))

def test_get_prep_value_correct_values_json(self):
f = JSignatureField()
val = [{"x": [1, 2], "y": [3, 4]}]
val_str = '[{"x":[1,2], "y":[3,4]}]'
val_prep = f.get_prep_value(val_str)
self.assertIsInstance(val_prep, string_types)
self.assertIsInstance(val_prep, str)
self.assertEqual(val, json.loads(val_prep))

def test_get_prep_value_incorrect_values(self):
Expand All @@ -66,3 +61,13 @@ def test_formfield(self):
f = JSignatureField()
cls = f.formfield().__class__
self.assertTrue(issubclass(cls, JSignatureFormField))

def test_modelform_media(self):
class TestModelForm(forms.ModelForm):
class Meta:
model = JSignatureTestModel
fields = forms.ALL_FIELDS

form = TestModelForm()
self.assertIn('jSignature.min.js', str(form.media))
self.assertIn('django_jsignature.js', str(form.media))

0 comments on commit 3a913ea

Please sign in to comment.