|
1
|
|
|
# -*- coding: utf-8 -*- |
|
2
|
|
|
|
|
3
|
|
|
import json |
|
4
|
|
|
|
|
5
|
|
|
import zope.component |
|
6
|
|
|
import zope.interface |
|
7
|
|
|
import zope.schema |
|
8
|
|
|
import zope.schema.interfaces |
|
9
|
|
|
from bika.lims import api |
|
10
|
|
|
from senaite.core.interfaces import ISenaiteFormLayer |
|
11
|
|
|
from senaite.core.schema.interfaces import IPhoneField |
|
12
|
|
|
from senaite.core.z3cform.interfaces import IPhoneWidget |
|
13
|
|
|
from z3c.form import interfaces |
|
14
|
|
|
from z3c.form.browser import text |
|
15
|
|
|
from z3c.form.browser import widget |
|
16
|
|
|
from z3c.form.interfaces import INPUT_MODE |
|
17
|
|
|
from z3c.form.widget import FieldWidget |
|
18
|
|
|
from zope.component import adapter |
|
19
|
|
|
from zope.interface import implementer_only |
|
20
|
|
|
|
|
21
|
|
|
|
|
22
|
|
|
@implementer_only(IPhoneWidget) |
|
23
|
|
|
class PhoneWidget(text.TextWidget): |
|
24
|
|
|
"""Input type "tel" widget implementation. |
|
25
|
|
|
""" |
|
26
|
|
|
klass = u"senaite-phone-widget" |
|
27
|
|
|
value = u"" |
|
28
|
|
|
initial_country = None |
|
29
|
|
|
preferred_countries = None |
|
30
|
|
|
|
|
31
|
|
|
def update(self): |
|
32
|
|
|
super(PhoneWidget, self).update() |
|
33
|
|
|
widget.addFieldClass(self) |
|
34
|
|
|
if self.mode == INPUT_MODE: |
|
35
|
|
|
self.addClass("form-control form-control-sm") |
|
36
|
|
|
|
|
37
|
|
|
def attrs(self): |
|
38
|
|
|
"""Return the template attributes for the date field |
|
39
|
|
|
|
|
40
|
|
|
:returns: dictionary of HTML attributes |
|
41
|
|
|
""" |
|
42
|
|
|
attrs = { |
|
43
|
|
|
"name": self.name, |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
initial_country = self.initial_country |
|
47
|
|
|
if initial_country is None: |
|
48
|
|
|
initial_country = self.get_default_country() |
|
49
|
|
|
attrs["data-initial_country"] = initial_country |
|
50
|
|
|
|
|
51
|
|
|
preferred_countries = self.preferred_countries |
|
52
|
|
|
if not isinstance(preferred_countries, list): |
|
53
|
|
|
preferred_countries = [] |
|
54
|
|
|
attrs["data-preferred_countries"] = json.dumps(preferred_countries) |
|
55
|
|
|
|
|
56
|
|
|
return attrs |
|
57
|
|
|
|
|
58
|
|
|
def get_default_country(self, default="us"): |
|
59
|
|
|
"""Return the default country from the system |
|
60
|
|
|
""" |
|
61
|
|
|
setup = api.get_setup() |
|
62
|
|
|
country = setup.getDefaultCountry() |
|
63
|
|
|
if not country: |
|
64
|
|
|
return default |
|
65
|
|
|
return country.lower() |
|
66
|
|
|
|
|
67
|
|
|
|
|
68
|
|
|
@adapter(IPhoneField, ISenaiteFormLayer) |
|
69
|
|
|
@zope.interface.implementer(interfaces.IFieldWidget) |
|
70
|
|
|
def PhoneWidgetFactory(field, request): |
|
71
|
|
|
"""IFieldWidget widget factory for NumberWidget. |
|
72
|
|
|
""" |
|
73
|
|
|
return FieldWidget(field, PhoneWidget(request)) |
|
74
|
|
|
|