-
Notifications
You must be signed in to change notification settings - Fork 24
Portal Dashboards, PF5 #504
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
Draft
mshriver
wants to merge
4
commits into
ibutsu:main
Choose a base branch
from
mshriver:satellite-dashboard-PF5
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.
Draft
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
151 changes: 151 additions & 0 deletions
151
backend/ibutsu_server/controllers/admin/portal_controller.py
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,151 @@ | ||
| from http import HTTPStatus | ||
|
|
||
| import connexion | ||
| from flask import abort | ||
|
|
||
| from ibutsu_server.constants import RESPONSE_JSON_REQ | ||
| from ibutsu_server.db.base import session | ||
| from ibutsu_server.db.models import Portal, User | ||
| from ibutsu_server.filters import convert_filter | ||
| from ibutsu_server.util.admin import check_user_is_admin | ||
| from ibutsu_server.util.query import get_offset | ||
| from ibutsu_server.util.uuid import convert_objectid_to_uuid, is_uuid, validate_uuid | ||
|
|
||
|
|
||
| def admin_add_portal(portal=None, token_info=None, user=None) -> tuple[dict, int]: | ||
| """Create a portal | ||
|
|
||
| :param body: Portal | ||
| :type body: dict | bytes | ||
|
|
||
| :rtype: Portal | ||
| """ | ||
| check_user_is_admin(user) | ||
| if not connexion.request.is_json: | ||
| return RESPONSE_JSON_REQ | ||
| portal = Portal.from_dict(**connexion.request.get_json()) | ||
| # check if portal already exists | ||
| if portal.id and Portal.query.get(portal.id): | ||
| return f"Portal id {portal.id} already exist", HTTPStatus.BAD_REQUEST | ||
| if user := User.query.get(user): | ||
| portal.owner = user | ||
| session.add(portal) | ||
| session.commit() | ||
| return portal.to_dict(), HTTPStatus.CREATED | ||
|
|
||
|
|
||
| @validate_uuid | ||
| def admin_get_portal(id_, token_info=None, user=None) -> dict: | ||
| """Get a single portal by ID | ||
|
|
||
| :param id: ID of test portal | ||
| :type id: str | ||
|
|
||
| :rtype: Portal | ||
| """ | ||
| check_user_is_admin(user) | ||
|
|
||
| # get by ID or check if the portal name matches the passed ID | ||
| if portal := Portal.query.get(id_) or Portal.query.filter(Portal.name == id_).first(): | ||
| return portal.to_dict(with_owner=True) | ||
| else: | ||
| abort(HTTPStatus.NOT_FOUND) | ||
|
|
||
|
|
||
| def admin_get_portal_list( | ||
| filter_=None, | ||
| owner_id=None, | ||
| group_id=None, | ||
| page=1, | ||
| page_size=25, | ||
| token_info=None, | ||
| user=None, | ||
| ) -> dict[list[dict], dict]: | ||
| """Get a list of portals | ||
|
|
||
| :param owner_id: Filter portals by owner ID | ||
| :type owner_id: str | ||
| :param group_id: Filter portals by group ID | ||
| :type group_id: str | ||
| :param limit: Limit the portals | ||
| :type limit: int | ||
| :param offset: Offset the portals | ||
| :type offset: int | ||
|
|
||
| :rtype: List[Portal] | ||
| """ | ||
| check_user_is_admin(user) | ||
| query = Portal.query | ||
|
|
||
| if filter_: | ||
| for filter_string in filter_: | ||
| filter_clause = convert_filter(filter_string, Portal) | ||
| if filter_clause is not None: | ||
| query = query.filter(filter_clause) | ||
| if owner_id: | ||
| query = query.filter(Portal.owner_id == owner_id) | ||
| if group_id: | ||
| query = query.filter(Portal.group_id == group_id) | ||
|
|
||
| offset = get_offset(page, page_size) | ||
| total_items = query.count() | ||
| total_pages = (total_items // page_size) + (1 if total_items % page_size > 0 else 0) | ||
| if offset > 9223372036854775807: # max value of bigint | ||
| return "The page number is too big.", HTTPStatus.BAD_REQUEST | ||
| portals = query.offset(offset).limit(page_size).all() | ||
| return { | ||
| "portals": [portal.to_dict(with_owner=True) for portal in portals], | ||
| "pagination": { | ||
| "page": page, | ||
| "pageSize": page_size, | ||
| "totalItems": total_items, | ||
| "totalPages": total_pages, | ||
| }, | ||
| } | ||
|
|
||
|
|
||
| @validate_uuid | ||
| def admin_update_portal(id_, portal=None, body=None, token_info=None, user=None): | ||
| """Update a portal | ||
|
|
||
| :param id: ID of portal | ||
| :type id: str | ||
| :param body: Portal | ||
| :type body: dict | bytes | ||
|
|
||
| :rtype: Portal | ||
| """ | ||
| check_user_is_admin(user) | ||
| if not connexion.request.is_json: | ||
| return RESPONSE_JSON_REQ | ||
| if not is_uuid(id_): | ||
| id_ = convert_objectid_to_uuid(id_) | ||
|
|
||
| if portal := Portal.query.get(id_): | ||
| # Grab the fields from the request | ||
| portal_dict = connexion.request.get_json() | ||
|
|
||
| # If the "owner" field is set, ignore it | ||
| portal_dict.pop("owner", None) | ||
|
|
||
| # update the portal info | ||
| portal.update(portal_dict) | ||
| session.add(portal) | ||
| session.commit() | ||
vcwild marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return portal.to_dict() | ||
| else: | ||
| abort(HTTPStatus.NOT_FOUND) | ||
|
|
||
|
|
||
| @validate_uuid | ||
| def admin_delete_portal(id_, token_info=None, user=None): | ||
| """Delete a single portal""" | ||
| check_user_is_admin(user) | ||
| if not is_uuid(id_): | ||
| return f"Portal ID {id_} is not in UUID format", HTTPStatus.BAD_REQUEST | ||
| if portal := Portal.query.get(id_): | ||
| session.delete(portal) | ||
| session.commit() | ||
| return HTTPStatus.OK.phrase, HTTPStatus.OK | ||
| else: | ||
| abort(HTTPStatus.NOT_FOUND) | ||
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
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.