def test_create_team_link(self): # The person picker widget shows a create team link. field = ITest['test_valid.item'] bound_field = field.bind(self.context) picker_widget = PersonPickerWidget(bound_field, self.vocabulary, self.request) picker_widget.show_create_team_link = True self.assertTrue(picker_widget.config['show_create_team'])
def test_widget_teamvalue_meta(self): # The person picker has the correct meta value for a team value. team = self.factory.makeTeam() bound_field = ITest['test_valid.item'].bind(team) person_picker_widget = PersonPickerWidget(bound_field, self.vocabulary, self.request) person_picker_widget.setRenderedValue(team) self.assertEqual( 'team', person_picker_widget.config['selected_value_metadata'])
def __init__(self, context, vocabulary, request): Widget.__init__(self, context, request) # This is a radio button widget so, since at least one radio # button will always be selected (and thus there will always # be input provided), we set required to False, to avoid # unnecessary 'required' UI connotations. # # See zope.formlib.interfaces.IInputWidget. self.required = False self.assignee_chooser_widget = PersonPickerWidget( context, context.vocabulary, request) self.setUpNames()
def test_widget_extra_buttons(self): # The picker widgets define defaults for the display of extra buttons. field = ITest['test_valid.item'] bound_field = field.bind(self.context) # A vocabulary widget does not show the extra buttons by default. picker_widget = VocabularyPickerWidget(bound_field, self.vocabulary, self.request) self.assertFalse(picker_widget.config['show_assign_me_button']) self.assertFalse(picker_widget.config['show_remove_button']) # A person picker widget does the assign button by default. person_picker_widget = PersonPickerWidget(bound_field, self.vocabulary, self.request) self.assertTrue(person_picker_widget.config['show_assign_me_button']) # But not the remove button. self.assertFalse(person_picker_widget.config['show_remove_button'])
class BugTaskAssigneeWidget(Widget): """A widget for setting the assignee on an IBugTask.""" implements(IInputWidget) __call__ = ViewPageTemplateFile("templates/bugtask-assignee-widget.pt") def __init__(self, context, vocabulary, request): Widget.__init__(self, context, request) # This is a radio button widget so, since at least one radio # button will always be selected (and thus there will always # be input provided), we set required to False, to avoid # unnecessary 'required' UI connotations. # # See zope.formlib.interfaces.IInputWidget. self.required = False self.assignee_chooser_widget = PersonPickerWidget( context, context.vocabulary, request) self.setUpNames() def setUpNames(self): """Set up the names used by this widget.""" self.assigned_to = "%s.assigned_to" % self.name self.assign_to_me = "%s.assign_to_me" % self.name self.assign_to_nobody = "%s.assign_to_nobody" % self.name self.assign_to = "%s.assign_to" % self.name self.assignee_chooser_widget.onKeyPress = ( "selectWidget('%s', event)" % self.assign_to) def setPrefix(self, prefix): Widget.setPrefix(self, prefix) self.assignee_chooser_widget.setPrefix(prefix) self.setUpNames() def validate(self): """ This method used to be part of zope.formlib.interfaces.IInputWidget in Zope 3.0, but is no longer part of the interface in Zope 3.2 """ # If the user has chosen to assign this bug to somebody else, # ensure that they actually provided a valid input value for # the assignee field. option = self.request.form_ng.getOne(self.name + ".option") if option == self.assign_to: if not self.assignee_chooser_widget.hasInput(): raise WidgetInputError( self.name, self.label, ValidationError("Missing value for assignee")) if not self.assignee_chooser_widget.hasValidInput(): raise WidgetInputError(self.name, self.label, ValidationError("Assignee not found")) def hasInput(self): """See zope.formlib.interfaces.IInputWidget.""" field_name = self.name + ".option" return field_name in self.request.form def hasValidInput(self): """See zope.formlib.interfaces.IInputWidget.""" try: self.validate() return True except InputErrors: return False def getInputValue(self): """See zope.formlib.interfaces.IInputWidget.""" self.validate() form = self.request.form_ng assignee_option = form.getOne(self.name + ".option") if assignee_option == self.assign_to: # The user has chosen to use the assignee chooser widget # to select an assignee. return self.assignee_chooser_widget.getInputValue() elif assignee_option == self.assign_to_me: # The user has choosen to 'take' this bug. return getUtility(ILaunchBag).user elif assignee_option == self.assigned_to: # This is effectively a no-op field = self.context bugtask = field.context return bugtask.assignee elif assignee_option == self.assign_to_nobody: return None raise WidgetInputError("Unknown assignee option chosen") def applyChanges(self, content): """See zope.formlib.interfaces.IInputWidget.""" assignee_field = self.context bugtask = assignee_field.context new_assignee = self.getInputValue() if bugtask.assignee != new_assignee: bugtask.transitionToAssignee(new_assignee) return True else: return False def assignedToCurrentUser(self): """Is this IBugTask assigned to the currently logged in user? Returns True if yes, otherwise False. """ current_user = getUtility(ILaunchBag).user if not current_user: return False field = self.context bugtask = field.context return current_user == bugtask.assignee def assignedToAnotherUser(self): """Is this IBugTask assigned to someone other than the current user? Returns True if yes, otherwise False. """ field = self.context bugtask = field.context if not bugtask.assignee: # This IBugTask is not yet assigned to anyone. return False current_user = getUtility(ILaunchBag).user return current_user != bugtask.assignee def getAssigneeDisplayValue(self): """Return a display value for current IBugTask.assignee. If no IBugTask.assignee, return None. If the assignee is not viewable, return u'<hidden>'. """ field = self.context bugtask = field.context if not bugtask.assignee: return None display_value = (TeamFormatterAPI( bugtask.assignee).unique_displayname(None)) return display_value def selectedRadioButton(self): """Return the radio button that should be selected. The return value will be one of: self.assigned_to self.assign_to_me self.assign_to_nobody self.assign_to """ # Give form values in the request precedence in deciding which # radio button should be selected. selected_option = self.request.form_ng.getOne(self.name + ".option") if selected_option: return selected_option # No value found in the request (e.g. the user might have just # clicked a link to arrive at this form), so let's figure out # which radio button makes sense to select. Note that # self.assign_to is no longer a possible return value, because # it doesn't make sense for this to be the selected radio # button when first entering the form. field = self.context bugtask = field.context assignee = bugtask.assignee if not assignee: return self.assign_to_nobody else: if assignee == getUtility(ILaunchBag).user: return self.assign_to_me else: return self.assigned_to def showUnassignOption(self): """Should the "unassign bugtask" option be shown? To avoid user confusion, we show this option only if the user can set the bug task assignee to None or if there is currently no assignee set. """ user = getUtility(ILaunchBag).user context = self.context.context return context.userCanUnassign(user) or context.assignee is None def showPersonChooserWidget(self): """Should the person chooser widget bw shown? The person chooser is shown only if the user can assign at least one other person or team in addition to himself. """ user = getUtility(ILaunchBag).user context = self.context.context return user is not None and (context.userCanSetAnyAssignee(user) or not user.teams_participated_in.is_empty())
class BugTaskAssigneeWidget(Widget): """A widget for setting the assignee on an IBugTask.""" implements(IInputWidget) __call__ = ViewPageTemplateFile( "templates/bugtask-assignee-widget.pt") def __init__(self, context, vocabulary, request): Widget.__init__(self, context, request) # This is a radio button widget so, since at least one radio # button will always be selected (and thus there will always # be input provided), we set required to False, to avoid # unnecessary 'required' UI connotations. # # See zope.formlib.interfaces.IInputWidget. self.required = False self.assignee_chooser_widget = PersonPickerWidget( context, context.vocabulary, request) self.setUpNames() def setUpNames(self): """Set up the names used by this widget.""" self.assigned_to = "%s.assigned_to" % self.name self.assign_to_me = "%s.assign_to_me" % self.name self.assign_to_nobody = "%s.assign_to_nobody" % self.name self.assign_to = "%s.assign_to" % self.name self.assignee_chooser_widget.onKeyPress = ( "selectWidget('%s', event)" % self.assign_to) def setPrefix(self, prefix): Widget.setPrefix(self, prefix) self.assignee_chooser_widget.setPrefix(prefix) self.setUpNames() def validate(self): """ This method used to be part of zope.formlib.interfaces.IInputWidget in Zope 3.0, but is no longer part of the interface in Zope 3.2 """ # If the user has chosen to assign this bug to somebody else, # ensure that they actually provided a valid input value for # the assignee field. option = self.request.form_ng.getOne(self.name + ".option") if option == self.assign_to: if not self.assignee_chooser_widget.hasInput(): raise WidgetInputError( self.name, self.label, ValidationError("Missing value for assignee")) if not self.assignee_chooser_widget.hasValidInput(): raise WidgetInputError( self.name, self.label, ValidationError("Assignee not found")) def hasInput(self): """See zope.formlib.interfaces.IInputWidget.""" field_name = self.name + ".option" return field_name in self.request.form def hasValidInput(self): """See zope.formlib.interfaces.IInputWidget.""" try: self.validate() return True except InputErrors: return False def getInputValue(self): """See zope.formlib.interfaces.IInputWidget.""" self.validate() form = self.request.form_ng assignee_option = form.getOne(self.name + ".option") if assignee_option == self.assign_to: # The user has chosen to use the assignee chooser widget # to select an assignee. return self.assignee_chooser_widget.getInputValue() elif assignee_option == self.assign_to_me: # The user has choosen to 'take' this bug. return getUtility(ILaunchBag).user elif assignee_option == self.assigned_to: # This is effectively a no-op field = self.context bugtask = field.context return bugtask.assignee elif assignee_option == self.assign_to_nobody: return None raise WidgetInputError("Unknown assignee option chosen") def applyChanges(self, content): """See zope.formlib.interfaces.IInputWidget.""" assignee_field = self.context bugtask = assignee_field.context new_assignee = self.getInputValue() if bugtask.assignee != new_assignee: bugtask.transitionToAssignee(new_assignee) return True else: return False def assignedToCurrentUser(self): """Is this IBugTask assigned to the currently logged in user? Returns True if yes, otherwise False. """ current_user = getUtility(ILaunchBag).user if not current_user: return False field = self.context bugtask = field.context return current_user == bugtask.assignee def assignedToAnotherUser(self): """Is this IBugTask assigned to someone other than the current user? Returns True if yes, otherwise False. """ field = self.context bugtask = field.context if not bugtask.assignee: # This IBugTask is not yet assigned to anyone. return False current_user = getUtility(ILaunchBag).user return current_user != bugtask.assignee def getAssigneeDisplayValue(self): """Return a display value for current IBugTask.assignee. If no IBugTask.assignee, return None. If the assignee is not viewable, return u'<hidden>'. """ field = self.context bugtask = field.context if not bugtask.assignee: return None display_value = ( TeamFormatterAPI(bugtask.assignee).unique_displayname(None)) return display_value def selectedRadioButton(self): """Return the radio button that should be selected. The return value will be one of: self.assigned_to self.assign_to_me self.assign_to_nobody self.assign_to """ # Give form values in the request precedence in deciding which # radio button should be selected. selected_option = self.request.form_ng.getOne(self.name + ".option") if selected_option: return selected_option # No value found in the request (e.g. the user might have just # clicked a link to arrive at this form), so let's figure out # which radio button makes sense to select. Note that # self.assign_to is no longer a possible return value, because # it doesn't make sense for this to be the selected radio # button when first entering the form. field = self.context bugtask = field.context assignee = bugtask.assignee if not assignee: return self.assign_to_nobody else: if assignee == getUtility(ILaunchBag).user: return self.assign_to_me else: return self.assigned_to def showUnassignOption(self): """Should the "unassign bugtask" option be shown? To avoid user confusion, we show this option only if the user can set the bug task assignee to None or if there is currently no assignee set. """ user = getUtility(ILaunchBag).user context = self.context.context return context.userCanUnassign(user) or context.assignee is None def showPersonChooserWidget(self): """Should the person chooser widget bw shown? The person chooser is shown only if the user can assign at least one other person or team in addition to himself. """ user = getUtility(ILaunchBag).user context = self.context.context return user is not None and ( context.userCanSetAnyAssignee(user) or not user.teams_participated_in.is_empty())