Sections refactoring completed. Next up, code-cleanup and testing

master
Hoffelhas 2020-12-28 23:18:54 +01:00
parent 3943c5c614
commit c50f62d946
1 changed files with 286 additions and 275 deletions

View File

@ -174,7 +174,6 @@ def main():
args.label, label_id)
else:
# Create a new label in Todoist
#TODO:
logging.info(
"\n\nLabel '{}' doesn't exist in your Todoist\n".format(args.label))
# sys.exit(1)
@ -274,33 +273,18 @@ def main():
return project_type, project_type_changed
def get_section_type(item_object, api):
def get_section_type(section_object):
"""Identifies how a section should be handled."""
if api is not None:
section = api.sections.all(lambda x: x['id'] == item_object['section_id'])
if section:
section_type, section_type_changed = get_type(section[0], 'section_type')
# Hier of later inbouwen?
# try:
# first_change = section['first_change']
# except:
# section['first_change'] = 0
else:
section_type = None
section_type_changed = 0
if section_type is not None:
print(section[0]['name'])
print(section_type)
if section_object is not None:
section_type, section_type_changed = get_type(
section_object, 'section_type')
else:
section_type = None
section_type_changed = 0
return section_type, section_type_changed
def get_item_type(item, project_type, api):
def get_item_type(item, project_type):
"""Identifies how an item with sub items should be handled."""
if project_type is None and item['parent_id'] != 0:
@ -313,14 +297,7 @@ def main():
else:
item_type, item_type_changed = get_type(item, 'item_type')
# If no type is found in parentless task name, try to find one in the section name.
if item_type is None:
section_type, section_type_changed = get_section_type(item, api)
else:
section_type = None
section_type_changed = 0
return item_type, item_type_changed, section_type, section_type_changed
return item_type, item_type_changed
def add_label(item, label):
if label not in item['labels']:
@ -353,6 +330,14 @@ def main():
labels = overview_item_labels[item_id]
api.items.update(item_id, labels=labels)
def create_none_section(): # TODO: actually only needs to be created once?
none_sec = {
'id': None,
'name': 'None',
'section_order': 0
}
return none_sec
# Check for updates
check_for_update(current_version)
@ -367,6 +352,9 @@ def main():
for project in api.projects.all():
if project['name'] == 'Test //':
print('here')
if label_id is not None:
# Get project type
project_type, project_type_changed = get_project_type(project)
@ -377,16 +365,33 @@ def main():
# items = api.items.all(
# lambda x: x['project_id'] == project['id'])
sections = api.sections.all(lambda x: x['project_id'] == project['id'])
section_ids = [x['id'] for x in sections]
section_ids.insert(0,None)
add_empty_section(sections) # Build class
# section_ids = [x['id'] for x in sections]
# section_ids.insert(0,None)
# sections.append(create_none_section()) # Add a None-section to handle items in a project that have no section, append is faster than insert(0,ind)
project_items = api.items.all(lambda x: x['project_id'] == project['id'])
# Run for both none-sectioned and sectioned items
for s in [0,1]:
if s == 0:
sections = [create_none_section()]
elif s == 1:
sections = api.sections.all(lambda x: x['project_id'] == project['id'])
for section in sections:
# Get section type
section_type, section_type_changed = get_section_type(
section)
logging.debug('Identified \'%s\' as %s type',
section['name'], section_type)
# Get all items for the section
items = api.items.all(lambda x: x['section_id'] == section['id'])
items = [x for x in project_items if x['section_id'] == section['id']]
# items = api.items.all(lambda x: x['section_id'] == section['id'])
# sections = [x['section_id'] for x in items] # better than api.sections.all(lambda x: x['project_id'] == project['id']), since then NoneTypes are not shown
# Change top parents_id in order to sort later on
@ -401,8 +406,8 @@ def main():
filter(lambda x: not x['content'].startswith('*'), items))
if label_id is not None:
# If project type has been changed, clean everything for good measure
if project_type_changed == 1:
# If some type has been changed, clean everything for good measure
if project_type_changed == 1 or section_type_changed == 1:
# Remove labels
[remove_label(item, label_id) for item in items]
# Remove parent types
@ -414,8 +419,9 @@ def main():
first_found_section = False
first_found_item = True
# For all items in this project
# For all items in this section
for item in items:
active_type = None # Reset
# Determine which child_items exist, both all and the ones that have not been checked yet
non_checked_items = list(
@ -531,49 +537,56 @@ def main():
continue
# Check item type
item_type, item_type_changed, section_type, section_type_changed = get_item_type(
item, project_type, api)
item_type, item_type_changed = get_item_type(
item, project_type)
logging.debug('Identified \'%s\' as %s type',
item['content'], item_type)
# If there is no item //TODO: or section type
if item_type is None:
# Handle parentless tasks
if item['parent_id'] == 0:
item_type = project_type
# Handle other tasks
else:
try:
if item['parent_type'] is None:
item_type = project_type
else:
item_type = item['parent_type']
except:
item_type = project_type
else:
# If there is no item
if item_type is not None:
# Reset in case that parentless task is tagged, overrules project
first_found_item = False
# Determine hierarchy types for logic
hierarchy_types = [item_type, section_type, project_type]
active_types = [type(x) != type(None) for x in hierarchy_types]
# If it is a parentless task
if item['parent_id'] == 0:
if active_types[0]:
# Do item types
active_type = item_type
add_label(item, label_id)
elif active_types[1]:
# Do section types
active_type = section_type
if section_type == 'sequential' or section_type == 's-p':
if not first_found_section:
add_label(item, label_id)
first_found_section = True
elif section_type == 'parallel' or section_type == 'p-s':
add_label(item, label_id)
elif active_types[2]:
# Do project types
active_type = project_type
if project_type == 'sequential' or project_type == 's-p':
if not first_found_project:
add_label(item, label_id)
first_found_project = True
elif not first_found_item and not project_type == 's-p':
add_label(item, label_id)
first_found_item = True
# elif not first_found_item and not project_type == 's-p':
# add_label(item, label_id)
# first_found_item = True
elif project_type == 'parallel' or project_type == 'p-s':
add_label(item, label_id)
if section_type:
add_label(item, label_id)
if item_type:
add_label(item, label_id)
# If there are children
if len(child_items) > 0:
# Check if item state has changed, if so clean children for good measure
@ -581,13 +594,11 @@ def main():
[remove_label(child_item, label_id)
for child_item in child_items]
#TODO: How to add if SECTION is sequential?
# Process sequential tagged items (item_type can overrule project_type)
if item_type == 'sequential' or item_type == 'p-s':
if active_type == 'sequential' or active_type == 'p-s':
for child_item in child_items:
# Pass item_type down to the children
child_item['parent_type'] = item_type
child_item['parent_type'] = active_type
# Pass label down to the first child
if child_item['checked'] == 0 and label_id in item['labels']:
add_label(child_item, label_id)
@ -597,10 +608,10 @@ def main():
remove_label(child_item, label_id)
# Process parallel tagged items or untagged parents
elif item_type == 'parallel' or (item_type == 's-p' and label_id in item['labels']):
elif active_type == 'parallel' or (active_type == 's-p' and label_id in item['labels']):
remove_label(item, label_id)
for child_item in child_items:
child_item['parent_type'] = item_type
child_item['parent_type'] = active_type
if child_item['checked'] == 0:
# child_first_found = True
add_label(child_item, label_id)