Permalink
Browse files

make views easier to subclass

svn path=/MoreFieldsAndWidgets/Products.MasterSelectWidget/trunk/; revision=10891
  • Loading branch information...
1 parent 528c0b6 commit 485c706f7e1cb6dd708204c68ed67d45a37c4374 @lrowe lrowe committed Mar 23, 2009
View
23 Products/MasterSelectWidget/browser.py
@@ -27,11 +27,13 @@ def boolean_value(value):
class SetupSlaves(BrowserView):
"""Generate Javascript to bind masters to slaves"""
+ def getSlaves(self, field):
+ for slave in getattr(field.widget, 'slave_fields', ()):
+ yield slave.copy()
+
def renderJS(self, field):
master = field.getName()
- slaves = getattr(field.widget, 'slave_fields', ())
- for s in slaves:
- slave = s.copy()
+ for slave in self.getSlaves(field):
slave['master'] = master
slave['absolute_url'] = self.context.absolute_url()
@@ -58,6 +60,14 @@ def __call__(self, field):
class MasterSelectJSONValue(BrowserView):
"""JSON vocabulary or value for the given slave field"""
+ def getSlaves(self, fieldname):
+ return getattr(self.context.Schema()[fieldname].widget,
+ 'slave_fields', ())
+
+ def getVocabulary(self, slave, value):
+ kw = { slave['control_param']: value }
+ result = getattr(self.context, slave['vocab_method'])(**kw)
+
def __call__(self):
self.request.response.setHeader(
'Content-Type', 'application/json; charset=utf-8')
@@ -66,18 +76,15 @@ def __call__(self):
slaveid = self.request['slave']
value = self.request['value']
- slaves = getattr(self.context.Schema()[field].widget,
- 'slave_fields', ())
- for slave in slaves:
+ for slave in self.getSlaves(field):
if slave['name'] != slaveid:
continue
action = slave.get('action')
if action not in ['vocabulary', 'value']:
raise ValueError('Invalid master-slave action')
- kw = { slave['control_param']: value }
- result = getattr(self.context, slave['vocab_method'])(**kw)
+ result = self.getVocabulary(slave, value)
if action == 'value':
return json.dumps(translate(result, self.request))
View
2 Products/MasterSelectWidget/profiles/default/metadata.xml
@@ -1,4 +1,4 @@
<?xml version="1.0"?>
<metadata>
- <version>1.0</version>
+ <version>0.4</version>
</metadata>

0 comments on commit 485c706

Please sign in to comment.