def __call__(self, task: Task, client: Client) -> None: custom_field = find_by_name(task.custom_fields, self.custom_field_name) if custom_field is None: _logger.warning( f"{task} has no custom field '{self.custom_field_name}'") return new_enum_value = find_by_name(custom_field.enum_options or [], self.enum_value_name) if new_enum_value is None: _logger.warning( f"{custom_field} has no enum option '{self.enum_value_name}'") return None if new_enum_value != custom_field.enum_value: client.set_enum_custom_field(task, custom_field, new_enum_value)
def _get_attr(self, task: Task) -> Optional[str]: custom_field = find_by_name(task.custom_fields, self.custom_field_name) if custom_field is None: return "" value = custom_field.enum_value if value is None: return None return value.name
def get_current_stage( self, task: Task ) -> Union[Tuple[Optional[WorkflowStage], _EnumCustomFieldWorkflowGetStageContext], str]: custom_field = find_by_name(task.custom_fields, self._name) if custom_field is None or custom_field.enum_options is None: return f"Unable to find enum custom field '{self._name}'" context = _EnumCustomFieldWorkflowGetStageContext( custom_field=custom_field, enum_options=custom_field.enum_options) if custom_field.enum_value is None: stage = None else: stage_name = custom_field.enum_value.name stage = find_by_name(self._stages, stage_name) if stage is None: return f"Unable to find stage '{stage_name}'" return stage, context
def get_current_stage( self, task: Task ) -> Union[Tuple[Optional[WorkflowStage], _SectionWorkflowGetStageContext], str]: membership = first_or_none(m for m in task.memberships if m.project.name == self._project_name) if membership is None: return f"Unable to find membership in '{self._project_name}'" context = _SectionWorkflowGetStageContext(membership.project) return find_by_name(self._stages, membership.section.name), context
def get_current_stage( self, task: Task ) -> Tuple[Optional[WorkflowStage], _ExternalDataWorkflowGetStageContext]: external = task.external or External(None, {}) workflows = (external.data or {}).get("workflows", {}) stage_name = workflows.get(self._name) return ( find_by_name(self._stages, stage_name), _ExternalDataWorkflowGetStageContext(external, workflows), )
def can_set_stage( self, stage: WorkflowStage, client: Client, context: _SectionWorkflowGetStageContext, ) -> Union[_SectionWorkflowSetStageContext, str]: sections = client.sections_by_project(context.project) new_section = find_by_name(sections, stage.name) if new_section is None: return f"Unable to find section '{stage.name}' in '{self._project_name}'" return _SectionWorkflowSetStageContext(new_section)
def can_set_stage( self, stage: WorkflowStage, client: Client, context: _EnumCustomFieldWorkflowGetStageContext, ) -> Union[_EnumCustomFieldWorkflowSetStageContext, str]: enum_option = find_by_name(context.enum_options, stage.name) if enum_option is None: return f"Unable to find enum option '{stage.name}'" return _EnumCustomFieldWorkflowSetStageContext(context.custom_field, enum_option)
def order(self, section_name: str, by: Sorter) -> None: """Register that a given section should be sorted with a given sorter. :param section_name: The name of the section to sort. :param by: The sorter defining a sort order. """ section = find_by_name(self._client.sections_by_project(self.project), section_name) if section is None: _logger.warning(f"{self.project} has no section '{section_name}'") return if section in self._section_to_sorter: _logger.warning(f"Sorter already defined for {section}") return self._section_to_sorter[section] = by
def test_find_none(self) -> None: found = find_by_name(self.items, "Third") self.assertIsNone(found)
def test_find_some(self) -> None: found = find_by_name(self.items, "Second") self.assertIs(found, self.second)
def key(self, task: Task) -> Tuple[float, ...]: custom_field = find_by_name(task.custom_fields, self.custom_field_name) if custom_field is None or custom_field.number_value is None: return (math.inf, ) return (self.order * custom_field.number_value, )