~launchpad-pqm/launchpad/devel

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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
# Copyright 2009-2011 Canonical Ltd.  This software is licensed under the
# GNU Affero General Public License version 3 (see the file LICENSE).

"""Widgets related to IBranch."""

__metaclass__ = type
__all__ = [
    'RecipeOwnerWidget',
    'TargetBranchWidget',
    ]


from datetime import (
    datetime,
    timedelta,
    )

from pytz import utc
from zope.app.form.browser.widget import renderElement
from zope.app.form.interfaces import (
    IInputWidget,
    InputErrors,
    )
from zope.app.form.utility import setUpWidget
from zope.component import (
    getMultiAdapter,
    getUtility,
    )
from zope.schema.vocabulary import (
    SimpleTerm,
    SimpleVocabulary,
    )

from canonical.launchpad.webapp import canonical_url
from canonical.launchpad.webapp.interfaces import ILaunchBag
from canonical.launchpad.webapp.menu import (
    escape,
    structured,
    )
from lp.app.widgets.itemswidgets import LaunchpadRadioWidget


class SuggestionWidget(LaunchpadRadioWidget):
    """Base class for widgets that suggest values.

    Users can pick a suggested value from a radio button list, or use the
    normal widget to pick any value they could normally pick.
    """

    def __init__(self, field, vocabulary, request):
        # Create the vocabulary and pass that to the radio button
        # constructor.
        self.suggestion_vocab = self._generateSuggestionVocab(
            field.context, vocabulary)

        LaunchpadRadioWidget.__init__(
            self, field, self.suggestion_vocab, request)

        self.other_selection_widget = getMultiAdapter(
            (field, request), IInputWidget)
        setUpWidget(
            self, 'other_selection', field, IInputWidget,
            prefix=self.name, context=field.context)

        # If there are suggestions to show explicitly, then we want to select
        # the 'Other' selection item when the user chooses a non-suggested
        # value.
        if self._shouldRenderSuggestions():
            self._autoselectOther()

    @classmethod
    def _generateSuggestionVocab(cls, context, full_vocabulary):
        """Generate a vocabulary for the suggestions.

        :param context: The context object to generate suggestions for.
        :param full_vocabulary: The vocabulary suggestions may be drawn from.
            suggestions not present in this vocabulary are ignored.
        """
        suggestions = cls._getSuggestions(context)
        terms = [
            term for term in full_vocabulary
            if term.value in suggestions]
        return SimpleVocabulary(terms)

    def _shouldRenderSuggestions(self):
        """Return True if suggestions should be rendered."""
        return len(self.suggestion_vocab) > 0

    def _optionId(self, index):
        return '%s.%d' % (escape(self.name), index)

    def _otherId(self):
        """Return the id of the "Other" option."""
        return self._optionId(len(self.suggestion_vocab))

    def _toFieldValue(self, form_value):
        """Convert the form value into the target value.

        If there were no radio button options, or 'other' was selected, then
        get the value from the other_selection widget, otherwise get the
        object reference from the built up vocabulary.
        """
        if not self._shouldRenderSuggestions() or form_value == "other":
            # Get the value from the other selector widget.
            try:
                return self.other_selection_widget.getInputValue()
            except InputErrors:
                self._error = self.other_selection_widget._error
                raise
        else:
            term = self.suggestion_vocab.getTermByToken(form_value)
            return term.value

    def hasInput(self):
        """Is there any input for the widget.

        We need to defer the call to the other widget when either there are no
        terms in the vocabulary or the other radio button was selected.
        """
        if not self._shouldRenderSuggestions():
            return self.other_selection_widget.hasInput()
        else:
            has_input = LaunchpadRadioWidget.hasInput(self)
            if has_input:
                if self._getFormInput() == "other":
                    return self.other_selection_widget.hasInput()
            return has_input

    def getInputValue(self):
        """Return the branch defined by the input value."""
        return self._toFieldValue(self._getFormInput())

    def setRenderedValue(self, value):
        """This widget does not support setting the value."""
        pass

    def _renderLabel(self, text, index):
        """Render a label for the option with the specified index."""
        label = structured(
            u'<label for="%s" style="font-weight: normal">%s</label>',
            self._optionId(index), text)
        return label

    def _renderSuggestionLabel(self, value, index):
        """Render a label for the option based on a branch."""
        return self._renderLabel(self._valueDisplayname(value), index)

    @staticmethod
    def _valueDisplayname(value):
        """Return the displayname for a value."""
        return value.displayname

    def renderItems(self, value):
        """Render the items for the selector."""
        field = self.context
        if value == self._missing:
            value = field.missing_value

        items = []
        index = 0
        # Render each of the suggestions with the first selected.
        for index, term in enumerate(self.suggestion_vocab):
            suggestion = term.value
            if index == 0:
                renderfunc = self.renderSelectedItem
            else:
                renderfunc = self.renderItem
            text = self._renderSuggestionLabel(suggestion, index)
            render_args = dict(
                index=index, text=text, value=term.token,
                name=self.name, cssClass=self.cssClass)
            items.append(renderfunc(**render_args))

        # Lastly render the other option.
        index = len(items)
        other_selection_text = "%s %s" % (
            escape(self._renderLabel("Other:", index)),
            self.other_selection_widget())
        other_selection_onclick = (
            "this.form['%s'].focus()" % self.other_selection_widget.name)

        elem = renderElement(u'input',
                             value="other",
                             name=self.name,
                             id='%s.%s' % (self.name, index),
                             cssClass=self.cssClass,
                             type='radio',
                             onClick=other_selection_onclick)

        other_radio_button = '%s&nbsp;%s' % (elem, other_selection_text)

        items.append(other_radio_button)

        return items

    def __call__(self):
        """Don't render the radio buttons if only one choice."""
        if not self._shouldRenderSuggestions():
            return self.other_selection_widget()
        else:
            return LaunchpadRadioWidget.__call__(self)


class TargetBranchWidget(SuggestionWidget):
    """Widget for selecting a target branch.

    The default branch for a new branch merge proposal should be
    the branch associated with the development focus series if there
    is one (that isn't an import branch).

    Also in the initial radio button selector are other branches for
    the product that the branch owner has specified as target branches
    for other merge proposals.

    Finally there is an "other" button that gets the user to use the
    normal branch selector.
    """

    @staticmethod
    def _generateSuggestionVocab(branch, full_vocabulary):
        """Generate the vocabulary for the radio buttons.

        The generated vocabulary contains the branch associated with the
        development series of the product if there is one, and also any other
        branches that the user has specified before as a target for a proposed
        merge.
        """
        default_target = branch.target.default_merge_target
        logged_in_user = getUtility(ILaunchBag).user
        since = datetime.now(utc) - timedelta(days=90)
        collection = branch.target.collection.targetedBy(logged_in_user,
            since)
        collection = collection.visibleByUser(logged_in_user)
        # May actually need some eager loading, but the API isn't fine grained
        # yet.
        branches = collection.getBranches(eager_load=False).config(distinct=True)
        target_branches = list(branches.config(limit=5))
        # If there is a development focus branch, make sure it is always
        # shown, and as the first item.
        if default_target is not None:
            if default_target in target_branches:
                target_branches.remove(default_target)
            target_branches.insert(0, default_target)

        # Make sure the source branch isn't in the target_branches.
        if branch in target_branches:
            target_branches.remove(branch)

        terms = []
        for branch in target_branches:
            terms.append(SimpleTerm(
                    branch, branch.unique_name))

        return SimpleVocabulary(terms)

    def _renderSuggestionLabel(self, branch, index):
        """Render a label for the option based on a branch."""
        # To aid usability there needs to be some text connected with the
        # radio buttons that is not a hyperlink in order to select the radio
        # button.  It was decided not to have the entire text as a link, but
        # instead to have a separate link to the branch details.
        text = '%s (<a href="%s">branch details</a>)' % (
            escape(branch.displayname), escape(canonical_url(branch)))
        # If the branch is the development focus, say so.
        if branch == self.context.context.target.default_merge_target:
            text = text + "&ndash; <em>development focus</em>"
        label = u'<label for="%s" style="font-weight: normal">%s</label>' % (
            self._optionId(index), text)
        return structured(label)

    def _autoselectOther(self):
        """Select "other" on keypress."""
        on_key_press = "selectWidget('%s', event);" % self._otherId()
        self.other_selection_widget.onKeyPress = on_key_press


class RecipeOwnerWidget(SuggestionWidget):
    """Widget for selecting a recipe owner.

    The current user and the base branch owner are suggested.
    """

    @staticmethod
    def _getSuggestions(branch):
        """Suggest the branch owner and current user."""
        logged_in_user = getUtility(ILaunchBag).user
        return set([branch.owner, logged_in_user])

    @staticmethod
    def _valueDisplayname(value):
        """Provide a specialized displayname for Persons"""
        return value.unique_displayname

    def _autoselectOther(self):
        """Select "other" on click."""
        on_click = "onClick=\"selectWidget('%s', event);\"" % self._otherId()
        self.other_selection_widget.extra = on_click