-
Notifications
You must be signed in to change notification settings - Fork 45
/
widgets.py
80 lines (66 loc) · 2.75 KB
/
widgets.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
"""
Provides a django form widget to handle a signature capture field with
with jSignature jQuery plugin
"""
import json
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
JSIGNATURE_EMPTY_VALUES = validators.EMPTY_VALUES + ('[]', )
class JSignatureWidget(HiddenInput):
"""
A widget handling a signature capture field with with jSignature
"""
# Actually, this widget has a display so we want it to behave like a
# normal field, not a hidden one
is_hidden = False
class Media:
js = ('js/jSignature.min.js',
'js/django_jsignature.js')
def __init__(self, attrs=None, jsignature_attrs=None):
super(JSignatureWidget, self).__init__(attrs)
# Store jSignature js config
self.jsignature_attrs = jsignature_attrs or {}
def build_jsignature_config(self):
""" Build javascript config for jSignature initialization.
It's a dict with for which default values come from settings
and can be overriden by jsignature_attrs, given at widget
instanciation time """
jsignature_config = JSIGNATURE_DEFAULT_CONFIG.copy()
jsignature_config.update(self.jsignature_attrs)
return jsignature_config
def build_jsignature_id(self, name):
""" Build HTML id for jsignature container.
It's important because it's used in javascript code """
return 'jsign_%s' % name
def prep_value(self, value):
""" Prepare value before effectively render widget """
if value in JSIGNATURE_EMPTY_VALUES:
return "[]"
elif isinstance(value, basestring):
return value
elif isinstance(value, list):
return json.dumps(value)
raise ValidationError('Invalid format.')
def render(self, name, value, attrs=None):
""" Render widget """
# Build config
jsign_id = self.build_jsignature_id(name)
jsignature_config = self.build_jsignature_config()
# Prepare value
value = self.prep_value(value)
# Build output
context = {
'hidden': super(JSignatureWidget, self).render(name, value, attrs),
'jsign_id': jsign_id,
'reset_btn_text': _('Reset'),
'config': jsignature_config,
'js_config': mark_safe(json.dumps(jsignature_config)),
'value': mark_safe(value),
}
out = render_to_string('jsignature/widget.html', context)
return mark_safe(out)