Skip to content

Commit

Permalink
Merge pull request #1331 from ScilifelabDataCentre/DDS-1404-project-i…
Browse files Browse the repository at this point in the history
…nfo-change-command

Add project info change command
  • Loading branch information
i-oden authored Dec 14, 2022
2 parents 72608ba + 809a0eb commit 170c3a0
Show file tree
Hide file tree
Showing 4 changed files with 441 additions and 110 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -181,3 +181,4 @@ Please add a _short_ line describing the PR you make, if the PR implements a spe
- Dependency: Bump `certifi` due to CVE-2022-23491 ([#1337](https://github.com/ScilifelabDataCentre/dds_web/pull/1337))
- Dependency: Bump `jwcrypto` due to CVE-2022-3102 ([#1339](https://github.com/ScilifelabDataCentre/dds_web/pull/1339))
- Cronjob: Get number of units and users for reporting ([#1324](https://github.com/ScilifelabDataCentre/dds_web/pull/1335))
- Add ability to change project information via ProjectInfo endpoint ([#1331](https://github.com/ScilifelabDataCentre/dds_web/pull/1331))
81 changes: 80 additions & 1 deletion dds_web/api/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -953,7 +953,7 @@ def put(self):


class ProjectInfo(flask_restful.Resource):
"""Get information for a specific project."""
"""Display and change Project information."""

@auth.login_required
@logging_bind_request
Expand All @@ -973,7 +973,86 @@ def get(self):
"Size": project.size,
"Title": project.title,
"Description": project.description,
"PI": project.pi,
}

return_info = {"project_info": project_info}
return return_info

@auth.login_required(role=["Unit Admin", "Unit Personnel", "Project Owner", "Researcher"])
@logging_bind_request
@dbsession
@json_required
def put(self):
"""Update Project information."""
# Get project ID, project and verify access
project_id = dds_web.utils.get_required_item(obj=flask.request.args, req="project")
project = dds_web.utils.collect_project(project_id=project_id)
dds_web.utils.verify_project_access(project=project)

# get the now info items
json_input = flask.request.json
new_title = json_input.get("title")
new_description = json_input.get("description")
new_pi = json_input.get("pi")

# if new title,validate title
if new_title:
title_validator = marshmallow.validate.And(
marshmallow.validate.Length(min=1),
dds_web.utils.contains_disallowed_characters,
error={
"required": {"message": "Title is required."},
"null": {"message": "Title is required."},
},
)
try:
title_validator(new_title)
except marshmallow.ValidationError as err:
raise DDSArgumentError(str(err))

# if new description,validate description
if new_description:
description_validator = marshmallow.validate.And(
marshmallow.validate.Length(min=1),
dds_web.utils.contains_unicode_emojis,
error={
"required": {"message": "A project description is required."},
"null": {"message": "A project description is required."},
},
)
try:
description_validator(new_description)
except marshmallow.ValidationError as err:
raise DDSArgumentError(str(err))

# if new PI,validate email address
if new_pi:
pi_validator = marshmallow.validate.Email(error="The PI email is invalid")
try:
pi_validator(new_pi)
except marshmallow.ValidationError as err:
raise DDSArgumentError(str(err))

# current date for date_updated
curr_date = dds_web.utils.current_time()

# update the items
if new_title:
project.title = new_title
if new_description:
project.description = new_description
if new_pi:
project.pi = new_pi
project.date_updated = curr_date
db.session.commit()

# return_message = {}
return_message = {
"message": f"{project.public_id} info was successfully updated.",
"title": project.title,
"description": project.description,
"pi": project.pi,
}

return return_message
Loading

0 comments on commit 170c3a0

Please sign in to comment.