From b18cd0fecb230e89814851d176faeb506479d051 Mon Sep 17 00:00:00 2001 From: Fabio Corneti Date: Sat, 12 Aug 2017 21:33:14 +0200 Subject: [PATCH 1/4] Added function to check if item is visible --- nextaction.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/nextaction.py b/nextaction.py index 0d96210..34bdf42 100755 --- a/nextaction.py +++ b/nextaction.py @@ -11,6 +11,14 @@ from datetime import datetime +def is_item_visible(item): + """Returns true if the item is visible.""" + for attr in ['is_deleted', 'is_archived', 'in_history', 'checked']: + if item[attr] == 1: + return False + return True + + def get_subitems(items, parent_item=None): """Search a flat item list for child items.""" result_items = [] @@ -131,7 +139,7 @@ def remove_label(item, label): # Get all items for the project, sort by the item_order field. items = sorted(api.items.all(lambda x: x['project_id'] == project['id']), key=lambda x: x['item_order']) - for item in items: + for item_index, item in enumerate(items): # If its too far in the future, remove the next_action tag and skip if args.hide_future > 0 and 'due_date_utc' in item.data and item['due_date_utc'] is not None: @@ -168,7 +176,7 @@ def remove_label(item, label): else: if item['indent'] == 1: if project_type == 'serial': - if item['item_order'] == 1: + if item_index == 0: add_label(item, label_id) else: remove_label(item, label_id) From 72e72a8fd9d43a029261cd36625cb1c44c92fb3c Mon Sep 17 00:00:00 2001 From: Fabio Corneti Date: Sat, 12 Aug 2017 22:34:42 +0200 Subject: [PATCH 2/4] Ignore invisible items when finding children --- nextaction.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/nextaction.py b/nextaction.py index 34bdf42..d35c1a8 100755 --- a/nextaction.py +++ b/nextaction.py @@ -28,6 +28,8 @@ def get_subitems(items, parent_item=None): else: required_indent = 1 for item in items: + if not is_item_visible(item): + continue if parent_item: if not found and item['id'] != parent_item['id']: continue From 68908826c205f8afd5841874d023155bb2bf14b7 Mon Sep 17 00:00:00 2001 From: Fabio Corneti Date: Sat, 12 Aug 2017 22:36:32 +0200 Subject: [PATCH 3/4] Ignore invisible items when looking for first item in serial projects --- nextaction.py | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/nextaction.py b/nextaction.py index d35c1a8..1c73188 100755 --- a/nextaction.py +++ b/nextaction.py @@ -141,7 +141,13 @@ def remove_label(item, label): # Get all items for the project, sort by the item_order field. items = sorted(api.items.all(lambda x: x['project_id'] == project['id']), key=lambda x: x['item_order']) - for item_index, item in enumerate(items): + # Tracks whether the first visible item at the root of the project has been found. + root_first_found = False + + for item in items: + + if not is_item_visible(item): + continue # If its too far in the future, remove the next_action tag and skip if args.hide_future > 0 and 'due_date_utc' in item.data and item['due_date_utc'] is not None: @@ -149,6 +155,7 @@ def remove_label(item, label): future_diff = (due_date - datetime.utcnow()).total_seconds() if future_diff >= (args.hide_future * 86400): remove_label(item, label_id) + root_first_found = True continue item_type = get_item_type(item) @@ -159,9 +166,9 @@ def remove_label(item, label): if item_type or len(child_items) > 0: # Process serial tagged items if item_type == 'serial': + first_found = False for child_item in child_items: - first_found = False - if child_item['checked'] == 0 and not first_found: + if is_item_visible(child_item) and not first_found: add_label(child_item, label_id) first_found = True else: @@ -173,13 +180,15 @@ def remove_label(item, label): # Remove the label from the parent remove_label(item, label_id) + root_first_found = True # Process items as per project type on indent 1 if untagged else: if item['indent'] == 1: if project_type == 'serial': - if item_index == 0: + if is_item_visible(item) and not root_first_found: add_label(item, label_id) + root_first_found = True else: remove_label(item, label_id) elif project_type == 'parallel': From 88ac79319ca5b75763b8c764b370b3b9af6e9c58 Mon Sep 17 00:00:00 2001 From: Fabio Corneti Date: Sat, 12 Aug 2017 23:32:38 +0200 Subject: [PATCH 4/4] Do not set next action in nested serial items if the project is serial and there's already a next action defined for it --- nextaction.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/nextaction.py b/nextaction.py index 1c73188..21333ba 100755 --- a/nextaction.py +++ b/nextaction.py @@ -143,6 +143,7 @@ def remove_label(item, label): # Tracks whether the first visible item at the root of the project has been found. root_first_found = False + project_has_next_action = False for item in items: @@ -164,12 +165,19 @@ def remove_label(item, label): logging.debug('Identified %s as %s type', item['content'], item_type) if item_type or len(child_items) > 0: + + # If the project is serial and there is a next action, + # remove the next_action from all children. + if project_type == 'serial' and project_has_next_action: + for child_item in child_items: + remove_label(child_item, label_id) # Process serial tagged items - if item_type == 'serial': + elif item_type == 'serial': first_found = False for child_item in child_items: if is_item_visible(child_item) and not first_found: add_label(child_item, label_id) + project_has_next_action = True first_found = True else: remove_label(child_item, label_id) @@ -189,6 +197,7 @@ def remove_label(item, label): if is_item_visible(item) and not root_first_found: add_label(item, label_id) root_first_found = True + project_has_next_action = True else: remove_label(item, label_id) elif project_type == 'parallel':