annotate MoinMoin/forms.py @ 2085:7f4520c9b5f1

solves Issue #328, multiple name must be unique with all names, added a function to validatename which takes care of the issue
author Ashutosh Singla <ashu1461@gmail.com>
date Tue, 09 Apr 2013 22:33:42 +0530
parents ecd902b614bd
children be05c7f6d216
rev   line source
1463
fc55b35066ee Add DateTime widget for the metadata editor.
Pavel Sviderski <pavel@psviderski.name>
parents: 1437
diff changeset
1 # Copyright: 2012 MoinMoin:PavelSviderski
1420
100197cb69ab Convert Flatland forms to contain rendering hints.
Cheer Xiao <xiaqqaix@gmail.com>
parents:
diff changeset
2 # Copyright: 2012 MoinMoin:CheerXiao
100197cb69ab Convert Flatland forms to contain rendering hints.
Cheer Xiao <xiaqqaix@gmail.com>
parents:
diff changeset
3 # License: GNU GPL v2 (or any later version), see LICENSE.txt for details.
100197cb69ab Convert Flatland forms to contain rendering hints.
Cheer Xiao <xiaqqaix@gmail.com>
parents:
diff changeset
4
100197cb69ab Convert Flatland forms to contain rendering hints.
Cheer Xiao <xiaqqaix@gmail.com>
parents:
diff changeset
5 """
100197cb69ab Convert Flatland forms to contain rendering hints.
Cheer Xiao <xiaqqaix@gmail.com>
parents:
diff changeset
6 MoinMoin - Flatland widgets
100197cb69ab Convert Flatland forms to contain rendering hints.
Cheer Xiao <xiaqqaix@gmail.com>
parents:
diff changeset
7
100197cb69ab Convert Flatland forms to contain rendering hints.
Cheer Xiao <xiaqqaix@gmail.com>
parents:
diff changeset
8 General Flatland widgets containing hints for the templates.
100197cb69ab Convert Flatland forms to contain rendering hints.
Cheer Xiao <xiaqqaix@gmail.com>
parents:
diff changeset
9 """
100197cb69ab Convert Flatland forms to contain rendering hints.
Cheer Xiao <xiaqqaix@gmail.com>
parents:
diff changeset
10
100197cb69ab Convert Flatland forms to contain rendering hints.
Cheer Xiao <xiaqqaix@gmail.com>
parents:
diff changeset
11
1975
384555088cab lots of pep8 fixes
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1974
diff changeset
12 import re
384555088cab lots of pep8 fixes
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1974
diff changeset
13 import datetime
1626
346c52c9c1e5 Fixes to forms.py and items/__init__.py
Cheer Xiao <xiaqqaix@gmail.com>
parents: 1625
diff changeset
14 import json
2019
ecd902b614bd factor out common code for building Select and MultiSelect widgets
Cheer Xiao <xiaqqaix@gmail.com>
parents: 2011
diff changeset
15 from operator import itemgetter
1437
368899856333 Alter the Tags widget to use JoinedString instead of MultiValue.
Cheer Xiao <xiaqqaix@gmail.com>
parents: 1436
diff changeset
16
2019
ecd902b614bd factor out common code for building Select and MultiSelect widgets
Cheer Xiao <xiaqqaix@gmail.com>
parents: 2011
diff changeset
17 from flatland import (Element, Form, String, Integer, Boolean, Enum as BaseEnum, Dict, JoinedString, List, Array,
1975
384555088cab lots of pep8 fixes
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1974
diff changeset
18 DateTime as _DateTime)
1668
237f790c0290 Implement Reference Flatland widget
Cheer Xiao <xiaqqaix@gmail.com>
parents: 1640
diff changeset
19 from flatland.util import class_cloner, Unspecified
1420
100197cb69ab Convert Flatland forms to contain rendering hints.
Cheer Xiao <xiaqqaix@gmail.com>
parents:
diff changeset
20 from flatland.validation import Validator, Present, IsEmail, ValueBetween, URLValidator, Converted, ValueAtLeast
1640
47fc0a130ae5 Improve DateTime implementation, it uses a UNIX timestamp as a native value.
Pavel Sviderski <pavel@psviderski.name>
parents: 1628
diff changeset
21 from flatland.exc import AdaptationError
1420
100197cb69ab Convert Flatland forms to contain rendering hints.
Cheer Xiao <xiaqqaix@gmail.com>
parents:
diff changeset
22
2085
7f4520c9b5f1 solves Issue #328, multiple name must be unique with all names, added a function to validatename which takes care of the issue
Ashutosh Singla <ashu1461@gmail.com>
parents: 2019
diff changeset
23 from whoosh.query import Term, Or, Not, And
7f4520c9b5f1 solves Issue #328, multiple name must be unique with all names, added a function to validatename which takes care of the issue
Ashutosh Singla <ashu1461@gmail.com>
parents: 2019
diff changeset
24
1668
237f790c0290 Implement Reference Flatland widget
Cheer Xiao <xiaqqaix@gmail.com>
parents: 1640
diff changeset
25 from flask import g as flaskg
237f790c0290 Implement Reference Flatland widget
Cheer Xiao <xiaqqaix@gmail.com>
parents: 1640
diff changeset
26
1420
100197cb69ab Convert Flatland forms to contain rendering hints.
Cheer Xiao <xiaqqaix@gmail.com>
parents:
diff changeset
27 from MoinMoin.constants.forms import *
2085
7f4520c9b5f1 solves Issue #328, multiple name must be unique with all names, added a function to validatename which takes care of the issue
Ashutosh Singla <ashu1461@gmail.com>
parents: 2019
diff changeset
28 from MoinMoin.constants.keys import ITEMID, NAME, LATEST_REVS
1420
100197cb69ab Convert Flatland forms to contain rendering hints.
Cheer Xiao <xiaqqaix@gmail.com>
parents:
diff changeset
29 from MoinMoin.i18n import _, L_, N_
100197cb69ab Convert Flatland forms to contain rendering hints.
Cheer Xiao <xiaqqaix@gmail.com>
parents:
diff changeset
30 from MoinMoin.util.forms import FileStorage
100197cb69ab Convert Flatland forms to contain rendering hints.
Cheer Xiao <xiaqqaix@gmail.com>
parents:
diff changeset
31
100197cb69ab Convert Flatland forms to contain rendering hints.
Cheer Xiao <xiaqqaix@gmail.com>
parents:
diff changeset
32
2019
ecd902b614bd factor out common code for building Select and MultiSelect widgets
Cheer Xiao <xiaqqaix@gmail.com>
parents: 2011
diff changeset
33 class Enum(BaseEnum):
ecd902b614bd factor out common code for building Select and MultiSelect widgets
Cheer Xiao <xiaqqaix@gmail.com>
parents: 2011
diff changeset
34 """
ecd902b614bd factor out common code for building Select and MultiSelect widgets
Cheer Xiao <xiaqqaix@gmail.com>
parents: 2011
diff changeset
35 An Enum with a convenience class method out_of.
ecd902b614bd factor out common code for building Select and MultiSelect widgets
Cheer Xiao <xiaqqaix@gmail.com>
parents: 2011
diff changeset
36 """
ecd902b614bd factor out common code for building Select and MultiSelect widgets
Cheer Xiao <xiaqqaix@gmail.com>
parents: 2011
diff changeset
37 @classmethod
ecd902b614bd factor out common code for building Select and MultiSelect widgets
Cheer Xiao <xiaqqaix@gmail.com>
parents: 2011
diff changeset
38 def out_of(cls, choice_specs, sort_by=None):
ecd902b614bd factor out common code for building Select and MultiSelect widgets
Cheer Xiao <xiaqqaix@gmail.com>
parents: 2011
diff changeset
39 """
ecd902b614bd factor out common code for building Select and MultiSelect widgets
Cheer Xiao <xiaqqaix@gmail.com>
parents: 2011
diff changeset
40 A convenience class method to build Enum with extra data attached to
ecd902b614bd factor out common code for building Select and MultiSelect widgets
Cheer Xiao <xiaqqaix@gmail.com>
parents: 2011
diff changeset
41 each valid value.
ecd902b614bd factor out common code for building Select and MultiSelect widgets
Cheer Xiao <xiaqqaix@gmail.com>
parents: 2011
diff changeset
42
ecd902b614bd factor out common code for building Select and MultiSelect widgets
Cheer Xiao <xiaqqaix@gmail.com>
parents: 2011
diff changeset
43 :param choice_specs: An iterable of tuples. The elements are collected
ecd902b614bd factor out common code for building Select and MultiSelect widgets
Cheer Xiao <xiaqqaix@gmail.com>
parents: 2011
diff changeset
44 into the choice_specs property; the tuples' first
ecd902b614bd factor out common code for building Select and MultiSelect widgets
Cheer Xiao <xiaqqaix@gmail.com>
parents: 2011
diff changeset
45 elements become the valid values of the Enum. e.g.
ecd902b614bd factor out common code for building Select and MultiSelect widgets
Cheer Xiao <xiaqqaix@gmail.com>
parents: 2011
diff changeset
46 for choice_specs = [(v1, ...), (v2, ...), ... ],
ecd902b614bd factor out common code for building Select and MultiSelect widgets
Cheer Xiao <xiaqqaix@gmail.com>
parents: 2011
diff changeset
47 the valid values are v1, v2, ...
ecd902b614bd factor out common code for building Select and MultiSelect widgets
Cheer Xiao <xiaqqaix@gmail.com>
parents: 2011
diff changeset
48
ecd902b614bd factor out common code for building Select and MultiSelect widgets
Cheer Xiao <xiaqqaix@gmail.com>
parents: 2011
diff changeset
49 :param sort_by: If not None, sort choice_specs by the sort_by'th
ecd902b614bd factor out common code for building Select and MultiSelect widgets
Cheer Xiao <xiaqqaix@gmail.com>
parents: 2011
diff changeset
50 element.
ecd902b614bd factor out common code for building Select and MultiSelect widgets
Cheer Xiao <xiaqqaix@gmail.com>
parents: 2011
diff changeset
51 """
ecd902b614bd factor out common code for building Select and MultiSelect widgets
Cheer Xiao <xiaqqaix@gmail.com>
parents: 2011
diff changeset
52 if sort_by is not None:
ecd902b614bd factor out common code for building Select and MultiSelect widgets
Cheer Xiao <xiaqqaix@gmail.com>
parents: 2011
diff changeset
53 choice_specs = sorted(choice_specs, key=itemgetter(sort_by))
ecd902b614bd factor out common code for building Select and MultiSelect widgets
Cheer Xiao <xiaqqaix@gmail.com>
parents: 2011
diff changeset
54 else:
ecd902b614bd factor out common code for building Select and MultiSelect widgets
Cheer Xiao <xiaqqaix@gmail.com>
parents: 2011
diff changeset
55 choice_specs = list(choice_specs)
ecd902b614bd factor out common code for building Select and MultiSelect widgets
Cheer Xiao <xiaqqaix@gmail.com>
parents: 2011
diff changeset
56 return cls.valued(*[e[0] for e in choice_specs]).with_properties(choice_specs=choice_specs)
ecd902b614bd factor out common code for building Select and MultiSelect widgets
Cheer Xiao <xiaqqaix@gmail.com>
parents: 2011
diff changeset
57
ecd902b614bd factor out common code for building Select and MultiSelect widgets
Cheer Xiao <xiaqqaix@gmail.com>
parents: 2011
diff changeset
58
1420
100197cb69ab Convert Flatland forms to contain rendering hints.
Cheer Xiao <xiaqqaix@gmail.com>
parents:
diff changeset
59 Text = String.with_properties(widget=WIDGET_TEXT)
100197cb69ab Convert Flatland forms to contain rendering hints.
Cheer Xiao <xiaqqaix@gmail.com>
parents:
diff changeset
60
100197cb69ab Convert Flatland forms to contain rendering hints.
Cheer Xiao <xiaqqaix@gmail.com>
parents:
diff changeset
61 MultilineText = String.with_properties(widget=WIDGET_MULTILINE_TEXT)
100197cb69ab Convert Flatland forms to contain rendering hints.
Cheer Xiao <xiaqqaix@gmail.com>
parents:
diff changeset
62
100197cb69ab Convert Flatland forms to contain rendering hints.
Cheer Xiao <xiaqqaix@gmail.com>
parents:
diff changeset
63 OptionalText = Text.using(optional=True)
100197cb69ab Convert Flatland forms to contain rendering hints.
Cheer Xiao <xiaqqaix@gmail.com>
parents:
diff changeset
64
100197cb69ab Convert Flatland forms to contain rendering hints.
Cheer Xiao <xiaqqaix@gmail.com>
parents:
diff changeset
65 RequiredText = Text.validated_by(Present())
100197cb69ab Convert Flatland forms to contain rendering hints.
Cheer Xiao <xiaqqaix@gmail.com>
parents:
diff changeset
66
100197cb69ab Convert Flatland forms to contain rendering hints.
Cheer Xiao <xiaqqaix@gmail.com>
parents:
diff changeset
67 OptionalMultilineText = MultilineText.using(optional=True)
100197cb69ab Convert Flatland forms to contain rendering hints.
Cheer Xiao <xiaqqaix@gmail.com>
parents:
diff changeset
68
100197cb69ab Convert Flatland forms to contain rendering hints.
Cheer Xiao <xiaqqaix@gmail.com>
parents:
diff changeset
69 RequiredMultilineText = MultilineText.validated_by(Present())
100197cb69ab Convert Flatland forms to contain rendering hints.
Cheer Xiao <xiaqqaix@gmail.com>
parents:
diff changeset
70
1625
6748d609a242 forms.py: Create JSON widget
Cheer Xiao <xiaqqaix@gmail.com>
parents: 1464
diff changeset
71
6748d609a242 forms.py: Create JSON widget
Cheer Xiao <xiaqqaix@gmail.com>
parents: 1464
diff changeset
72 class ValidJSON(Validator):
6748d609a242 forms.py: Create JSON widget
Cheer Xiao <xiaqqaix@gmail.com>
parents: 1464
diff changeset
73 """Validator for JSON
6748d609a242 forms.py: Create JSON widget
Cheer Xiao <xiaqqaix@gmail.com>
parents: 1464
diff changeset
74 """
6748d609a242 forms.py: Create JSON widget
Cheer Xiao <xiaqqaix@gmail.com>
parents: 1464
diff changeset
75 invalid_json_msg = L_('Invalid JSON.')
2085
7f4520c9b5f1 solves Issue #328, multiple name must be unique with all names, added a function to validatename which takes care of the issue
Ashutosh Singla <ashu1461@gmail.com>
parents: 2019
diff changeset
76 invalid_name_msg = ""
7f4520c9b5f1 solves Issue #328, multiple name must be unique with all names, added a function to validatename which takes care of the issue
Ashutosh Singla <ashu1461@gmail.com>
parents: 2019
diff changeset
77
7f4520c9b5f1 solves Issue #328, multiple name must be unique with all names, added a function to validatename which takes care of the issue
Ashutosh Singla <ashu1461@gmail.com>
parents: 2019
diff changeset
78 def validname(self, meta, name, itemid):
7f4520c9b5f1 solves Issue #328, multiple name must be unique with all names, added a function to validatename which takes care of the issue
Ashutosh Singla <ashu1461@gmail.com>
parents: 2019
diff changeset
79 names = meta.get(NAME)
7f4520c9b5f1 solves Issue #328, multiple name must be unique with all names, added a function to validatename which takes care of the issue
Ashutosh Singla <ashu1461@gmail.com>
parents: 2019
diff changeset
80 if names is None:
7f4520c9b5f1 solves Issue #328, multiple name must be unique with all names, added a function to validatename which takes care of the issue
Ashutosh Singla <ashu1461@gmail.com>
parents: 2019
diff changeset
81 self.invalid_name_msg = L_("No name field in the JSON meta.")
7f4520c9b5f1 solves Issue #328, multiple name must be unique with all names, added a function to validatename which takes care of the issue
Ashutosh Singla <ashu1461@gmail.com>
parents: 2019
diff changeset
82 return False
7f4520c9b5f1 solves Issue #328, multiple name must be unique with all names, added a function to validatename which takes care of the issue
Ashutosh Singla <ashu1461@gmail.com>
parents: 2019
diff changeset
83 if len(names) != len(set(names)):
7f4520c9b5f1 solves Issue #328, multiple name must be unique with all names, added a function to validatename which takes care of the issue
Ashutosh Singla <ashu1461@gmail.com>
parents: 2019
diff changeset
84 self.invalid_name_msg = L_("The names in the JSON name list must be unique.")
7f4520c9b5f1 solves Issue #328, multiple name must be unique with all names, added a function to validatename which takes care of the issue
Ashutosh Singla <ashu1461@gmail.com>
parents: 2019
diff changeset
85 return False
7f4520c9b5f1 solves Issue #328, multiple name must be unique with all names, added a function to validatename which takes care of the issue
Ashutosh Singla <ashu1461@gmail.com>
parents: 2019
diff changeset
86 query = Or([Term(NAME, x) for x in names])
7f4520c9b5f1 solves Issue #328, multiple name must be unique with all names, added a function to validatename which takes care of the issue
Ashutosh Singla <ashu1461@gmail.com>
parents: 2019
diff changeset
87 if itemid is not None:
7f4520c9b5f1 solves Issue #328, multiple name must be unique with all names, added a function to validatename which takes care of the issue
Ashutosh Singla <ashu1461@gmail.com>
parents: 2019
diff changeset
88 query = And([query, Not(Term(ITEMID, itemid))])
7f4520c9b5f1 solves Issue #328, multiple name must be unique with all names, added a function to validatename which takes care of the issue
Ashutosh Singla <ashu1461@gmail.com>
parents: 2019
diff changeset
89 duplicate_names = set()
7f4520c9b5f1 solves Issue #328, multiple name must be unique with all names, added a function to validatename which takes care of the issue
Ashutosh Singla <ashu1461@gmail.com>
parents: 2019
diff changeset
90 with flaskg.storage.indexer.ix[LATEST_REVS].searcher() as searcher:
7f4520c9b5f1 solves Issue #328, multiple name must be unique with all names, added a function to validatename which takes care of the issue
Ashutosh Singla <ashu1461@gmail.com>
parents: 2019
diff changeset
91 results = searcher.search(query)
7f4520c9b5f1 solves Issue #328, multiple name must be unique with all names, added a function to validatename which takes care of the issue
Ashutosh Singla <ashu1461@gmail.com>
parents: 2019
diff changeset
92 for result in results:
7f4520c9b5f1 solves Issue #328, multiple name must be unique with all names, added a function to validatename which takes care of the issue
Ashutosh Singla <ashu1461@gmail.com>
parents: 2019
diff changeset
93 duplicate_names |= set([x for x in result[NAME] if x in names])
7f4520c9b5f1 solves Issue #328, multiple name must be unique with all names, added a function to validatename which takes care of the issue
Ashutosh Singla <ashu1461@gmail.com>
parents: 2019
diff changeset
94 if duplicate_names:
7f4520c9b5f1 solves Issue #328, multiple name must be unique with all names, added a function to validatename which takes care of the issue
Ashutosh Singla <ashu1461@gmail.com>
parents: 2019
diff changeset
95 self.invalid_name_msg = L_("Item(s) named %(duplicate_names)s already exist.", duplicate_names=", ".join(duplicate_names))
7f4520c9b5f1 solves Issue #328, multiple name must be unique with all names, added a function to validatename which takes care of the issue
Ashutosh Singla <ashu1461@gmail.com>
parents: 2019
diff changeset
96 return False
7f4520c9b5f1 solves Issue #328, multiple name must be unique with all names, added a function to validatename which takes care of the issue
Ashutosh Singla <ashu1461@gmail.com>
parents: 2019
diff changeset
97 return True
1625
6748d609a242 forms.py: Create JSON widget
Cheer Xiao <xiaqqaix@gmail.com>
parents: 1464
diff changeset
98
6748d609a242 forms.py: Create JSON widget
Cheer Xiao <xiaqqaix@gmail.com>
parents: 1464
diff changeset
99 def validate(self, element, state):
6748d609a242 forms.py: Create JSON widget
Cheer Xiao <xiaqqaix@gmail.com>
parents: 1464
diff changeset
100 try:
2085
7f4520c9b5f1 solves Issue #328, multiple name must be unique with all names, added a function to validatename which takes care of the issue
Ashutosh Singla <ashu1461@gmail.com>
parents: 2019
diff changeset
101 meta = json.loads(element.value)
1975
384555088cab lots of pep8 fixes
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1974
diff changeset
102 except: # catch ANY exception that happens due to unserializing
1625
6748d609a242 forms.py: Create JSON widget
Cheer Xiao <xiaqqaix@gmail.com>
parents: 1464
diff changeset
103 return self.note_error(element, state, 'invalid_json_msg')
2085
7f4520c9b5f1 solves Issue #328, multiple name must be unique with all names, added a function to validatename which takes care of the issue
Ashutosh Singla <ashu1461@gmail.com>
parents: 2019
diff changeset
104 if not self.validname(meta, state[NAME], state[ITEMID]):
7f4520c9b5f1 solves Issue #328, multiple name must be unique with all names, added a function to validatename which takes care of the issue
Ashutosh Singla <ashu1461@gmail.com>
parents: 2019
diff changeset
105 return self.note_error(element, state, 'invalid_name_msg')
1625
6748d609a242 forms.py: Create JSON widget
Cheer Xiao <xiaqqaix@gmail.com>
parents: 1464
diff changeset
106 return True
6748d609a242 forms.py: Create JSON widget
Cheer Xiao <xiaqqaix@gmail.com>
parents: 1464
diff changeset
107
6748d609a242 forms.py: Create JSON widget
Cheer Xiao <xiaqqaix@gmail.com>
parents: 1464
diff changeset
108 JSON = OptionalMultilineText.with_properties(lang='en', dir='ltr').validated_by(ValidJSON())
6748d609a242 forms.py: Create JSON widget
Cheer Xiao <xiaqqaix@gmail.com>
parents: 1464
diff changeset
109
1420
100197cb69ab Convert Flatland forms to contain rendering hints.
Cheer Xiao <xiaqqaix@gmail.com>
parents:
diff changeset
110 URL = String.with_properties(widget=WIDGET_TEXT).validated_by(URLValidator())
100197cb69ab Convert Flatland forms to contain rendering hints.
Cheer Xiao <xiaqqaix@gmail.com>
parents:
diff changeset
111
100197cb69ab Convert Flatland forms to contain rendering hints.
Cheer Xiao <xiaqqaix@gmail.com>
parents:
diff changeset
112 OpenID = URL.using(label=L_('OpenID')).with_properties(placeholder=L_("OpenID address"))
100197cb69ab Convert Flatland forms to contain rendering hints.
Cheer Xiao <xiaqqaix@gmail.com>
parents:
diff changeset
113
100197cb69ab Convert Flatland forms to contain rendering hints.
Cheer Xiao <xiaqqaix@gmail.com>
parents:
diff changeset
114 YourOpenID = OpenID.with_properties(placeholder=L_("Your OpenID address"))
100197cb69ab Convert Flatland forms to contain rendering hints.
Cheer Xiao <xiaqqaix@gmail.com>
parents:
diff changeset
115
1975
384555088cab lots of pep8 fixes
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1974
diff changeset
116 Email = String.using(label=L_('E-Mail')).with_properties(widget=WIDGET_EMAIL,
384555088cab lots of pep8 fixes
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1974
diff changeset
117 placeholder=L_("E-Mail address")).validated_by(IsEmail())
1420
100197cb69ab Convert Flatland forms to contain rendering hints.
Cheer Xiao <xiaqqaix@gmail.com>
parents:
diff changeset
118
100197cb69ab Convert Flatland forms to contain rendering hints.
Cheer Xiao <xiaqqaix@gmail.com>
parents:
diff changeset
119 YourEmail = Email.with_properties(placeholder=L_("Your E-Mail address"))
100197cb69ab Convert Flatland forms to contain rendering hints.
Cheer Xiao <xiaqqaix@gmail.com>
parents:
diff changeset
120
100197cb69ab Convert Flatland forms to contain rendering hints.
Cheer Xiao <xiaqqaix@gmail.com>
parents:
diff changeset
121 Password = Text.with_properties(widget=WIDGET_PASSWORD).using(label=L_('Password'))
100197cb69ab Convert Flatland forms to contain rendering hints.
Cheer Xiao <xiaqqaix@gmail.com>
parents:
diff changeset
122
100197cb69ab Convert Flatland forms to contain rendering hints.
Cheer Xiao <xiaqqaix@gmail.com>
parents:
diff changeset
123 RequiredPassword = Password.validated_by(Present())
100197cb69ab Convert Flatland forms to contain rendering hints.
Cheer Xiao <xiaqqaix@gmail.com>
parents:
diff changeset
124
100197cb69ab Convert Flatland forms to contain rendering hints.
Cheer Xiao <xiaqqaix@gmail.com>
parents:
diff changeset
125 Checkbox = Boolean.with_properties(widget=WIDGET_CHECKBOX).using(optional=True, default=1)
100197cb69ab Convert Flatland forms to contain rendering hints.
Cheer Xiao <xiaqqaix@gmail.com>
parents:
diff changeset
126
100197cb69ab Convert Flatland forms to contain rendering hints.
Cheer Xiao <xiaqqaix@gmail.com>
parents:
diff changeset
127 InlineCheckbox = Checkbox.with_properties(widget=WIDGET_INLINE_CHECKBOX)
100197cb69ab Convert Flatland forms to contain rendering hints.
Cheer Xiao <xiaqqaix@gmail.com>
parents:
diff changeset
128
100197cb69ab Convert Flatland forms to contain rendering hints.
Cheer Xiao <xiaqqaix@gmail.com>
parents:
diff changeset
129 Select = Enum.with_properties(widget=WIDGET_SELECT)
100197cb69ab Convert Flatland forms to contain rendering hints.
Cheer Xiao <xiaqqaix@gmail.com>
parents:
diff changeset
130
1994
02f24ba9623d add SelectSubmit widget
Cheer Xiao <xiaqqaix@gmail.com>
parents: 1988
diff changeset
131 # SelectSubmit is like Select in that it is rendered as a group of controls
02f24ba9623d add SelectSubmit widget
Cheer Xiao <xiaqqaix@gmail.com>
parents: 1988
diff changeset
132 # with different (predefined) `value`s for the same `name`. But the controls are
02f24ba9623d add SelectSubmit widget
Cheer Xiao <xiaqqaix@gmail.com>
parents: 1988
diff changeset
133 # submit buttons instead of radio buttons.
02f24ba9623d add SelectSubmit widget
Cheer Xiao <xiaqqaix@gmail.com>
parents: 1988
diff changeset
134 #
02f24ba9623d add SelectSubmit widget
Cheer Xiao <xiaqqaix@gmail.com>
parents: 1988
diff changeset
135 # This is used to present the user several "OK" buttons with slightly different
02f24ba9623d add SelectSubmit widget
Cheer Xiao <xiaqqaix@gmail.com>
parents: 1988
diff changeset
136 # semantics, like "Update" and "Update and Close" on a ticket page, or
02f24ba9623d add SelectSubmit widget
Cheer Xiao <xiaqqaix@gmail.com>
parents: 1988
diff changeset
137 # "Save as Draft" and "Publish" when editing a blog entry.
02f24ba9623d add SelectSubmit widget
Cheer Xiao <xiaqqaix@gmail.com>
parents: 1988
diff changeset
138 SelectSubmit = Enum.with_properties(widget=WIDGET_SELECT_SUBMIT)
02f24ba9623d add SelectSubmit widget
Cheer Xiao <xiaqqaix@gmail.com>
parents: 1988
diff changeset
139
1628
95161306ec62 Improve Tags implementation, eliminating tags property fiddling in
Cheer Xiao <xiaqqaix@gmail.com>
parents: 1626
diff changeset
140
95161306ec62 Improve Tags implementation, eliminating tags property fiddling in
Cheer Xiao <xiaqqaix@gmail.com>
parents: 1626
diff changeset
141 # Need a better name to capture the behavior
95161306ec62 Improve Tags implementation, eliminating tags property fiddling in
Cheer Xiao <xiaqqaix@gmail.com>
parents: 1626
diff changeset
142 class MyJoinedString(JoinedString):
95161306ec62 Improve Tags implementation, eliminating tags property fiddling in
Cheer Xiao <xiaqqaix@gmail.com>
parents: 1626
diff changeset
143 """
95161306ec62 Improve Tags implementation, eliminating tags property fiddling in
Cheer Xiao <xiaqqaix@gmail.com>
parents: 1626
diff changeset
144 A JoinedString that offers the list of children (not the joined string) as
95161306ec62 Improve Tags implementation, eliminating tags property fiddling in
Cheer Xiao <xiaqqaix@gmail.com>
parents: 1626
diff changeset
145 value property.
95161306ec62 Improve Tags implementation, eliminating tags property fiddling in
Cheer Xiao <xiaqqaix@gmail.com>
parents: 1626
diff changeset
146 """
95161306ec62 Improve Tags implementation, eliminating tags property fiddling in
Cheer Xiao <xiaqqaix@gmail.com>
parents: 1626
diff changeset
147 @property
95161306ec62 Improve Tags implementation, eliminating tags property fiddling in
Cheer Xiao <xiaqqaix@gmail.com>
parents: 1626
diff changeset
148 def value(self):
95161306ec62 Improve Tags implementation, eliminating tags property fiddling in
Cheer Xiao <xiaqqaix@gmail.com>
parents: 1626
diff changeset
149 return [child.value for child in self]
95161306ec62 Improve Tags implementation, eliminating tags property fiddling in
Cheer Xiao <xiaqqaix@gmail.com>
parents: 1626
diff changeset
150
95161306ec62 Improve Tags implementation, eliminating tags property fiddling in
Cheer Xiao <xiaqqaix@gmail.com>
parents: 1626
diff changeset
151 @property
95161306ec62 Improve Tags implementation, eliminating tags property fiddling in
Cheer Xiao <xiaqqaix@gmail.com>
parents: 1626
diff changeset
152 def u(self):
95161306ec62 Improve Tags implementation, eliminating tags property fiddling in
Cheer Xiao <xiaqqaix@gmail.com>
parents: 1626
diff changeset
153 return self.separator.join(child.u for child in self)
95161306ec62 Improve Tags implementation, eliminating tags property fiddling in
Cheer Xiao <xiaqqaix@gmail.com>
parents: 1626
diff changeset
154
2011
791bdedb0c20 fix lots of pep8 issues, mainly E12x
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1994
diff changeset
155 Tags = MyJoinedString.of(String).with_properties(widget=WIDGET_TEXT).using(
791bdedb0c20 fix lots of pep8 issues, mainly E12x
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1994
diff changeset
156 label=L_('Tags'), optional=True, separator=', ', separator_regex=re.compile(r'\s*,\s*'))
1420
100197cb69ab Convert Flatland forms to contain rendering hints.
Cheer Xiao <xiaqqaix@gmail.com>
parents:
diff changeset
157
2011
791bdedb0c20 fix lots of pep8 issues, mainly E12x
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1994
diff changeset
158 Names = MyJoinedString.of(String).with_properties(widget=WIDGET_TEXT).using(
791bdedb0c20 fix lots of pep8 issues, mainly E12x
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1994
diff changeset
159 label=L_('Names'), optional=True, separator=', ', separator_regex=re.compile(r'\s*,\s*'))
1952
7587ba27a85a usersettings: allow multiple names
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1742
diff changeset
160
1420
100197cb69ab Convert Flatland forms to contain rendering hints.
Cheer Xiao <xiaqqaix@gmail.com>
parents:
diff changeset
161 Search = Text.using(default=u'', optional=True).with_properties(widget=WIDGET_SEARCH, placeholder=L_("Search Query"))
100197cb69ab Convert Flatland forms to contain rendering hints.
Cheer Xiao <xiaqqaix@gmail.com>
parents:
diff changeset
162
100197cb69ab Convert Flatland forms to contain rendering hints.
Cheer Xiao <xiaqqaix@gmail.com>
parents:
diff changeset
163 _Integer = Integer.validated_by(Converted())
100197cb69ab Convert Flatland forms to contain rendering hints.
Cheer Xiao <xiaqqaix@gmail.com>
parents:
diff changeset
164
100197cb69ab Convert Flatland forms to contain rendering hints.
Cheer Xiao <xiaqqaix@gmail.com>
parents:
diff changeset
165 AnyInteger = _Integer.with_properties(widget=WIDGET_ANY_INTEGER)
100197cb69ab Convert Flatland forms to contain rendering hints.
Cheer Xiao <xiaqqaix@gmail.com>
parents:
diff changeset
166
100197cb69ab Convert Flatland forms to contain rendering hints.
Cheer Xiao <xiaqqaix@gmail.com>
parents:
diff changeset
167 Natural = AnyInteger.validated_by(ValueAtLeast(0))
100197cb69ab Convert Flatland forms to contain rendering hints.
Cheer Xiao <xiaqqaix@gmail.com>
parents:
diff changeset
168
100197cb69ab Convert Flatland forms to contain rendering hints.
Cheer Xiao <xiaqqaix@gmail.com>
parents:
diff changeset
169 SmallNatural = _Integer.with_properties(widget=WIDGET_SMALL_NATURAL)
100197cb69ab Convert Flatland forms to contain rendering hints.
Cheer Xiao <xiaqqaix@gmail.com>
parents:
diff changeset
170
1640
47fc0a130ae5 Improve DateTime implementation, it uses a UNIX timestamp as a native value.
Pavel Sviderski <pavel@psviderski.name>
parents: 1628
diff changeset
171
47fc0a130ae5 Improve DateTime implementation, it uses a UNIX timestamp as a native value.
Pavel Sviderski <pavel@psviderski.name>
parents: 1628
diff changeset
172 class DateTimeUNIX(_DateTime):
47fc0a130ae5 Improve DateTime implementation, it uses a UNIX timestamp as a native value.
Pavel Sviderski <pavel@psviderski.name>
parents: 1628
diff changeset
173 """
47fc0a130ae5 Improve DateTime implementation, it uses a UNIX timestamp as a native value.
Pavel Sviderski <pavel@psviderski.name>
parents: 1628
diff changeset
174 A DateTime that uses a UNIX timestamp instead of datetime as internal
47fc0a130ae5 Improve DateTime implementation, it uses a UNIX timestamp as a native value.
Pavel Sviderski <pavel@psviderski.name>
parents: 1628
diff changeset
175 representation of DateTime.
47fc0a130ae5 Improve DateTime implementation, it uses a UNIX timestamp as a native value.
Pavel Sviderski <pavel@psviderski.name>
parents: 1628
diff changeset
176 """
47fc0a130ae5 Improve DateTime implementation, it uses a UNIX timestamp as a native value.
Pavel Sviderski <pavel@psviderski.name>
parents: 1628
diff changeset
177 def serialize(self, value):
47fc0a130ae5 Improve DateTime implementation, it uses a UNIX timestamp as a native value.
Pavel Sviderski <pavel@psviderski.name>
parents: 1628
diff changeset
178 """Serializes value to string."""
47fc0a130ae5 Improve DateTime implementation, it uses a UNIX timestamp as a native value.
Pavel Sviderski <pavel@psviderski.name>
parents: 1628
diff changeset
179 if isinstance(value, int):
47fc0a130ae5 Improve DateTime implementation, it uses a UNIX timestamp as a native value.
Pavel Sviderski <pavel@psviderski.name>
parents: 1628
diff changeset
180 try:
47fc0a130ae5 Improve DateTime implementation, it uses a UNIX timestamp as a native value.
Pavel Sviderski <pavel@psviderski.name>
parents: 1628
diff changeset
181 value = datetime.datetime.utcfromtimestamp(value)
47fc0a130ae5 Improve DateTime implementation, it uses a UNIX timestamp as a native value.
Pavel Sviderski <pavel@psviderski.name>
parents: 1628
diff changeset
182 except ValueError:
47fc0a130ae5 Improve DateTime implementation, it uses a UNIX timestamp as a native value.
Pavel Sviderski <pavel@psviderski.name>
parents: 1628
diff changeset
183 pass
47fc0a130ae5 Improve DateTime implementation, it uses a UNIX timestamp as a native value.
Pavel Sviderski <pavel@psviderski.name>
parents: 1628
diff changeset
184 return super(DateTimeUNIX, self).serialize(value)
47fc0a130ae5 Improve DateTime implementation, it uses a UNIX timestamp as a native value.
Pavel Sviderski <pavel@psviderski.name>
parents: 1628
diff changeset
185
47fc0a130ae5 Improve DateTime implementation, it uses a UNIX timestamp as a native value.
Pavel Sviderski <pavel@psviderski.name>
parents: 1628
diff changeset
186 def adapt(self, value):
47fc0a130ae5 Improve DateTime implementation, it uses a UNIX timestamp as a native value.
Pavel Sviderski <pavel@psviderski.name>
parents: 1628
diff changeset
187 """Coerces value to a native UNIX timestamp.
47fc0a130ae5 Improve DateTime implementation, it uses a UNIX timestamp as a native value.
Pavel Sviderski <pavel@psviderski.name>
parents: 1628
diff changeset
188
47fc0a130ae5 Improve DateTime implementation, it uses a UNIX timestamp as a native value.
Pavel Sviderski <pavel@psviderski.name>
parents: 1628
diff changeset
189 If value is an instance of int and it is a correct UNIX timestamp,
47fc0a130ae5 Improve DateTime implementation, it uses a UNIX timestamp as a native value.
Pavel Sviderski <pavel@psviderski.name>
parents: 1628
diff changeset
190 returns it unchanged. Otherwise uses DateTime superclass to parse it.
47fc0a130ae5 Improve DateTime implementation, it uses a UNIX timestamp as a native value.
Pavel Sviderski <pavel@psviderski.name>
parents: 1628
diff changeset
191 """
47fc0a130ae5 Improve DateTime implementation, it uses a UNIX timestamp as a native value.
Pavel Sviderski <pavel@psviderski.name>
parents: 1628
diff changeset
192 if isinstance(value, int):
47fc0a130ae5 Improve DateTime implementation, it uses a UNIX timestamp as a native value.
Pavel Sviderski <pavel@psviderski.name>
parents: 1628
diff changeset
193 try:
47fc0a130ae5 Improve DateTime implementation, it uses a UNIX timestamp as a native value.
Pavel Sviderski <pavel@psviderski.name>
parents: 1628
diff changeset
194 # check if a value is a correct timestamp
47fc0a130ae5 Improve DateTime implementation, it uses a UNIX timestamp as a native value.
Pavel Sviderski <pavel@psviderski.name>
parents: 1628
diff changeset
195 dt = datetime.datetime.utcfromtimestamp(value)
47fc0a130ae5 Improve DateTime implementation, it uses a UNIX timestamp as a native value.
Pavel Sviderski <pavel@psviderski.name>
parents: 1628
diff changeset
196 return value
47fc0a130ae5 Improve DateTime implementation, it uses a UNIX timestamp as a native value.
Pavel Sviderski <pavel@psviderski.name>
parents: 1628
diff changeset
197 except ValueError:
47fc0a130ae5 Improve DateTime implementation, it uses a UNIX timestamp as a native value.
Pavel Sviderski <pavel@psviderski.name>
parents: 1628
diff changeset
198 raise AdaptationError()
47fc0a130ae5 Improve DateTime implementation, it uses a UNIX timestamp as a native value.
Pavel Sviderski <pavel@psviderski.name>
parents: 1628
diff changeset
199 dt = super(DateTimeUNIX, self).adapt(value)
47fc0a130ae5 Improve DateTime implementation, it uses a UNIX timestamp as a native value.
Pavel Sviderski <pavel@psviderski.name>
parents: 1628
diff changeset
200 if isinstance(dt, datetime.datetime):
47fc0a130ae5 Improve DateTime implementation, it uses a UNIX timestamp as a native value.
Pavel Sviderski <pavel@psviderski.name>
parents: 1628
diff changeset
201 # XXX forces circular dependency when it is in the head import block
47fc0a130ae5 Improve DateTime implementation, it uses a UNIX timestamp as a native value.
Pavel Sviderski <pavel@psviderski.name>
parents: 1628
diff changeset
202 from MoinMoin.themes import utctimestamp
47fc0a130ae5 Improve DateTime implementation, it uses a UNIX timestamp as a native value.
Pavel Sviderski <pavel@psviderski.name>
parents: 1628
diff changeset
203 # TODO: Add support for timezones
47fc0a130ae5 Improve DateTime implementation, it uses a UNIX timestamp as a native value.
Pavel Sviderski <pavel@psviderski.name>
parents: 1628
diff changeset
204 dt = utctimestamp(dt)
47fc0a130ae5 Improve DateTime implementation, it uses a UNIX timestamp as a native value.
Pavel Sviderski <pavel@psviderski.name>
parents: 1628
diff changeset
205 return dt
47fc0a130ae5 Improve DateTime implementation, it uses a UNIX timestamp as a native value.
Pavel Sviderski <pavel@psviderski.name>
parents: 1628
diff changeset
206
1975
384555088cab lots of pep8 fixes
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1974
diff changeset
207 DateTime = (DateTimeUNIX.with_properties(widget=WIDGET_DATETIME,
384555088cab lots of pep8 fixes
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1974
diff changeset
208 placeholder=_("YYYY-MM-DD HH:MM:SS (example: 2013-12-31 23:59:59)"))
1464
89a6cda81ffe pep8 minor fix
Pavel Sviderski <pavel@psviderski.name>
parents: 1463
diff changeset
209 .validated_by(Converted(incorrect=L_("Please use the following format: YYYY-MM-DD HH:MM:SS"))))
1463
fc55b35066ee Add DateTime widget for the metadata editor.
Pavel Sviderski <pavel@psviderski.name>
parents: 1437
diff changeset
210
1420
100197cb69ab Convert Flatland forms to contain rendering hints.
Cheer Xiao <xiaqqaix@gmail.com>
parents:
diff changeset
211 File = FileStorage.with_properties(widget=WIDGET_FILE)
100197cb69ab Convert Flatland forms to contain rendering hints.
Cheer Xiao <xiaqqaix@gmail.com>
parents:
diff changeset
212
100197cb69ab Convert Flatland forms to contain rendering hints.
Cheer Xiao <xiaqqaix@gmail.com>
parents:
diff changeset
213 Hidden = String.using(optional=True).with_properties(widget=WIDGET_HIDDEN)
1668
237f790c0290 Implement Reference Flatland widget
Cheer Xiao <xiaqqaix@gmail.com>
parents: 1640
diff changeset
214
1673
c796a6ca82c4 Add BackReference widget
Cheer Xiao <xiaqqaix@gmail.com>
parents: 1669
diff changeset
215 # optional=True is needed to get rid of the "required field" indicator on the UI (usually an asterisk)
c796a6ca82c4 Add BackReference widget
Cheer Xiao <xiaqqaix@gmail.com>
parents: 1669
diff changeset
216 ReadonlyStringList = List.of(String).using(optional=True).with_properties(widget=WIDGET_READONLY_STRING_LIST)
c796a6ca82c4 Add BackReference widget
Cheer Xiao <xiaqqaix@gmail.com>
parents: 1669
diff changeset
217
c796a6ca82c4 Add BackReference widget
Cheer Xiao <xiaqqaix@gmail.com>
parents: 1669
diff changeset
218 ReadonlyItemLinkList = ReadonlyStringList.with_properties(widget=WIDGET_READONLY_ITEM_LINK_LIST)
c796a6ca82c4 Add BackReference widget
Cheer Xiao <xiaqqaix@gmail.com>
parents: 1669
diff changeset
219
1668
237f790c0290 Implement Reference Flatland widget
Cheer Xiao <xiaqqaix@gmail.com>
parents: 1640
diff changeset
220
237f790c0290 Implement Reference Flatland widget
Cheer Xiao <xiaqqaix@gmail.com>
parents: 1640
diff changeset
221 # XXX When some user chooses a Reference candidate that is removed before the
237f790c0290 Implement Reference Flatland widget
Cheer Xiao <xiaqqaix@gmail.com>
parents: 1640
diff changeset
222 # user POSTs, the validator fails. This can be confusing.
237f790c0290 Implement Reference Flatland widget
Cheer Xiao <xiaqqaix@gmail.com>
parents: 1640
diff changeset
223 class ValidReference(Validator):
237f790c0290 Implement Reference Flatland widget
Cheer Xiao <xiaqqaix@gmail.com>
parents: 1640
diff changeset
224 """
237f790c0290 Implement Reference Flatland widget
Cheer Xiao <xiaqqaix@gmail.com>
parents: 1640
diff changeset
225 Validator for Reference
237f790c0290 Implement Reference Flatland widget
Cheer Xiao <xiaqqaix@gmail.com>
parents: 1640
diff changeset
226 """
237f790c0290 Implement Reference Flatland widget
Cheer Xiao <xiaqqaix@gmail.com>
parents: 1640
diff changeset
227 invalid_reference_msg = L_('Invalid Reference.')
237f790c0290 Implement Reference Flatland widget
Cheer Xiao <xiaqqaix@gmail.com>
parents: 1640
diff changeset
228
237f790c0290 Implement Reference Flatland widget
Cheer Xiao <xiaqqaix@gmail.com>
parents: 1640
diff changeset
229 def validate(self, element, state):
1669
dfe5dd95eaeb Fix ValidReference, add it to Reference's validator.
Cheer Xiao <xiaqqaix@gmail.com>
parents: 1668
diff changeset
230 if element.value not in element.valid_values:
1668
237f790c0290 Implement Reference Flatland widget
Cheer Xiao <xiaqqaix@gmail.com>
parents: 1640
diff changeset
231 return self.note_error(element, state, 'invalid_reference_msg')
237f790c0290 Implement Reference Flatland widget
Cheer Xiao <xiaqqaix@gmail.com>
parents: 1640
diff changeset
232 return True
237f790c0290 Implement Reference Flatland widget
Cheer Xiao <xiaqqaix@gmail.com>
parents: 1640
diff changeset
233
1975
384555088cab lots of pep8 fixes
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1974
diff changeset
234
1669
dfe5dd95eaeb Fix ValidReference, add it to Reference's validator.
Cheer Xiao <xiaqqaix@gmail.com>
parents: 1668
diff changeset
235 class Reference(Select.with_properties(empty_label=L_(u'(None)')).validated_by(ValidReference())):
1668
237f790c0290 Implement Reference Flatland widget
Cheer Xiao <xiaqqaix@gmail.com>
parents: 1640
diff changeset
236 """
237f790c0290 Implement Reference Flatland widget
Cheer Xiao <xiaqqaix@gmail.com>
parents: 1640
diff changeset
237 A metadata property that points to another item selected out of the
237f790c0290 Implement Reference Flatland widget
Cheer Xiao <xiaqqaix@gmail.com>
parents: 1640
diff changeset
238 Results of a search query.
237f790c0290 Implement Reference Flatland widget
Cheer Xiao <xiaqqaix@gmail.com>
parents: 1640
diff changeset
239 """
237f790c0290 Implement Reference Flatland widget
Cheer Xiao <xiaqqaix@gmail.com>
parents: 1640
diff changeset
240 @class_cloner
237f790c0290 Implement Reference Flatland widget
Cheer Xiao <xiaqqaix@gmail.com>
parents: 1640
diff changeset
241 def to(cls, query, query_args={}):
237f790c0290 Implement Reference Flatland widget
Cheer Xiao <xiaqqaix@gmail.com>
parents: 1640
diff changeset
242 cls._query = query
237f790c0290 Implement Reference Flatland widget
Cheer Xiao <xiaqqaix@gmail.com>
parents: 1640
diff changeset
243 cls._query_args = query_args
237f790c0290 Implement Reference Flatland widget
Cheer Xiao <xiaqqaix@gmail.com>
parents: 1640
diff changeset
244 return cls
237f790c0290 Implement Reference Flatland widget
Cheer Xiao <xiaqqaix@gmail.com>
parents: 1640
diff changeset
245
237f790c0290 Implement Reference Flatland widget
Cheer Xiao <xiaqqaix@gmail.com>
parents: 1640
diff changeset
246 @classmethod
237f790c0290 Implement Reference Flatland widget
Cheer Xiao <xiaqqaix@gmail.com>
parents: 1640
diff changeset
247 def _get_choices(cls):
237f790c0290 Implement Reference Flatland widget
Cheer Xiao <xiaqqaix@gmail.com>
parents: 1640
diff changeset
248 revs = flaskg.storage.search(cls._query, **cls._query_args)
237f790c0290 Implement Reference Flatland widget
Cheer Xiao <xiaqqaix@gmail.com>
parents: 1640
diff changeset
249 choices = [(rev.meta[ITEMID], rev.meta[NAME]) for rev in revs]
237f790c0290 Implement Reference Flatland widget
Cheer Xiao <xiaqqaix@gmail.com>
parents: 1640
diff changeset
250 if cls.optional:
237f790c0290 Implement Reference Flatland widget
Cheer Xiao <xiaqqaix@gmail.com>
parents: 1640
diff changeset
251 choices.append((u'', cls.properties['empty_label']))
237f790c0290 Implement Reference Flatland widget
Cheer Xiao <xiaqqaix@gmail.com>
parents: 1640
diff changeset
252 return choices
237f790c0290 Implement Reference Flatland widget
Cheer Xiao <xiaqqaix@gmail.com>
parents: 1640
diff changeset
253
237f790c0290 Implement Reference Flatland widget
Cheer Xiao <xiaqqaix@gmail.com>
parents: 1640
diff changeset
254 def __init__(self, value=Unspecified, **kw):
237f790c0290 Implement Reference Flatland widget
Cheer Xiao <xiaqqaix@gmail.com>
parents: 1640
diff changeset
255 super(Reference, self).__init__(value, **kw)
237f790c0290 Implement Reference Flatland widget
Cheer Xiao <xiaqqaix@gmail.com>
parents: 1640
diff changeset
256 # NOTE There is a slight chance of two instances of the same Reference
237f790c0290 Implement Reference Flatland widget
Cheer Xiao <xiaqqaix@gmail.com>
parents: 1640
diff changeset
257 # subclass having different set of choices when the storage changes
237f790c0290 Implement Reference Flatland widget
Cheer Xiao <xiaqqaix@gmail.com>
parents: 1640
diff changeset
258 # between their initialization.
237f790c0290 Implement Reference Flatland widget
Cheer Xiao <xiaqqaix@gmail.com>
parents: 1640
diff changeset
259 choices = self._get_choices()
237f790c0290 Implement Reference Flatland widget
Cheer Xiao <xiaqqaix@gmail.com>
parents: 1640
diff changeset
260 self.properties['labels'] = dict(choices)
237f790c0290 Implement Reference Flatland widget
Cheer Xiao <xiaqqaix@gmail.com>
parents: 1640
diff changeset
261 self.valid_values = [id_ for id_, name in choices]
1673
c796a6ca82c4 Add BackReference widget
Cheer Xiao <xiaqqaix@gmail.com>
parents: 1669
diff changeset
262
c796a6ca82c4 Add BackReference widget
Cheer Xiao <xiaqqaix@gmail.com>
parents: 1669
diff changeset
263
c796a6ca82c4 Add BackReference widget
Cheer Xiao <xiaqqaix@gmail.com>
parents: 1669
diff changeset
264 class BackReference(ReadonlyItemLinkList):
c796a6ca82c4 Add BackReference widget
Cheer Xiao <xiaqqaix@gmail.com>
parents: 1669
diff changeset
265 """
c796a6ca82c4 Add BackReference widget
Cheer Xiao <xiaqqaix@gmail.com>
parents: 1669
diff changeset
266 Back references built from Whoosh query.
c796a6ca82c4 Add BackReference widget
Cheer Xiao <xiaqqaix@gmail.com>
parents: 1669
diff changeset
267 """
c796a6ca82c4 Add BackReference widget
Cheer Xiao <xiaqqaix@gmail.com>
parents: 1669
diff changeset
268 def set(self, query, **query_args):
c796a6ca82c4 Add BackReference widget
Cheer Xiao <xiaqqaix@gmail.com>
parents: 1669
diff changeset
269 revs = flaskg.storage.search(query, **query_args)
c796a6ca82c4 Add BackReference widget
Cheer Xiao <xiaqqaix@gmail.com>
parents: 1669
diff changeset
270 super(BackReference, self).set([rev.meta[NAME] for rev in revs])
1742
8f18745faf4a In +index view, use a multivalue instead of multiple booleans for contentype
Cheer Xiao <xiaqqaix@gmail.com>
parents: 1689
diff changeset
271
8f18745faf4a In +index view, use a multivalue instead of multiple booleans for contentype
Cheer Xiao <xiaqqaix@gmail.com>
parents: 1689
diff changeset
272
8f18745faf4a In +index view, use a multivalue instead of multiple booleans for contentype
Cheer Xiao <xiaqqaix@gmail.com>
parents: 1689
diff changeset
273 MultiSelect = Array.with_properties(widget=WIDGET_MULTI_SELECT)