author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Tue, 22 Dec 2009 19:23:59 +0100 | |
changeset 4186 | ca7e526b07b6 |
parent 4181 | c79135c217df |
child 4225 | c49bb6e3d343 |
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 |
4158
0e97cf2cf55b
missing imports
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4156
diff
changeset
|
14 |
from logilab.common.decorators import cached |
0e97cf2cf55b
missing imports
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4156
diff
changeset
|
15 |
|
4160 | 16 |
from yams.schema import KNOWN_METAATTRIBUTES |
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
|
17 |
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
|
18 |
FormatConstraint) |
1095
6917ebe281e9
test and fix guess_field, some pylint fixes
sylvain.thenault@logilab.fr
parents:
1081
diff
changeset
|
19 |
|
2678
0308dc159b96
use yams api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2568
diff
changeset
|
20 |
from cubicweb.utils import ustrftime |
4023
eae23c40627a
drop common subpackage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4014
diff
changeset
|
21 |
from cubicweb import tags, uilib |
4158
0e97cf2cf55b
missing imports
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4156
diff
changeset
|
22 |
from cubicweb.web import INTERNAL_FIELD_VALUE, eid_param |
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
|
23 |
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
|
24 |
HiddenInput, TextInput, FileInput, PasswordInput, TextArea, FCKEditor, |
1437 | 25 |
Radio, Select, DateTimePicker) |
1095
6917ebe281e9
test and fix guess_field, some pylint fixes
sylvain.thenault@logilab.fr
parents:
1081
diff
changeset
|
26 |
|
1986
96c0e56cb0cf
move widget selection's logic on fields
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
27 |
|
3334
8d831c02da9a
fix sort of fields vocabulary: should consider option groups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3308
diff
changeset
|
28 |
def vocab_sort(vocab): |
8d831c02da9a
fix sort of fields vocabulary: should consider option groups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3308
diff
changeset
|
29 |
"""sort vocabulary, considering option groups""" |
8d831c02da9a
fix sort of fields vocabulary: should consider option groups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3308
diff
changeset
|
30 |
result = [] |
8d831c02da9a
fix sort of fields vocabulary: should consider option groups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3308
diff
changeset
|
31 |
partresult = [] |
8d831c02da9a
fix sort of fields vocabulary: should consider option groups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3308
diff
changeset
|
32 |
for label, value in vocab: |
8d831c02da9a
fix sort of fields vocabulary: should consider option groups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3308
diff
changeset
|
33 |
if value is None: # opt group start |
8d831c02da9a
fix sort of fields vocabulary: should consider option groups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3308
diff
changeset
|
34 |
if partresult: |
8d831c02da9a
fix sort of fields vocabulary: should consider option groups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3308
diff
changeset
|
35 |
result += sorted(partresult) |
8d831c02da9a
fix sort of fields vocabulary: should consider option groups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3308
diff
changeset
|
36 |
partresult = [] |
8d831c02da9a
fix sort of fields vocabulary: should consider option groups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3308
diff
changeset
|
37 |
result.append( (label, value) ) |
8d831c02da9a
fix sort of fields vocabulary: should consider option groups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3308
diff
changeset
|
38 |
else: |
8d831c02da9a
fix sort of fields vocabulary: should consider option groups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3308
diff
changeset
|
39 |
partresult.append( (label, value) ) |
8d831c02da9a
fix sort of fields vocabulary: should consider option groups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3308
diff
changeset
|
40 |
result += sorted(partresult) |
8d831c02da9a
fix sort of fields vocabulary: should consider option groups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3308
diff
changeset
|
41 |
return result |
8d831c02da9a
fix sort of fields vocabulary: should consider option groups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3308
diff
changeset
|
42 |
|
4159
6b2b20c73d59
refactor form field value handling, to get a nicer api and an easier algorithm to get field's value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4158
diff
changeset
|
43 |
_MARKER = object() |
3334
8d831c02da9a
fix sort of fields vocabulary: should consider option groups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3308
diff
changeset
|
44 |
|
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
45 |
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
|
46 |
"""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
|
47 |
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
|
48 |
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
|
49 |
|
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 |
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
|
51 |
---------- |
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 |
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
|
53 |
overriden by value given to field's constructor. |
1437 | 54 |
|
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
|
55 |
: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
|
56 |
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
|
57 |
: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
|
58 |
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
|
59 |
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
|
60 |
: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
|
61 |
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
|
62 |
: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
|
63 |
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
|
64 |
: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
|
65 |
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
|
66 |
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
|
67 |
: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
|
68 |
bool flag telling if the field is required or not. |
4159
6b2b20c73d59
refactor form field value handling, to get a nicer api and an easier algorithm to get field's value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4158
diff
changeset
|
69 |
:value: |
6b2b20c73d59
refactor form field value handling, to get a nicer api and an easier algorithm to get field's value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4158
diff
changeset
|
70 |
field's value, used when no value specified by other means. XXX explain |
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
|
71 |
: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
|
72 |
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
|
73 |
(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
|
74 |
: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
|
75 |
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
|
76 |
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
|
77 |
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
|
78 |
: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
|
79 |
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
|
80 |
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
|
81 |
: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
|
82 |
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
|
83 |
: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
|
84 |
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
|
85 |
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
|
86 |
: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
|
87 |
optional fieldset to which this field belongs to |
3874
7d0d4a6be046
[formfields] allow fields ordering with autoform_field_kwargs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3873
diff
changeset
|
88 |
:order: |
7d0d4a6be046
[formfields] allow fields ordering with autoform_field_kwargs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3873
diff
changeset
|
89 |
key used by automatic forms to sort fields |
1437 | 90 |
|
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
91 |
""" |
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
|
92 |
# 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
|
93 |
# instance |
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
94 |
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
|
95 |
# does this field requires a multipart form |
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
96 |
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
|
97 |
# 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
|
98 |
__creation_rank = 0 |
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
99 |
|
4159
6b2b20c73d59
refactor form field value handling, to get a nicer api and an easier algorithm to get field's value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4158
diff
changeset
|
100 |
eidparam = False |
6b2b20c73d59
refactor form field value handling, to get a nicer api and an easier algorithm to get field's value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4158
diff
changeset
|
101 |
role = None |
6b2b20c73d59
refactor form field value handling, to get a nicer api and an easier algorithm to get field's value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4158
diff
changeset
|
102 |
id = None |
6b2b20c73d59
refactor form field value handling, to get a nicer api and an easier algorithm to get field's value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4158
diff
changeset
|
103 |
help = None |
6b2b20c73d59
refactor form field value handling, to get a nicer api and an easier algorithm to get field's value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4158
diff
changeset
|
104 |
required = False |
6b2b20c73d59
refactor form field value handling, to get a nicer api and an easier algorithm to get field's value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4158
diff
changeset
|
105 |
choices = None |
6b2b20c73d59
refactor form field value handling, to get a nicer api and an easier algorithm to get field's value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4158
diff
changeset
|
106 |
sort = True |
6b2b20c73d59
refactor form field value handling, to get a nicer api and an easier algorithm to get field's value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4158
diff
changeset
|
107 |
internationalizable = False |
6b2b20c73d59
refactor form field value handling, to get a nicer api and an easier algorithm to get field's value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4158
diff
changeset
|
108 |
fieldset = None |
6b2b20c73d59
refactor form field value handling, to get a nicer api and an easier algorithm to get field's value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4158
diff
changeset
|
109 |
order = None |
6b2b20c73d59
refactor form field value handling, to get a nicer api and an easier algorithm to get field's value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4158
diff
changeset
|
110 |
value = _MARKER |
6b2b20c73d59
refactor form field value handling, to get a nicer api and an easier algorithm to get field's value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4158
diff
changeset
|
111 |
|
6b2b20c73d59
refactor form field value handling, to get a nicer api and an easier algorithm to get field's value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4158
diff
changeset
|
112 |
def __init__(self, name=None, label=None, widget=None, **kwargs): |
6b2b20c73d59
refactor form field value handling, to get a nicer api and an easier algorithm to get field's value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4158
diff
changeset
|
113 |
for key, val in kwargs.items(): |
6b2b20c73d59
refactor form field value handling, to get a nicer api and an easier algorithm to get field's value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4158
diff
changeset
|
114 |
if key == 'initial': |
6b2b20c73d59
refactor form field value handling, to get a nicer api and an easier algorithm to get field's value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4158
diff
changeset
|
115 |
warn('[3.6] use value instead of initial', DeprecationWarning, |
6b2b20c73d59
refactor form field value handling, to get a nicer api and an easier algorithm to get field's value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4158
diff
changeset
|
116 |
stacklevel=3) |
6b2b20c73d59
refactor form field value handling, to get a nicer api and an easier algorithm to get field's value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4158
diff
changeset
|
117 |
key = 'value' |
6b2b20c73d59
refactor form field value handling, to get a nicer api and an easier algorithm to get field's value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4158
diff
changeset
|
118 |
assert hasattr(self.__class__, key) and not key[0] == '_', key |
6b2b20c73d59
refactor form field value handling, to get a nicer api and an easier algorithm to get field's value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4158
diff
changeset
|
119 |
setattr(self, key, val) |
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
|
120 |
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
|
121 |
self.label = label or name |
4159
6b2b20c73d59
refactor form field value handling, to get a nicer api and an easier algorithm to get field's value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4158
diff
changeset
|
122 |
# has to be done after other attributes initialization |
1986
96c0e56cb0cf
move widget selection's logic on fields
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
123 |
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
|
124 |
# 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
|
125 |
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
|
126 |
Field.__creation_rank += 1 |
1437 | 127 |
|
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
128 |
def __unicode__(self): |
4159
6b2b20c73d59
refactor form field value handling, to get a nicer api and an easier algorithm to get field's value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4158
diff
changeset
|
129 |
return u'<%s name=%r eidparam=%s role=%r id=%r value=%r visible=%r @%x>' % ( |
6b2b20c73d59
refactor form field value handling, to get a nicer api and an easier algorithm to get field's value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4158
diff
changeset
|
130 |
self.__class__.__name__, self.name, self.eidparam, self.role, |
6b2b20c73d59
refactor form field value handling, to get a nicer api and an easier algorithm to get field's value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4158
diff
changeset
|
131 |
self.id, self.value, self.is_visible(), id(self)) |
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
132 |
|
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
133 |
def __repr__(self): |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
134 |
return self.__unicode__().encode('utf-8') |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
135 |
|
1986
96c0e56cb0cf
move widget selection's logic on fields
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
136 |
def init_widget(self, widget): |
96c0e56cb0cf
move widget selection's logic on fields
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
137 |
if widget is not None: |
96c0e56cb0cf
move widget selection's logic on fields
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
138 |
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
|
139 |
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
|
140 |
self.widget = Select() |
1986
96c0e56cb0cf
move widget selection's logic on fields
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
141 |
if isinstance(self.widget, type): |
96c0e56cb0cf
move widget selection's logic on fields
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
142 |
self.widget = self.widget() |
96c0e56cb0cf
move widget selection's logic on fields
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
143 |
|
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
144 |
def set_name(self, name): |
4159
6b2b20c73d59
refactor form field value handling, to get a nicer api and an easier algorithm to get field's value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4158
diff
changeset
|
145 |
"""automatically set .label when name is set""" |
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
146 |
assert name |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
147 |
self.name = name |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
148 |
if not self.label: |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
149 |
self.label = name |
1437 | 150 |
|
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
151 |
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
|
152 |
"""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
|
153 |
return not isinstance(self.widget, HiddenInput) |
1437 | 154 |
|
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
155 |
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
|
156 |
"""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
|
157 |
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
|
158 |
""" |
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
159 |
yield self |
1437 | 160 |
|
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
161 |
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
|
162 |
"""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
|
163 |
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
|
164 |
""" |
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
165 |
if isinstance(value, (list, tuple)): |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
166 |
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
|
167 |
return self.format_single_value(req, value) |
1437 | 168 |
|
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
169 |
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
|
170 |
"""return value suitable for display""" |
1306 | 171 |
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
|
172 |
return u'' |
1306 | 173 |
if value is True: |
174 |
return u'1' |
|
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
175 |
return unicode(value) |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
176 |
|
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
177 |
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
|
178 |
"""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
|
179 |
return self.widget |
1437 | 180 |
|
4156
1bbb0ee42c8e
drop form_field_name/form_field_id methods from form object, in favor of field.input_name(form) / field.dom_id(form)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4110
diff
changeset
|
181 |
# cached is necessary else we get some pb on entity creation : entity.eid is |
1bbb0ee42c8e
drop form_field_name/form_field_id methods from form object, in favor of field.input_name(form) / field.dom_id(form)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4110
diff
changeset
|
182 |
# modified from creation mark (eg 'X') to its actual eid (eg 123), and then |
1bbb0ee42c8e
drop form_field_name/form_field_id methods from form object, in favor of field.input_name(form) / field.dom_id(form)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4110
diff
changeset
|
183 |
# `field.input_name()` won't return the right key anymore if not cached |
1bbb0ee42c8e
drop form_field_name/form_field_id methods from form object, in favor of field.input_name(form) / field.dom_id(form)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4110
diff
changeset
|
184 |
# (first call to input_name done *before* eventual eid affectation). |
1bbb0ee42c8e
drop form_field_name/form_field_id methods from form object, in favor of field.input_name(form) / field.dom_id(form)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4110
diff
changeset
|
185 |
@cached |
1bbb0ee42c8e
drop form_field_name/form_field_id methods from form object, in favor of field.input_name(form) / field.dom_id(form)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4110
diff
changeset
|
186 |
def input_name(self, form, suffix=None): |
1bbb0ee42c8e
drop form_field_name/form_field_id methods from form object, in favor of field.input_name(form) / field.dom_id(form)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4110
diff
changeset
|
187 |
"""return 'qualified name' for this field""" |
1bbb0ee42c8e
drop form_field_name/form_field_id methods from form object, in favor of field.input_name(form) / field.dom_id(form)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4110
diff
changeset
|
188 |
if self.eidparam: |
1bbb0ee42c8e
drop form_field_name/form_field_id methods from form object, in favor of field.input_name(form) / field.dom_id(form)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4110
diff
changeset
|
189 |
if suffix is None: |
1bbb0ee42c8e
drop form_field_name/form_field_id methods from form object, in favor of field.input_name(form) / field.dom_id(form)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4110
diff
changeset
|
190 |
return eid_param(self.role_name(), form.edited_entity.eid) |
1bbb0ee42c8e
drop form_field_name/form_field_id methods from form object, in favor of field.input_name(form) / field.dom_id(form)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4110
diff
changeset
|
191 |
return eid_param(self.role_name() + suffix, form.edited_entity.eid) |
1bbb0ee42c8e
drop form_field_name/form_field_id methods from form object, in favor of field.input_name(form) / field.dom_id(form)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4110
diff
changeset
|
192 |
if suffix is None: |
1bbb0ee42c8e
drop form_field_name/form_field_id methods from form object, in favor of field.input_name(form) / field.dom_id(form)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4110
diff
changeset
|
193 |
return self.name |
1bbb0ee42c8e
drop form_field_name/form_field_id methods from form object, in favor of field.input_name(form) / field.dom_id(form)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4110
diff
changeset
|
194 |
return self.name + suffix |
1bbb0ee42c8e
drop form_field_name/form_field_id methods from form object, in favor of field.input_name(form) / field.dom_id(form)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4110
diff
changeset
|
195 |
|
1bbb0ee42c8e
drop form_field_name/form_field_id methods from form object, in favor of field.input_name(form) / field.dom_id(form)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4110
diff
changeset
|
196 |
def role_name(self): |
1bbb0ee42c8e
drop form_field_name/form_field_id methods from form object, in favor of field.input_name(form) / field.dom_id(form)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4110
diff
changeset
|
197 |
"""return <field.name>-<field.role> if role is specified, else field.name""" |
1bbb0ee42c8e
drop form_field_name/form_field_id methods from form object, in favor of field.input_name(form) / field.dom_id(form)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4110
diff
changeset
|
198 |
if self.role is not None: |
1bbb0ee42c8e
drop form_field_name/form_field_id methods from form object, in favor of field.input_name(form) / field.dom_id(form)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4110
diff
changeset
|
199 |
return '%s-%s' % (self.name, self.role) |
1bbb0ee42c8e
drop form_field_name/form_field_id methods from form object, in favor of field.input_name(form) / field.dom_id(form)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4110
diff
changeset
|
200 |
return self.name |
1bbb0ee42c8e
drop form_field_name/form_field_id methods from form object, in favor of field.input_name(form) / field.dom_id(form)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4110
diff
changeset
|
201 |
|
1bbb0ee42c8e
drop form_field_name/form_field_id methods from form object, in favor of field.input_name(form) / field.dom_id(form)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4110
diff
changeset
|
202 |
def dom_id(self, form): |
1bbb0ee42c8e
drop form_field_name/form_field_id methods from form object, in favor of field.input_name(form) / field.dom_id(form)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4110
diff
changeset
|
203 |
"""return an html dom identifier for this field""" |
1bbb0ee42c8e
drop form_field_name/form_field_id methods from form object, in favor of field.input_name(form) / field.dom_id(form)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4110
diff
changeset
|
204 |
id = self.id or self.role_name() |
1bbb0ee42c8e
drop form_field_name/form_field_id methods from form object, in favor of field.input_name(form) / field.dom_id(form)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4110
diff
changeset
|
205 |
if self.eidparam: |
1bbb0ee42c8e
drop form_field_name/form_field_id methods from form object, in favor of field.input_name(form) / field.dom_id(form)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4110
diff
changeset
|
206 |
return eid_param(id, form.edited_entity.eid) |
1bbb0ee42c8e
drop form_field_name/form_field_id methods from form object, in favor of field.input_name(form) / field.dom_id(form)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4110
diff
changeset
|
207 |
return id |
1bbb0ee42c8e
drop form_field_name/form_field_id methods from form object, in favor of field.input_name(form) / field.dom_id(form)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4110
diff
changeset
|
208 |
|
4159
6b2b20c73d59
refactor form field value handling, to get a nicer api and an easier algorithm to get field's value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4158
diff
changeset
|
209 |
def display_value(self, form): |
6b2b20c73d59
refactor form field value handling, to get a nicer api and an easier algorithm to get field's value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4158
diff
changeset
|
210 |
"""return field's *string* value to use for display |
6b2b20c73d59
refactor form field value handling, to get a nicer api and an easier algorithm to get field's value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4158
diff
changeset
|
211 |
|
6b2b20c73d59
refactor form field value handling, to get a nicer api and an easier algorithm to get field's value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4158
diff
changeset
|
212 |
looks in |
6b2b20c73d59
refactor form field value handling, to get a nicer api and an easier algorithm to get field's value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4158
diff
changeset
|
213 |
1. previously submitted form values if any (eg on validation error) |
6b2b20c73d59
refactor form field value handling, to get a nicer api and an easier algorithm to get field's value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4158
diff
changeset
|
214 |
2. req.form |
6b2b20c73d59
refactor form field value handling, to get a nicer api and an easier algorithm to get field's value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4158
diff
changeset
|
215 |
3. extra form args given to render_form |
6b2b20c73d59
refactor form field value handling, to get a nicer api and an easier algorithm to get field's value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4158
diff
changeset
|
216 |
4. field's typed value |
6b2b20c73d59
refactor form field value handling, to get a nicer api and an easier algorithm to get field's value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4158
diff
changeset
|
217 |
|
6b2b20c73d59
refactor form field value handling, to get a nicer api and an easier algorithm to get field's value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4158
diff
changeset
|
218 |
values found in 1. and 2. are expected te be already some 'display' |
6b2b20c73d59
refactor form field value handling, to get a nicer api and an easier algorithm to get field's value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4158
diff
changeset
|
219 |
value while those found in 3. and 4. are expected to be correctly typed. |
6b2b20c73d59
refactor form field value handling, to get a nicer api and an easier algorithm to get field's value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4158
diff
changeset
|
220 |
""" |
6b2b20c73d59
refactor form field value handling, to get a nicer api and an easier algorithm to get field's value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4158
diff
changeset
|
221 |
qname = self.input_name(form) |
6b2b20c73d59
refactor form field value handling, to get a nicer api and an easier algorithm to get field's value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4158
diff
changeset
|
222 |
if qname in form.form_previous_values: |
6b2b20c73d59
refactor form field value handling, to get a nicer api and an easier algorithm to get field's value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4158
diff
changeset
|
223 |
return form.form_previous_values[qname] |
6b2b20c73d59
refactor form field value handling, to get a nicer api and an easier algorithm to get field's value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4158
diff
changeset
|
224 |
if qname in form._cw.form: |
6b2b20c73d59
refactor form field value handling, to get a nicer api and an easier algorithm to get field's value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4158
diff
changeset
|
225 |
return form._cw.form[qname] |
6b2b20c73d59
refactor form field value handling, to get a nicer api and an easier algorithm to get field's value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4158
diff
changeset
|
226 |
if self.name != qname and self.name in form._cw.form: |
6b2b20c73d59
refactor form field value handling, to get a nicer api and an easier algorithm to get field's value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4158
diff
changeset
|
227 |
return form._cw.form[self.name] |
6b2b20c73d59
refactor form field value handling, to get a nicer api and an easier algorithm to get field's value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4158
diff
changeset
|
228 |
for key in (self, qname): |
6b2b20c73d59
refactor form field value handling, to get a nicer api and an easier algorithm to get field's value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4158
diff
changeset
|
229 |
try: |
6b2b20c73d59
refactor form field value handling, to get a nicer api and an easier algorithm to get field's value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4158
diff
changeset
|
230 |
value = form.formvalues[key] |
6b2b20c73d59
refactor form field value handling, to get a nicer api and an easier algorithm to get field's value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4158
diff
changeset
|
231 |
break |
6b2b20c73d59
refactor form field value handling, to get a nicer api and an easier algorithm to get field's value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4158
diff
changeset
|
232 |
except: |
6b2b20c73d59
refactor form field value handling, to get a nicer api and an easier algorithm to get field's value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4158
diff
changeset
|
233 |
continue |
6b2b20c73d59
refactor form field value handling, to get a nicer api and an easier algorithm to get field's value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4158
diff
changeset
|
234 |
else: |
6b2b20c73d59
refactor form field value handling, to get a nicer api and an easier algorithm to get field's value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4158
diff
changeset
|
235 |
if self.name != qname and self.name in form.formvalues: |
6b2b20c73d59
refactor form field value handling, to get a nicer api and an easier algorithm to get field's value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4158
diff
changeset
|
236 |
value = form.formvalues[self.name] |
6b2b20c73d59
refactor form field value handling, to get a nicer api and an easier algorithm to get field's value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4158
diff
changeset
|
237 |
else: |
6b2b20c73d59
refactor form field value handling, to get a nicer api and an easier algorithm to get field's value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4158
diff
changeset
|
238 |
value = self.typed_value(form) |
6b2b20c73d59
refactor form field value handling, to get a nicer api and an easier algorithm to get field's value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4158
diff
changeset
|
239 |
if value != INTERNAL_FIELD_VALUE: |
6b2b20c73d59
refactor form field value handling, to get a nicer api and an easier algorithm to get field's value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4158
diff
changeset
|
240 |
value = self.format_value(form._cw, value) |
6b2b20c73d59
refactor form field value handling, to get a nicer api and an easier algorithm to get field's value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4158
diff
changeset
|
241 |
return value |
6b2b20c73d59
refactor form field value handling, to get a nicer api and an easier algorithm to get field's value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4158
diff
changeset
|
242 |
|
6b2b20c73d59
refactor form field value handling, to get a nicer api and an easier algorithm to get field's value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4158
diff
changeset
|
243 |
def typed_value(self, form, load_bytes=False): |
6b2b20c73d59
refactor form field value handling, to get a nicer api and an easier algorithm to get field's value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4158
diff
changeset
|
244 |
if self.value is not _MARKER: |
6b2b20c73d59
refactor form field value handling, to get a nicer api and an easier algorithm to get field's value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4158
diff
changeset
|
245 |
if callable(self.value): |
6b2b20c73d59
refactor form field value handling, to get a nicer api and an easier algorithm to get field's value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4158
diff
changeset
|
246 |
return self.value(form) |
6b2b20c73d59
refactor form field value handling, to get a nicer api and an easier algorithm to get field's value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4158
diff
changeset
|
247 |
return self.value |
6b2b20c73d59
refactor form field value handling, to get a nicer api and an easier algorithm to get field's value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4158
diff
changeset
|
248 |
return self._typed_value(form, load_bytes) |
6b2b20c73d59
refactor form field value handling, to get a nicer api and an easier algorithm to get field's value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4158
diff
changeset
|
249 |
|
6b2b20c73d59
refactor form field value handling, to get a nicer api and an easier algorithm to get field's value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4158
diff
changeset
|
250 |
def _typed_value(self, form, load_bytes=False): |
6b2b20c73d59
refactor form field value handling, to get a nicer api and an easier algorithm to get field's value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4158
diff
changeset
|
251 |
if self.eidparam: |
6b2b20c73d59
refactor form field value handling, to get a nicer api and an easier algorithm to get field's value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4158
diff
changeset
|
252 |
assert form._cw.vreg.schema.rschema(self.name).final |
6b2b20c73d59
refactor form field value handling, to get a nicer api and an easier algorithm to get field's value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4158
diff
changeset
|
253 |
entity = form.edited_entity |
6b2b20c73d59
refactor form field value handling, to get a nicer api and an easier algorithm to get field's value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4158
diff
changeset
|
254 |
if entity.has_eid() or self.name in entity: |
6b2b20c73d59
refactor form field value handling, to get a nicer api and an easier algorithm to get field's value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4158
diff
changeset
|
255 |
return getattr(entity, self.name) |
6b2b20c73d59
refactor form field value handling, to get a nicer api and an easier algorithm to get field's value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4158
diff
changeset
|
256 |
formattr = '%s_%s_default' % (self.role, self.name) |
6b2b20c73d59
refactor form field value handling, to get a nicer api and an easier algorithm to get field's value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4158
diff
changeset
|
257 |
if hasattr(form, formattr): |
6b2b20c73d59
refactor form field value handling, to get a nicer api and an easier algorithm to get field's value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4158
diff
changeset
|
258 |
warn('[3.6] %s.%s deprecated, use field.value' % ( |
6b2b20c73d59
refactor form field value handling, to get a nicer api and an easier algorithm to get field's value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4158
diff
changeset
|
259 |
form.__class__.__name__, formattr), DeprecationWarning) |
6b2b20c73d59
refactor form field value handling, to get a nicer api and an easier algorithm to get field's value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4158
diff
changeset
|
260 |
return getattr(form, formattr)() |
6b2b20c73d59
refactor form field value handling, to get a nicer api and an easier algorithm to get field's value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4158
diff
changeset
|
261 |
if self.eidparam: |
6b2b20c73d59
refactor form field value handling, to get a nicer api and an easier algorithm to get field's value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4158
diff
changeset
|
262 |
return entity.e_schema.default(self.name) |
6b2b20c73d59
refactor form field value handling, to get a nicer api and an easier algorithm to get field's value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4158
diff
changeset
|
263 |
return None |
6b2b20c73d59
refactor form field value handling, to get a nicer api and an easier algorithm to get field's value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4158
diff
changeset
|
264 |
|
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
265 |
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
|
266 |
"""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
|
267 |
field |
1437 | 268 |
""" |
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
269 |
return u'' |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
270 |
|
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
271 |
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
|
272 |
"""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
|
273 |
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
|
274 |
""" |
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
|
275 |
widget = self.get_widget(form) |
3873
4d95109582c7
[web] remove widget.render backward compatibility
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3790
diff
changeset
|
276 |
return widget.render(form, self, renderer) |
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
277 |
|
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
278 |
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
|
279 |
"""return vocabulary for this field. This method will be called by |
4161
4273f5094651
refactor vocabulary handling to avoid having to define methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4160
diff
changeset
|
280 |
widgets which requires a vocabulary. |
4273f5094651
refactor vocabulary handling to avoid having to define methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4160
diff
changeset
|
281 |
""" |
4273f5094651
refactor vocabulary handling to avoid having to define methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4160
diff
changeset
|
282 |
assert self.choices is not None |
4273f5094651
refactor vocabulary handling to avoid having to define methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4160
diff
changeset
|
283 |
if callable(self.choices): |
4273f5094651
refactor vocabulary handling to avoid having to define methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4160
diff
changeset
|
284 |
try: |
4273f5094651
refactor vocabulary handling to avoid having to define methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4160
diff
changeset
|
285 |
vocab = self.choices(form=form) |
4273f5094651
refactor vocabulary handling to avoid having to define methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4160
diff
changeset
|
286 |
except TypeError: |
4273f5094651
refactor vocabulary handling to avoid having to define methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4160
diff
changeset
|
287 |
warn('[3.3] vocabulary method (eg field.choices) should now take ' |
4273f5094651
refactor vocabulary handling to avoid having to define methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4160
diff
changeset
|
288 |
'the form instance as argument', DeprecationWarning) |
4273f5094651
refactor vocabulary handling to avoid having to define methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4160
diff
changeset
|
289 |
vocab = self.choices(req=form._cw) |
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
|
290 |
else: |
4170
c325c62cec8e
crecord introduced junk...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4168
diff
changeset
|
291 |
vocab = self.choices |
c325c62cec8e
crecord introduced junk...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4168
diff
changeset
|
292 |
if vocab and not isinstance(vocab[0], (list, tuple)): |
c325c62cec8e
crecord introduced junk...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4168
diff
changeset
|
293 |
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
|
294 |
if self.internationalizable: |
3790
34fdb8fb49d1
[B] web: weird display with internationalizable (Closes: #503071)
Julien Jehannet <julien.jehannet@logilab.fr>
parents:
3689
diff
changeset
|
295 |
# the short-cirtcuit 'and' boolean operator is used here to permit |
34fdb8fb49d1
[B] web: weird display with internationalizable (Closes: #503071)
Julien Jehannet <julien.jehannet@logilab.fr>
parents:
3689
diff
changeset
|
296 |
# a valid empty string in vocabulary without attempting to translate |
34fdb8fb49d1
[B] web: weird display with internationalizable (Closes: #503071)
Julien Jehannet <julien.jehannet@logilab.fr>
parents:
3689
diff
changeset
|
297 |
# it by gettext (which can lead to weird strings display) |
3890
d7a270f50f54
backport stable branch (one more time painfully)
Sylvain Thénault <sylvain.thenault@logilab.fr>
diff
changeset
|
298 |
vocab = [(label and form._cw._(label), value) for label, value 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
|
299 |
if self.sort: |
3334
8d831c02da9a
fix sort of fields vocabulary: should consider option groups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3308
diff
changeset
|
300 |
vocab = vocab_sort(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
|
301 |
return vocab |
1437 | 302 |
|
4162
d2663bcf5306
replace form_field_[encoding|vocabulary] methods on form by encoding|vocabylary(form) methods on fields
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4161
diff
changeset
|
303 |
def format(self, form): |
d2663bcf5306
replace form_field_[encoding|vocabulary] methods on form by encoding|vocabylary(form) methods on fields
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4161
diff
changeset
|
304 |
"""return MIME type used for the given (text or bytes) field""" |
d2663bcf5306
replace form_field_[encoding|vocabulary] methods on form by encoding|vocabylary(form) methods on fields
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4161
diff
changeset
|
305 |
if self.eidparam and self.role == 'subject': |
d2663bcf5306
replace form_field_[encoding|vocabulary] methods on form by encoding|vocabylary(form) methods on fields
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4161
diff
changeset
|
306 |
entity = form.edited_entity |
d2663bcf5306
replace form_field_[encoding|vocabulary] methods on form by encoding|vocabylary(form) methods on fields
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4161
diff
changeset
|
307 |
if entity.e_schema.has_metadata(self.name, 'format') and ( |
d2663bcf5306
replace form_field_[encoding|vocabulary] methods on form by encoding|vocabylary(form) methods on fields
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4161
diff
changeset
|
308 |
entity.has_eid() or '%s_format' % self.name in entity): |
d2663bcf5306
replace form_field_[encoding|vocabulary] methods on form by encoding|vocabylary(form) methods on fields
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4161
diff
changeset
|
309 |
return form.edited_entity.attr_metadata(self.name, 'format') |
d2663bcf5306
replace form_field_[encoding|vocabulary] methods on form by encoding|vocabylary(form) methods on fields
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4161
diff
changeset
|
310 |
return form._cw.property_value('ui.default-text-format') |
d2663bcf5306
replace form_field_[encoding|vocabulary] methods on form by encoding|vocabylary(form) methods on fields
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4161
diff
changeset
|
311 |
|
d2663bcf5306
replace form_field_[encoding|vocabulary] methods on form by encoding|vocabylary(form) methods on fields
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4161
diff
changeset
|
312 |
def encoding(self, form): |
d2663bcf5306
replace form_field_[encoding|vocabulary] methods on form by encoding|vocabylary(form) methods on fields
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4161
diff
changeset
|
313 |
"""return encoding used for the given (text) field""" |
d2663bcf5306
replace form_field_[encoding|vocabulary] methods on form by encoding|vocabylary(form) methods on fields
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4161
diff
changeset
|
314 |
if self.eidparam: |
d2663bcf5306
replace form_field_[encoding|vocabulary] methods on form by encoding|vocabylary(form) methods on fields
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4161
diff
changeset
|
315 |
entity = form.edited_entity |
d2663bcf5306
replace form_field_[encoding|vocabulary] methods on form by encoding|vocabylary(form) methods on fields
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4161
diff
changeset
|
316 |
if entity.e_schema.has_metadata(self.name, 'encoding') and ( |
d2663bcf5306
replace form_field_[encoding|vocabulary] methods on form by encoding|vocabylary(form) methods on fields
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4161
diff
changeset
|
317 |
entity.has_eid() or '%s_encoding' % self.name in entity): |
d2663bcf5306
replace form_field_[encoding|vocabulary] methods on form by encoding|vocabylary(form) methods on fields
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4161
diff
changeset
|
318 |
return form.edited_entity.attr_metadata(self.name, 'encoding') |
d2663bcf5306
replace form_field_[encoding|vocabulary] methods on form by encoding|vocabylary(form) methods on fields
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4161
diff
changeset
|
319 |
return form._cw.encoding |
d2663bcf5306
replace form_field_[encoding|vocabulary] methods on form by encoding|vocabylary(form) methods on fields
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4161
diff
changeset
|
320 |
|
1307 | 321 |
def form_init(self, form): |
3510
bf746bf4a394
rename form_build_context to build_context, and call it from form, not renderer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3496
diff
changeset
|
322 |
"""method called before by build_context to trigger potential field |
1307 | 323 |
initialization requiring the form instance |
324 |
""" |
|
325 |
pass |
|
1437 | 326 |
|
4162
d2663bcf5306
replace form_field_[encoding|vocabulary] methods on form by encoding|vocabylary(form) methods on fields
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4161
diff
changeset
|
327 |
def has_been_modified(self, form): |
d2663bcf5306
replace form_field_[encoding|vocabulary] methods on form by encoding|vocabylary(form) methods on fields
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4161
diff
changeset
|
328 |
if self.is_visible(): |
d2663bcf5306
replace form_field_[encoding|vocabulary] methods on form by encoding|vocabylary(form) methods on fields
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4161
diff
changeset
|
329 |
# fields not corresponding to an entity attribute / relations |
d2663bcf5306
replace form_field_[encoding|vocabulary] methods on form by encoding|vocabylary(form) methods on fields
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4161
diff
changeset
|
330 |
# are considered modified |
d2663bcf5306
replace form_field_[encoding|vocabulary] methods on form by encoding|vocabylary(form) methods on fields
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4161
diff
changeset
|
331 |
if not self.eidparam or not self.role or not form.edited_entity.has_eid(): |
d2663bcf5306
replace form_field_[encoding|vocabulary] methods on form by encoding|vocabylary(form) methods on fields
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4161
diff
changeset
|
332 |
return True # XXX |
d2663bcf5306
replace form_field_[encoding|vocabulary] methods on form by encoding|vocabylary(form) methods on fields
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4161
diff
changeset
|
333 |
try: |
d2663bcf5306
replace form_field_[encoding|vocabulary] methods on form by encoding|vocabylary(form) methods on fields
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4161
diff
changeset
|
334 |
if self.role == 'subject': |
d2663bcf5306
replace form_field_[encoding|vocabulary] methods on form by encoding|vocabylary(form) methods on fields
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4161
diff
changeset
|
335 |
previous_value = getattr(form.edited_entity, self.name) |
d2663bcf5306
replace form_field_[encoding|vocabulary] methods on form by encoding|vocabylary(form) methods on fields
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4161
diff
changeset
|
336 |
else: |
d2663bcf5306
replace form_field_[encoding|vocabulary] methods on form by encoding|vocabylary(form) methods on fields
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4161
diff
changeset
|
337 |
previous_value = getattr(form.edited_entity, |
d2663bcf5306
replace form_field_[encoding|vocabulary] methods on form by encoding|vocabylary(form) methods on fields
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4161
diff
changeset
|
338 |
'reverse_%s' % self.name) |
d2663bcf5306
replace form_field_[encoding|vocabulary] methods on form by encoding|vocabylary(form) methods on fields
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4161
diff
changeset
|
339 |
except AttributeError: |
d2663bcf5306
replace form_field_[encoding|vocabulary] methods on form by encoding|vocabylary(form) methods on fields
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4161
diff
changeset
|
340 |
# fields with eidparam=True but not corresponding to an actual |
d2663bcf5306
replace form_field_[encoding|vocabulary] methods on form by encoding|vocabylary(form) methods on fields
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4161
diff
changeset
|
341 |
# attribute or relation |
d2663bcf5306
replace form_field_[encoding|vocabulary] methods on form by encoding|vocabylary(form) methods on fields
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4161
diff
changeset
|
342 |
return True |
d2663bcf5306
replace form_field_[encoding|vocabulary] methods on form by encoding|vocabylary(form) methods on fields
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4161
diff
changeset
|
343 |
# if it's a non final relation, we need the eids |
d2663bcf5306
replace form_field_[encoding|vocabulary] methods on form by encoding|vocabylary(form) methods on fields
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4161
diff
changeset
|
344 |
if isinstance(previous_value, list): |
d2663bcf5306
replace form_field_[encoding|vocabulary] methods on form by encoding|vocabylary(form) methods on fields
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4161
diff
changeset
|
345 |
# widget should return untyped eids |
d2663bcf5306
replace form_field_[encoding|vocabulary] methods on form by encoding|vocabylary(form) methods on fields
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4161
diff
changeset
|
346 |
previous_value = set(unicode(e.eid) for e in previous_value) |
d2663bcf5306
replace form_field_[encoding|vocabulary] methods on form by encoding|vocabylary(form) methods on fields
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4161
diff
changeset
|
347 |
if form.edited_entity.has_eid() and (previous_value == self.process_form_value(form)): |
d2663bcf5306
replace form_field_[encoding|vocabulary] methods on form by encoding|vocabylary(form) methods on fields
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4161
diff
changeset
|
348 |
return False # not modified |
d2663bcf5306
replace form_field_[encoding|vocabulary] methods on form by encoding|vocabylary(form) methods on fields
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4161
diff
changeset
|
349 |
return True |
d2663bcf5306
replace form_field_[encoding|vocabulary] methods on form by encoding|vocabylary(form) methods on fields
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4161
diff
changeset
|
350 |
return False |
d2663bcf5306
replace form_field_[encoding|vocabulary] methods on form by encoding|vocabylary(form) methods on fields
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4161
diff
changeset
|
351 |
|
3387
a357d4147eee
[forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3385
diff
changeset
|
352 |
def process_form_value(self, form): |
a357d4147eee
[forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3385
diff
changeset
|
353 |
"""process posted form and return correctly typed value""" |
a357d4147eee
[forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3385
diff
changeset
|
354 |
widget = self.get_widget(form) |
a357d4147eee
[forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3385
diff
changeset
|
355 |
return widget.process_field_data(form, self) |
a357d4147eee
[forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3385
diff
changeset
|
356 |
|
a357d4147eee
[forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3385
diff
changeset
|
357 |
def process_posted(self, form): |
a357d4147eee
[forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3385
diff
changeset
|
358 |
for field in self.actual_fields(form): |
a357d4147eee
[forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3385
diff
changeset
|
359 |
if field is self: |
4166
677e487e691a
process_posted yield field instead of field's name
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4165
diff
changeset
|
360 |
yield field, field.process_form_value(form) |
3387
a357d4147eee
[forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3385
diff
changeset
|
361 |
else: |
a357d4147eee
[forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3385
diff
changeset
|
362 |
# recursive function: we might have compound fields |
a357d4147eee
[forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3385
diff
changeset
|
363 |
# of compound fields (of compound fields of ...) |
4166
677e487e691a
process_posted yield field instead of field's name
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4165
diff
changeset
|
364 |
for field, value in field.process_posted(form): |
677e487e691a
process_posted yield field instead of field's name
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4165
diff
changeset
|
365 |
yield field, value |
3387
a357d4147eee
[forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3385
diff
changeset
|
366 |
|
1986
96c0e56cb0cf
move widget selection's logic on fields
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
367 |
|
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
368 |
class StringField(Field): |
1986
96c0e56cb0cf
move widget selection's logic on fields
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
369 |
widget = TextArea |
96c0e56cb0cf
move widget selection's logic on fields
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
370 |
|
4168
9d83f9c80c1c
nicer StringField __init__
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4166
diff
changeset
|
371 |
def __init__(self, name=None, max_length=None, **kwargs): |
1986
96c0e56cb0cf
move widget selection's logic on fields
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
372 |
self.max_length = max_length # must be set before super call |
4168
9d83f9c80c1c
nicer StringField __init__
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4166
diff
changeset
|
373 |
super(StringField, self).__init__(name=name, **kwargs) |
1986
96c0e56cb0cf
move widget selection's logic on fields
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
374 |
|
96c0e56cb0cf
move widget selection's logic on fields
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
375 |
def init_widget(self, widget): |
96c0e56cb0cf
move widget selection's logic on fields
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
376 |
if widget is None: |
96c0e56cb0cf
move widget selection's logic on fields
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
377 |
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
|
378 |
widget = Select() |
1986
96c0e56cb0cf
move widget selection's logic on fields
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
379 |
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
|
380 |
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
|
381 |
|
1986
96c0e56cb0cf
move widget selection's logic on fields
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
382 |
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
|
383 |
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
|
384 |
self.init_text_area(self.widget) |
2362 | 385 |
elif isinstance(self.widget, TextInput): |
2360
1d43aa551ba9
[formwidgets,formfields] hum, this way
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
2356
diff
changeset
|
386 |
self.init_text_input(self.widget) |
1d43aa551ba9
[formwidgets,formfields] hum, this way
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
2356
diff
changeset
|
387 |
|
1d43aa551ba9
[formwidgets,formfields] hum, this way
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
2356
diff
changeset
|
388 |
def init_text_input(self, widget): |
1d43aa551ba9
[formwidgets,formfields] hum, this way
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
2356
diff
changeset
|
389 |
if self.max_length: |
1d43aa551ba9
[formwidgets,formfields] hum, this way
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
2356
diff
changeset
|
390 |
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
|
391 |
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
|
392 |
|
d34589d35daa
drop TextField, this is simply a StringField with a text area widget
sylvain.thenault@logilab.fr
parents:
1564
diff
changeset
|
393 |
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
|
394 |
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
|
395 |
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
|
396 |
widget.attrs.setdefault('rows', 5) |
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
397 |
|
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
398 |
|
4159
6b2b20c73d59
refactor form field value handling, to get a nicer api and an easier algorithm to get field's value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4158
diff
changeset
|
399 |
class PasswordField(StringField): |
6b2b20c73d59
refactor form field value handling, to get a nicer api and an easier algorithm to get field's value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4158
diff
changeset
|
400 |
widget = PasswordInput |
6b2b20c73d59
refactor form field value handling, to get a nicer api and an easier algorithm to get field's value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4158
diff
changeset
|
401 |
|
6b2b20c73d59
refactor form field value handling, to get a nicer api and an easier algorithm to get field's value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4158
diff
changeset
|
402 |
def _typed_value(self, form, load_bytes=False): |
6b2b20c73d59
refactor form field value handling, to get a nicer api and an easier algorithm to get field's value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4158
diff
changeset
|
403 |
if self.eidparam: |
6b2b20c73d59
refactor form field value handling, to get a nicer api and an easier algorithm to get field's value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4158
diff
changeset
|
404 |
# no way to fetch actual password value with cw |
6b2b20c73d59
refactor form field value handling, to get a nicer api and an easier algorithm to get field's value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4158
diff
changeset
|
405 |
if form.edited_entity.has_eid(): |
6b2b20c73d59
refactor form field value handling, to get a nicer api and an easier algorithm to get field's value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4158
diff
changeset
|
406 |
return INTERNAL_FIELD_VALUE |
6b2b20c73d59
refactor form field value handling, to get a nicer api and an easier algorithm to get field's value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4158
diff
changeset
|
407 |
return form.edited_entity.e_schema.default(self.name) |
6b2b20c73d59
refactor form field value handling, to get a nicer api and an easier algorithm to get field's value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4158
diff
changeset
|
408 |
return super(PasswordField, self)._typed_value(form, load_bytes) |
6b2b20c73d59
refactor form field value handling, to get a nicer api and an easier algorithm to get field's value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4158
diff
changeset
|
409 |
|
6b2b20c73d59
refactor form field value handling, to get a nicer api and an easier algorithm to get field's value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4158
diff
changeset
|
410 |
|
1573
d34589d35daa
drop TextField, this is simply a StringField with a text area widget
sylvain.thenault@logilab.fr
parents:
1564
diff
changeset
|
411 |
class RichTextField(StringField): |
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
412 |
widget = None |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
413 |
def __init__(self, format_field=None, **kwargs): |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
414 |
super(RichTextField, self).__init__(**kwargs) |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
415 |
self.format_field = format_field |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
416 |
|
2348
acf4b6a59558
[formwidgets] ensure textarea for richstring has not ridicuously small size (close #344547)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
2347
diff
changeset
|
417 |
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
|
418 |
pass |
acf4b6a59558
[formwidgets] ensure textarea for richstring has not ridicuously small size (close #344547)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
2347
diff
changeset
|
419 |
|
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
420 |
def get_widget(self, form): |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
421 |
if self.widget is None: |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
422 |
if self.use_fckeditor(form): |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
423 |
return FCKEditor() |
1573
d34589d35daa
drop TextField, this is simply a StringField with a text area widget
sylvain.thenault@logilab.fr
parents:
1564
diff
changeset
|
424 |
widget = TextArea() |
d34589d35daa
drop TextField, this is simply a StringField with a text area widget
sylvain.thenault@logilab.fr
parents:
1564
diff
changeset
|
425 |
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
|
426 |
return widget |
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
427 |
return self.widget |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
428 |
|
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
429 |
def get_format_field(self, form): |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
430 |
if self.format_field: |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
431 |
return self.format_field |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
432 |
# 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
|
433 |
# context dictionnary |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3388
diff
changeset
|
434 |
req = form._cw |
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
435 |
try: |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
436 |
return req.data[self] |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
437 |
except KeyError: |
4165
eb9acad29407
proper field's role handling: may be 'subject' / 'object' *in case
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4162
diff
changeset
|
438 |
fkwargs = {'eidparam': self.eidparam, 'role': self.role} |
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
439 |
if self.use_fckeditor(form): |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
440 |
# 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
|
441 |
# 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
|
442 |
fkwargs['widget'] = HiddenInput() |
4159
6b2b20c73d59
refactor form field value handling, to get a nicer api and an easier algorithm to get field's value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4158
diff
changeset
|
443 |
fkwargs['value'] = 'text/html' |
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
444 |
else: |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
445 |
# 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
|
446 |
fkwargs['widget'] = Select() |
1095
6917ebe281e9
test and fix guess_field, some pylint fixes
sylvain.thenault@logilab.fr
parents:
1081
diff
changeset
|
447 |
fcstr = FormatConstraint() |
3347
428f95118556
fix vocab param to avoid deprecation warning
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3337
diff
changeset
|
448 |
fkwargs['choices'] = fcstr.vocabulary(form=form) |
1858
69e41c88e195
connect format field to form_field_format for its initial value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1836
diff
changeset
|
449 |
fkwargs['internationalizable'] = True |
4159
6b2b20c73d59
refactor form field value handling, to get a nicer api and an easier algorithm to get field's value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4158
diff
changeset
|
450 |
fkwargs['value'] = self.format |
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
|
451 |
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
|
452 |
field = StringField(name=self.name + '_format', **fkwargs) |
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
453 |
req.data[self] = field |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
454 |
return field |
1437 | 455 |
|
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
456 |
def actual_fields(self, form): |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
457 |
yield self |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
458 |
format_field = self.get_format_field(form) |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
459 |
if format_field: |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
460 |
yield format_field |
1437 | 461 |
|
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
462 |
def use_fckeditor(self, form): |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
463 |
"""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
|
464 |
`attr`, according to user preferences |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
465 |
""" |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3388
diff
changeset
|
466 |
if form._cw.use_fckeditor(): |
4162
d2663bcf5306
replace form_field_[encoding|vocabulary] methods on form by encoding|vocabylary(form) methods on fields
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4161
diff
changeset
|
467 |
return self.format(form) == 'text/html' |
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
468 |
return False |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
469 |
|
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
470 |
def render(self, form, renderer): |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
471 |
format_field = self.get_format_field(form) |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
472 |
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
|
473 |
# XXX we want both fields to remain vertically aligned |
2723
d38c2b01d766
don't set display: block on hidden input
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2362
diff
changeset
|
474 |
if format_field.is_visible(): |
d38c2b01d766
don't set display: block on hidden input
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2362
diff
changeset
|
475 |
format_field.widget.attrs['style'] = 'display: block' |
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
476 |
result = format_field.render(form, renderer) |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
477 |
else: |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
478 |
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
|
479 |
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
|
480 |
|
1417 | 481 |
|
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
482 |
class FileField(StringField): |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
483 |
widget = FileInput |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
484 |
needs_multipart = True |
1437 | 485 |
|
3496
35a67ac6efe8
support name metadata on guess_field; add name_field to FileField
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3347
diff
changeset
|
486 |
def __init__(self, format_field=None, encoding_field=None, name_field=None, |
35a67ac6efe8
support name metadata on guess_field; add name_field to FileField
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3347
diff
changeset
|
487 |
**kwargs): |
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
488 |
super(FileField, self).__init__(**kwargs) |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
489 |
self.format_field = format_field |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
490 |
self.encoding_field = encoding_field |
3496
35a67ac6efe8
support name metadata on guess_field; add name_field to FileField
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3347
diff
changeset
|
491 |
self.name_field = name_field |
1437 | 492 |
|
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
493 |
def actual_fields(self, form): |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
494 |
yield self |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
495 |
if self.format_field: |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
496 |
yield self.format_field |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
497 |
if self.encoding_field: |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
498 |
yield self.encoding_field |
3496
35a67ac6efe8
support name metadata on guess_field; add name_field to FileField
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3347
diff
changeset
|
499 |
if self.name_field: |
35a67ac6efe8
support name metadata on guess_field; add name_field to FileField
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3347
diff
changeset
|
500 |
yield self.name_field |
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
501 |
|
4159
6b2b20c73d59
refactor form field value handling, to get a nicer api and an easier algorithm to get field's value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4158
diff
changeset
|
502 |
def _typed_value(self, form, load_bytes=False): |
6b2b20c73d59
refactor form field value handling, to get a nicer api and an easier algorithm to get field's value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4158
diff
changeset
|
503 |
if self.eidparam: |
6b2b20c73d59
refactor form field value handling, to get a nicer api and an easier algorithm to get field's value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4158
diff
changeset
|
504 |
if form.edited_entity.has_eid(): |
6b2b20c73d59
refactor form field value handling, to get a nicer api and an easier algorithm to get field's value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4158
diff
changeset
|
505 |
if load_bytes: |
6b2b20c73d59
refactor form field value handling, to get a nicer api and an easier algorithm to get field's value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4158
diff
changeset
|
506 |
return getattr(form.edited_entity, self.name) |
6b2b20c73d59
refactor form field value handling, to get a nicer api and an easier algorithm to get field's value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4158
diff
changeset
|
507 |
# don't actually load data |
6b2b20c73d59
refactor form field value handling, to get a nicer api and an easier algorithm to get field's value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4158
diff
changeset
|
508 |
# XXX value should reflect if some file is already attached |
6b2b20c73d59
refactor form field value handling, to get a nicer api and an easier algorithm to get field's value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4158
diff
changeset
|
509 |
# * try to display name metadata |
6b2b20c73d59
refactor form field value handling, to get a nicer api and an easier algorithm to get field's value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4158
diff
changeset
|
510 |
# * check length(data) / data != null |
6b2b20c73d59
refactor form field value handling, to get a nicer api and an easier algorithm to get field's value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4158
diff
changeset
|
511 |
return True |
6b2b20c73d59
refactor form field value handling, to get a nicer api and an easier algorithm to get field's value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4158
diff
changeset
|
512 |
return False |
6b2b20c73d59
refactor form field value handling, to get a nicer api and an easier algorithm to get field's value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4158
diff
changeset
|
513 |
return super(FileField, self)._typed_value(form, load_bytes) |
6b2b20c73d59
refactor form field value handling, to get a nicer api and an easier algorithm to get field's value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4158
diff
changeset
|
514 |
|
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
515 |
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
|
516 |
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
|
517 |
if self.format_field or self.encoding_field: |
4156
1bbb0ee42c8e
drop form_field_name/form_field_id methods from form object, in favor of field.input_name(form) / field.dom_id(form)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4110
diff
changeset
|
518 |
divid = '%s-advanced' % self.input_name(form) |
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
519 |
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
|
520 |
(xml_escape(uilib.toggle_action(divid)), |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3388
diff
changeset
|
521 |
form._cw._('show advanced fields'), |
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3388
diff
changeset
|
522 |
xml_escape(form._cw.build_url('data/puce_down.png')), |
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3388
diff
changeset
|
523 |
form._cw._('show advanced fields'))) |
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
524 |
wdgs.append(u'<div id="%s" class="hidden">' % divid) |
3496
35a67ac6efe8
support name metadata on guess_field; add name_field to FileField
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3347
diff
changeset
|
525 |
if self.name_field: |
35a67ac6efe8
support name metadata on guess_field; add name_field to FileField
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3347
diff
changeset
|
526 |
wdgs.append(self.render_subfield(form, self.name_field, renderer)) |
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
527 |
if self.format_field: |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
528 |
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
|
529 |
if self.encoding_field: |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
530 |
wdgs.append(self.render_subfield(form, self.encoding_field, renderer)) |
1437 | 531 |
wdgs.append(u'</div>') |
4159
6b2b20c73d59
refactor form field value handling, to get a nicer api and an easier algorithm to get field's value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4158
diff
changeset
|
532 |
if not self.required and self.display_value(form): |
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
533 |
# 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
|
534 |
wdgs.append(u'<br/>') |
4156
1bbb0ee42c8e
drop form_field_name/form_field_id methods from form object, in favor of field.input_name(form) / field.dom_id(form)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4110
diff
changeset
|
535 |
wdgs.append(tags.input(name=self.input_name(form, u'__detach'), |
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
536 |
type=u'checkbox')) |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3388
diff
changeset
|
537 |
wdgs.append(form._cw._('detach attached file')) |
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
538 |
return u'\n'.join(wdgs) |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
539 |
|
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
540 |
def render_subfield(self, form, field, renderer): |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
541 |
return (renderer.render_label(form, field) |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
542 |
+ field.render(form, renderer) |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
543 |
+ renderer.render_help(form, field) |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
544 |
+ u'<br/>') |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
545 |
|
3387
a357d4147eee
[forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3385
diff
changeset
|
546 |
def process_form_value(self, form): |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3388
diff
changeset
|
547 |
posted = form._cw.form |
4156
1bbb0ee42c8e
drop form_field_name/form_field_id methods from form object, in favor of field.input_name(form) / field.dom_id(form)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4110
diff
changeset
|
548 |
if self.input_name(form, u'__detach') in posted: |
3387
a357d4147eee
[forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3385
diff
changeset
|
549 |
# drop current file value |
a357d4147eee
[forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3385
diff
changeset
|
550 |
value = None |
4156
1bbb0ee42c8e
drop form_field_name/form_field_id methods from form object, in favor of field.input_name(form) / field.dom_id(form)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4110
diff
changeset
|
551 |
else: |
1bbb0ee42c8e
drop form_field_name/form_field_id methods from form object, in favor of field.input_name(form) / field.dom_id(form)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4110
diff
changeset
|
552 |
value = posted.get(self.input_name(form)) |
3387
a357d4147eee
[forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3385
diff
changeset
|
553 |
# no need to check value when nor explicit detach nor new file |
a357d4147eee
[forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3385
diff
changeset
|
554 |
# submitted, since it will think the attribute is not modified |
4170
c325c62cec8e
crecord introduced junk...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4168
diff
changeset
|
555 |
if value: |
3387
a357d4147eee
[forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3385
diff
changeset
|
556 |
filename, _, stream = value |
a357d4147eee
[forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3385
diff
changeset
|
557 |
# value is a 3-uple (filename, mimetype, stream) |
a357d4147eee
[forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3385
diff
changeset
|
558 |
value = Binary(stream.read()) |
a357d4147eee
[forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3385
diff
changeset
|
559 |
if not val.getvalue(): # usually an unexistant file |
a357d4147eee
[forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3385
diff
changeset
|
560 |
value = None |
a357d4147eee
[forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3385
diff
changeset
|
561 |
else: |
a357d4147eee
[forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3385
diff
changeset
|
562 |
value.filename = filename |
a357d4147eee
[forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3385
diff
changeset
|
563 |
return value |
a357d4147eee
[forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3385
diff
changeset
|
564 |
|
1437 | 565 |
|
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
566 |
class EditableFileField(FileField): |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
567 |
editable_formats = ('text/plain', 'text/html', 'text/rest') |
1437 | 568 |
|
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
569 |
def render(self, form, renderer): |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
570 |
wdgs = [super(EditableFileField, self).render(form, renderer)] |
4159
6b2b20c73d59
refactor form field value handling, to get a nicer api and an easier algorithm to get field's value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4158
diff
changeset
|
571 |
if self.format(form) in self.editable_formats: |
6b2b20c73d59
refactor form field value handling, to get a nicer api and an easier algorithm to get field's value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4158
diff
changeset
|
572 |
data = self.typed_value(form, load_bytes=True) |
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
573 |
if data: |
4162
d2663bcf5306
replace form_field_[encoding|vocabulary] methods on form by encoding|vocabylary(form) methods on fields
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4161
diff
changeset
|
574 |
encoding = self.encoding(form) |
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
575 |
try: |
4159
6b2b20c73d59
refactor form field value handling, to get a nicer api and an easier algorithm to get field's value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4158
diff
changeset
|
576 |
form.formvalues[self] = unicode(data.getvalue(), encoding) |
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
577 |
except UnicodeError: |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
578 |
pass |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
579 |
else: |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
580 |
if not self.required: |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3388
diff
changeset
|
581 |
msg = form._cw._( |
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
582 |
'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
|
583 |
', 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
|
584 |
'"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
|
585 |
'with the widget below.') |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
586 |
else: |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3388
diff
changeset
|
587 |
msg = form._cw._( |
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
588 |
'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
|
589 |
', 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
|
590 |
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
|
591 |
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
|
592 |
# XXX restore form context? |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
593 |
return '\n'.join(wdgs) |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
594 |
|
3387
a357d4147eee
[forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3385
diff
changeset
|
595 |
def process_form_value(self, form): |
4156
1bbb0ee42c8e
drop form_field_name/form_field_id methods from form object, in favor of field.input_name(form) / field.dom_id(form)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4110
diff
changeset
|
596 |
value = form._cw.form.get(self.input_name(form)) |
3387
a357d4147eee
[forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3385
diff
changeset
|
597 |
if isinstance(value, unicode): |
a357d4147eee
[forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3385
diff
changeset
|
598 |
# file modified using a text widget |
4162
d2663bcf5306
replace form_field_[encoding|vocabulary] methods on form by encoding|vocabylary(form) methods on fields
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4161
diff
changeset
|
599 |
return Binary(value.encode(self.encoding(form))) |
3387
a357d4147eee
[forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3385
diff
changeset
|
600 |
return super(EditableFileField, self).process_form_value(form) |
a357d4147eee
[forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3385
diff
changeset
|
601 |
|
1437 | 602 |
|
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
603 |
class IntField(Field): |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
604 |
def __init__(self, min=None, max=None, **kwargs): |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
605 |
super(IntField, self).__init__(**kwargs) |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
606 |
self.min = min |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
607 |
self.max = max |
1758 | 608 |
if isinstance(self.widget, TextInput): |
609 |
self.widget.attrs.setdefault('size', 5) |
|
610 |
self.widget.attrs.setdefault('maxlength', 15) |
|
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
611 |
|
3387
a357d4147eee
[forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3385
diff
changeset
|
612 |
def process_form_value(self, form): |
a357d4147eee
[forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3385
diff
changeset
|
613 |
return int(Field.process_form_value(self, form)) |
1986
96c0e56cb0cf
move widget selection's logic on fields
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
614 |
|
96c0e56cb0cf
move widget selection's logic on fields
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
615 |
|
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
616 |
class BooleanField(Field): |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
617 |
widget = Radio |
1437 | 618 |
|
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
619 |
def vocabulary(self, form): |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
620 |
if self.choices: |
4131
d8ca873142f4
call super class if choices specified to get std behaviour
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3874
diff
changeset
|
621 |
return super(BooleanField, self).vocabulary(form) |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3388
diff
changeset
|
622 |
return [(form._cw._('yes'), '1'), (form._cw._('no'), '')] |
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
623 |
|
3387
a357d4147eee
[forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3385
diff
changeset
|
624 |
def process_form_value(self, form): |
a357d4147eee
[forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3385
diff
changeset
|
625 |
return bool(Field.process_form_value(self, form)) |
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
626 |
|
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
627 |
|
1437 | 628 |
class FloatField(IntField): |
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
629 |
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
|
630 |
formatstr = req.property_value('ui.float-format') |
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
631 |
if value is None: |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
632 |
return u'' |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
633 |
return formatstr % float(value) |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
634 |
|
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
635 |
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
|
636 |
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
|
637 |
|
3387
a357d4147eee
[forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3385
diff
changeset
|
638 |
def process_form_value(self, form): |
a357d4147eee
[forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3385
diff
changeset
|
639 |
return float(Field.process_form_value(self, form)) |
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
640 |
|
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
641 |
|
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
642 |
class DateField(StringField): |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
643 |
format_prop = 'ui.date-format' |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
644 |
widget = DateTimePicker |
1437 | 645 |
|
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
646 |
def format_single_value(self, req, value): |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
647 |
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
|
648 |
|
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
649 |
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
|
650 |
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
|
651 |
|
3387
a357d4147eee
[forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3385
diff
changeset
|
652 |
def process_form_value(self, form): |
a357d4147eee
[forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3385
diff
changeset
|
653 |
# widget is supposed to return a date as a correctly formatted string |
a357d4147eee
[forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3385
diff
changeset
|
654 |
date = Field.process_form_value(self, form) |
a357d4147eee
[forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3385
diff
changeset
|
655 |
# but for some widgets, it might be simpler to return date objects |
a357d4147eee
[forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3385
diff
changeset
|
656 |
# directly, so handle that case : |
a357d4147eee
[forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3385
diff
changeset
|
657 |
if isinstance(date, basestring): |
a357d4147eee
[forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3385
diff
changeset
|
658 |
date = form.parse_date(wdgdate, 'Date') |
a357d4147eee
[forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3385
diff
changeset
|
659 |
return date |
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
660 |
|
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
661 |
|
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
662 |
class DateTimeField(DateField): |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
663 |
format_prop = 'ui.datetime-format' |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
664 |
|
3387
a357d4147eee
[forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3385
diff
changeset
|
665 |
def process_form_value(self, form): |
a357d4147eee
[forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3385
diff
changeset
|
666 |
# widget is supposed to return a date as a correctly formatted string |
a357d4147eee
[forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3385
diff
changeset
|
667 |
date = Field.process_form_value(self, form) |
a357d4147eee
[forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3385
diff
changeset
|
668 |
# but for some widgets, it might be simpler to return date objects |
a357d4147eee
[forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3385
diff
changeset
|
669 |
# directly, so handle that case : |
a357d4147eee
[forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3385
diff
changeset
|
670 |
if isinstance(date, basestring): |
4101
7fa86fa51229
[mq]: fix_typo_and_forms
Arthur Lutz <arthur.lutz@logilab.fr>
parents:
4069
diff
changeset
|
671 |
date = form.parse_datetime(date, 'Datetime') |
3387
a357d4147eee
[forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3385
diff
changeset
|
672 |
return date |
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
673 |
|
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
674 |
|
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
675 |
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
|
676 |
format_prop = 'ui.time-format' |
1758 | 677 |
widget = TextInput |
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
678 |
|
3387
a357d4147eee
[forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3385
diff
changeset
|
679 |
def process_form_value(self, form): |
a357d4147eee
[forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3385
diff
changeset
|
680 |
# widget is supposed to return a date as a correctly formatted string |
a357d4147eee
[forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3385
diff
changeset
|
681 |
time = Field.process_form_value(self, form) |
a357d4147eee
[forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3385
diff
changeset
|
682 |
# but for some widgets, it might be simpler to return time objects |
a357d4147eee
[forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3385
diff
changeset
|
683 |
# directly, so handle that case : |
a357d4147eee
[forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3385
diff
changeset
|
684 |
if isinstance(time, basestring): |
a357d4147eee
[forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3385
diff
changeset
|
685 |
time = form.parse_time(wdgdate, 'Time') |
a357d4147eee
[forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3385
diff
changeset
|
686 |
return time |
1986
96c0e56cb0cf
move widget selection's logic on fields
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
687 |
|
4161
4273f5094651
refactor vocabulary handling to avoid having to define methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4160
diff
changeset
|
688 |
|
4273f5094651
refactor vocabulary handling to avoid having to define methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4160
diff
changeset
|
689 |
# relation vocabulary helper functions ######################################### |
4273f5094651
refactor vocabulary handling to avoid having to define methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4160
diff
changeset
|
690 |
|
4273f5094651
refactor vocabulary handling to avoid having to define methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4160
diff
changeset
|
691 |
def relvoc_linkedto(entity, rtype, role): |
4273f5094651
refactor vocabulary handling to avoid having to define methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4160
diff
changeset
|
692 |
# first see if its specified by __linkto form parameters |
4273f5094651
refactor vocabulary handling to avoid having to define methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4160
diff
changeset
|
693 |
linkedto = entity.linked_to(rtype, role) |
4273f5094651
refactor vocabulary handling to avoid having to define methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4160
diff
changeset
|
694 |
if linkedto: |
4273f5094651
refactor vocabulary handling to avoid having to define methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4160
diff
changeset
|
695 |
buildent = entity._cw.entity_from_eid |
4273f5094651
refactor vocabulary handling to avoid having to define methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4160
diff
changeset
|
696 |
return [(buildent(eid).view('combobox'), eid) for eid in linkedto] |
4273f5094651
refactor vocabulary handling to avoid having to define methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4160
diff
changeset
|
697 |
return [] |
4273f5094651
refactor vocabulary handling to avoid having to define methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4160
diff
changeset
|
698 |
|
4273f5094651
refactor vocabulary handling to avoid having to define methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4160
diff
changeset
|
699 |
def relvoc_init(entity, rtype, role, required=False): |
4273f5094651
refactor vocabulary handling to avoid having to define methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4160
diff
changeset
|
700 |
# it isn't, check if the entity provides a method to get correct values |
4273f5094651
refactor vocabulary handling to avoid having to define methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4160
diff
changeset
|
701 |
vocab = [] |
4273f5094651
refactor vocabulary handling to avoid having to define methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4160
diff
changeset
|
702 |
if not required: |
4273f5094651
refactor vocabulary handling to avoid having to define methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4160
diff
changeset
|
703 |
vocab.append(('', INTERNAL_FIELD_VALUE)) |
4273f5094651
refactor vocabulary handling to avoid having to define methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4160
diff
changeset
|
704 |
# vocabulary doesn't include current values, add them |
4273f5094651
refactor vocabulary handling to avoid having to define methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4160
diff
changeset
|
705 |
if entity.has_eid(): |
4273f5094651
refactor vocabulary handling to avoid having to define methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4160
diff
changeset
|
706 |
rset = entity.related(rtype, role) |
4273f5094651
refactor vocabulary handling to avoid having to define methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4160
diff
changeset
|
707 |
vocab += [(e.view('combobox'), e.eid) for e in rset.entities()] |
4273f5094651
refactor vocabulary handling to avoid having to define methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4160
diff
changeset
|
708 |
return vocab |
1437 | 709 |
|
4161
4273f5094651
refactor vocabulary handling to avoid having to define methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4160
diff
changeset
|
710 |
def relvoc_unrelated(entity, rtype, role, limit=None): |
4273f5094651
refactor vocabulary handling to avoid having to define methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4160
diff
changeset
|
711 |
if isinstance(rtype, basestring): |
4273f5094651
refactor vocabulary handling to avoid having to define methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4160
diff
changeset
|
712 |
rtype = entity._cw.vreg.schema.rschema(rtype) |
4273f5094651
refactor vocabulary handling to avoid having to define methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4160
diff
changeset
|
713 |
if entity.has_eid(): |
4273f5094651
refactor vocabulary handling to avoid having to define methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4160
diff
changeset
|
714 |
done = set(row[0] for row in entity.related(rtype, role)) |
4273f5094651
refactor vocabulary handling to avoid having to define methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4160
diff
changeset
|
715 |
else: |
4273f5094651
refactor vocabulary handling to avoid having to define methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4160
diff
changeset
|
716 |
done = None |
4273f5094651
refactor vocabulary handling to avoid having to define methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4160
diff
changeset
|
717 |
result = [] |
4273f5094651
refactor vocabulary handling to avoid having to define methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4160
diff
changeset
|
718 |
rsetsize = None |
4273f5094651
refactor vocabulary handling to avoid having to define methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4160
diff
changeset
|
719 |
for objtype in rtype.targets(entity.e_schema, role): |
4273f5094651
refactor vocabulary handling to avoid having to define methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4160
diff
changeset
|
720 |
if limit is not None: |
4273f5094651
refactor vocabulary handling to avoid having to define methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4160
diff
changeset
|
721 |
rsetsize = limit - len(result) |
4273f5094651
refactor vocabulary handling to avoid having to define methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4160
diff
changeset
|
722 |
result += _relvoc_unrelated(entity, rtype, objtype, role, rsetsize, done) |
4273f5094651
refactor vocabulary handling to avoid having to define methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4160
diff
changeset
|
723 |
if limit is not None and len(result) >= limit: |
4273f5094651
refactor vocabulary handling to avoid having to define methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4160
diff
changeset
|
724 |
break |
4273f5094651
refactor vocabulary handling to avoid having to define methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4160
diff
changeset
|
725 |
return result |
4273f5094651
refactor vocabulary handling to avoid having to define methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4160
diff
changeset
|
726 |
|
4273f5094651
refactor vocabulary handling to avoid having to define methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4160
diff
changeset
|
727 |
def _relvoc_unrelated(entity, rtype, targettype, role, limit, done): |
4273f5094651
refactor vocabulary handling to avoid having to define methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4160
diff
changeset
|
728 |
"""return unrelated entities for a given relation and target entity type |
4273f5094651
refactor vocabulary handling to avoid having to define methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4160
diff
changeset
|
729 |
for use in vocabulary |
4273f5094651
refactor vocabulary handling to avoid having to define methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4160
diff
changeset
|
730 |
""" |
4273f5094651
refactor vocabulary handling to avoid having to define methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4160
diff
changeset
|
731 |
if done is None: |
4273f5094651
refactor vocabulary handling to avoid having to define methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4160
diff
changeset
|
732 |
done = set() |
4273f5094651
refactor vocabulary handling to avoid having to define methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4160
diff
changeset
|
733 |
res = [] |
4273f5094651
refactor vocabulary handling to avoid having to define methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4160
diff
changeset
|
734 |
for entity in entity.unrelated(rtype, targettype, role, limit).entities(): |
4273f5094651
refactor vocabulary handling to avoid having to define methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4160
diff
changeset
|
735 |
if entity.eid in done: |
4273f5094651
refactor vocabulary handling to avoid having to define methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4160
diff
changeset
|
736 |
continue |
4273f5094651
refactor vocabulary handling to avoid having to define methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4160
diff
changeset
|
737 |
done.add(entity.eid) |
4273f5094651
refactor vocabulary handling to avoid having to define methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4160
diff
changeset
|
738 |
res.append((entity.view('combobox'), entity.eid)) |
4273f5094651
refactor vocabulary handling to avoid having to define methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4160
diff
changeset
|
739 |
return res |
2146
6645e18e8c93
edit[s|o] field's value should be formatted as the associated field
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2095
diff
changeset
|
740 |
|
1437 | 741 |
|
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
742 |
class RelationField(Field): |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
743 |
|
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
744 |
@staticmethod |
1095
6917ebe281e9
test and fix guess_field, some pylint fixes
sylvain.thenault@logilab.fr
parents:
1081
diff
changeset
|
745 |
def fromcardinality(card, **kwargs): |
1738
2cfd50c8a415
should not override potential explicit widget
sylvain.thenault@logilab.fr
parents:
1709
diff
changeset
|
746 |
kwargs.setdefault('widget', Select(multiple=card in '*+')) |
2cfd50c8a415
should not override potential explicit widget
sylvain.thenault@logilab.fr
parents:
1709
diff
changeset
|
747 |
return RelationField(**kwargs) |
1437 | 748 |
|
4161
4273f5094651
refactor vocabulary handling to avoid having to define methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4160
diff
changeset
|
749 |
def choices(self, form, limit=None): |
1147 | 750 |
entity = form.edited_entity |
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
751 |
# first see if its specified by __linkto form parameters |
4161
4273f5094651
refactor vocabulary handling to avoid having to define methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4160
diff
changeset
|
752 |
linkedto = relvoc_linkedto(entity, self.name, self.role) |
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
753 |
if linkedto: |
4161
4273f5094651
refactor vocabulary handling to avoid having to define methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4160
diff
changeset
|
754 |
return linkedto |
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
755 |
# it isn't, check if the entity provides a method to get correct values |
4161
4273f5094651
refactor vocabulary handling to avoid having to define methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4160
diff
changeset
|
756 |
vocab = relvoc_init(entity, self.name, self.role, self.required) |
4273f5094651
refactor vocabulary handling to avoid having to define methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4160
diff
changeset
|
757 |
method = '%s_%s_vocabulary' % (self.role, self.name) |
4273f5094651
refactor vocabulary handling to avoid having to define methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4160
diff
changeset
|
758 |
try: |
4273f5094651
refactor vocabulary handling to avoid having to define methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4160
diff
changeset
|
759 |
vocab += getattr(form, method)(rtype, limit) |
4273f5094651
refactor vocabulary handling to avoid having to define methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4160
diff
changeset
|
760 |
warn('[3.6] found %s on %s, should override field.choices instead (need tweaks)' |
4273f5094651
refactor vocabulary handling to avoid having to define methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4160
diff
changeset
|
761 |
% (method, form), DeprecationWarning) |
4273f5094651
refactor vocabulary handling to avoid having to define methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4160
diff
changeset
|
762 |
except AttributeError: |
4273f5094651
refactor vocabulary handling to avoid having to define methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4160
diff
changeset
|
763 |
vocab += relvoc_unrelated(entity, self.name, self.role, limit) |
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
|
764 |
if self.sort: |
3334
8d831c02da9a
fix sort of fields vocabulary: should consider option groups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3308
diff
changeset
|
765 |
vocab = vocab_sort(vocab) |
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
|
766 |
return vocab |
1437 | 767 |
|
4159
6b2b20c73d59
refactor form field value handling, to get a nicer api and an easier algorithm to get field's value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4158
diff
changeset
|
768 |
def form_init(self, form): |
6b2b20c73d59
refactor form field value handling, to get a nicer api and an easier algorithm to get field's value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4158
diff
changeset
|
769 |
if not self.display_value(form): |
6b2b20c73d59
refactor form field value handling, to get a nicer api and an easier algorithm to get field's value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4158
diff
changeset
|
770 |
value = form.edited_entity.linked_to(self.name, self.role) |
6b2b20c73d59
refactor form field value handling, to get a nicer api and an easier algorithm to get field's value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4158
diff
changeset
|
771 |
if value: |
6b2b20c73d59
refactor form field value handling, to get a nicer api and an easier algorithm to get field's value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4158
diff
changeset
|
772 |
searchedvalues = ['%s:%s:%s' % (self.name, eid, self.role) |
6b2b20c73d59
refactor form field value handling, to get a nicer api and an easier algorithm to get field's value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4158
diff
changeset
|
773 |
for eid in value] |
6b2b20c73d59
refactor form field value handling, to get a nicer api and an easier algorithm to get field's value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4158
diff
changeset
|
774 |
# remove associated __linkto hidden fields |
6b2b20c73d59
refactor form field value handling, to get a nicer api and an easier algorithm to get field's value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4158
diff
changeset
|
775 |
for field in form.root_form.fields_by_name('__linkto'): |
6b2b20c73d59
refactor form field value handling, to get a nicer api and an easier algorithm to get field's value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4158
diff
changeset
|
776 |
if field.value in searchedvalues: |
6b2b20c73d59
refactor form field value handling, to get a nicer api and an easier algorithm to get field's value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4158
diff
changeset
|
777 |
form.root_form.remove_field(field) |
6b2b20c73d59
refactor form field value handling, to get a nicer api and an easier algorithm to get field's value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4158
diff
changeset
|
778 |
form.formvalues[self] = value |
6b2b20c73d59
refactor form field value handling, to get a nicer api and an easier algorithm to get field's value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4158
diff
changeset
|
779 |
|
6b2b20c73d59
refactor form field value handling, to get a nicer api and an easier algorithm to get field's value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4158
diff
changeset
|
780 |
def _typed_value(self, form, load_bytes=False): |
6b2b20c73d59
refactor form field value handling, to get a nicer api and an easier algorithm to get field's value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4158
diff
changeset
|
781 |
entity = form.edited_entity |
6b2b20c73d59
refactor form field value handling, to get a nicer api and an easier algorithm to get field's value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4158
diff
changeset
|
782 |
# non final relation field |
6b2b20c73d59
refactor form field value handling, to get a nicer api and an easier algorithm to get field's value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4158
diff
changeset
|
783 |
if entity.has_eid() or entity.relation_cached(self.name, self.role): |
6b2b20c73d59
refactor form field value handling, to get a nicer api and an easier algorithm to get field's value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4158
diff
changeset
|
784 |
return [r[0] for r in entity.related(self.name, self.role)] |
6b2b20c73d59
refactor form field value handling, to get a nicer api and an easier algorithm to get field's value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4158
diff
changeset
|
785 |
return () |
6b2b20c73d59
refactor form field value handling, to get a nicer api and an easier algorithm to get field's value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4158
diff
changeset
|
786 |
|
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
787 |
def format_single_value(self, req, value): |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
788 |
return value |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
789 |
|
4110 | 790 |
def process_form_value(self, form): |
791 |
"""process posted form and return correctly typed value""" |
|
792 |
widget = self.get_widget(form) |
|
4171
f1b9f0ed1253
make new editcontroller works, based on a _cw_edited_fields hidden input
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4170
diff
changeset
|
793 |
values = widget.process_field_data(form, self) |
f1b9f0ed1253
make new editcontroller works, based on a _cw_edited_fields hidden input
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4170
diff
changeset
|
794 |
if values is None: |
f1b9f0ed1253
make new editcontroller works, based on a _cw_edited_fields hidden input
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4170
diff
changeset
|
795 |
values = () |
f1b9f0ed1253
make new editcontroller works, based on a _cw_edited_fields hidden input
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4170
diff
changeset
|
796 |
elif not isinstance(values, list): |
f1b9f0ed1253
make new editcontroller works, based on a _cw_edited_fields hidden input
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4170
diff
changeset
|
797 |
values = (values,) |
f1b9f0ed1253
make new editcontroller works, based on a _cw_edited_fields hidden input
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4170
diff
changeset
|
798 |
eids = set() |
f1b9f0ed1253
make new editcontroller works, based on a _cw_edited_fields hidden input
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4170
diff
changeset
|
799 |
for eid in values: |
f1b9f0ed1253
make new editcontroller works, based on a _cw_edited_fields hidden input
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4170
diff
changeset
|
800 |
if not eid: # AutoCompletionWidget XXX deal with this in the widget |
f1b9f0ed1253
make new editcontroller works, based on a _cw_edited_fields hidden input
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4170
diff
changeset
|
801 |
continue |
f1b9f0ed1253
make new editcontroller works, based on a _cw_edited_fields hidden input
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4170
diff
changeset
|
802 |
typed_eid = form.actual_eid(eid) |
f1b9f0ed1253
make new editcontroller works, based on a _cw_edited_fields hidden input
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4170
diff
changeset
|
803 |
if typed_eid is None: |
f1b9f0ed1253
make new editcontroller works, based on a _cw_edited_fields hidden input
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4170
diff
changeset
|
804 |
form._cw.data['pendingfields'].append( (form, self) ) |
f1b9f0ed1253
make new editcontroller works, based on a _cw_edited_fields hidden input
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4170
diff
changeset
|
805 |
return None |
f1b9f0ed1253
make new editcontroller works, based on a _cw_edited_fields hidden input
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4170
diff
changeset
|
806 |
eids.add(typed_eid) |
f1b9f0ed1253
make new editcontroller works, based on a _cw_edited_fields hidden input
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4170
diff
changeset
|
807 |
return eids |
f1b9f0ed1253
make new editcontroller works, based on a _cw_edited_fields hidden input
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4170
diff
changeset
|
808 |
|
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
809 |
|
2523
1d245fbbeb90
some new field/widgets classes: CompoundField, IntervalWidget, HorizontalLayoutWidget
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2522
diff
changeset
|
810 |
class CompoundField(Field): |
1d245fbbeb90
some new field/widgets classes: CompoundField, IntervalWidget, HorizontalLayoutWidget
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2522
diff
changeset
|
811 |
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
|
812 |
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
|
813 |
self.fields = fields |
1d245fbbeb90
some new field/widgets classes: CompoundField, IntervalWidget, HorizontalLayoutWidget
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2522
diff
changeset
|
814 |
|
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
|
815 |
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
|
816 |
return self.fields |
2568 | 817 |
|
2523
1d245fbbeb90
some new field/widgets classes: CompoundField, IntervalWidget, HorizontalLayoutWidget
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2522
diff
changeset
|
818 |
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
|
819 |
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
|
820 |
|
1d245fbbeb90
some new field/widgets classes: CompoundField, IntervalWidget, HorizontalLayoutWidget
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2522
diff
changeset
|
821 |
|
1453
a9841184be7c
guess_field now takes an entity schema as first argument, not an entity class
sylvain.thenault@logilab.fr
parents:
1437
diff
changeset
|
822 |
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
|
823 |
"""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
|
824 |
'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
|
825 |
""" |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
826 |
fieldclass = None |
4014
24f7d7eb4c23
yams api update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3890
diff
changeset
|
827 |
rdef = eschema.rdef(rschema, role) |
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
828 |
if role == 'subject': |
4014
24f7d7eb4c23
yams api update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3890
diff
changeset
|
829 |
targetschema = rdef.object |
3689
deb13e88e037
follow yams 0.25 api changes to improve performance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3575
diff
changeset
|
830 |
if rschema.final: |
4069
5d149ba65dd0
fix guess_field to handle non internationalizable definition
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
4044
diff
changeset
|
831 |
if rdef.get('internationalizable'): |
2095
897732d3ee5a
avoid overriding specified values when guessing field
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2091
diff
changeset
|
832 |
kwargs.setdefault('internationalizable', True) |
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
833 |
else: |
4014
24f7d7eb4c23
yams api update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3890
diff
changeset
|
834 |
targetschema = rdef.subject |
4044
3876c894e018
card referenced later
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4023
diff
changeset
|
835 |
card = rdef.role_cardinality(role) |
1095
6917ebe281e9
test and fix guess_field, some pylint fixes
sylvain.thenault@logilab.fr
parents:
1081
diff
changeset
|
836 |
kwargs['required'] = card in '1+' |
6917ebe281e9
test and fix guess_field, some pylint fixes
sylvain.thenault@logilab.fr
parents:
1081
diff
changeset
|
837 |
kwargs['name'] = rschema.type |
4165
eb9acad29407
proper field's role handling: may be 'subject' / 'object' *in case
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4162
diff
changeset
|
838 |
kwargs['role'] = role |
3308
88f5f89d8d1b
fix generated label for object relation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3299
diff
changeset
|
839 |
if role == 'object': |
3574
f179ccbd13e6
[forms] fix generated label for fields
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3563
diff
changeset
|
840 |
kwargs.setdefault('label', (eschema.type, rschema.type + '_object')) |
3308
88f5f89d8d1b
fix generated label for object relation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3299
diff
changeset
|
841 |
else: |
3574
f179ccbd13e6
[forms] fix generated label for fields
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3563
diff
changeset
|
842 |
kwargs.setdefault('label', (eschema.type, 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
|
843 |
kwargs['eidparam'] = True |
4014
24f7d7eb4c23
yams api update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3890
diff
changeset
|
844 |
kwargs.setdefault('help', rdef.description) |
3689
deb13e88e037
follow yams 0.25 api changes to improve performance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3575
diff
changeset
|
845 |
if rschema.final: |
1104
58f27c3c0167
more guess_field tests and fixes
sylvain.thenault@logilab.fr
parents:
1101
diff
changeset
|
846 |
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
|
847 |
return None |
6917ebe281e9
test and fix guess_field, some pylint fixes
sylvain.thenault@logilab.fr
parents:
1081
diff
changeset
|
848 |
fieldclass = FIELDS[targetschema] |
6917ebe281e9
test and fix guess_field, some pylint fixes
sylvain.thenault@logilab.fr
parents:
1081
diff
changeset
|
849 |
if fieldclass is StringField: |
1104
58f27c3c0167
more guess_field tests and fixes
sylvain.thenault@logilab.fr
parents:
1101
diff
changeset
|
850 |
if eschema.has_metadata(rschema, 'format'): |
58f27c3c0167
more guess_field tests and fixes
sylvain.thenault@logilab.fr
parents:
1101
diff
changeset
|
851 |
# use RichTextField instead of StringField if the attribute has |
58f27c3c0167
more guess_field tests and fixes
sylvain.thenault@logilab.fr
parents:
1101
diff
changeset
|
852 |
# a "format" metadata. But getting information from constraints |
58f27c3c0167
more guess_field tests and fixes
sylvain.thenault@logilab.fr
parents:
1101
diff
changeset
|
853 |
# may be useful anyway... |
4014
24f7d7eb4c23
yams api update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3890
diff
changeset
|
854 |
for cstr in rdef.constraints: |
1104
58f27c3c0167
more guess_field tests and fixes
sylvain.thenault@logilab.fr
parents:
1101
diff
changeset
|
855 |
if isinstance(cstr, StaticVocabularyConstraint): |
58f27c3c0167
more guess_field tests and fixes
sylvain.thenault@logilab.fr
parents:
1101
diff
changeset
|
856 |
raise Exception('rich text field with static vocabulary') |
58f27c3c0167
more guess_field tests and fixes
sylvain.thenault@logilab.fr
parents:
1101
diff
changeset
|
857 |
return RichTextField(**kwargs) |
1574 | 858 |
# init StringField parameters according to constraints |
4014
24f7d7eb4c23
yams api update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3890
diff
changeset
|
859 |
for cstr in rdef.constraints: |
1574 | 860 |
if isinstance(cstr, StaticVocabularyConstraint): |
1577 | 861 |
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
|
862 |
break |
4014
24f7d7eb4c23
yams api update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3890
diff
changeset
|
863 |
for cstr in rdef.constraints: |
1574 | 864 |
if isinstance(cstr, SizeConstraint) and cstr.max is not None: |
865 |
kwargs['max_length'] = cstr.max |
|
866 |
return StringField(**kwargs) |
|
1104
58f27c3c0167
more guess_field tests and fixes
sylvain.thenault@logilab.fr
parents:
1101
diff
changeset
|
867 |
if fieldclass is FileField: |
4160 | 868 |
for metadata in KNOWN_METAATTRIBUTES: |
1104
58f27c3c0167
more guess_field tests and fixes
sylvain.thenault@logilab.fr
parents:
1101
diff
changeset
|
869 |
metaschema = eschema.has_metadata(rschema, metadata) |
58f27c3c0167
more guess_field tests and fixes
sylvain.thenault@logilab.fr
parents:
1101
diff
changeset
|
870 |
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
|
871 |
kwargs['%s_field' % metadata] = guess_field(eschema, metaschema, |
1104
58f27c3c0167
more guess_field tests and fixes
sylvain.thenault@logilab.fr
parents:
1101
diff
changeset
|
872 |
skip_meta_attr=False) |
1095
6917ebe281e9
test and fix guess_field, some pylint fixes
sylvain.thenault@logilab.fr
parents:
1081
diff
changeset
|
873 |
return fieldclass(**kwargs) |
6917ebe281e9
test and fix guess_field, some pylint fixes
sylvain.thenault@logilab.fr
parents:
1081
diff
changeset
|
874 |
return RelationField.fromcardinality(card, **kwargs) |
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
875 |
|
1986
96c0e56cb0cf
move widget selection's logic on fields
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
876 |
|
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
877 |
FIELDS = { |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
878 |
'Boolean': BooleanField, |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
879 |
'Bytes': FileField, |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
880 |
'Date': DateField, |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
881 |
'Datetime': DateTimeField, |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
882 |
'Int': IntField, |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
883 |
'Float': FloatField, |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
884 |
'Decimal': StringField, |
4159
6b2b20c73d59
refactor form field value handling, to get a nicer api and an easier algorithm to get field's value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4158
diff
changeset
|
885 |
'Password': PasswordField, |
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
886 |
'String' : StringField, |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
887 |
'Time': TimeField, |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
888 |
} |