fix: populate edit defaults with the existing data

This commit is contained in:
Johann Dreo 2023-07-28 23:08:39 +02:00
commit 22a8630a83
2 changed files with 23 additions and 8 deletions

View file

@ -1,5 +1,5 @@
"ID","STATUS","TITLE","DETAILS","TAGS","DEADLINE","TOUCHED"
0,"TODO","Use click-option-group","To help sort options in categories in help.","","","2023-07-28T12:04:02.615501"
0,"TODO","Use click-option-group","To help sort options in categories in help.","UX","","2023-07-28T23:07:24.677746"
1,"TODO","Use click-aliases","To allow for aliases (TBC: user-defined in config file?)","UX","","2023-07-28T17:10:35.635275"
2,"TODO","edit existing","When calling edit, populate defaults with existing data.","","","2023-07-28T12:07:08.177802"
2,"DONE","edit existing","When calling edit, populate defaults with existing data.","","","2023-07-28T23:08:18.324797"
3,"TODO","sanity checks","Check data consistency in load_data and save_data.","","","2023-07-28T12:08:10.272349"

1 ID STATUS TITLE DETAILS TAGS DEADLINE TOUCHED
2 0 TODO Use click-option-group To help sort options in categories in help. UX 2023-07-28T12:04:02.615501 2023-07-28T23:07:24.677746
3 1 TODO Use click-aliases To allow for aliases (TBC: user-defined in config file?) UX 2023-07-28T17:10:35.635275
4 2 TODO DONE edit existing When calling edit, populate defaults with existing data. 2023-07-28T12:07:08.177802 2023-07-28T23:08:18.324797
5 3 TODO sanity checks Check data consistency in load_data and save_data. 2023-07-28T12:08:10.272349

View file

@ -101,6 +101,7 @@ def check_id(context, param, value):
return value
# Global group holding global options.
@click.group(invoke_without_command=True)
# Core options.
@ -304,14 +305,28 @@ def add(context, title, status, details, tags, deadline):
context.invoke(show)
def default_from_existing(key):
class OptionDefaultFromContext(click.Option):
def get_default(self, context):
tid = context.params['tid']
df = load_data(context)
assert(tid in df.index)
row = df.loc[tid]
value = row[context.obj[key]]
if str(value) != "nan": # FIXME WTF?
self.default = value
else:
self.default = ""
return super(OptionDefaultFromContext, self).get_default(context)
return OptionDefaultFromContext
@cli.command()
@click.argument('TID', required=True, type=int, is_eager=True, callback=check_id)
@click.option('-t', '--title' , type=str, prompt=True)
@click.option('-s', '--status' , type=str, prompt=True)
@click.option('-d', '--details' , type=str, prompt=True, default="")
@click.option('-t', '--tags' , type=str, prompt=True, default="")
@click.option('-a', '--deadline', type=str, prompt=True, default="")
# FIXME populate the defaults with the existing data.
@click.option('-t', '--title' , type=str, prompt=True, cls = default_from_existing('title_key'))
@click.option('-s', '--status' , type=str, prompt=True, cls = default_from_existing('status_key'))
@click.option('-d', '--details' , type=str, prompt=True, cls = default_from_existing('details_key'))
@click.option('-t', '--tags' , type=str, prompt=True, cls = default_from_existing('tags_key'))
@click.option('-a', '--deadline', type=str, prompt=True, cls = default_from_existing('deadline_key'))
@click.pass_context
def edit(context, tid, title, status, details, tags, deadline):
"""Add a new task."""