Initial update of check_header function. Still to finish and test

pull/30/head
Hoffelhas 2023-01-09 22:33:15 +01:00
parent 4aad462ce3
commit 619489a1d0
1 changed files with 97 additions and 88 deletions

View File

@ -150,10 +150,10 @@ def db_check_existance(connection, model):
if isinstance(model, Task): if isinstance(model, Task):
q_create = """ q_create = """
INSERT INTO INSERT INTO
tasks (task_id, task_type, parent_type, r_tag) tasks (task_id, task_type, parent_type, due_date, r_tag)
VALUES VALUES
(%r, %s, %s, %i); (%r, %s, %s, %s, %i);
""" % (model.id, 'NULL', 'NULL', 0) """ % (model.id, 'NULL', 'NULL', 'NULL', 0)
if isinstance(model, Section): if isinstance(model, Section):
q_create = """ q_create = """
@ -208,6 +208,7 @@ def initialise_sqlite():
task_id INTEGER, task_id INTEGER,
task_type TEXT, task_type TEXT,
parent_type TEXT, parent_type TEXT,
due_date TEXT,
r_tag INTEGER r_tag INTEGER
); );
""" """
@ -506,7 +507,7 @@ def get_type(args, connection, model, key):
elif isinstance(model, Project): elif isinstance(model, Project):
current_type = check_name(args, model.name, 3) # Projects current_type = check_name(args, model.name, 3) # Projects
# Check if project type changed with respect to previous run # Check if type changed with respect to previous run
if old_type == current_type: if old_type == current_type:
type_changed = 0 type_changed = 0
else: else:
@ -607,25 +608,36 @@ def update_labels(api, overview_task_ids, overview_task_labels):
# Check if header logic needs to be applied # Check if header logic needs to be applied
def check_header(level): def check_header(api, model):
header_all_in_level = False header_all_in_level = False
unheader_all_in_level = False unheader_all_in_level = False
method = 0 regex_a = '(^[*]{2}\s*)(.*)'
regex_b = '(^!\*\s*)(.*)'
if method == 1: if isinstance(model, Task):
if name[:3] == '** ': ra = re.search(regex_a, model.content)
rb = re.search(regex_b, model.content)
prefix_a = ra[0]
prefix_b = rb[0]
if prefix_a:
header_all_in_level = True header_all_in_level = True
level.update(name=name[3:]) api.update_task(task_id=model.id, content=ra[2])
if name[:3] == '!* ' or name[:3] == '_* ': if prefix_b:
unheader_all_in_level = True unheader_all_in_level = True
level.update(name=name[3:]) api.update_task(task_id=model.id, content=ra[2])
elif method == 2:
if content[:3] == '** ': # api.update_section(section_id="7025", name="Supermarket")
header_all_in_level = True # api.update_project(project_id="2203306141", name="Things To Buy")
level.update(content=content[3:])
if content[:3] == '!* ' or content[:3] == '_* ': # elif isinstance(model, Section) or isinstance(model, Project):
unheader_all_in_level = True # if name[:3] == '** ':
level.update(content=content[3:]) # header_all_in_level = True
# level.update(name=name[3:])
# if name[:3] == '!* ' or name[:3] == '_* ':
# unheader_all_in_level = True
# level.update(name=name[3:])
else: else:
pass pass
@ -687,43 +699,52 @@ def check_regen_mode(api, item, regen_labels_id):
# Recurring lists logic # Recurring lists logic
def run_recurring_lists_logic(args, api, item, child_items, child_items_all, regen_labels_id): def run_recurring_lists_logic(args, api,connection, task, task_items, task_items_all, regen_labels_id):
if item['parent_id'] == 0: if task.parent_id == 0:
try: try:
if item['due']['is_recurring']: if task.due.is_recurring:
try: try:
# Check if the T0 task date has changed db_task_due_date = db_read_value(connection, task, 'due_date')[0][0]
if item['due']['date'][:10] != item['date_old']:
# Mark children for action based on mode if db_task_due_date is None:
if args.regeneration is not None: # If date has never been saved before, create a new entry
logging.debug(
'New recurring task detected: %s' % task.content)
db_update_value(connection, task, 'due_date', task.due.date)
# Check if task has a regen label # Check if the T0 task date has changed, because a user has checked the task
regen_mode = check_regen_mode( if task.due.date != db_task_due_date:
api, item, regen_labels_id)
# If no label, use general mode instead #TODO: reevaluate regeneration mode. Disabled for now.
if regen_mode is None: # # Mark children for action based on mode
regen_mode = args.regeneration # if args.regeneration is not None:
logging.debug('Using general recurring mode \'%s\' for item: %s',
regen_mode, item.content)
else:
logging.debug('Using recurring label \'%s\' for item: %s',
regen_mode, item.content)
# Apply tags based on mode # # Check if task has a regen label
give_regen_tag = 0 # regen_mode = check_regen_mode(
# api, item, regen_labels_id)
if regen_mode == 1: # Regen all # # If no label, use general mode instead
give_regen_tag = 1 # if regen_mode is None:
elif regen_mode == 2: # Regen if all sub-tasks completed # regen_mode = args.regeneration
if not child_items: # logging.debug('Using general recurring mode \'%s\' for item: %s',
give_regen_tag = 1 # regen_mode, item.content)
# else:
# logging.debug('Using recurring label \'%s\' for item: %s',
# regen_mode, item.content)
if give_regen_tag == 1: # # Apply tags based on mode
for child_item in child_items_all: # give_regen_tag = 0
child_item['r_tag'] = 1
# if regen_mode == 1: # Regen all
# give_regen_tag = 1
# elif regen_mode == 2: # Regen if all sub-tasks completed
# if not child_items:
# give_regen_tag = 1
# if give_regen_tag == 1:
# for child_item in child_items_all:
# child_item['r_tag'] = 1
# If alternative end of day, fix due date if needed # If alternative end of day, fix due date if needed
if args.end is not None: if args.end is not None:
@ -735,10 +756,8 @@ def run_recurring_lists_logic(args, api, item, child_items, child_items_all, reg
if (args.end - current_hour) > 0: if (args.end - current_hour) > 0:
# Determine the difference in days set by todoist # Determine the difference in days set by todoist
nd = [ nd = [int(x) for x in task.due.date.split('-')]
int(x) for x in item['due']['date'][:10].split('-')] od = [int(x) for x in db_task_due_date.split('-')]
od = [
int(x) for x in item['date_old'][:10].split('-')]
new_date = datetime( new_date = datetime(
nd[0], nd[1], nd[2]) nd[0], nd[1], nd[2])
@ -755,49 +774,39 @@ def run_recurring_lists_logic(args, api, item, child_items, child_items_all, reg
if days_overdue >= 1 and days_difference == 1: if days_overdue >= 1 and days_difference == 1:
# Find current date in string format # Find current date in string format
today_str = [str(x) for x in [ today_str = t.strftime("%Y-%m-%d")
today.year, today.month, today.day]]
if len(today_str[1]) == 1:
today_str[1] = ''.join(
['0', today_str[1]])
# Update due-date to today # Update due-date to today
item_due = item['due'] api.update_task(task_id=task.id, due_date=today_str) #TODO: Apparently this breaks the reccuring string...
item_due['date'] = '-'.join( logging.info("Update date on task: '%s'" % (task.content))
today_str)
item.update(due=item_due)
# item.update(due={'date': '2020-05-29', 'is_recurring': True, 'string': 'every day'})
# Save the new date for reference us # Save the new date for reference us
item.update( db_update_value(connection, task, 'due_date', task.due.date)
date_old=item['due']['date'][:10])
except: except:
# If date has never been saved before, create a new entry # If date has never been saved before, create a new entry
logging.debug( logging.debug(
'New recurring task detected: %s' % item.content) 'New recurring task detected: %s' % task.content)
item['date_old'] = item['due']['date'][:10] db_update_value(connection, task, 'due_date', task.due.date)
api.items.update(item['id'])
except: except:
# logging.debug(
# 'Parent not recurring: %s' % item.content)
pass pass
#TODO: reevaluate regeneration mode. Disabled for now.
# if args.regeneration is not None and item.parent_id != 0:
# try:
# if item['r_tag'] == 1:
# item.update(checked=0)
# item.update(in_history=0)
# item['r_tag'] = 0
# api.items.update(item['id'])
if args.regeneration is not None and item['parent_id'] != 0: # for child_item in child_items_all:
try: # child_item['r_tag'] = 1
if item['r_tag'] == 1: # except:
item.update(checked=0) # # logging.debug('Child not recurring: %s' %
item.update(in_history=0) # # item.content)
item['r_tag'] = 0 # pass
api.items.update(item['id'])
for child_item in child_items_all:
child_item['r_tag'] = 1
except:
# logging.debug('Child not recurring: %s' %
# item.content)
pass
# Find and clean all children under a task # Find and clean all children under a task
@ -840,7 +849,7 @@ def autodoist_magic(args, api, connection):
db_check_existance(connection, project) db_check_existance(connection, project)
# Check if we need to (un)header entire project # Check if we need to (un)header entire project
header_all_in_p, unheader_all_in_p = check_header(project) header_all_in_p, unheader_all_in_p = check_header(api, project)
# Get project type # Get project type
if next_action_label is not None: if next_action_label is not None:
@ -896,7 +905,7 @@ def autodoist_magic(args, api, connection):
db_check_existance(connection, section) db_check_existance(connection, section)
# Check if we need to (un)header entire secion # Check if we need to (un)header entire secion
header_all_in_s, unheader_all_in_s = check_header(section) header_all_in_s, unheader_all_in_s = check_header(api, section)
# Get section type # Get section type
if next_action_label: if next_action_label:
@ -951,7 +960,7 @@ def autodoist_magic(args, api, connection):
filter(lambda x: x.parent_id == task.id, non_completed_tasks)) filter(lambda x: x.parent_id == task.id, non_completed_tasks))
# Check if we need to (un)header entire task tree # Check if we need to (un)header entire task tree
header_all_in_t, unheader_all_in_t = check_header(task) header_all_in_t, unheader_all_in_t = check_header(api, task)
# Modify headers where needed # Modify headers where needed
# TODO: DISABLED FOR NOW, FIX LATER # TODO: DISABLED FOR NOW, FIX LATER
@ -968,10 +977,10 @@ def autodoist_magic(args, api, connection):
# except: # except:
# pass # pass
# # If options turned on, start recurring lists logic # If options turned on, start recurring lists logic #TODO: regeneration currently doesn't work, becaue TASK_ENDPOINT doesn't show completed tasks. Use workaround.
# if args.regeneration is not None or args.end: if args.regeneration is not None or args.end:
# run_recurring_lists_logic( run_recurring_lists_logic(
# args, api, item, child_items, child_items_all, regen_labels_id) args, api, connection, task, child_tasks, child_tasks_all, regen_labels_id)
# If options turned on, start labelling logic # If options turned on, start labelling logic
if next_action_label is not None: if next_action_label is not None: