Skip to content

Commit

Permalink
feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
rv0lt committed Oct 20, 2023
1 parent 68d317c commit f83a1cf
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 49 deletions.
13 changes: 9 additions & 4 deletions dds_cli/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1247,20 +1247,25 @@ def delete_project(click_ctx, project: str):
@project_status.command(name="extend", no_args_is_help=True)
# Options
@project_option(required=True)
@click.option(
"--new_deadline",
required=False,
type=int,
help="Number of days to extend the deadline.",
)
@click.pass_obj
def extend_deadline(click_ctx, project: str):
def extend_deadline(click_ctx, project: str, new_deadline: int):
"""Extend a project deadline by an specified number of days.
This operation has the same effect as expiring and re-releasing a project
Therefore it consumes one of the possible expiring times.
It consumes one of allowed times to renew data access.
"""
try:
with dds_cli.project_status.ProjectStatusManager(
project=project,
no_prompt=click_ctx.get("NO_PROMPT", False),
token_path=click_ctx.get("TOKEN_PATH"),
) as updater:
updater.extend_deadline()
updater.extend_deadline(new_deadline=new_deadline)
except (
dds_cli.exceptions.APIError,
dds_cli.exceptions.AuthenticationError,
Expand Down
57 changes: 29 additions & 28 deletions dds_cli/project_status.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ def update_status(self, new_status, deadline=None, is_aborted=False, no_mail=Fal

dds_cli.utils.console.print(f"Project {response_json.get('message')}")

def extend_deadline(self):
def extend_deadline(self, new_deadline=None):
"""Extend the project deadline."""
# Define initial parameters
extra_params = {"send_email": False}
Expand Down Expand Up @@ -184,7 +184,7 @@ def extend_deadline(self):
# Extract default unit days and current deadline
default_unit_days = response_json.get("default_unit_days")
current_deadline = response_json.get("project_status").get("current_deadline")

project_id = response_json.get("project_info").get("Project ID")
# print information about the project status and table with the project info
print_info = (
f"\nCurrent deadline: [b][green]{current_deadline}[/green][/b]\n"
Expand All @@ -194,47 +194,46 @@ def extend_deadline(self):
dds_cli.utils.console.print(table)
dds_cli.utils.console.print(print_info)

# First question, number of days to extend the deadline
prompt_question = (
f"Enter the number of days you want to extend the project, "
f"the number of days has to be equal or same as "
f"[b][green]{default_unit_days}[/green][/b].\n"
f"Or leave it empty to apply the default "
f"[b][green]{default_unit_days} days [/green][/b]"
)

dds_cli.utils.console.print(prompt_question)
extend_deadline = rich.prompt.Prompt.ask("-")
if not extend_deadline:
# Set extend_deadline to default
extend_deadline = default_unit_days
# If it wasnt provided during the command click, ask the user for the new deadline
if not new_deadline:
# Question number of days to extend the deadline
prompt_question = (
f"How many days would you like to extend the project deadline with? "
f"Leave empty in order to choose the default ([b][green]{default_unit_days}[/green][/b])."
)
new_deadline = rich.prompt.Prompt.ask(prompt_question)
if not new_deadline:
# Set new_deadline to default
new_deadline = default_unit_days
try:
# the input was an string --> convert to integer
extend_deadline = int(extend_deadline)
if extend_deadline > default_unit_days:
raise DDSCLIException(
"\n[b][red]The number of days has to be lower than or equal to your unit's default: {default_unit_days}[/b][/red]\n"
new_deadline = int(new_deadline)
if new_deadline > default_unit_days:
raise exceptions.DDSCLIException(
f"\n[b][red]The number of days has to be lower than or equal to your unit's default: {default_unit_days}[/b][/red]\n"
)
except ValueError:
raise DDSCLIException(
raise exceptions.DDSCLIException(
"\n[b][red]Invalid value. Remember to enter a digit (not letters) when being asked for the number of days.[/b][/red]\n"
)

# Second question, confirm operation
# Confirm operation question
from dateutil.parser import parse

new_deadline_date = str(parse(current_deadline) + datetime.timedelta(days=new_deadline))
prompt_question = (
f"\nThe new deadline for project {project_id} will be: [b][blue]{new_deadline_date}[/b][/blue]"
f"\n\n[b][blue]Are you sure [/b][/blue]you want to perform this operation?. "
f"\nThis will extend the deadline by [b][blue]{extend_deadline} days[/b][/blue]."
"\nYou can only extend the data availability a maximum of "
"[b][blue]3 times[/b][/blue], this consumes one of those times."
)

dds_cli.utils.console.print(prompt_question)
if not rich.prompt.Confirm.ask("-"):
if not rich.prompt.Confirm.ask(prompt_question):
LOG.info("Probably for the best. Exiting.")
sys.exit(0)

# Update parameters for the second request
extra_params = {**extra_params, "confirmed": True, "new_deadline_in": extend_deadline}
extra_params = {**extra_params, "confirmed": True, "new_deadline_in": new_deadline}

response_json, _ = dds_cli.utils.perform_request(
endpoint=DDSEndpoint.UPDATE_PROJ_STATUS,
Expand All @@ -245,8 +244,10 @@ def extend_deadline(self):
)
message = response_json.get("message")
if not message:
raise DDSCLIException("No message returned from API. Cannot verify extension of project deadline.")

raise exceptions.DDSCLIException(
"No message returned from API. Cannot verify extension of project deadline."
)

LOG.info(message)


Expand Down
33 changes: 16 additions & 17 deletions tests/test_project_status.py
Original file line number Diff line number Diff line change
Expand Up @@ -387,19 +387,16 @@ def test_extend_deadline_too_many_number_of_days(
)

# capture system exit on not accepting operation
with pytest.raises(SystemExit):
with pytest.raises(DDSCLIException) as err:
with project_status.ProjectStatusManager(
project=project_name, no_prompt=True, authenticate=False
) as status_mngr:
status_mngr.token = {} # required, otherwise none
status_mngr.extend_deadline()

captured_output = capsys.readouterr()
assert (
"The number of days has to be lower than the default deadline extension number"
in captured_output.out
)
check_output_extend_deadline(captured_output=captured_output, caplog_tuples=None)
assert "The number of days has to be lower" in str(err.value)


def test_extend_deadline_wrong_number_of_days(
Expand All @@ -425,16 +422,16 @@ def test_extend_deadline_wrong_number_of_days(
)

# capture system exit on not accepting operation
with pytest.raises(SystemExit):
with pytest.raises(DDSCLIException) as err:
with project_status.ProjectStatusManager(
project=project_name, no_prompt=True, authenticate=False
) as status_mngr:
status_mngr.token = {} # required, otherwise none
status_mngr.extend_deadline()

captured_output = capsys.readouterr()
assert "Remember to write the number of days using numbers" in captured_output.out
check_output_extend_deadline(captured_output=captured_output, caplog_tuples=None)
assert "Remember to enter a digit (not letters)" in str(err.value)


def test_extend_deadline_confirmed_ok(
Expand All @@ -443,6 +440,7 @@ def test_extend_deadline_confirmed_ok(
"""test that the operation is performed - ok"""

confirmed = True
caplog.set_level(logging.INFO)

# Create mocker
with Mocker() as mock:
Expand All @@ -468,11 +466,11 @@ def test_extend_deadline_confirmed_ok(
status_mngr.extend_deadline()

captured_output = capsys.readouterr()
assert returned_response_extend_deadline_ok["message"] in captured_output.out
assert "This will extend the deadline by" in captured_output.out
assert f"{days_to_extend}" in captured_output.out
assert "You can only extend the data availability a maximum of" in captured_output.out

assert (
"dds_cli.project_status",
logging.INFO,
returned_response_extend_deadline_ok["message"],
) in caplog.record_tuples
check_output_extend_deadline(captured_output=captured_output, caplog_tuples=None)


Expand All @@ -482,7 +480,7 @@ def test_extend_deadline_confirmed_ok_default_days(
"""test that the operation is performed when the default days to extend is used"""

confirmed = True

caplog.set_level(logging.INFO)
# Create mocker
with Mocker() as mock:
# set confirmation object to true
Expand All @@ -506,9 +504,10 @@ def test_extend_deadline_confirmed_ok_default_days(
status_mngr.extend_deadline()

captured_output = capsys.readouterr()
assert returned_response_extend_deadline_ok["message"] in captured_output.out
assert "This will extend the deadline by" in captured_output.out
assert f"{default_unit_days}" in captured_output.out
assert "You can only extend the data availability a maximum of" in captured_output.out
assert (
"dds_cli.project_status",
logging.INFO,
returned_response_extend_deadline_ok["message"],
) in caplog.record_tuples

check_output_extend_deadline(captured_output=captured_output, caplog_tuples=None)

0 comments on commit f83a1cf

Please sign in to comment.