-
Notifications
You must be signed in to change notification settings - Fork 0
Change transfers status remotely for librarian self #139
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
Open
iparask
wants to merge
6
commits into
main
Choose a base branch
from
gp/feat/disable_transfers
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
040bb3e
WIP: change transfers status remotely for librarian self
iparask b261c7a
wip:endpoint
iparask 3723fc5
wip:correcting some requests
iparask 05abb46
WIP:adding CLI support
iparask f36d7da
fix: typo
iparask e25d522
first integration test for disabling transfers
iparask File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| """ | ||
| Pydantic modems for the admin endpoints | ||
| """ | ||
|
|
||
| from datetime import datetime | ||
| from typing import Optional | ||
|
|
||
| from pydantic import BaseModel, RootModel | ||
|
|
||
|
|
||
| class LibrarianTransfersStatusRequest(BaseModel): | ||
| """ | ||
| A request to report the transfer status of a librarian, either | ||
| to enable or disable outbound transfers. | ||
| """ | ||
| "The name of the librarian to change the transfer status of." | ||
| librarian_name: str | ||
|
|
||
| class LibrarianTransfersStatusResponse(BaseModel): | ||
| """ | ||
| A response to a user transfer status request. | ||
| """ | ||
| "The name of the librarian that was changed." | ||
| librarian_name: str | ||
|
|
||
| "Whether the librarian has outbound transfers enabled." | ||
| transfers_enabled: bool | ||
|
|
||
| class LibrarianTransfersUpdateRequest(BaseModel): | ||
| """ | ||
| A request to change the transfer status of a librarian, either | ||
| to enable or disable outbound transfers. | ||
| """ | ||
|
|
||
| "The name of the librarian to change the transfer status of." | ||
| librarian_name: str | ||
|
|
||
| "Whether to enable or disable outbound transfers." | ||
| transfers_enabled: bool | ||
|
|
||
| class LibrarianTransfersUpdateResponse(BaseModel): | ||
| """ | ||
| A response to a user change request. | ||
| """ | ||
|
|
||
| "The name of the librarian that was changed." | ||
| librarian_name: str | ||
|
|
||
| "Whether the librarian has outbound transfers enabled." | ||
| transfers_enabled: bool | ||
|
|
||
| class LibrarianTransfersFailedResponse(BaseModel): | ||
| reason: str | ||
| "The reason why the search failed." | ||
|
|
||
| suggested_remedy: str | ||
| "A suggested remedy for the failure." | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,108 @@ | ||
| """ | ||
|
Check failure on line 1 in librarian_server/api/transfers.py
|
||
| Check in and modify the states of source and destination | ||
| transfers. | ||
| """ | ||
|
|
||
| from fastapi import APIRouter, Depends, HTTPException, Response, status | ||
| from sqlalchemy import select | ||
| from sqlalchemy.orm import Session | ||
|
|
||
| from hera_librarian.models.status import ( | ||
| LibrarianTransfersStatusRequest, | ||
| LibrarianTransfersStatusResponse, | ||
| LibrarianTransfersUpdateRequest, | ||
| LibrarianTransfersUpdateResponse, | ||
| LibrarianTransfersFailedResponse, | ||
| ) | ||
|
|
||
| from ..orm.librarian import Librarian | ||
| from ..database import yield_session | ||
| from ..logger import log | ||
| from .auth import CallbackUserDependency | ||
|
|
||
| router = APIRouter(prefix="/api/v2/transfers") | ||
|
|
||
| @router.post("/status", response_model=LibrarianTransfersStatusResponse) | ||
| def update( | ||
| request: LibrarianTransfersStatusRequest, | ||
| response: Response, | ||
| user: CallbackUserDependency, | ||
| session: Session = Depends(yield_session), | ||
| ) -> LibrarianTransfersStatusResponse: | ||
| """ | ||
| Checkin and request the transfer status. | ||
| """ | ||
|
|
||
| log.debug(f"Recieved checkin transfer status request from {user.username}: {request}") | ||
iparask marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| librarian = ( | ||
| session.query(Librarian).filter_by(name=user.username).one_or_none() | ||
| ) | ||
|
|
||
| if librarian is None: | ||
| response.status_code = status.HTTP_400_BAD_REQUEST | ||
| return LibrarianTransfersFailedResponse( | ||
| reason=f"Librarian {request.librarian_name} does not exist", | ||
| suggested_remedy="Please verify that the requested librarian exists", | ||
| ) | ||
|
|
||
| if librarian.name != request.librarian_name: | ||
| response.status_code = status.HTTP_403_FORBIDDEN | ||
| return LibrarianTransfersFailedResponse( | ||
| reason="Cannot check the status of another librarian.", | ||
| suggested_remedy="Please verify that you are the librarian making this request", | ||
| ) | ||
|
|
||
| response = LibrarianTransfersStatusResponse( | ||
| librarian_name=librarian.name, | ||
| transfers_enabled=librarian.transfers_enabled, | ||
| ) | ||
|
|
||
| log.debug(f"Responding to checkin request with: {response}.") | ||
|
|
||
| return response | ||
|
|
||
|
|
||
| @router.post("/update", response_model=LibrarianTransfersUpdateResponse) | ||
| def update( | ||
| request: LibrarianTransfersUpdateRequest, | ||
| response: Response, | ||
| user: CallbackUserDependency, | ||
| session: Session = Depends(yield_session), | ||
| ) -> LibrarianTransfersUpdateResponse: | ||
| """ | ||
| Update the transfer status. | ||
| """ | ||
|
|
||
| log.debug(f"Recieved update transfer status request from {user.username}: {request}") | ||
iparask marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| librarian = ( | ||
| session.query(Librarian).filter_by(name=user.username).one_or_none() | ||
| ) | ||
|
|
||
| if librarian is None: | ||
| response.status_code = status.HTTP_400_BAD_REQUEST | ||
| return LibrarianTransfersFailedResponse( | ||
| reason=f"Librarian {request.librarian_name} does not exist", | ||
| suggested_remedy="Please verify that the requested librarian exists", | ||
| ) | ||
|
|
||
| if librarian.name != request.librarian_name: | ||
| response.status_code = status.HTTP_403_FORBIDDEN | ||
| return LibrarianTransfersFailedResponse( | ||
| reason="Cannot change the status of another librarian.", | ||
| suggested_remedy="Please verify that you are the librarian making this request", | ||
| ) | ||
|
|
||
| librarian.transfers_enabled = request.transfers_enabled | ||
|
|
||
| session.commit() | ||
|
|
||
| response = LibrarianTransfersUpdateResponse( | ||
| librarian_name=librarian.name, | ||
| transfers_enabled=librarian.transfers_enabled, | ||
| ) | ||
|
|
||
| log.debug(f"Responding to checkin request with: {response}.") | ||
iparask marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| return response | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.