From 3c88411ba2106e38516132dd1ca01bc52ff4a1b5 Mon Sep 17 00:00:00 2001 From: shadowgate15 Date: Sun, 22 Mar 2020 14:31:48 -0500 Subject: [PATCH] Change iteration of tasks Originally, tasks were iterated through to get children first then project level items. This created an issue where task items would set next label even though the project method shouldn't have even started that task. Now, tasks are iterated through to get project level items and based on if the project level item has the next label it will move the next label accordingly. Also, made a change to the Procfile so no cacheing occurs. --- Procfile | 2 +- nextaction.py | 83 ++++++++++++++++++--------------------------------- 2 files changed, 30 insertions(+), 55 deletions(-) diff --git a/Procfile b/Procfile index 7fb6eb5..4e43088 100644 --- a/Procfile +++ b/Procfile @@ -1 +1 @@ -nextaction: python nextaction.py -a $TODOIST_API_KEY -l $TODOIST_NEXT_ACTION_LABEL $DEBUG \ No newline at end of file +nextaction: python nextaction.py -a $TODOIST_API_KEY -l $TODOIST_NEXT_ACTION_LABEL $DEBUG --nocache \ No newline at end of file diff --git a/nextaction.py b/nextaction.py index 69a3571..52a9e81 100755 --- a/nextaction.py +++ b/nextaction.py @@ -12,29 +12,6 @@ from datetime import datetime CHECKED = 1 -def get_subitems(items, parent_item=None): - """Search a flat item list for child items.""" - result_items = [] - found = False - if parent_item: - required_parent_id = parent_item['parent_id'] + 1 - else: - required_parent_id = 1 - for item in items: - if parent_item: - if not found and item['id'] != parent_item['id']: - continue - else: - found = True - if item['parent_id'] == parent_item['parent_id'] and item['id'] != parent_item['id']: - return result_items - elif item['parent_id'] == required_parent_id and found: - result_items.append(item) - elif item['parent_id'] == required_parent_id: - result_items.append(item) - return result_items - - def main(): """Main process function.""" parser = argparse.ArgumentParser() @@ -57,7 +34,6 @@ def main(): log_level = logging.DEBUG else: log_level = logging.INFO - # TODO add info log every x amount of time logging.basicConfig(level=log_level) # Check we have a API key @@ -133,6 +109,8 @@ def main(): # Get all items for the project items = sorted(api.items.all(lambda x: x['project_id'] == project['id']), key=lambda x: x['child_order']) + # filter for completable items + items = list(filter(lambda x: not x['content'].startswith('*'), items)) first_found = False @@ -146,45 +124,42 @@ def main(): remove_label(item, label_id) continue + if item['parent_id'] is None: + if project_type == 'serial': + if not first_found: + add_label(item, label_id) + first_found = True + else: + remove_label(item, label_id) + elif project_type == 'parallel': + add_label(item, label_id) + item_type = get_item_type(item) - # child_items = get_subitems(items, item) - child_items = sorted(list(filter(lambda x: x['parent_id'] == item['id'], items)), - key=lambda x: x['child_order']) + child_items = list(filter(lambda x: x['parent_id'] == item['id'], items)) if item_type: logging.debug('Identified \'%s\' as %s type', item['content'], item_type) if item_type or len(child_items) > 0: - # Process serial tagged items - if item_type == 'serial': - child_first_found = False - for child_item in child_items: - if child_item['checked'] == 0 and not child_first_found: - if not child_item['content'].startswith('*'): + if label_id in item['labels']: + # Process serial tagged items + if item_type == 'serial': + child_first_found = False + for child_item in child_items: + if child_item['checked'] == 0 and not child_first_found: add_label(child_item, label_id) child_first_found = True - else: - remove_label(child_item, label_id) - # Process parallel tagged items or untagged parents - else: - for child_item in child_items: - if not child_item['content'].startswith('*'): + else: + remove_label(child_item, label_id) + # Process parallel tagged items or untagged parents + elif item_type == 'parallel': + for child_item in child_items: add_label(child_item, label_id) - # Remove the label from the parent - remove_label(item, label_id) - - # Process items(w/ project as parent and no children) per project type - else: - if item['parent_id'] is None: - if project_type == 'serial': - if not first_found and not item['content'].startswith('*'): - add_label(item, label_id) - first_found = True - else: - remove_label(item, label_id) - elif project_type == 'parallel': - if not item['content'].startswith('*'): - add_label(item, label_id) + # Remove the label from the parent + remove_label(item, label_id) + else: + for child_item in child_items: + remove_label(child_item, label_id) if len(api.queue): logging.debug('%d changes queued for sync... commiting to Todoist.', len(api.queue))