← Back to team overview

launchpad-reviewers team mailing list archive

[Merge] lp:~wgrant/launchpad/launchpadtargetwidget-prefix-respect into lp:launchpad

 

William Grant has proposed merging lp:~wgrant/launchpad/launchpadtargetwidget-prefix-respect into lp:launchpad.

Requested reviews:
  Launchpad code reviewers (launchpad-reviewers)

For more details, see:
https://code.launchpad.net/~wgrant/launchpad/launchpadtargetwidget-prefix-respect/+merge/69187

LaunchpadTargetWidget presently creates its subwidgets in __init__. This is OK for Answers, the sole current user, because it doesn't do anything special like set a custom prefix. But views that want a custom prefix set it after __init__, so it doesn't the subwidgets do not inherit it. In the case of BugTaskEditView, this means that the widgets for each task have conflicting names and IDs.
-- 
https://code.launchpad.net/~wgrant/launchpad/launchpadtargetwidget-prefix-respect/+merge/69187
Your team Launchpad code reviewers is requested to review the proposed merge of lp:~wgrant/launchpad/launchpadtargetwidget-prefix-respect into lp:launchpad.
=== modified file 'lib/lp/app/widgets/doc/launchpad-target-widget.txt'
--- lib/lp/app/widgets/doc/launchpad-target-widget.txt	2011-02-01 21:46:58 +0000
+++ lib/lp/app/widgets/doc/launchpad-target-widget.txt	2011-07-26 00:52:33 +0000
@@ -314,3 +314,17 @@
              id="field.bugtarget.option.product"
              name="field.bugtarget" type="radio" value="product" />
     ...
+
+
+== Custom prefixes ==
+
+Setting a prefix on the widget also applies to all the subordinate widgets.
+
+    >>> widget = LaunchpadTargetWidget(
+    ...     IFrontPageBugAddForm['bugtarget'], empty_request)
+    >>> widget.setPrefix("task")
+    >>> rendered = widget()
+    >>> 'field.bugtarget' in rendered
+    False
+    >>> 'task.bugtarget' in rendered
+    True

=== modified file 'lib/lp/app/widgets/launchpadtarget.py'
--- lib/lp/app/widgets/launchpadtarget.py	2011-06-10 20:23:19 +0000
+++ lib/lp/app/widgets/launchpadtarget.py	2011-07-26 00:52:33 +0000
@@ -48,12 +48,11 @@
 
     template = ViewPageTemplateFile('templates/launchpad-target.pt')
     default_option = "package"
+    _widgets_set_up = False
 
-    def __init__(self, field, request):
-        # Shut off the pylint warning about not calling __init__()
-        # on a Mixin class.
-        # pylint: disable-msg=W0231
-        BrowserWidget.__init__(self, field, request)
+    def setUpSubWidgets(self):
+        if self._widgets_set_up:
+            return
         fields = [
             Choice(
                 __name__='product', title=u'Project',
@@ -71,6 +70,7 @@
         for field in fields:
             setUpWidget(
                 self, field.__name__, field, IInputWidget, prefix=self.name)
+        self._widgets_set_up = True
 
     def setUpOptions(self):
         """Set up options to be rendered."""
@@ -101,6 +101,7 @@
 
     def getInputValue(self):
         """See zope.app.form.interfaces.IInputWidget."""
+        self.setUpSubWidgets()
         form_value = self.request.form_ng.getOne(self.name)
         if form_value == 'product':
             try:
@@ -150,6 +151,7 @@
 
     def setRenderedValue(self, value):
         """See IWidget."""
+        self.setUpSubWidgets()
         if IProduct.providedBy(value):
             self.default_option = 'product'
             self.product_widget.setRenderedValue(value)
@@ -174,5 +176,6 @@
 
     def __call__(self):
         """See zope.app.form.interfaces.IBrowserWidget."""
+        self.setUpSubWidgets()
         self.setUpOptions()
         return self.template()