1
|
|
|
# -*- coding: utf-8 -*- |
2
|
|
|
# |
3
|
|
|
# This file is part of SENAITE.CORE. |
4
|
|
|
# |
5
|
|
|
# SENAITE.CORE is free software: you can redistribute it and/or modify it under |
6
|
|
|
# the terms of the GNU General Public License as published by the Free Software |
7
|
|
|
# Foundation, version 2. |
8
|
|
|
# |
9
|
|
|
# This program is distributed in the hope that it will be useful, but WITHOUT |
10
|
|
|
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
11
|
|
|
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
12
|
|
|
# details. |
13
|
|
|
# |
14
|
|
|
# You should have received a copy of the GNU General Public License along with |
15
|
|
|
# this program; if not, write to the Free Software Foundation, Inc., 51 |
16
|
|
|
# Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
17
|
|
|
# |
18
|
|
|
# Copyright 2018-2024 by it's authors. |
19
|
|
|
# Some rights reserved, see README and LICENSE. |
20
|
|
|
|
21
|
|
|
from senaite.core.interfaces import ISenaiteFormLayer |
22
|
|
|
from senaite.core.schema.interfaces import ICoordinateField |
23
|
|
|
from senaite.core.z3cform.interfaces import ICoordinateWidget |
24
|
|
|
from senaite.core.z3cform.widgets.basewidget import BaseWidget |
25
|
|
|
from z3c.form.browser import widget |
26
|
|
|
from z3c.form.browser.widget import HTMLInputWidget |
27
|
|
|
from z3c.form.converter import BaseDataConverter |
28
|
|
|
from z3c.form.interfaces import IFieldWidget |
29
|
|
|
from z3c.form.widget import FieldWidget |
30
|
|
|
from zope.component import adapter |
31
|
|
|
from zope.interface import implementer |
32
|
|
|
|
33
|
|
|
|
34
|
|
|
@adapter(ICoordinateField, ICoordinateWidget) |
35
|
|
|
class CoordinateDataConverter(BaseDataConverter): |
36
|
|
|
"""Converts the value between the field and the widget |
37
|
|
|
""" |
38
|
|
|
|
39
|
|
|
def toWidgetValue(self, value): |
40
|
|
|
"""Converts from field value to widget |
41
|
|
|
""" |
42
|
|
|
if not isinstance(value, dict): |
43
|
|
|
return {} |
44
|
|
|
return value |
45
|
|
|
|
46
|
|
|
def toFieldValue(self, value): |
47
|
|
|
"""Converts from widget to field value |
48
|
|
|
""" |
49
|
|
|
if not isinstance(value, dict): |
50
|
|
|
return {} |
51
|
|
|
return value |
52
|
|
|
|
53
|
|
|
|
54
|
|
|
@implementer(ICoordinateWidget) |
55
|
|
|
class CoordinateWidget(HTMLInputWidget, BaseWidget): |
56
|
|
|
"""Widget for coordinate in degrees, minutes, seconds and bearing |
57
|
|
|
""" |
58
|
|
|
klass = u"senaite-coordinate-widget" |
59
|
|
|
|
60
|
|
|
@property |
61
|
|
|
def bearing_options(self): |
62
|
|
|
return self.field.bearing |
63
|
|
|
|
64
|
|
|
def update(self): |
65
|
|
|
"""Computes self.value for the widget templates |
66
|
|
|
|
67
|
|
|
see z3c.form.widget.Widget |
68
|
|
|
""" |
69
|
|
|
super(CoordinateWidget, self).update() |
70
|
|
|
widget.addFieldClass(self) |
71
|
|
|
|
72
|
|
|
|
73
|
|
|
@adapter(ICoordinateField, ISenaiteFormLayer) |
74
|
|
|
@implementer(IFieldWidget) |
75
|
|
|
def CoordinateWidgetFactory(field, request): |
76
|
|
|
"""Widget factory for coordinate field |
77
|
|
|
""" |
78
|
|
|
return FieldWidget(field, CoordinateWidget(request)) |
79
|
|
|
|