Compare commits
11 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
667a5fdb46 | ||
|
|
86e3e185f7 | ||
|
|
6da81f2115 | ||
|
|
d12b1e80ac | ||
|
|
880033d3a5 | ||
|
|
c15074873b | ||
|
|
8e912f9738 | ||
|
|
70b4f2e9b6 | ||
|
|
124fe21d06 | ||
|
|
738eb869fc | ||
|
|
32566873a4 |
25
.travis.yml
25
.travis.yml
@@ -1,19 +1,32 @@
|
||||
language: python
|
||||
|
||||
python:
|
||||
- "2.7"
|
||||
- 2.7
|
||||
- 3.3
|
||||
- 3.4
|
||||
- 3.5
|
||||
|
||||
env:
|
||||
- DJANGO_VERSION=1.4
|
||||
- DJANGO_VERSION=1.5
|
||||
- DJANGO_VERSION=1.7 MODULE=jsignature.tests
|
||||
- DJANGO_VERSION=1.8 MODULE=jsignature.tests
|
||||
- DJANGO_VERSION=1.9 MODULE=jsignature.tests
|
||||
- DJANGO_VERSION=1.10 MODULE=jsignature.tests
|
||||
|
||||
install:
|
||||
- pip install -r requirements.txt --use-mirrors
|
||||
- pip install -q Django==$DJANGO_VERSION --use-mirrors
|
||||
- pip install -r requirements.txt
|
||||
- pip install -q Django==$DJANGO_VERSION
|
||||
- pip install coverage
|
||||
|
||||
script: coverage run quicktest.py jsignature
|
||||
script: coverage run quicktest.py $MODULE
|
||||
|
||||
after_success:
|
||||
- pip install coveralls
|
||||
- coveralls
|
||||
|
||||
# We need to exclude old versions of Python for tests with Django >= 1.9.
|
||||
matrix:
|
||||
exclude:
|
||||
- python: 3.3
|
||||
env: DJANGO_VERSION=1.9 MODULE=jsignature.tests
|
||||
- python: 3.3
|
||||
env: DJANGO_VERSION=1.10 MODULE=jsignature.tests
|
||||
|
||||
9
CHANGES
9
CHANGES
@@ -2,6 +2,15 @@
|
||||
CHANGELOG
|
||||
=========
|
||||
|
||||
0.8 (2014-12-04)
|
||||
==================
|
||||
|
||||
** New **
|
||||
|
||||
- Add support for Python 3 (@Gagaro)
|
||||
- Add support for Django 1.7 (@Gagaro)
|
||||
|
||||
|
||||
0.7.6 (2014-11-26)
|
||||
==================
|
||||
|
||||
|
||||
@@ -3,8 +3,11 @@
|
||||
with jSignature jQuery plugin
|
||||
"""
|
||||
import json
|
||||
import six
|
||||
|
||||
from django.db import models
|
||||
from django.core.exceptions import ValidationError
|
||||
|
||||
from .forms import (
|
||||
JSignatureField as JSignatureFormField,
|
||||
JSIGNATURE_EMPTY_VALUES)
|
||||
@@ -15,16 +18,11 @@ class JSignatureField(models.Field):
|
||||
A model field handling a signature captured with jSignature
|
||||
"""
|
||||
description = "A signature captured with jSignature"
|
||||
__metaclass__ = models.SubfieldBase
|
||||
|
||||
def get_internal_type(self):
|
||||
return 'TextField'
|
||||
|
||||
def to_python(self, value):
|
||||
"""
|
||||
Validates that the input can be red as a JSON object. Returns a Python
|
||||
datetime.date object.
|
||||
"""
|
||||
if value in JSIGNATURE_EMPTY_VALUES:
|
||||
return None
|
||||
elif isinstance(value, list):
|
||||
@@ -34,10 +32,18 @@ class JSignatureField(models.Field):
|
||||
except ValueError:
|
||||
raise ValidationError('Invalid JSON format.')
|
||||
|
||||
def from_db_value(self, value, expression, connection, context):
|
||||
if value in JSIGNATURE_EMPTY_VALUES:
|
||||
return None
|
||||
try:
|
||||
return json.loads(value)
|
||||
except ValueError:
|
||||
raise ValidationError('Invalid JSON format.')
|
||||
|
||||
def get_prep_value(self, value):
|
||||
if value in JSIGNATURE_EMPTY_VALUES:
|
||||
return None
|
||||
elif isinstance(value, basestring):
|
||||
elif isinstance(value, six.string_types):
|
||||
return value
|
||||
elif isinstance(value, list):
|
||||
return json.dumps(value)
|
||||
@@ -48,8 +54,3 @@ class JSignatureField(models.Field):
|
||||
defaults.update(kwargs)
|
||||
return super(JSignatureField, self).formfield(**defaults)
|
||||
|
||||
try:
|
||||
from south.modelsinspector import add_introspection_rules
|
||||
add_introspection_rules([], ["jsignature.fields.JSignatureField"])
|
||||
except ImportError:
|
||||
pass
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
import json
|
||||
import six
|
||||
|
||||
from django.test import SimpleTestCase
|
||||
from django.core.exceptions import ValidationError
|
||||
|
||||
@@ -8,35 +10,64 @@ from ..forms import JSignatureField as JSignatureFormField
|
||||
|
||||
class JSignatureFieldTest(SimpleTestCase):
|
||||
|
||||
def test_to_python(self):
|
||||
def test_to_python_empty(self):
|
||||
f = JSignatureField()
|
||||
# Empty values
|
||||
for val in ['', [], '[]']:
|
||||
self.assertIsNone(f.to_python(val))
|
||||
# Correct values
|
||||
|
||||
def test_to_python_correct_value_python(self):
|
||||
f = JSignatureField()
|
||||
val = [{"x": [1, 2], "y": [3, 4]}]
|
||||
self.assertEquals(val, f.to_python(val))
|
||||
|
||||
def test_to_python_correct_value_json(self):
|
||||
f = JSignatureField()
|
||||
val = [{"x": [1, 2], "y": [3, 4]}]
|
||||
val_str = '[{"x":[1,2], "y":[3,4]}]'
|
||||
self.assertEquals(val, f.to_python(val_str))
|
||||
# Incorrect values
|
||||
|
||||
def test_to_python_incorrect_value(self):
|
||||
f = JSignatureField()
|
||||
val = 'foo'
|
||||
self.assertRaises(ValidationError, f.to_python, val)
|
||||
|
||||
def test_get_prep_value(self):
|
||||
def test_from_db_value_empty(self):
|
||||
f = JSignatureField()
|
||||
self.assertIsNone(f.from_db_value(''))
|
||||
|
||||
def test_from_db_value_correct_value_json(self):
|
||||
f = JSignatureField()
|
||||
val = [{"x": [1, 2], "y": [3, 4]}]
|
||||
val_str = '[{"x":[1,2], "y":[3,4]}]'
|
||||
self.assertEquals(val, f.from_db_value(val_str))
|
||||
|
||||
def test_from_db_value_incorrect_value(self):
|
||||
f = JSignatureField()
|
||||
val = 'foo'
|
||||
self.assertRaises(ValidationError, f.to_python, val)
|
||||
|
||||
def test_get_prep_value_empty(self):
|
||||
f = JSignatureField()
|
||||
# Empty values
|
||||
for val in ['', [], '[]']:
|
||||
self.assertIsNone(f.get_prep_value(val))
|
||||
# Correct values
|
||||
|
||||
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, basestring)
|
||||
self.assertIsInstance(val_prep, six.string_types)
|
||||
self.assertEquals(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, basestring)
|
||||
self.assertIsInstance(val_prep, six.string_types)
|
||||
self.assertEquals(val, json.loads(val_prep))
|
||||
# Incorrect values
|
||||
|
||||
def test_get_prep_value_incorrect_values(self):
|
||||
f = JSignatureField()
|
||||
val = type('Foo')
|
||||
self.assertRaises(ValidationError, f.get_prep_value, val)
|
||||
|
||||
|
||||
@@ -11,14 +11,17 @@ class JSignatureFormFieldTest(SimpleTestCase):
|
||||
f = JSignatureField()
|
||||
self.assertIsInstance(f.widget, JSignatureWidget)
|
||||
|
||||
def test_to_python(self):
|
||||
def test_to_python_empty_values(self):
|
||||
f = JSignatureField()
|
||||
# Empty values
|
||||
for val in ['', [], '[]']:
|
||||
self.assertIsNone(f.to_python(val))
|
||||
# Correct values
|
||||
|
||||
def test_to_python_correct_values(self):
|
||||
f = JSignatureField()
|
||||
val = '[{"x":[1,2], "y":[3,4]}]'
|
||||
self.assertEquals([{'x': [1, 2], 'y': [3, 4]}], f.to_python(val))
|
||||
# Incorrect values
|
||||
|
||||
def test_to_python_incorrect_values(self):
|
||||
f = JSignatureField()
|
||||
val = 'foo'
|
||||
self.assertRaises(ValidationError, f.to_python, val)
|
||||
|
||||
@@ -19,7 +19,7 @@ class JSignatureFieldsMixinTest(SimpleTestCase):
|
||||
def tearDown(self):
|
||||
settings.INSTALLED_APPS = self.old_installed_apps
|
||||
|
||||
def test_save(self):
|
||||
def test_save_create(self):
|
||||
# If an object is created signed, signature date must be set
|
||||
signature_value = [{"x": [1, 2], "y": [3, 4]}]
|
||||
i = JSignatureTestModel(signature=signature_value)
|
||||
@@ -27,16 +27,19 @@ class JSignatureFieldsMixinTest(SimpleTestCase):
|
||||
i = JSignatureTestModel.objects.get(pk=i.pk)
|
||||
self.assertEqual(date.today(), i.signature_date.date())
|
||||
|
||||
def test_save_no_change(self):
|
||||
# If signature doesn't change, signature date must not be updated
|
||||
signature_value = [{"x": [1, 2], "y": [3, 4]}]
|
||||
i = JSignatureTestModel(signature=signature_value)
|
||||
i.save()
|
||||
i.signature_date = date(2013, 1, 1)
|
||||
i.signature = signature_value
|
||||
i.save()
|
||||
i = JSignatureTestModel.objects.get(pk=i.pk)
|
||||
self.assertEqual(date(2013, 1, 1), i.signature_date.date())
|
||||
|
||||
def test_save_change(self):
|
||||
# If signature changes, signature date must be updated too
|
||||
signature_value = [{"x": [1, 2], "y": [3, 4]}]
|
||||
new_signature_value = [{"x": [5, 6], "y": [7, 8]}]
|
||||
i = JSignatureTestModel(signature=signature_value,
|
||||
signature_date=date(2013, 1, 1))
|
||||
@@ -47,7 +50,9 @@ class JSignatureFieldsMixinTest(SimpleTestCase):
|
||||
i = JSignatureTestModel.objects.get(pk=i.pk)
|
||||
self.assertEqual(date.today(), i.signature_date.date())
|
||||
|
||||
def test_save_none(self):
|
||||
# If sinature is set to None, it must be the same for signature_date
|
||||
signature_value = [{"x": [1, 2], "y": [3, 4]}]
|
||||
i = JSignatureTestModel(signature=signature_value)
|
||||
i.save()
|
||||
i.signature = None
|
||||
|
||||
@@ -13,22 +13,24 @@ DUMMY_STR_VALUE = json.dumps(DUMMY_VALUE)
|
||||
|
||||
class UtilsTest(SimpleTestCase):
|
||||
|
||||
def test_inputs(self):
|
||||
# Bad str value
|
||||
def test_inputs_bad_str_value(self):
|
||||
self.assertRaises(ValueError, draw_signature, 'foo_bar')
|
||||
# Bad type value
|
||||
|
||||
def test_inputs_bad_type_value(self):
|
||||
self.assertRaises(ValueError, draw_signature, object())
|
||||
# Good list value
|
||||
|
||||
def test_inputs_good_list_value(self):
|
||||
draw_signature(DUMMY_VALUE)
|
||||
# Good str value
|
||||
|
||||
def test_inputs_good_str_value(self):
|
||||
draw_signature(DUMMY_STR_VALUE)
|
||||
|
||||
def test_outputs(self):
|
||||
# As a file
|
||||
def test_outputs_as_file(self):
|
||||
output = draw_signature(DUMMY_VALUE, as_file=True)
|
||||
self.assertTrue(os.path.isfile(output))
|
||||
self.assertIsNotNone(imghdr.what(output))
|
||||
# As an Image
|
||||
|
||||
def test_outputs_as_image(self):
|
||||
output = draw_signature(DUMMY_VALUE)
|
||||
self.assertTrue(issubclass(output.__class__, Image.Image))
|
||||
self.assertTrue(all(output.getbbox()))
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import json
|
||||
from pyquery import PyQuery as pq
|
||||
import six
|
||||
|
||||
from django.test import SimpleTestCase
|
||||
from django.core.exceptions import ValidationError
|
||||
@@ -39,21 +40,28 @@ class JSignatureWidgetTest(SimpleTestCase):
|
||||
self.assertEqual(400, config.get('width'))
|
||||
self.assertEqual(JSIGNATURE_HEIGHT, config.get('height'))
|
||||
|
||||
def test_prep_value(self):
|
||||
def test_prep_value_empty_values(self):
|
||||
w = JSignatureWidget()
|
||||
# Empty values
|
||||
for val in ['', [], '[]']:
|
||||
self.assertEqual('[]', w.prep_value(val))
|
||||
# Correct values
|
||||
|
||||
def test_prep_value_correct_values_python(self):
|
||||
w = JSignatureWidget()
|
||||
val = [{"x": [1, 2], "y": [3, 4]}]
|
||||
val_prep = w.prep_value(val)
|
||||
self.assertIsInstance(val_prep, basestring)
|
||||
self.assertIsInstance(val_prep, six.string_types)
|
||||
self.assertEquals(val, json.loads(val_prep))
|
||||
|
||||
def test_prep_value_correct_values_json(self):
|
||||
w = JSignatureWidget()
|
||||
val = [{"x": [1, 2], "y": [3, 4]}]
|
||||
val_str = '[{"x":[1,2], "y":[3,4]}]'
|
||||
val_prep = w.prep_value(val_str)
|
||||
self.assertIsInstance(val_prep, basestring)
|
||||
self.assertIsInstance(val_prep, six.string_types)
|
||||
self.assertEquals(val, json.loads(val_prep))
|
||||
# Incorrect values
|
||||
|
||||
def test_prep_value_incorrect_values(self):
|
||||
w = JSignatureWidget()
|
||||
val = type('Foo')
|
||||
self.assertRaises(ValidationError, w.prep_value, val)
|
||||
|
||||
@@ -64,11 +72,12 @@ class JSignatureWidgetTest(SimpleTestCase):
|
||||
self.assertEqual(1, len(pq('.jsign-wrapper', output)))
|
||||
self.assertEqual(1, len(pq('[type=hidden]', output)))
|
||||
|
||||
def test_render_reset_button(self):
|
||||
def test_render_reset_button_true(self):
|
||||
w = JSignatureWidget(jsignature_attrs={'ResetButton': True})
|
||||
output = w.render(name='foo', value=None)
|
||||
self.assertEqual(1, len(pq('[type=button]', output)))
|
||||
|
||||
def test_render_reset_button_false(self):
|
||||
w = JSignatureWidget(jsignature_attrs={'ResetButton': False})
|
||||
output = w.render(name='foo', value=None)
|
||||
self.assertEqual(0, len(pq('[type=button]', output)))
|
||||
|
||||
@@ -3,11 +3,13 @@
|
||||
with jSignature jQuery plugin
|
||||
"""
|
||||
import json
|
||||
import six
|
||||
|
||||
from django.template.loader import render_to_string
|
||||
from django.forms.widgets import HiddenInput
|
||||
from django.core import validators
|
||||
from django.core.exceptions import ValidationError
|
||||
|
||||
from django.utils.safestring import mark_safe
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
from jsignature.settings import JSIGNATURE_DEFAULT_CONFIG
|
||||
@@ -51,7 +53,7 @@ class JSignatureWidget(HiddenInput):
|
||||
""" Prepare value before effectively render widget """
|
||||
if value in JSIGNATURE_EMPTY_VALUES:
|
||||
return "[]"
|
||||
elif isinstance(value, basestring):
|
||||
elif isinstance(value, six.string_types):
|
||||
return value
|
||||
elif isinstance(value, list):
|
||||
return json.dumps(value)
|
||||
|
||||
24
quicktest.py
24
quicktest.py
@@ -3,6 +3,7 @@ import sys
|
||||
import argparse
|
||||
from django.conf import settings
|
||||
|
||||
|
||||
class QuickDjangoTest(object):
|
||||
"""
|
||||
A quick way to run the Django test suite without a fully-configured project.
|
||||
@@ -16,8 +17,6 @@ class QuickDjangoTest(object):
|
||||
"""
|
||||
DIRNAME = os.path.dirname(__file__)
|
||||
INSTALLED_APPS = (
|
||||
'django.contrib.contenttypes',
|
||||
'django.contrib.sessions',
|
||||
)
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
@@ -29,6 +28,9 @@ class QuickDjangoTest(object):
|
||||
Fire up the Django test suite developed for version 1.2
|
||||
"""
|
||||
settings.configure(
|
||||
TEMPLATE_DIRS = ('jsignature/templates/',),
|
||||
ROOT_URLCONF = 'jsignature.tests',
|
||||
DEBUG = True,
|
||||
DATABASES = {
|
||||
'default': {
|
||||
'ENGINE': 'django.db.backends.sqlite3',
|
||||
@@ -39,8 +41,24 @@ class QuickDjangoTest(object):
|
||||
'PORT': '',
|
||||
}
|
||||
},
|
||||
INSTALLED_APPS=self.INSTALLED_APPS + self.apps,
|
||||
MIDDLEWARE_CLASSES = (
|
||||
'django.contrib.sessions.middleware.SessionMiddleware',
|
||||
'django.middleware.common.CommonMiddleware',
|
||||
'django.middleware.csrf.CsrfViewMiddleware',
|
||||
'django.contrib.auth.middleware.AuthenticationMiddleware',
|
||||
'django.contrib.messages.middleware.MessageMiddleware',
|
||||
),
|
||||
INSTALLED_APPS = self.INSTALLED_APPS + self.apps
|
||||
)
|
||||
# Setup is needed for Django >= 1.7
|
||||
import django
|
||||
if hasattr(django, 'setup'):
|
||||
django.setup()
|
||||
try:
|
||||
from django.test.runner import DiscoverRunner
|
||||
failures = DiscoverRunner().run_tests(self.apps, verbosity=1)
|
||||
except ImportError:
|
||||
# DjangoTestSuiteRunner has been deprecated in Django 1.7
|
||||
from django.test.simple import DjangoTestSuiteRunner
|
||||
failures = DjangoTestSuiteRunner().run_tests(self.apps, verbosity=1)
|
||||
if failures: # pragma: no cover
|
||||
|
||||
@@ -1,2 +1,3 @@
|
||||
pillow
|
||||
pyquery
|
||||
six
|
||||
|
||||
16
setup.py
16
setup.py
@@ -5,12 +5,12 @@ here = os.path.abspath(os.path.dirname(__file__))
|
||||
|
||||
setup(
|
||||
name='django-jsignature',
|
||||
version='0.7.6',
|
||||
version='0.8',
|
||||
author='Florent Lebreton',
|
||||
author_email='florent.lebreton@makina-corpus.com',
|
||||
url='https://github.com/fle/django-jsignature',
|
||||
download_url="https://github.com/fle/django-jsignature/tarball/0.7.6",
|
||||
description="Use jSignature jQuery plugin in your django projects",
|
||||
download_url='https://github.com/fle/django-jsignature/tarball/0.8',
|
||||
description='Use jSignature jQuery plugin in your django projects',
|
||||
long_description=open(os.path.join(here, 'README.rst')).read() + '\n\n' +
|
||||
open(os.path.join(here, 'CHANGES')).read(),
|
||||
license='LPGL, see LICENSE file.',
|
||||
@@ -26,5 +26,13 @@ setup(
|
||||
'Environment :: Web Environment',
|
||||
'Framework :: Django',
|
||||
'Development Status :: 5 - Production/Stable',
|
||||
'Programming Language :: Python :: 2.7'],
|
||||
'Programming Language :: Python',
|
||||
'Programming Language :: Python :: 2',
|
||||
'Programming Language :: Python :: 2.6',
|
||||
'Programming Language :: Python :: 2.7',
|
||||
'Programming Language :: Python :: 3',
|
||||
'Programming Language :: Python :: 3.2',
|
||||
'Programming Language :: Python :: 3.3',
|
||||
'Programming Language :: Python :: 3.4'
|
||||
],
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user