Skip to content

Commit

Permalink
Merge branch 'dev' into DDS-1701-new-release-for-cli
Browse files Browse the repository at this point in the history
  • Loading branch information
i-oden authored Oct 24, 2023
2 parents c907e48 + 932963d commit 431fe34
Show file tree
Hide file tree
Showing 5 changed files with 348 additions and 7 deletions.
3 changes: 2 additions & 1 deletion SPRINTLOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -305,5 +305,6 @@ _Nothing merged in CLI during this sprint_

# 2023-10-16 - 2023-10-27

- Change "Checksum verification successful. File integrity verified." logging level from INFO to DEBUG in order to not print for all files ([#662])(https://github.com/ScilifelabDataCentre/dds_cli/pull/661)
- Change "Checksum verification successful. File integrity verified." logging level from INFO to DEBUG in order to not print for all files ([#662](https://github.com/ScilifelabDataCentre/dds_cli/pull/662)
- New command `dds project status extend` to allow extension of project deadline ([#661](https://github.com/ScilifelabDataCentre/dds_cli/pull/661)
- New version: 2.5.2 ([#660](https://github.com/ScilifelabDataCentre/dds_cli/pull/660))
34 changes: 34 additions & 0 deletions dds_cli/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1243,6 +1243,40 @@ def delete_project(click_ctx, project: str):
sys.exit(1)


# -- dds project status extend -- #
@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, new_deadline: int):
"""Extend a project deadline by an specified number of days.
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(new_deadline=new_deadline)
except (
dds_cli.exceptions.APIError,
dds_cli.exceptions.AuthenticationError,
dds_cli.exceptions.DDSCLIException,
dds_cli.exceptions.ApiResponseError,
dds_cli.exceptions.ApiRequestError,
) as err:
LOG.error(err)
sys.exit(1)


# -- dds project status busy -- #
@project_status.command(name="busy", no_args_is_help=False)
# Flags
Expand Down
112 changes: 112 additions & 0 deletions dds_cli/project_status.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import logging
import typing
import sys
from dateutil.parser import parse

# Installed
import pytz
Expand Down Expand Up @@ -148,6 +149,117 @@ 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, new_deadline=None):
"""Extend the project deadline."""
# Define initial parameters
extra_params = {"send_email": False}

# Fetch project status and default deadline
response_json, _ = dds_cli.utils.perform_request(
endpoint=DDSEndpoint.UPDATE_PROJ_STATUS,
headers=self.token,
method="patch",
params={"project": self.project},
json=extra_params,
error_message="Failed to extend project deadline",
)

# Structure of the response:
# {
# 'default_unit_days': 30,
# 'project_info': {
# 'Created by': 'First Unit User',
# 'Description': 'This is a test project',
# 'Last updated': 'Wed, 18 Oct 2023 08:40:43 GMT',
# 'PI': '[email protected]',
# 'Project ID': 'project_1',
# 'Size': 0,
# 'Status': 'Available',
# 'Title': 'First Project'
# },
# 'project_status': {
# 'current_deadline': 'Sat, 04 Nov 2023 23:59:59 GMT',
# 'current_status': 'Available'},
# }

# Check that the returned information was ok
keys = ["project_info", "project_status", "default_unit_days"]
(
project_info,
project_status,
default_unit_days,
*_,
) = dds_cli.utils.get_required_in_response(keys=keys, response=response_json)

# Check and extract the required information for the operation
current_status, *_ = dds_cli.utils.get_required_in_response(
keys=["current_status"], response=project_status
)

# if the project is still in progress it won't have a current_deadline parameter
if not current_status == "Available":
raise exceptions.DDSCLIException(
"You can only extend the deadline for a project that has the status 'Available'."
)

current_deadline, *_ = dds_cli.utils.get_required_in_response(
keys=["current_deadline"], response=project_status
)
project_id, *_ = dds_cli.utils.get_required_in_response(
keys=["Project ID"], response=project_info
)

# print information about the project status and table with the project info
print_info = (
f"\nCurrent deadline: [b][green]{current_deadline}[/green][/b]\n"
f"Default deadline extension: [b][green]{default_unit_days}[/green][/b] days\n"
)
table = self.generate_project_table(project_info=project_info)
dds_cli.utils.console.print(table)
dds_cli.utils.console.print(print_info)

# 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 = (
"How many days would you like to extend the project deadline with? "
"Leave empty in order to choose the default"
)
new_deadline = rich.prompt.IntPrompt.ask(prompt_question, default=default_unit_days)

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

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": new_deadline}

response_json, _ = dds_cli.utils.perform_request(
endpoint=DDSEndpoint.UPDATE_PROJ_STATUS,
headers=self.token,
method="patch",
params={"project": self.project},
json=extra_params,
error_message="Failed to extend project deadline",
)
message = response_json.get("message")
if not message:
raise exceptions.DDSCLIException(
"No message returned from API. Cannot verify extension of project deadline."
)

LOG.info(message)


class ProjectBusyStatusManager(base.DDSBaseClass):
"""Project Busy Status manager class."""
Expand Down
2 changes: 2 additions & 0 deletions dds_cli/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,8 @@ def perform_request(
request_method = requests.post
elif method == "delete":
request_method = requests.delete
elif method == "patch":
request_method = requests.patch

def transform_paths(json_input):
"""Make paths serializable."""
Expand Down
Loading

0 comments on commit 431fe34

Please sign in to comment.