class BaseOperation(Executable): def __init__(self, trello_element, previous_operation=None): self.custom_actions = CustomActions(self) self.trello_element = trello_element self.previous_operation = previous_operation self.after_init() def after_init(self): pass def items(self): self.set_collection() return self.custom_actions.encapsulate(self.collection.names()) def set_collection(self): self.collection = TrelloCollection(self.trello_element, self.trello_element_property()) def trello_element_property(self): return "" def trello_element_name(self): return self.__class__.__name__.replace("Operation", "") def callback(self, index): if self.custom_actions.has(index): self.custom_actions.call(index) else: self.command.defer(lambda: self.execute_command( index - self.custom_actions.len())) def get_name(self, label="Name"): self.command.input(label, self.deferred_add) def deferred_add(self, text=None): if text: self.command.defer(lambda: self.base_add(text)) def base_add(self, text): self.add(text) self.restart() def add(self, text): pass def restart(self): self.trello_element.reload() self.reexecute() def execute_command(self, index): if self.collection.has(index): Operation = self.next_operation_class() Operation(self.collection.find(index), self).execute(self.command) def next_operation_class(self): pass def open_in_browser(self): self.command.defer(super().open_in_browser)
class BaseOperation(Executable): def __init__(self, trello_element, previous_operation = None): self.custom_actions = CustomActions(self) self.trello_element = trello_element self.previous_operation = previous_operation self.after_init() def after_init(self): pass def items(self): self.set_collection() return self.custom_actions.encapsulate(self.collection.names()) def set_collection(self): self.collection = TrelloCollection(self.trello_element, self.trello_element_property()) def trello_element_property(self): return "" def trello_element_name(self): return self.__class__.__name__.replace("Operation", "") def callback(self, index): if self.custom_actions.has(index): self.custom_actions.call(index) else: self.command.defer(lambda: self.execute_command(index - self.custom_actions.len())) def get_name(self): self.command.input("Name", self.deferred_add) def deferred_add(self, text = None): if text: self.command.defer(lambda: self.base_add(text)) def base_add(self, text): self.add(text) self.restart() def add(self, text): pass def restart(self): self.trello_element.reload() self.reexecute() def execute_command(self, index): if self.collection.has(index): Operation = self.next_operation_class() Operation(self.collection.find(index), self).execute(self.command) def next_operation_class(self): pass def open_in_browser(self): self.command.defer(super().open_in_browser)
class TrelloCollectionTests(unittest.TestCase): def setUp(self): self.collection = TrelloElementMock.collection() self.trello_collection = TrelloCollection(self.collection) def test_it_filters_the_closed_elements(self): collection = TrelloElementMock.collection() collection[0].closed = True trello_collection = TrelloCollection(collection) self.assertEqual(len(trello_collection.elements), len(collection) - 1) def test_names_returns_every_name_from_the_collection(self): self.assertEqual(self.trello_collection.names(), ["first", "second"]) def test_find_gets_an_element_from_the_collection_by_index(self): self.assertEqual(self.trello_collection.find(1), self.collection[1]) def test_on_init_it_gets_the_property_from_the_trello_element(self): trello_element = TrelloElementMock("card") trello_element.collection = TrelloElementMock.collection() collection = TrelloCollection(trello_element, "collection") self.assertEqual(collection.elements, trello_element.collection)
class CardOptions(Executable): def __init__(self, card, previous_operation=None): self.options = [{ 'name': "..", 'action': self.go_back }, { 'name': "Open in browser", 'action': self.open_in_browser }, { 'name': "Insert link", 'action': self.insert_link }, { 'name': "Show", 'action': self.show, 'single_step': True }, { 'name': "Comments", 'action': self.comments, 'single_step': True }, { 'name': "Comment", 'action': self.comment }, { 'name': "Add label", 'action': self.set_label }, { 'name': "Remove label", 'action': self.clear_label }, { 'name': "Move to another list", 'action': self.move }, { 'name': "Archive", 'action': self.close, 'single_step': True }, { 'name': "Exit", 'action': self.noop }] self.trello_element = self.card = card self.previous_operation = previous_operation def items(self): return [option['name'] for option in self.options] def callback(self, index): option = self.options[index] if not option is None: self.command.defer(option['action']) if 'single_step' in option: self.reexecute() # Actions def show(self): self.command.output(Output.card(self.card)) def insert_link(self): self.command.insert_text(Output.link(self.card.short_url)) def comments(self): self.command.output(Output.comments(self.card.comments())) def comment(self): self.run_action_with_callback("Comment text", self.card.add_comment) def set_label(self): self.run_action_with_callback(self.label_input_text(), self.card.set_label) def clear_label(self): self.run_action_with_callback(self.label_input_text(), self.card.clear_label) def run_action_with_callback(self, input_text, callback): def action(text=None): if text is None: self.command.input(input_text, action) else: self.command.defer(lambda: callback(text)) self.card.reload() self.reexecute() action() def move(self, index=None): if index is None: self.list_collection = TrelloCollection(self.card.board, "lists") self.command.display(self.list_collection.names(), self.move) else: selected_list = self.list_collection.find(index) self.command.defer(lambda: self.card.move_to_list(selected_list)) selected_list.reload() self.reexecute() def close(self): self.card.close() self.card.list.reload() def noop(self): pass def label_input_text(self): valid_label_colors = [ 'green', 'yellow', 'orange', 'red', 'purple', 'blue' ] current_colors = [label['color'] for label in self.card.labels] available_choices = [ label + ("*" if label in current_colors else "") for label in valid_label_colors ] return "Colors (* is active): " + ", ".join(available_choices)
class CardOptions(Executable): def __init__(self, card, previous_operation = None): self.options = [ { 'name': "..", 'action': self.go_back }, { 'name': "Open in Browser", 'action': self.open_in_browser }, { 'name': "Show", 'action': self.show, 'single_step': True }, { 'name': "Comments", 'action': self.comments, 'single_step': True }, { 'name': "Comment", 'action': self.comment }, { 'name': "Add label", 'action': self.set_label }, { 'name': "Remove label", 'action': self.clear_label }, { 'name': "Move to another List", 'action': self.move }, { 'name': "Archive", 'action': self.close, 'single_step': True }, { 'name': "Exit", 'action': self.noop } ] self.trello_element = self.card = card self.previous_operation = previous_operation def items(self): return [option['name'] for option in self.options] def callback(self, index): option = self.options[index] if not option is None: self.command.defer(option['action']) if 'single_step' in option: self.reexecute() # Actions def show(self): self.command.output(Output.card(self.card)) def comments(self): self.command.output(Output.comments(self.card.comments())) def comment(self): self.run_action_with_callback("Comment text", self.card.add_comment) def set_label(self): self.run_action_with_callback(self.label_input_text(), self.card.set_label) def clear_label(self): self.run_action_with_callback(self.label_input_text(), self.card.clear_label) def run_action_with_callback(self, input_text, callback): def action(text = None): if text is None: self.command.input(input_text, action) else: self.command.defer(lambda: callback(text)) self.card.reload() self.reexecute() action() def move(self, index = None): if index is None: self.list_collection = TrelloCollection(self.card.board, "lists") self.command.display(self.list_collection.names(), self.move) else: selected_list = self.list_collection.find(index) self.command.defer(lambda: self.card.move_to_list(selected_list)) selected_list.reload() self.reexecute() def close(self): self.card.close() self.card.list.reload() def noop(self): pass def label_input_text(self): valid_label_colors = ['green', 'yellow', 'orange', 'red', 'purple', 'blue'] current_colors = [label['color'] for label in self.card.labels] available_choices = [label + ("*" if label in current_colors else "") for label in valid_label_colors] return "Colors (* is active): " + ", ".join(available_choices)