author | Adrien Di Mascio <Adrien.DiMascio@logilab.fr> |
Wed, 05 Aug 2009 09:14:34 +0200 | |
changeset 3386 | ab797c5374b7 |
parent 3385 | 68953ecddd2b |
child 3387 | a357d4147eee |
permissions | -rw-r--r-- |
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
1 |
"""field classes for form construction |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
2 |
|
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
3 |
:organization: Logilab |
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1858
diff
changeset
|
4 |
:copyright: 2009 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2. |
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
5 |
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr |
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1858
diff
changeset
|
6 |
:license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses |
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
7 |
""" |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
8 |
__docformat__ = "restructuredtext en" |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
9 |
|
2244
52e2431e7cce
missing import
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2196
diff
changeset
|
10 |
from warnings import warn |
1095
6917ebe281e9
test and fix guess_field, some pylint fixes
sylvain.thenault@logilab.fr
parents:
1081
diff
changeset
|
11 |
from datetime import datetime |
6917ebe281e9
test and fix guess_field, some pylint fixes
sylvain.thenault@logilab.fr
parents:
1081
diff
changeset
|
12 |
|
2312
af4d8f75c5db
use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2244
diff
changeset
|
13 |
from logilab.mtconverter import xml_escape |
2459
d088d0ff48a1
move RichString and co to yams, keeping only a small monkeypatch for cw-page-template here
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2362
diff
changeset
|
14 |
from yams.constraints import (SizeConstraint, StaticVocabularyConstraint, |
d088d0ff48a1
move RichString and co to yams, keeping only a small monkeypatch for cw-page-template here
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2362
diff
changeset
|
15 |
FormatConstraint) |
1095
6917ebe281e9
test and fix guess_field, some pylint fixes
sylvain.thenault@logilab.fr
parents:
1081
diff
changeset
|
16 |
|
2678
0308dc159b96
use yams api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2568
diff
changeset
|
17 |
from cubicweb.utils import ustrftime |
1095
6917ebe281e9
test and fix guess_field, some pylint fixes
sylvain.thenault@logilab.fr
parents:
1081
diff
changeset
|
18 |
from cubicweb.common import tags, uilib |
6917ebe281e9
test and fix guess_field, some pylint fixes
sylvain.thenault@logilab.fr
parents:
1081
diff
changeset
|
19 |
from cubicweb.web import INTERNAL_FIELD_VALUE |
1265
e5cdd5c0dce3
handle sort/internationalizable on Field base class + fix guess_field to deal with internationalizable and default values
sylvain.thenault@logilab.fr
parents:
1181
diff
changeset
|
20 |
from cubicweb.web.formwidgets import ( |
e5cdd5c0dce3
handle sort/internationalizable on Field base class + fix guess_field to deal with internationalizable and default values
sylvain.thenault@logilab.fr
parents:
1181
diff
changeset
|
21 |
HiddenInput, TextInput, FileInput, PasswordInput, TextArea, FCKEditor, |
1437 | 22 |
Radio, Select, DateTimePicker) |
1095
6917ebe281e9
test and fix guess_field, some pylint fixes
sylvain.thenault@logilab.fr
parents:
1081
diff
changeset
|
23 |
|
1986
96c0e56cb0cf
move widget selection's logic on fields
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
24 |
|
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
25 |
class Field(object): |
1265
e5cdd5c0dce3
handle sort/internationalizable on Field base class + fix guess_field to deal with internationalizable and default values
sylvain.thenault@logilab.fr
parents:
1181
diff
changeset
|
26 |
"""field class is introduced to control what's displayed in forms. It makes |
e5cdd5c0dce3
handle sort/internationalizable on Field base class + fix guess_field to deal with internationalizable and default values
sylvain.thenault@logilab.fr
parents:
1181
diff
changeset
|
27 |
the link between something to edit and its display in the form. Actual |
e5cdd5c0dce3
handle sort/internationalizable on Field base class + fix guess_field to deal with internationalizable and default values
sylvain.thenault@logilab.fr
parents:
1181
diff
changeset
|
28 |
display is handled by a widget associated to the field. |
e5cdd5c0dce3
handle sort/internationalizable on Field base class + fix guess_field to deal with internationalizable and default values
sylvain.thenault@logilab.fr
parents:
1181
diff
changeset
|
29 |
|
e5cdd5c0dce3
handle sort/internationalizable on Field base class + fix guess_field to deal with internationalizable and default values
sylvain.thenault@logilab.fr
parents:
1181
diff
changeset
|
30 |
Attributes |
e5cdd5c0dce3
handle sort/internationalizable on Field base class + fix guess_field to deal with internationalizable and default values
sylvain.thenault@logilab.fr
parents:
1181
diff
changeset
|
31 |
---------- |
e5cdd5c0dce3
handle sort/internationalizable on Field base class + fix guess_field to deal with internationalizable and default values
sylvain.thenault@logilab.fr
parents:
1181
diff
changeset
|
32 |
all the attributes described below have sensible default value which may be |
e5cdd5c0dce3
handle sort/internationalizable on Field base class + fix guess_field to deal with internationalizable and default values
sylvain.thenault@logilab.fr
parents:
1181
diff
changeset
|
33 |
overriden by value given to field's constructor. |
1437 | 34 |
|
1265
e5cdd5c0dce3
handle sort/internationalizable on Field base class + fix guess_field to deal with internationalizable and default values
sylvain.thenault@logilab.fr
parents:
1181
diff
changeset
|
35 |
:name: |
e5cdd5c0dce3
handle sort/internationalizable on Field base class + fix guess_field to deal with internationalizable and default values
sylvain.thenault@logilab.fr
parents:
1181
diff
changeset
|
36 |
name of the field (basestring), should be unique in a form. |
e5cdd5c0dce3
handle sort/internationalizable on Field base class + fix guess_field to deal with internationalizable and default values
sylvain.thenault@logilab.fr
parents:
1181
diff
changeset
|
37 |
:id: |
e5cdd5c0dce3
handle sort/internationalizable on Field base class + fix guess_field to deal with internationalizable and default values
sylvain.thenault@logilab.fr
parents:
1181
diff
changeset
|
38 |
dom identifier (default to the same value as `name`), should be unique in |
e5cdd5c0dce3
handle sort/internationalizable on Field base class + fix guess_field to deal with internationalizable and default values
sylvain.thenault@logilab.fr
parents:
1181
diff
changeset
|
39 |
a form. |
e5cdd5c0dce3
handle sort/internationalizable on Field base class + fix guess_field to deal with internationalizable and default values
sylvain.thenault@logilab.fr
parents:
1181
diff
changeset
|
40 |
:label: |
e5cdd5c0dce3
handle sort/internationalizable on Field base class + fix guess_field to deal with internationalizable and default values
sylvain.thenault@logilab.fr
parents:
1181
diff
changeset
|
41 |
label of the field (default to the same value as `name`). |
e5cdd5c0dce3
handle sort/internationalizable on Field base class + fix guess_field to deal with internationalizable and default values
sylvain.thenault@logilab.fr
parents:
1181
diff
changeset
|
42 |
:help: |
e5cdd5c0dce3
handle sort/internationalizable on Field base class + fix guess_field to deal with internationalizable and default values
sylvain.thenault@logilab.fr
parents:
1181
diff
changeset
|
43 |
help message about this field. |
e5cdd5c0dce3
handle sort/internationalizable on Field base class + fix guess_field to deal with internationalizable and default values
sylvain.thenault@logilab.fr
parents:
1181
diff
changeset
|
44 |
:widget: |
e5cdd5c0dce3
handle sort/internationalizable on Field base class + fix guess_field to deal with internationalizable and default values
sylvain.thenault@logilab.fr
parents:
1181
diff
changeset
|
45 |
widget associated to the field. Each field class has a default widget |
e5cdd5c0dce3
handle sort/internationalizable on Field base class + fix guess_field to deal with internationalizable and default values
sylvain.thenault@logilab.fr
parents:
1181
diff
changeset
|
46 |
class which may be overriden per instance. |
e5cdd5c0dce3
handle sort/internationalizable on Field base class + fix guess_field to deal with internationalizable and default values
sylvain.thenault@logilab.fr
parents:
1181
diff
changeset
|
47 |
:required: |
e5cdd5c0dce3
handle sort/internationalizable on Field base class + fix guess_field to deal with internationalizable and default values
sylvain.thenault@logilab.fr
parents:
1181
diff
changeset
|
48 |
bool flag telling if the field is required or not. |
e5cdd5c0dce3
handle sort/internationalizable on Field base class + fix guess_field to deal with internationalizable and default values
sylvain.thenault@logilab.fr
parents:
1181
diff
changeset
|
49 |
:initial: |
e5cdd5c0dce3
handle sort/internationalizable on Field base class + fix guess_field to deal with internationalizable and default values
sylvain.thenault@logilab.fr
parents:
1181
diff
changeset
|
50 |
initial value, used when no value specified by other means. |
e5cdd5c0dce3
handle sort/internationalizable on Field base class + fix guess_field to deal with internationalizable and default values
sylvain.thenault@logilab.fr
parents:
1181
diff
changeset
|
51 |
:choices: |
e5cdd5c0dce3
handle sort/internationalizable on Field base class + fix guess_field to deal with internationalizable and default values
sylvain.thenault@logilab.fr
parents:
1181
diff
changeset
|
52 |
static vocabulary for this field. May be a list of values or a list of |
e5cdd5c0dce3
handle sort/internationalizable on Field base class + fix guess_field to deal with internationalizable and default values
sylvain.thenault@logilab.fr
parents:
1181
diff
changeset
|
53 |
(label, value) tuples if specified. |
e5cdd5c0dce3
handle sort/internationalizable on Field base class + fix guess_field to deal with internationalizable and default values
sylvain.thenault@logilab.fr
parents:
1181
diff
changeset
|
54 |
:sort: |
e5cdd5c0dce3
handle sort/internationalizable on Field base class + fix guess_field to deal with internationalizable and default values
sylvain.thenault@logilab.fr
parents:
1181
diff
changeset
|
55 |
bool flag telling if the vocabulary (either static vocabulary specified |
e5cdd5c0dce3
handle sort/internationalizable on Field base class + fix guess_field to deal with internationalizable and default values
sylvain.thenault@logilab.fr
parents:
1181
diff
changeset
|
56 |
in `choices` or dynamic vocabulary fetched from the form) should be |
e5cdd5c0dce3
handle sort/internationalizable on Field base class + fix guess_field to deal with internationalizable and default values
sylvain.thenault@logilab.fr
parents:
1181
diff
changeset
|
57 |
sorted on label. |
e5cdd5c0dce3
handle sort/internationalizable on Field base class + fix guess_field to deal with internationalizable and default values
sylvain.thenault@logilab.fr
parents:
1181
diff
changeset
|
58 |
:internationalizable: |
e5cdd5c0dce3
handle sort/internationalizable on Field base class + fix guess_field to deal with internationalizable and default values
sylvain.thenault@logilab.fr
parents:
1181
diff
changeset
|
59 |
bool flag telling if the vocabulary labels should be translated using the |
e5cdd5c0dce3
handle sort/internationalizable on Field base class + fix guess_field to deal with internationalizable and default values
sylvain.thenault@logilab.fr
parents:
1181
diff
changeset
|
60 |
current request language. |
e5cdd5c0dce3
handle sort/internationalizable on Field base class + fix guess_field to deal with internationalizable and default values
sylvain.thenault@logilab.fr
parents:
1181
diff
changeset
|
61 |
:eidparam: |
e5cdd5c0dce3
handle sort/internationalizable on Field base class + fix guess_field to deal with internationalizable and default values
sylvain.thenault@logilab.fr
parents:
1181
diff
changeset
|
62 |
bool flag telling if this field is linked to a specific entity |
e5cdd5c0dce3
handle sort/internationalizable on Field base class + fix guess_field to deal with internationalizable and default values
sylvain.thenault@logilab.fr
parents:
1181
diff
changeset
|
63 |
:role: |
e5cdd5c0dce3
handle sort/internationalizable on Field base class + fix guess_field to deal with internationalizable and default values
sylvain.thenault@logilab.fr
parents:
1181
diff
changeset
|
64 |
when the field is linked to an entity attribute or relation, tells the |
e5cdd5c0dce3
handle sort/internationalizable on Field base class + fix guess_field to deal with internationalizable and default values
sylvain.thenault@logilab.fr
parents:
1181
diff
changeset
|
65 |
role of the entity in the relation (eg 'subject' or 'object') |
2520
8c5cf48ae9ea
new fieldset attribute on field, use to group fields by the default form renderer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2459
diff
changeset
|
66 |
:fieldset: |
8c5cf48ae9ea
new fieldset attribute on field, use to group fields by the default form renderer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2459
diff
changeset
|
67 |
optional fieldset to which this field belongs to |
1437 | 68 |
|
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
69 |
""" |
1265
e5cdd5c0dce3
handle sort/internationalizable on Field base class + fix guess_field to deal with internationalizable and default values
sylvain.thenault@logilab.fr
parents:
1181
diff
changeset
|
70 |
# default widget associated to this class of fields. May be overriden per |
e5cdd5c0dce3
handle sort/internationalizable on Field base class + fix guess_field to deal with internationalizable and default values
sylvain.thenault@logilab.fr
parents:
1181
diff
changeset
|
71 |
# instance |
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
72 |
widget = TextInput |
1265
e5cdd5c0dce3
handle sort/internationalizable on Field base class + fix guess_field to deal with internationalizable and default values
sylvain.thenault@logilab.fr
parents:
1181
diff
changeset
|
73 |
# does this field requires a multipart form |
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
74 |
needs_multipart = False |
1265
e5cdd5c0dce3
handle sort/internationalizable on Field base class + fix guess_field to deal with internationalizable and default values
sylvain.thenault@logilab.fr
parents:
1181
diff
changeset
|
75 |
# class attribute used for ordering of fields in a form |
e5cdd5c0dce3
handle sort/internationalizable on Field base class + fix guess_field to deal with internationalizable and default values
sylvain.thenault@logilab.fr
parents:
1181
diff
changeset
|
76 |
__creation_rank = 0 |
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
77 |
|
1437 | 78 |
def __init__(self, name=None, id=None, label=None, help=None, |
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
79 |
widget=None, required=False, initial=None, |
1265
e5cdd5c0dce3
handle sort/internationalizable on Field base class + fix guess_field to deal with internationalizable and default values
sylvain.thenault@logilab.fr
parents:
1181
diff
changeset
|
80 |
choices=None, sort=True, internationalizable=False, |
2520
8c5cf48ae9ea
new fieldset attribute on field, use to group fields by the default form renderer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2459
diff
changeset
|
81 |
eidparam=False, role='subject', fieldset=None): |
1265
e5cdd5c0dce3
handle sort/internationalizable on Field base class + fix guess_field to deal with internationalizable and default values
sylvain.thenault@logilab.fr
parents:
1181
diff
changeset
|
82 |
self.name = name |
e5cdd5c0dce3
handle sort/internationalizable on Field base class + fix guess_field to deal with internationalizable and default values
sylvain.thenault@logilab.fr
parents:
1181
diff
changeset
|
83 |
self.id = id or name |
e5cdd5c0dce3
handle sort/internationalizable on Field base class + fix guess_field to deal with internationalizable and default values
sylvain.thenault@logilab.fr
parents:
1181
diff
changeset
|
84 |
self.label = label or name |
e5cdd5c0dce3
handle sort/internationalizable on Field base class + fix guess_field to deal with internationalizable and default values
sylvain.thenault@logilab.fr
parents:
1181
diff
changeset
|
85 |
self.help = help |
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
86 |
self.required = required |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
87 |
self.initial = initial |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
88 |
self.choices = choices |
1265
e5cdd5c0dce3
handle sort/internationalizable on Field base class + fix guess_field to deal with internationalizable and default values
sylvain.thenault@logilab.fr
parents:
1181
diff
changeset
|
89 |
self.sort = sort |
e5cdd5c0dce3
handle sort/internationalizable on Field base class + fix guess_field to deal with internationalizable and default values
sylvain.thenault@logilab.fr
parents:
1181
diff
changeset
|
90 |
self.internationalizable = internationalizable |
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
91 |
self.eidparam = eidparam |
1095
6917ebe281e9
test and fix guess_field, some pylint fixes
sylvain.thenault@logilab.fr
parents:
1081
diff
changeset
|
92 |
self.role = role |
2520
8c5cf48ae9ea
new fieldset attribute on field, use to group fields by the default form renderer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2459
diff
changeset
|
93 |
self.fieldset = fieldset |
1986
96c0e56cb0cf
move widget selection's logic on fields
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
94 |
self.init_widget(widget) |
1265
e5cdd5c0dce3
handle sort/internationalizable on Field base class + fix guess_field to deal with internationalizable and default values
sylvain.thenault@logilab.fr
parents:
1181
diff
changeset
|
95 |
# ordering number for this field instance |
e5cdd5c0dce3
handle sort/internationalizable on Field base class + fix guess_field to deal with internationalizable and default values
sylvain.thenault@logilab.fr
parents:
1181
diff
changeset
|
96 |
self.creation_rank = Field.__creation_rank |
e5cdd5c0dce3
handle sort/internationalizable on Field base class + fix guess_field to deal with internationalizable and default values
sylvain.thenault@logilab.fr
parents:
1181
diff
changeset
|
97 |
Field.__creation_rank += 1 |
1437 | 98 |
|
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
99 |
def __unicode__(self): |
1562
e6d2c07c0c58
[forms/widgets] fix relation field not sorting its vocabulary, revert hack on Select widget
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1505
diff
changeset
|
100 |
return u'<%s name=%r label=%r id=%r initial=%r visible=%r @%x>' % ( |
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
101 |
self.__class__.__name__, self.name, self.label, |
1562
e6d2c07c0c58
[forms/widgets] fix relation field not sorting its vocabulary, revert hack on Select widget
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1505
diff
changeset
|
102 |
self.id, self.initial, self.is_visible(), id(self)) |
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
103 |
|
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
104 |
def __repr__(self): |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
105 |
return self.__unicode__().encode('utf-8') |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
106 |
|
1986
96c0e56cb0cf
move widget selection's logic on fields
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
107 |
def init_widget(self, widget): |
96c0e56cb0cf
move widget selection's logic on fields
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
108 |
if widget is not None: |
96c0e56cb0cf
move widget selection's logic on fields
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
109 |
self.widget = widget |
2091
a7ea618e5478
don't set select widget when a vocabulary widget is already specified on the field class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2086
diff
changeset
|
110 |
elif self.choices and not self.widget.vocabulary_widget: |
a7ea618e5478
don't set select widget when a vocabulary widget is already specified on the field class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2086
diff
changeset
|
111 |
self.widget = Select() |
1986
96c0e56cb0cf
move widget selection's logic on fields
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
112 |
if isinstance(self.widget, type): |
96c0e56cb0cf
move widget selection's logic on fields
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
113 |
self.widget = self.widget() |
96c0e56cb0cf
move widget selection's logic on fields
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
114 |
|
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
115 |
def set_name(self, name): |
1265
e5cdd5c0dce3
handle sort/internationalizable on Field base class + fix guess_field to deal with internationalizable and default values
sylvain.thenault@logilab.fr
parents:
1181
diff
changeset
|
116 |
"""automatically set .id and .label when name is set""" |
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
117 |
assert name |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
118 |
self.name = name |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
119 |
if not self.id: |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
120 |
self.id = name |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
121 |
if not self.label: |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
122 |
self.label = name |
1437 | 123 |
|
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
124 |
def is_visible(self): |
1265
e5cdd5c0dce3
handle sort/internationalizable on Field base class + fix guess_field to deal with internationalizable and default values
sylvain.thenault@logilab.fr
parents:
1181
diff
changeset
|
125 |
"""return true if the field is not an hidden field""" |
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
126 |
return not isinstance(self.widget, HiddenInput) |
1437 | 127 |
|
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
128 |
def actual_fields(self, form): |
1265
e5cdd5c0dce3
handle sort/internationalizable on Field base class + fix guess_field to deal with internationalizable and default values
sylvain.thenault@logilab.fr
parents:
1181
diff
changeset
|
129 |
"""return actual fields composing this field in case of a compound |
e5cdd5c0dce3
handle sort/internationalizable on Field base class + fix guess_field to deal with internationalizable and default values
sylvain.thenault@logilab.fr
parents:
1181
diff
changeset
|
130 |
field, usually simply return self |
e5cdd5c0dce3
handle sort/internationalizable on Field base class + fix guess_field to deal with internationalizable and default values
sylvain.thenault@logilab.fr
parents:
1181
diff
changeset
|
131 |
""" |
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
132 |
yield self |
1437 | 133 |
|
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
134 |
def format_value(self, req, value): |
1265
e5cdd5c0dce3
handle sort/internationalizable on Field base class + fix guess_field to deal with internationalizable and default values
sylvain.thenault@logilab.fr
parents:
1181
diff
changeset
|
135 |
"""return value suitable for display where value may be a list or tuple |
e5cdd5c0dce3
handle sort/internationalizable on Field base class + fix guess_field to deal with internationalizable and default values
sylvain.thenault@logilab.fr
parents:
1181
diff
changeset
|
136 |
of values |
e5cdd5c0dce3
handle sort/internationalizable on Field base class + fix guess_field to deal with internationalizable and default values
sylvain.thenault@logilab.fr
parents:
1181
diff
changeset
|
137 |
""" |
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
138 |
if isinstance(value, (list, tuple)): |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
139 |
return [self.format_single_value(req, val) for val in value] |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
140 |
return self.format_single_value(req, value) |
1437 | 141 |
|
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
142 |
def format_single_value(self, req, value): |
1265
e5cdd5c0dce3
handle sort/internationalizable on Field base class + fix guess_field to deal with internationalizable and default values
sylvain.thenault@logilab.fr
parents:
1181
diff
changeset
|
143 |
"""return value suitable for display""" |
1306 | 144 |
if value is None or value is False: |
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
145 |
return u'' |
1306 | 146 |
if value is True: |
147 |
return u'1' |
|
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
148 |
return unicode(value) |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
149 |
|
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
150 |
def get_widget(self, form): |
1265
e5cdd5c0dce3
handle sort/internationalizable on Field base class + fix guess_field to deal with internationalizable and default values
sylvain.thenault@logilab.fr
parents:
1181
diff
changeset
|
151 |
"""return the widget instance associated to this field""" |
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
152 |
return self.widget |
1437 | 153 |
|
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
154 |
def example_format(self, req): |
1265
e5cdd5c0dce3
handle sort/internationalizable on Field base class + fix guess_field to deal with internationalizable and default values
sylvain.thenault@logilab.fr
parents:
1181
diff
changeset
|
155 |
"""return a sample string describing what can be given as input for this |
e5cdd5c0dce3
handle sort/internationalizable on Field base class + fix guess_field to deal with internationalizable and default values
sylvain.thenault@logilab.fr
parents:
1181
diff
changeset
|
156 |
field |
1437 | 157 |
""" |
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
158 |
return u'' |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
159 |
|
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
160 |
def render(self, form, renderer): |
1265
e5cdd5c0dce3
handle sort/internationalizable on Field base class + fix guess_field to deal with internationalizable and default values
sylvain.thenault@logilab.fr
parents:
1181
diff
changeset
|
161 |
"""render this field, which is part of form, using the given form |
e5cdd5c0dce3
handle sort/internationalizable on Field base class + fix guess_field to deal with internationalizable and default values
sylvain.thenault@logilab.fr
parents:
1181
diff
changeset
|
162 |
renderer |
e5cdd5c0dce3
handle sort/internationalizable on Field base class + fix guess_field to deal with internationalizable and default values
sylvain.thenault@logilab.fr
parents:
1181
diff
changeset
|
163 |
""" |
2522
562f5dcf2345
widget.render now takes the renderer as third argument (keeping minimal bw compat)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2520
diff
changeset
|
164 |
widget = self.get_widget(form) |
562f5dcf2345
widget.render now takes the renderer as third argument (keeping minimal bw compat)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2520
diff
changeset
|
165 |
try: |
562f5dcf2345
widget.render now takes the renderer as third argument (keeping minimal bw compat)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2520
diff
changeset
|
166 |
return widget.render(form, self, renderer) |
562f5dcf2345
widget.render now takes the renderer as third argument (keeping minimal bw compat)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2520
diff
changeset
|
167 |
except TypeError: |
562f5dcf2345
widget.render now takes the renderer as third argument (keeping minimal bw compat)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2520
diff
changeset
|
168 |
warn('widget.render now take the renderer as third argument, please update %s implementation' |
562f5dcf2345
widget.render now takes the renderer as third argument (keeping minimal bw compat)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2520
diff
changeset
|
169 |
% widget.__class__.__name__, DeprecationWarning) |
562f5dcf2345
widget.render now takes the renderer as third argument (keeping minimal bw compat)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2520
diff
changeset
|
170 |
return widget.render(form, self) |
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
171 |
|
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
172 |
def vocabulary(self, form): |
1265
e5cdd5c0dce3
handle sort/internationalizable on Field base class + fix guess_field to deal with internationalizable and default values
sylvain.thenault@logilab.fr
parents:
1181
diff
changeset
|
173 |
"""return vocabulary for this field. This method will be called by |
1437 | 174 |
widgets which desire it.""" |
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
175 |
if self.choices is not None: |
1181
620ec8e6ae19
cleanup, various fix to get something working
sylvain.thenault@logilab.fr
parents:
1147
diff
changeset
|
176 |
if callable(self.choices): |
2196
b08521e4eaab
vocabulary method (eg field.choices) should now take the form instance as argument
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2146
diff
changeset
|
177 |
try: |
b08521e4eaab
vocabulary method (eg field.choices) should now take the form instance as argument
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2146
diff
changeset
|
178 |
vocab = self.choices(form=form) |
b08521e4eaab
vocabulary method (eg field.choices) should now take the form instance as argument
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2146
diff
changeset
|
179 |
except TypeError: |
b08521e4eaab
vocabulary method (eg field.choices) should now take the form instance as argument
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2146
diff
changeset
|
180 |
warn('vocabulary method (eg field.choices) should now take ' |
b08521e4eaab
vocabulary method (eg field.choices) should now take the form instance as argument
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2146
diff
changeset
|
181 |
'the form instance as argument', DeprecationWarning) |
b08521e4eaab
vocabulary method (eg field.choices) should now take the form instance as argument
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2146
diff
changeset
|
182 |
vocab = self.choices(req=form.req) |
1181
620ec8e6ae19
cleanup, various fix to get something working
sylvain.thenault@logilab.fr
parents:
1147
diff
changeset
|
183 |
else: |
620ec8e6ae19
cleanup, various fix to get something working
sylvain.thenault@logilab.fr
parents:
1147
diff
changeset
|
184 |
vocab = self.choices |
620ec8e6ae19
cleanup, various fix to get something working
sylvain.thenault@logilab.fr
parents:
1147
diff
changeset
|
185 |
if vocab and not isinstance(vocab[0], (list, tuple)): |
620ec8e6ae19
cleanup, various fix to get something working
sylvain.thenault@logilab.fr
parents:
1147
diff
changeset
|
186 |
vocab = [(x, x) for x in vocab] |
1265
e5cdd5c0dce3
handle sort/internationalizable on Field base class + fix guess_field to deal with internationalizable and default values
sylvain.thenault@logilab.fr
parents:
1181
diff
changeset
|
187 |
else: |
e5cdd5c0dce3
handle sort/internationalizable on Field base class + fix guess_field to deal with internationalizable and default values
sylvain.thenault@logilab.fr
parents:
1181
diff
changeset
|
188 |
vocab = form.form_field_vocabulary(self) |
e5cdd5c0dce3
handle sort/internationalizable on Field base class + fix guess_field to deal with internationalizable and default values
sylvain.thenault@logilab.fr
parents:
1181
diff
changeset
|
189 |
if self.internationalizable: |
e5cdd5c0dce3
handle sort/internationalizable on Field base class + fix guess_field to deal with internationalizable and default values
sylvain.thenault@logilab.fr
parents:
1181
diff
changeset
|
190 |
vocab = [(form.req._(label), value) for label, value in vocab] |
e5cdd5c0dce3
handle sort/internationalizable on Field base class + fix guess_field to deal with internationalizable and default values
sylvain.thenault@logilab.fr
parents:
1181
diff
changeset
|
191 |
if self.sort: |
e5cdd5c0dce3
handle sort/internationalizable on Field base class + fix guess_field to deal with internationalizable and default values
sylvain.thenault@logilab.fr
parents:
1181
diff
changeset
|
192 |
vocab = sorted(vocab) |
e5cdd5c0dce3
handle sort/internationalizable on Field base class + fix guess_field to deal with internationalizable and default values
sylvain.thenault@logilab.fr
parents:
1181
diff
changeset
|
193 |
return vocab |
1437 | 194 |
|
1307 | 195 |
def form_init(self, form): |
196 |
"""method called before by form_build_context to trigger potential field |
|
197 |
initialization requiring the form instance |
|
198 |
""" |
|
199 |
pass |
|
1437 | 200 |
|
1986
96c0e56cb0cf
move widget selection's logic on fields
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
201 |
|
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
202 |
class StringField(Field): |
1986
96c0e56cb0cf
move widget selection's logic on fields
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
203 |
widget = TextArea |
96c0e56cb0cf
move widget selection's logic on fields
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
204 |
|
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
205 |
def __init__(self, max_length=None, **kwargs): |
1986
96c0e56cb0cf
move widget selection's logic on fields
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
206 |
self.max_length = max_length # must be set before super call |
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
207 |
super(StringField, self).__init__(**kwargs) |
1986
96c0e56cb0cf
move widget selection's logic on fields
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
208 |
|
96c0e56cb0cf
move widget selection's logic on fields
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
209 |
def init_widget(self, widget): |
96c0e56cb0cf
move widget selection's logic on fields
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
210 |
if widget is None: |
96c0e56cb0cf
move widget selection's logic on fields
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
211 |
if self.choices: |
1988
b5c5c088bb03
if choices is set and widget not explicitly specified, use Select in base field
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1986
diff
changeset
|
212 |
widget = Select() |
1986
96c0e56cb0cf
move widget selection's logic on fields
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
213 |
elif self.max_length and self.max_length < 257: |
1988
b5c5c088bb03
if choices is set and widget not explicitly specified, use Select in base field
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1986
diff
changeset
|
214 |
widget = TextInput() |
2344
22b5ea0679ed
[formfields] set a reasonable minimal minimum length to input/text, also set the max length if applicable (closes #344538)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
2332
diff
changeset
|
215 |
|
1986
96c0e56cb0cf
move widget selection's logic on fields
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
216 |
super(StringField, self).init_widget(widget) |
1573
d34589d35daa
drop TextField, this is simply a StringField with a text area widget
sylvain.thenault@logilab.fr
parents:
1564
diff
changeset
|
217 |
if isinstance(self.widget, TextArea): |
d34589d35daa
drop TextField, this is simply a StringField with a text area widget
sylvain.thenault@logilab.fr
parents:
1564
diff
changeset
|
218 |
self.init_text_area(self.widget) |
2362 | 219 |
elif isinstance(self.widget, TextInput): |
2360
1d43aa551ba9
[formwidgets,formfields] hum, this way
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
2356
diff
changeset
|
220 |
self.init_text_input(self.widget) |
1d43aa551ba9
[formwidgets,formfields] hum, this way
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
2356
diff
changeset
|
221 |
|
1d43aa551ba9
[formwidgets,formfields] hum, this way
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
2356
diff
changeset
|
222 |
def init_text_input(self, widget): |
1d43aa551ba9
[formwidgets,formfields] hum, this way
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
2356
diff
changeset
|
223 |
if self.max_length: |
1d43aa551ba9
[formwidgets,formfields] hum, this way
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
2356
diff
changeset
|
224 |
widget.attrs.setdefault('size', min(45, self.max_length)) |
1d43aa551ba9
[formwidgets,formfields] hum, this way
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
2356
diff
changeset
|
225 |
widget.attrs.setdefault('maxlength', self.max_length) |
1573
d34589d35daa
drop TextField, this is simply a StringField with a text area widget
sylvain.thenault@logilab.fr
parents:
1564
diff
changeset
|
226 |
|
d34589d35daa
drop TextField, this is simply a StringField with a text area widget
sylvain.thenault@logilab.fr
parents:
1564
diff
changeset
|
227 |
def init_text_area(self, widget): |
d34589d35daa
drop TextField, this is simply a StringField with a text area widget
sylvain.thenault@logilab.fr
parents:
1564
diff
changeset
|
228 |
if self.max_length < 513: |
d34589d35daa
drop TextField, this is simply a StringField with a text area widget
sylvain.thenault@logilab.fr
parents:
1564
diff
changeset
|
229 |
widget.attrs.setdefault('cols', 60) |
d34589d35daa
drop TextField, this is simply a StringField with a text area widget
sylvain.thenault@logilab.fr
parents:
1564
diff
changeset
|
230 |
widget.attrs.setdefault('rows', 5) |
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
231 |
|
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
232 |
|
1573
d34589d35daa
drop TextField, this is simply a StringField with a text area widget
sylvain.thenault@logilab.fr
parents:
1564
diff
changeset
|
233 |
class RichTextField(StringField): |
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
234 |
widget = None |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
235 |
def __init__(self, format_field=None, **kwargs): |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
236 |
super(RichTextField, self).__init__(**kwargs) |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
237 |
self.format_field = format_field |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
238 |
|
2348
acf4b6a59558
[formwidgets] ensure textarea for richstring has not ridicuously small size (close #344547)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
2347
diff
changeset
|
239 |
def init_text_area(self, widget): |
acf4b6a59558
[formwidgets] ensure textarea for richstring has not ridicuously small size (close #344547)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
2347
diff
changeset
|
240 |
pass |
acf4b6a59558
[formwidgets] ensure textarea for richstring has not ridicuously small size (close #344547)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
2347
diff
changeset
|
241 |
|
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
242 |
def get_widget(self, form): |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
243 |
if self.widget is None: |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
244 |
if self.use_fckeditor(form): |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
245 |
return FCKEditor() |
1573
d34589d35daa
drop TextField, this is simply a StringField with a text area widget
sylvain.thenault@logilab.fr
parents:
1564
diff
changeset
|
246 |
widget = TextArea() |
d34589d35daa
drop TextField, this is simply a StringField with a text area widget
sylvain.thenault@logilab.fr
parents:
1564
diff
changeset
|
247 |
self.init_text_area(widget) |
d34589d35daa
drop TextField, this is simply a StringField with a text area widget
sylvain.thenault@logilab.fr
parents:
1564
diff
changeset
|
248 |
return widget |
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
249 |
return self.widget |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
250 |
|
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
251 |
def get_format_field(self, form): |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
252 |
if self.format_field: |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
253 |
return self.format_field |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
254 |
# we have to cache generated field since it's use as key in the |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
255 |
# context dictionnary |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
256 |
req = form.req |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
257 |
try: |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
258 |
return req.data[self] |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
259 |
except KeyError: |
1858
69e41c88e195
connect format field to form_field_format for its initial value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1836
diff
changeset
|
260 |
fkwargs = {} |
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
261 |
if self.use_fckeditor(form): |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
262 |
# if fckeditor is used and format field isn't explicitly |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
263 |
# deactivated, we want an hidden field for the format |
1858
69e41c88e195
connect format field to form_field_format for its initial value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1836
diff
changeset
|
264 |
fkwargs['widget'] = HiddenInput() |
69e41c88e195
connect format field to form_field_format for its initial value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1836
diff
changeset
|
265 |
fkwargs['initial'] = 'text/html' |
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
266 |
else: |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
267 |
# else we want a format selector |
1858
69e41c88e195
connect format field to form_field_format for its initial value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1836
diff
changeset
|
268 |
fkwargs['widget'] = Select() |
1095
6917ebe281e9
test and fix guess_field, some pylint fixes
sylvain.thenault@logilab.fr
parents:
1081
diff
changeset
|
269 |
fcstr = FormatConstraint() |
1858
69e41c88e195
connect format field to form_field_format for its initial value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1836
diff
changeset
|
270 |
fkwargs['choices'] = fcstr.vocabulary(req=req) |
69e41c88e195
connect format field to form_field_format for its initial value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1836
diff
changeset
|
271 |
fkwargs['internationalizable'] = True |
69e41c88e195
connect format field to form_field_format for its initial value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1836
diff
changeset
|
272 |
fkwargs['initial'] = lambda f: f.form_field_format(self) |
3385
68953ecddd2b
[forms] work-in-progress: let guess_field and RichTextField.get_format_field handle eidparam of meta fields
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3384
diff
changeset
|
273 |
fkwargs['eidparam'] = self.eidparam |
1858
69e41c88e195
connect format field to form_field_format for its initial value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1836
diff
changeset
|
274 |
field = StringField(name=self.name + '_format', **fkwargs) |
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
275 |
req.data[self] = field |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
276 |
return field |
1437 | 277 |
|
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
278 |
def actual_fields(self, form): |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
279 |
yield self |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
280 |
format_field = self.get_format_field(form) |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
281 |
if format_field: |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
282 |
yield format_field |
1437 | 283 |
|
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
284 |
def use_fckeditor(self, form): |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
285 |
"""return True if fckeditor should be used to edit entity's attribute named |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
286 |
`attr`, according to user preferences |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
287 |
""" |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
288 |
if form.req.use_fckeditor(): |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
289 |
return form.form_field_format(self) == 'text/html' |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
290 |
return False |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
291 |
|
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
292 |
def render(self, form, renderer): |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
293 |
format_field = self.get_format_field(form) |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
294 |
if format_field: |
1793
fdac26e003e7
fix vertical alignment pb. with descr. format list and textarea inputs
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1758
diff
changeset
|
295 |
# XXX we want both fields to remain vertically aligned |
fdac26e003e7
fix vertical alignment pb. with descr. format list and textarea inputs
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1758
diff
changeset
|
296 |
format_field.widget.attrs['style'] = 'display: block' |
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
297 |
result = format_field.render(form, renderer) |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
298 |
else: |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
299 |
result = u'' |
2522
562f5dcf2345
widget.render now takes the renderer as third argument (keeping minimal bw compat)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2520
diff
changeset
|
300 |
return result + self.get_widget(form).render(form, self, renderer) |
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
301 |
|
1417 | 302 |
|
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
303 |
class FileField(StringField): |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
304 |
widget = FileInput |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
305 |
needs_multipart = True |
1437 | 306 |
|
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
307 |
def __init__(self, format_field=None, encoding_field=None, **kwargs): |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
308 |
super(FileField, self).__init__(**kwargs) |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
309 |
self.format_field = format_field |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
310 |
self.encoding_field = encoding_field |
1437 | 311 |
|
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
312 |
def actual_fields(self, form): |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
313 |
yield self |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
314 |
if self.format_field: |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
315 |
yield self.format_field |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
316 |
if self.encoding_field: |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
317 |
yield self.encoding_field |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
318 |
|
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
319 |
def render(self, form, renderer): |
2522
562f5dcf2345
widget.render now takes the renderer as third argument (keeping minimal bw compat)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2520
diff
changeset
|
320 |
wdgs = [self.get_widget(form).render(form, self, renderer)] |
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
321 |
if self.format_field or self.encoding_field: |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
322 |
divid = '%s-advanced' % form.context[self]['name'] |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
323 |
wdgs.append(u'<a href="%s" title="%s"><img src="%s" alt="%s"/></a>' % |
2312
af4d8f75c5db
use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2244
diff
changeset
|
324 |
(xml_escape(uilib.toggle_action(divid)), |
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
325 |
form.req._('show advanced fields'), |
2312
af4d8f75c5db
use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2244
diff
changeset
|
326 |
xml_escape(form.req.build_url('data/puce_down.png')), |
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
327 |
form.req._('show advanced fields'))) |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
328 |
wdgs.append(u'<div id="%s" class="hidden">' % divid) |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
329 |
if self.format_field: |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
330 |
wdgs.append(self.render_subfield(form, self.format_field, renderer)) |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
331 |
if self.encoding_field: |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
332 |
wdgs.append(self.render_subfield(form, self.encoding_field, renderer)) |
1437 | 333 |
wdgs.append(u'</div>') |
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
334 |
if not self.required and form.context[self]['value']: |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
335 |
# trick to be able to delete an uploaded file |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
336 |
wdgs.append(u'<br/>') |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
337 |
wdgs.append(tags.input(name=u'%s__detach' % form.context[self]['name'], |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
338 |
type=u'checkbox')) |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
339 |
wdgs.append(form.req._('detach attached file')) |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
340 |
return u'\n'.join(wdgs) |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
341 |
|
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
342 |
def render_subfield(self, form, field, renderer): |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
343 |
return (renderer.render_label(form, field) |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
344 |
+ field.render(form, renderer) |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
345 |
+ renderer.render_help(form, field) |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
346 |
+ u'<br/>') |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
347 |
|
1437 | 348 |
|
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
349 |
class EditableFileField(FileField): |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
350 |
editable_formats = ('text/plain', 'text/html', 'text/rest') |
1437 | 351 |
|
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
352 |
def render(self, form, renderer): |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
353 |
wdgs = [super(EditableFileField, self).render(form, renderer)] |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
354 |
if form.form_field_format(self) in self.editable_formats: |
1392
d6279efff7b3
refactor the way field value/display value are handled to avoid getting a 'display' value when expected a 'typed' value
sylvain.thenault@logilab.fr
parents:
1307
diff
changeset
|
355 |
data = form.form_field_value(self, load_bytes=True) |
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
356 |
if data: |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
357 |
encoding = form.form_field_encoding(self) |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
358 |
try: |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
359 |
form.context[self]['value'] = unicode(data.getvalue(), encoding) |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
360 |
except UnicodeError: |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
361 |
pass |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
362 |
else: |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
363 |
if not self.required: |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
364 |
msg = form.req._( |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
365 |
'You can either submit a new file using the browse button above' |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
366 |
', or choose to remove already uploaded file by checking the ' |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
367 |
'"detach attached file" check-box, or edit file content online ' |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
368 |
'with the widget below.') |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
369 |
else: |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
370 |
msg = form.req._( |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
371 |
'You can either submit a new file using the browse button above' |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
372 |
', or edit file content online with the widget below.') |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
373 |
wdgs.append(u'<p><b>%s</b></p>' % msg) |
2522
562f5dcf2345
widget.render now takes the renderer as third argument (keeping minimal bw compat)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2520
diff
changeset
|
374 |
wdgs.append(TextArea(setdomid=False).render(form, self, renderer)) |
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
375 |
# XXX restore form context? |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
376 |
return '\n'.join(wdgs) |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
377 |
|
1437 | 378 |
|
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
379 |
class IntField(Field): |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
380 |
def __init__(self, min=None, max=None, **kwargs): |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
381 |
super(IntField, self).__init__(**kwargs) |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
382 |
self.min = min |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
383 |
self.max = max |
1758 | 384 |
if isinstance(self.widget, TextInput): |
385 |
self.widget.attrs.setdefault('size', 5) |
|
386 |
self.widget.attrs.setdefault('maxlength', 15) |
|
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
387 |
|
1986
96c0e56cb0cf
move widget selection's logic on fields
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
388 |
|
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
389 |
class BooleanField(Field): |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
390 |
widget = Radio |
1437 | 391 |
|
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
392 |
def vocabulary(self, form): |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
393 |
if self.choices: |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
394 |
return self.choices |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
395 |
return [(form.req._('yes'), '1'), (form.req._('no'), '')] |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
396 |
|
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
397 |
|
1437 | 398 |
class FloatField(IntField): |
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
399 |
def format_single_value(self, req, value): |
1095
6917ebe281e9
test and fix guess_field, some pylint fixes
sylvain.thenault@logilab.fr
parents:
1081
diff
changeset
|
400 |
formatstr = req.property_value('ui.float-format') |
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
401 |
if value is None: |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
402 |
return u'' |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
403 |
return formatstr % float(value) |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
404 |
|
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
405 |
def render_example(self, req): |
2086
be76ce00a05e
fix TimeField format_prop, use format_single_value directly in render_example
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1989
diff
changeset
|
406 |
return self.format_single_value(req, 1.234) |
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
407 |
|
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
408 |
|
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
409 |
class DateField(StringField): |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
410 |
format_prop = 'ui.date-format' |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
411 |
widget = DateTimePicker |
1437 | 412 |
|
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
413 |
def format_single_value(self, req, value): |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
414 |
return value and ustrftime(value, req.property_value(self.format_prop)) or u'' |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
415 |
|
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
416 |
def render_example(self, req): |
2086
be76ce00a05e
fix TimeField format_prop, use format_single_value directly in render_example
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1989
diff
changeset
|
417 |
return self.format_single_value(req, datetime.now()) |
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
418 |
|
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
419 |
|
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
420 |
class DateTimeField(DateField): |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
421 |
format_prop = 'ui.datetime-format' |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
422 |
|
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
423 |
|
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
424 |
class TimeField(DateField): |
2086
be76ce00a05e
fix TimeField format_prop, use format_single_value directly in render_example
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1989
diff
changeset
|
425 |
format_prop = 'ui.time-format' |
1758 | 426 |
widget = TextInput |
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
427 |
|
1986
96c0e56cb0cf
move widget selection's logic on fields
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
428 |
|
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
429 |
class RelationField(Field): |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
430 |
def __init__(self, **kwargs): |
1758 | 431 |
kwargs.setdefault('sort', False) |
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
432 |
super(RelationField, self).__init__(**kwargs) |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
433 |
|
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
434 |
@staticmethod |
1095
6917ebe281e9
test and fix guess_field, some pylint fixes
sylvain.thenault@logilab.fr
parents:
1081
diff
changeset
|
435 |
def fromcardinality(card, **kwargs): |
1738
2cfd50c8a415
should not override potential explicit widget
sylvain.thenault@logilab.fr
parents:
1709
diff
changeset
|
436 |
kwargs.setdefault('widget', Select(multiple=card in '*+')) |
2cfd50c8a415
should not override potential explicit widget
sylvain.thenault@logilab.fr
parents:
1709
diff
changeset
|
437 |
return RelationField(**kwargs) |
1437 | 438 |
|
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
439 |
def vocabulary(self, form): |
1147 | 440 |
entity = form.edited_entity |
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
441 |
req = entity.req |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
442 |
# first see if its specified by __linkto form parameters |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
443 |
linkedto = entity.linked_to(self.name, self.role) |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
444 |
if linkedto: |
2680
66472d85d548
[R] use req.entity_from_eid
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2678
diff
changeset
|
445 |
entities = (req.entity_from_eid(eid) for eid in linkedto) |
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
446 |
return [(entity.view('combobox'), entity.eid) for entity in entities] |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
447 |
# it isn't, check if the entity provides a method to get correct values |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
448 |
res = [] |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
449 |
if not self.required: |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
450 |
res.append(('', INTERNAL_FIELD_VALUE)) |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
451 |
# vocabulary doesn't include current values, add them |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
452 |
if entity.has_eid(): |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
453 |
rset = entity.related(self.name, self.role) |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
454 |
relatedvocab = [(e.view('combobox'), e.eid) for e in rset.entities()] |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
455 |
else: |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
456 |
relatedvocab = [] |
1562
e6d2c07c0c58
[forms/widgets] fix relation field not sorting its vocabulary, revert hack on Select widget
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1505
diff
changeset
|
457 |
vocab = res + form.form_field_vocabulary(self) + relatedvocab |
e6d2c07c0c58
[forms/widgets] fix relation field not sorting its vocabulary, revert hack on Select widget
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1505
diff
changeset
|
458 |
if self.sort: |
e6d2c07c0c58
[forms/widgets] fix relation field not sorting its vocabulary, revert hack on Select widget
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1505
diff
changeset
|
459 |
vocab = sorted(vocab) |
e6d2c07c0c58
[forms/widgets] fix relation field not sorting its vocabulary, revert hack on Select widget
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1505
diff
changeset
|
460 |
return vocab |
1437 | 461 |
|
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
462 |
def format_single_value(self, req, value): |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
463 |
return value |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
464 |
|
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
465 |
|
2523
1d245fbbeb90
some new field/widgets classes: CompoundField, IntervalWidget, HorizontalLayoutWidget
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2522
diff
changeset
|
466 |
class CompoundField(Field): |
1d245fbbeb90
some new field/widgets classes: CompoundField, IntervalWidget, HorizontalLayoutWidget
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2522
diff
changeset
|
467 |
def __init__(self, fields, *args, **kwargs): |
1d245fbbeb90
some new field/widgets classes: CompoundField, IntervalWidget, HorizontalLayoutWidget
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2522
diff
changeset
|
468 |
super(CompoundField, self).__init__(*args, **kwargs) |
1d245fbbeb90
some new field/widgets classes: CompoundField, IntervalWidget, HorizontalLayoutWidget
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2522
diff
changeset
|
469 |
self.fields = fields |
1d245fbbeb90
some new field/widgets classes: CompoundField, IntervalWidget, HorizontalLayoutWidget
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2522
diff
changeset
|
470 |
|
2549
3d8c62e5e2d4
[R forms] use a subfields(form) method to get a chance to adapt to context
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2523
diff
changeset
|
471 |
def subfields(self, form): |
3d8c62e5e2d4
[R forms] use a subfields(form) method to get a chance to adapt to context
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2523
diff
changeset
|
472 |
return self.fields |
2568 | 473 |
|
2523
1d245fbbeb90
some new field/widgets classes: CompoundField, IntervalWidget, HorizontalLayoutWidget
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2522
diff
changeset
|
474 |
def actual_fields(self, form): |
1d245fbbeb90
some new field/widgets classes: CompoundField, IntervalWidget, HorizontalLayoutWidget
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2522
diff
changeset
|
475 |
return [self] + list(self.fields) |
1d245fbbeb90
some new field/widgets classes: CompoundField, IntervalWidget, HorizontalLayoutWidget
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2522
diff
changeset
|
476 |
|
1d245fbbeb90
some new field/widgets classes: CompoundField, IntervalWidget, HorizontalLayoutWidget
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2522
diff
changeset
|
477 |
|
1453
a9841184be7c
guess_field now takes an entity schema as first argument, not an entity class
sylvain.thenault@logilab.fr
parents:
1437
diff
changeset
|
478 |
def guess_field(eschema, rschema, role='subject', skip_meta_attr=True, **kwargs): |
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
479 |
"""return the most adapated widget to edit the relation |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
480 |
'subjschema rschema objschema' according to information found in the schema |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
481 |
""" |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
482 |
fieldclass = None |
2678
0308dc159b96
use yams api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2568
diff
changeset
|
483 |
card = eschema.cardinality(rschema, role) |
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
484 |
if role == 'subject': |
1095
6917ebe281e9
test and fix guess_field, some pylint fixes
sylvain.thenault@logilab.fr
parents:
1081
diff
changeset
|
485 |
targetschema = rschema.objects(eschema)[0] |
1268
5db94912650b
get field's help in guess_field
sylvain.thenault@logilab.fr
parents:
1265
diff
changeset
|
486 |
help = rschema.rproperty(eschema, targetschema, 'description') |
1265
e5cdd5c0dce3
handle sort/internationalizable on Field base class + fix guess_field to deal with internationalizable and default values
sylvain.thenault@logilab.fr
parents:
1181
diff
changeset
|
487 |
if rschema.is_final(): |
e5cdd5c0dce3
handle sort/internationalizable on Field base class + fix guess_field to deal with internationalizable and default values
sylvain.thenault@logilab.fr
parents:
1181
diff
changeset
|
488 |
if rschema.rproperty(eschema, targetschema, 'internationalizable'): |
2095
897732d3ee5a
avoid overriding specified values when guessing field
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2091
diff
changeset
|
489 |
kwargs.setdefault('internationalizable', True) |
1709
f7110f533d14
use yams.schema method for initial value
sylvain.thenault@logilab.fr
parents:
1706
diff
changeset
|
490 |
def get_default(form, es=eschema, rs=rschema): |
f7110f533d14
use yams.schema method for initial value
sylvain.thenault@logilab.fr
parents:
1706
diff
changeset
|
491 |
return es.default(rs) |
2095
897732d3ee5a
avoid overriding specified values when guessing field
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2091
diff
changeset
|
492 |
kwargs.setdefault('initial', get_default) |
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
493 |
else: |
1095
6917ebe281e9
test and fix guess_field, some pylint fixes
sylvain.thenault@logilab.fr
parents:
1081
diff
changeset
|
494 |
targetschema = rschema.subjects(eschema)[0] |
1268
5db94912650b
get field's help in guess_field
sylvain.thenault@logilab.fr
parents:
1265
diff
changeset
|
495 |
help = rschema.rproperty(targetschema, eschema, 'description') |
1095
6917ebe281e9
test and fix guess_field, some pylint fixes
sylvain.thenault@logilab.fr
parents:
1081
diff
changeset
|
496 |
kwargs['required'] = card in '1+' |
6917ebe281e9
test and fix guess_field, some pylint fixes
sylvain.thenault@logilab.fr
parents:
1081
diff
changeset
|
497 |
kwargs['name'] = rschema.type |
3385
68953ecddd2b
[forms] work-in-progress: let guess_field and RichTextField.get_format_field handle eidparam of meta fields
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3384
diff
changeset
|
498 |
kwargs['eidparam'] = True |
2095
897732d3ee5a
avoid overriding specified values when guessing field
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2091
diff
changeset
|
499 |
kwargs.setdefault('help', help) |
1095
6917ebe281e9
test and fix guess_field, some pylint fixes
sylvain.thenault@logilab.fr
parents:
1081
diff
changeset
|
500 |
if rschema.is_final(): |
1104
58f27c3c0167
more guess_field tests and fixes
sylvain.thenault@logilab.fr
parents:
1101
diff
changeset
|
501 |
if skip_meta_attr and rschema in eschema.meta_attributes(): |
1095
6917ebe281e9
test and fix guess_field, some pylint fixes
sylvain.thenault@logilab.fr
parents:
1081
diff
changeset
|
502 |
return None |
6917ebe281e9
test and fix guess_field, some pylint fixes
sylvain.thenault@logilab.fr
parents:
1081
diff
changeset
|
503 |
fieldclass = FIELDS[targetschema] |
6917ebe281e9
test and fix guess_field, some pylint fixes
sylvain.thenault@logilab.fr
parents:
1081
diff
changeset
|
504 |
if fieldclass is StringField: |
1104
58f27c3c0167
more guess_field tests and fixes
sylvain.thenault@logilab.fr
parents:
1101
diff
changeset
|
505 |
if targetschema == 'Password': |
58f27c3c0167
more guess_field tests and fixes
sylvain.thenault@logilab.fr
parents:
1101
diff
changeset
|
506 |
# special case for Password field: specific PasswordInput widget |
1147 | 507 |
kwargs.setdefault('widget', PasswordInput()) |
508 |
return StringField(**kwargs) |
|
1104
58f27c3c0167
more guess_field tests and fixes
sylvain.thenault@logilab.fr
parents:
1101
diff
changeset
|
509 |
if eschema.has_metadata(rschema, 'format'): |
58f27c3c0167
more guess_field tests and fixes
sylvain.thenault@logilab.fr
parents:
1101
diff
changeset
|
510 |
# use RichTextField instead of StringField if the attribute has |
58f27c3c0167
more guess_field tests and fixes
sylvain.thenault@logilab.fr
parents:
1101
diff
changeset
|
511 |
# a "format" metadata. But getting information from constraints |
58f27c3c0167
more guess_field tests and fixes
sylvain.thenault@logilab.fr
parents:
1101
diff
changeset
|
512 |
# may be useful anyway... |
58f27c3c0167
more guess_field tests and fixes
sylvain.thenault@logilab.fr
parents:
1101
diff
changeset
|
513 |
constraints = rschema.rproperty(eschema, targetschema, 'constraints') |
58f27c3c0167
more guess_field tests and fixes
sylvain.thenault@logilab.fr
parents:
1101
diff
changeset
|
514 |
for cstr in constraints: |
58f27c3c0167
more guess_field tests and fixes
sylvain.thenault@logilab.fr
parents:
1101
diff
changeset
|
515 |
if isinstance(cstr, StaticVocabularyConstraint): |
58f27c3c0167
more guess_field tests and fixes
sylvain.thenault@logilab.fr
parents:
1101
diff
changeset
|
516 |
raise Exception('rich text field with static vocabulary') |
58f27c3c0167
more guess_field tests and fixes
sylvain.thenault@logilab.fr
parents:
1101
diff
changeset
|
517 |
return RichTextField(**kwargs) |
1095
6917ebe281e9
test and fix guess_field, some pylint fixes
sylvain.thenault@logilab.fr
parents:
1081
diff
changeset
|
518 |
constraints = rschema.rproperty(eschema, targetschema, 'constraints') |
1574 | 519 |
# init StringField parameters according to constraints |
520 |
for cstr in constraints: |
|
521 |
if isinstance(cstr, StaticVocabularyConstraint): |
|
1577 | 522 |
kwargs.setdefault('choices', cstr.vocabulary) |
1986
96c0e56cb0cf
move widget selection's logic on fields
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
523 |
break |
1836
d3f0c6e868ef
fix StringField widget selection with SizeConstraint and StaticVocabularyConstraint constraints
Florent <florent@secondweb.fr>
parents:
1803
diff
changeset
|
524 |
for cstr in constraints: |
1574 | 525 |
if isinstance(cstr, SizeConstraint) and cstr.max is not None: |
526 |
kwargs['max_length'] = cstr.max |
|
527 |
return StringField(**kwargs) |
|
1104
58f27c3c0167
more guess_field tests and fixes
sylvain.thenault@logilab.fr
parents:
1101
diff
changeset
|
528 |
if fieldclass is FileField: |
58f27c3c0167
more guess_field tests and fixes
sylvain.thenault@logilab.fr
parents:
1101
diff
changeset
|
529 |
for metadata in ('format', 'encoding'): |
58f27c3c0167
more guess_field tests and fixes
sylvain.thenault@logilab.fr
parents:
1101
diff
changeset
|
530 |
metaschema = eschema.has_metadata(rschema, metadata) |
58f27c3c0167
more guess_field tests and fixes
sylvain.thenault@logilab.fr
parents:
1101
diff
changeset
|
531 |
if metaschema is not None: |
1453
a9841184be7c
guess_field now takes an entity schema as first argument, not an entity class
sylvain.thenault@logilab.fr
parents:
1437
diff
changeset
|
532 |
kwargs['%s_field' % metadata] = guess_field(eschema, metaschema, |
1104
58f27c3c0167
more guess_field tests and fixes
sylvain.thenault@logilab.fr
parents:
1101
diff
changeset
|
533 |
skip_meta_attr=False) |
1095
6917ebe281e9
test and fix guess_field, some pylint fixes
sylvain.thenault@logilab.fr
parents:
1081
diff
changeset
|
534 |
return fieldclass(**kwargs) |
6917ebe281e9
test and fix guess_field, some pylint fixes
sylvain.thenault@logilab.fr
parents:
1081
diff
changeset
|
535 |
kwargs['role'] = role |
6917ebe281e9
test and fix guess_field, some pylint fixes
sylvain.thenault@logilab.fr
parents:
1081
diff
changeset
|
536 |
return RelationField.fromcardinality(card, **kwargs) |
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
537 |
|
1986
96c0e56cb0cf
move widget selection's logic on fields
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
538 |
|
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
539 |
FIELDS = { |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
540 |
'Boolean': BooleanField, |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
541 |
'Bytes': FileField, |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
542 |
'Date': DateField, |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
543 |
'Datetime': DateTimeField, |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
544 |
'Int': IntField, |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
545 |
'Float': FloatField, |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
546 |
'Decimal': StringField, |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
547 |
'Password': StringField, |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
548 |
'String' : StringField, |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
549 |
'Time': TimeField, |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
550 |
} |