/usr/lib/python2.7/dist-packages/sprox/viewbase.py is in python-sprox 0.9.6-1build1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 | import inspect
from sprox.util import name2label, is_widget, is_widget_class
try: #pragma: no cover
from tw2.core import Widget, Deferred
from tw2.core.widgets import WidgetMeta
from tw2.forms import HiddenField
except ImportError: #pragma: no cover
from tw.api import Widget
from tw.forms import HiddenField
class WidgetMeta(object):
"""TW2 WidgetMetaClass"""
from .configbase import ConfigBase, ConfigBaseError
from .widgetselector import WidgetSelector
#sa 0.5 support
try: #pragma:no cover
from sqlalchemy.types import Enum
except: #pragma:no cover
class Enum:
pass
class ClassViewer(object):
"""class wrapper to expose items of a class. Needed to pass classes to TW as params"""
def __init__(self, klass):
self.__name__ = klass.__name__
class ViewBaseError(Exception):pass
class ViewBase(ConfigBase):
"""
:Modifiers:
+-----------------------------------+--------------------------------------------+------------------------------+
| Name | Description | Default |
+===================================+============================================+==============================+
| __widget_selector_type__ | What class to use for widget selection. | WidgetSelector |
+-----------------------------------+--------------------------------------------+------------------------------+
| __field_widgets__ | A dictionary of widgets to replace the | {} |
| | ones that would be chosen by the selector | |
+-----------------------------------+--------------------------------------------+------------------------------+
| __field_widget_types__ | A dictionary of types of widgets, allowing | {} |
| | sprox to determine the widget args | |
+-----------------------------------+--------------------------------------------+------------------------------+
| __field_widget_args__ | A dictionary of types of args for widgets, | {} |
| | you to override the args sent to the fields| |
+-----------------------------------+--------------------------------------------+------------------------------+
| __base_widget_type__ | The base widget for this config | Widget |
+-----------------------------------+--------------------------------------------+------------------------------+
| __base_widget_args__ | Args to pass into the widget overrides any | {} |
| | defaults that are set in sprox creation | |
+-----------------------------------+--------------------------------------------+------------------------------+
| __widget_selector__ | an instantiated object to use for widget | None |
| | selection. | |
+-----------------------------------+--------------------------------------------+------------------------------+
Also, see the :mod:`sprox.configbase` modifiers.
"""
__field_widgets__ = None
__field_widget_types__ = None
__field_widget_args__ = None
__ignore_field_names__ = None
#object overrides
__base_widget_type__ = Widget
__base_widget_args__ = None
__widget_selector_type__ = WidgetSelector
__widget_selector__ = None
def _do_init_attrs(self):
super(ViewBase, self)._do_init_attrs()
if self.__base_widget_args__ is None:
self.__base_widget_args__ = {}
if self.__field_widgets__ is None:
self.__field_widgets__ = {}
if self.__field_widget_args__ is None:
self.__field_widget_args__ = {}
if self.__field_widget_types__ is None:
self.__field_widget_types__ = {}
if self.__widget_selector__ is None:
self.__widget_selector__ = self.__widget_selector_type__()
if self.__ignore_field_names__ is None:
self.__ignore_field_names__ = ['sprox_id', '_method']
for attr in dir(self):
if not attr.startswith('__'):
value = getattr(self, attr)
if is_widget(value):
if not getattr(value, 'id', None):
raise ViewBaseError('Widgets must provide an id argument for use as a field within a ViewBase')
self.__add_fields__[attr] = value
try:
if is_widget_class(value):
self.__field_widget_types__[attr] = value
except TypeError:
pass
@property
def __widget__(self):
widget = getattr(self, '___widget__', None)
if not widget:
self.___widget__ = self.__base_widget_type__(**self.__widget_args__)
return self.___widget__
#try to act like a widget as much as possible
def __call__(self, *args, **kw):
return self.display(*args, **kw)
def display(self, *args, **kw):
if 'value' not in kw and args:
args = list(args)
kw['value'] = args.pop(0)
return self.__widget__.display(*args, **kw)
@property
def __widget_args__(self):
return self._do_get_widget_args()
def _do_get_widget_args(self):
widget_dict = self._do_get_field_widgets(self.__fields__)
field_widgets = []
for key in self.__fields__:
if key not in widget_dict:
continue
value = widget_dict[key]
#sometimes a field will have two widgets associated with it (disabled fields)
if hasattr(value,'__iter__'):
field_widgets.extend(value)
continue
field_widgets.append(value)
d = dict(children=field_widgets)
d.update(self.__base_widget_args__)
return d
def _do_get_disabled_fields(self):
return self.__disable_fields__
def _do_get_field_widget_args(self, field_name, field):
# toscawidgets does not like ids that have '.' in them. This does not
# work for databases with schemas.
field_name = field_name.replace('.', '_')
args = {}
#this is sort of a hack around TW evaluating _some_ params that are classes.
entity = field
if inspect.isclass(field):
entity = ClassViewer(field)
if hasattr(Widget, 'req'):
args.update({'id':'sx_'+field_name, 'key':field_name})
else: #pragma: no cover
args.update({'id':field_name})
args.update({'name':field_name,
'identity':self.__entity__.__name__+'_'+field_name,
'entity':entity, 'provider':self.__provider__,
'label':name2label(field_name), 'label_text':name2label(field_name)})
field_default_value = self.__provider__.get_field_default(entity)
if field_default_value[0]:
if hasattr(Widget, 'req'):
if callable(field_default_value[1]):
args['value'] = Deferred(field_default_value[1])
else:
args['value'] = field_default_value[1]
else: #pragma: no cover
args['default'] = field_default_value[1]
#enum support works completely differently.
#if isinstance(entity, Column) and isinstance(entity.type, Enum):
# args['options'] = entity.type.enums
if field_name in self.__field_attrs__:
args['attrs'] = self.__field_attrs__[field_name]
provider_widget_args = self.__provider__.get_field_provider_specific_widget_args(self.__entity__, field, field_name)
if provider_widget_args:
args.update(provider_widget_args)
if field_name in self.__field_widget_args__:
args.update(self.__field_widget_args__[field_name])
return args
def __create_hidden_fields(self):
fields = {}
fields['sprox_id'] = HiddenField(id='sprox_id')
for field_name in self.__hide_fields__:
if field_name not in self.__omit_fields__:
args = {}
try:
field = self.__metadata__[field_name]
args = self._do_get_field_widget_args(field_name, field)
except KeyError: #pragma: no cover
pass
if field_name in self.__field_widget_args__:
args.update(self.__field_widget_args__[field_name])
fields[field_name] = HiddenField(**args)
return fields
def _do_get_field_widget_type(self, field_name, field):
return self.__field_widget_types__.get(field_name, self.__widget_selector__.select(field))
# This was a typo once, keeping it around for backwards compatibility
_do_get_field_wiget_type = _do_get_field_widget_type
def _do_get_field_widgets(self, fields):
metadata_keys = list(self.__metadata__.keys())
widgets = {}
for field_name in fields:
if field_name in self.__field_widgets__:
widgets[field_name] = self.__field_widgets__[field_name]
continue
if field_name in self.__add_fields__:
widget = self.__add_fields__[field_name]
if widget is None:
widget = Widget(field_name)
widgets[field_name] = widget
continue
if field_name in self.__ignore_field_names__:
continue
if field_name in self.__hide_fields__:
continue
if field_name not in metadata_keys:
continue
field = self.__metadata__[field_name]
field_widget_type = self._do_get_field_widget_type(field_name, field)
field_widget_args = self._do_get_field_widget_args(field_name, field)
if field_name in self._do_get_disabled_fields():
# in this case, we display the current field, disabling it, and also add
# a hidden field into th emix
field_widget_args['disabled'] = True
field_widget_args['attrs'] = {'disabled':True}
if hasattr(Widget, 'req'):
hidden_id='disabled_' + field_name.replace('.','_')
else: #pragma: no cover
hidden_id=field_name.replace('.','_')
widgets[field_name] = (HiddenField(id=hidden_id, key=field_name,
name=field_name,
identifier=field_name),
field_widget_type(**field_widget_args))
else:
widgets[field_name] = field_widget_type(**field_widget_args)
widgets.update(self.__create_hidden_fields())
return widgets
|