IEEE.org     |     IEEE Xplore Digital Library     |     IEEE Standards     |     IEEE Spectrum     |     More Sites

Verified Commit 4ea9ec54 authored by Emi Simpson's avatar Emi Simpson
Browse files

[new arch] Even more comments for actions.py

parent 01c3ef6d
Pipeline #898 passed with stage
in 1 minute
......@@ -52,18 +52,26 @@ def delete_source(req: Request) -> Outcomes | Query[Outcomes, Outcomes]:
"""
Remove a source with the ID in `form[source_id]` from a project the active user owns
"""
# Check that the user is logged in
if req.user is not None:
try:
# If so, try building the query
return queries.MappedQuery(
# Delete the source
queries.DeleteSourceIfOwned(int(req.form['source_id']), req.user),
# If successful, rebuild projects.json
lambda _: (outcome.RebuildProjectsJson(),),
# Otherwise, just put out an error
lambda id: (outcome.Error(GenericAlert(), f"The source with id {id} doesn't exist, or you don't have permission to delete it"),)
)
except ValueError:
# If the `source_id` was malformed, do an error
return (outcome.Error(GenericAlert(), "Source ID is not in a valid integer format"),)
except KeyError:
# If the `source_id` wasn't present, that's also an error
return (outcome.Error(GenericAlert(), 'Must include a \"source_id\" to delete'),)
else:
# If the user wasn't logged in, send them to the login page
return (outcome.ForceLogin(),)
def add_source(req: Request, pid: UncheckedPID, user: UserID) -> Outcomes | Query[Outcomes, Outcomes]:
......@@ -311,34 +319,46 @@ def auth_action(action: AuthedProjectAction) -> Action:
This allows using these actions as standard actions
"""
# unauthed_action is the standard signature for actions
def unauthed_action(req: Request) -> Outcomes | Query[Outcomes, Outcomes]:
if req.user is None:
# If the user isn't logged in, send them to the login page
return force_login
else:
# Otherwise, the user is logged in, and we can try to detect what project
# they're talking about
project_id = try_get_pid(req)
if isinstance(project_id, outcome.Error):
# If that didn't work though, that's an error
return (project_id,)
else:
# Otherwise, we now know the project and the user, and can proceed
return action(req, project_id, req.user)
return unauthed_action
def try_get_pid(req: Request) -> outcome.Error | UncheckedPID:
"""
Attempt to determine the PID specified in a :class:`Request`
Attempt to determine the project ID specified in a :class:`Request`
"""
try:
# The happy path: Pull the ID out of the form, parse it, and send it on its way
return UncheckedPID(parse_alphaid(req.form['id']))
except MalformedId as e:
# if the ID was malformed, that's an error
return outcome.Error(GenericAlert(), str(e))
except KeyError:
# If the ID wasn't even specified, that's also an error
return outcome.Error(GenericAlert(), "No project id specified")
def validate_pid(project_id: UncheckedPID, user_id: UserID) -> Query[ProjectID, Tuple[outcome.Error]]:
"""
Shorthand for a query that validates the existance of a PID or produces an :class:`outcome.Error`
"""
return queries.MappedQuery(
# Check that we own the project
queries.ValidateProjectOwned(project_id, user_id),
lambda pid: pid,
lambda pid: pid, # Pass through the ID on a success
# But if we don't own it or it doesn't exist, stop execution with an error
lambda _: (outcome.Error(GenericAlert(), "Either you don't own this project, or it doesn't exist"
),)
)
......@@ -347,8 +367,10 @@ def validate_and_fetch_pid(project_id: UncheckedPID, user_id: UserID) -> Query[P
Equivilent to :meth:`validate_pid()`, but returns more information about the PID
"""
return queries.MappedQuery(
# Check that we own the project
queries.RetreiveProjectInfoIfOwned(project_id, user_id),
lambda info: info,
lambda info: info, # Pass through project info if we succeed
# Or else halt execution with an error
lambda _: (outcome.Error(GenericAlert(), "Either you don't own this project, or it doesn't exist"),))
#class RemoveOwner(Action): pass
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment