Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Edit date validation #203

Merged
merged 7 commits into from Jun 26, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions .gitignore
@@ -1,5 +1,5 @@
# editors / IDEs
.vscode
# editors and ides
.vscode/

# directories

Expand Down
60 changes: 42 additions & 18 deletions watson/cli.py
Expand Up @@ -852,27 +852,51 @@ def edit(watson, id):
data['stop'] = frame.stop.format(datetime_format)

text = json.dumps(data, indent=4, sort_keys=True, ensure_ascii=False)
output = click.edit(text, extension='.json')

if not output:
click.echo("No change made.")
return
start = None
stop = None

try:
data = json.loads(output)
project = data['project']
tags = data['tags']
start = arrow.get(data['start'], datetime_format).replace(
tzinfo=local_tz).to('utc')
stop = arrow.get(data['stop'], datetime_format).replace(
tzinfo=local_tz).to('utc') if id else None
except (ValueError, RuntimeError) as e:
raise click.ClickException("Error saving edited frame: {}".format(e))
except KeyError:
raise click.ClickException(
"The edited frame must contain the project, start and stop keys."
)
# enter into while loop until succesful and validated
# edit has been performed
while True:
output = click.edit(text, extension='.json')

if not output:
click.echo("No change made.")
return

try:
data = json.loads(output)
project = data['project']
tags = data['tags']
start = arrow.get(data['start'], datetime_format).replace(
tzinfo=local_tz).to('utc')
stop = arrow.get(data['stop'], datetime_format).replace(
tzinfo=local_tz).to('utc') if id else None
# if start time of the project is not before end time
# raise ValueException
if not watson.is_started and start > stop:
raise ValueError(
"Tasks start date needs to occur before end date.")
# break out of while loop and continue execution of
# the edit function normally
break
except (ValueError, RuntimeError) as e:
click.echo("Error while parsing inputted values: {}".format(e),
err=True)
except KeyError:
click.echo(
"The edited frame must contain the project, \
start and stop keys.", err=True)
# we reach here if exception was thrown, wait for user
# to acknowledge the error before looping in while and
# showing user the editor again
click.pause(err=True)
# use previous entered values to the user in editor
# instead of original ones
text = output

# we reach this when we break out of the while loop above
if id:
watson.frames[id] = (project, start, stop, tags)
else:
Expand Down