Skip to content
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

Add FastAPI routes for tours #11089

Merged
merged 15 commits into from
Jan 13, 2021
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions config/plugins/tours/core.history.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
id: galaxy_history
jdavcs marked this conversation as resolved.
Show resolved Hide resolved
name: History Introduction
description: A detailed introduction to the Galaxy History
title_default: "Galaxy History Introduction"
Expand Down
3 changes: 2 additions & 1 deletion config/plugins/tours/core.scratchbook.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
id: galaxy_scratchbook
name: Scratchbook - Introduction
title_default: "Scratchbook Introduction"
description: "An introduction on how to display multiple datasets and visualizations next to each other."
title_default: "Scratchbook Introduction"
tags:
- "core"
- "UI"
Expand Down
37 changes: 34 additions & 3 deletions lib/galaxy/schema/__init__.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,44 @@
import typing
from typing import (
Dict,
List,
Optional,
)

from pydantic import BaseModel


class BootstrapAdminUser(BaseModel):
id = 0
email: typing.Optional[str] = None
preferences: typing.Dict[str, str] = {}
email: Optional[str] = None
preferences: Dict[str, str] = {}
bootstrap_admin_user = True

def all_roles(*args) -> list:
return []


class Tour(BaseModel):
jdavcs marked this conversation as resolved.
Show resolved Hide resolved
jdavcs marked this conversation as resolved.
Show resolved Hide resolved
id: str
name: str
description: str
tags: List[str]
jdavcs marked this conversation as resolved.
Show resolved Hide resolved


class TourList(BaseModel):
__root__: List[Tour] = []


class TourStep(BaseModel):
title: Optional[str] = None
jdavcs marked this conversation as resolved.
Show resolved Hide resolved
content: Optional[str] = None
element: Optional[str] = None
placement: Optional[str] = None
preclick: Optional[list] = None
postclick: Optional[list] = None
textinsert: Optional[str] = None
backdrop: Optional[bool] = None


class TourDetails(Tour):
title_default: Optional[str] = None
steps: List[TourStep]
29 changes: 18 additions & 11 deletions lib/galaxy/tours/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@
import os

import yaml
from pydantic import parse_obj_as

from galaxy import util
from galaxy.schema import TourList


log = logging.getLogger(__name__)


def tour_loader(contents_dict):
def load_steps(contents_dict):
# Some of this can be done on the clientside. Maybe even should?
title_default = contents_dict.get('title_default', None)
for step in contents_dict['steps']:
Expand All @@ -23,20 +26,24 @@ def tour_loader(contents_dict):
step['orphan'] = True
if title_default and 'title' not in step:
step['title'] = title_default
return contents_dict


class ToursRegistry:
def __init__(self, tour_directories):
self.tour_directories = util.config_directories_from_setting(tour_directories)
self.load_tours()

def tours_by_id_with_description(self):
return [{'id': k,
'description': self.tours[k].get('description', None),
'name': self.tours[k].get('name', None),
'tags': self.tours[k].get('tags', None)}
for k in self.tours.keys()]
def tours_by_id_with_description(self) -> TourList:
tours = []
for k in self.tours.keys():
tourdata = {
'id': k,
'description': self.tours[k].get('description'),
'name': self.tours[k].get('name'),
'tags': self.tours[k].get('tags')
}
tours.append(tourdata)
return parse_obj_as(TourList, tours)
jdavcs marked this conversation as resolved.
Show resolved Hide resolved

def load_tour(self, tour_id):
for tour_dir in self.tour_directories:
Expand Down Expand Up @@ -73,9 +80,9 @@ def _load_tour_from_path(self, tour_path):
tour_id = os.path.splitext(filename)[0]
try:
with open(tour_path) as handle:
conf = yaml.safe_load(handle)
tour = tour_loader(conf)
self.tours[tour_id] = tour_loader(conf)
tour = yaml.safe_load(handle)
load_steps(tour)
self.tours[tour_id] = tour
log.info("Loaded tour '%s'" % tour_id)
return tour
except OSError:
Expand Down
36 changes: 36 additions & 0 deletions lib/galaxy/webapps/galaxy/api/tours.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,52 @@
"""
import logging

from fastapi import Depends
from fastapi_utils.cbv import cbv
from fastapi_utils.inferring_router import InferringRouter as APIRouter

from galaxy.app import UniverseApplication
from galaxy.schema import (
TourDetails,
TourList,
)
from galaxy.web import (
expose_api_anonymous_and_sessionless,
legacy_expose_api,
require_admin
)
from galaxy.webapps.base.controller import BaseAPIController
from . import (
get_admin_user,
get_app,
)

log = logging.getLogger(__name__)


router = APIRouter(tags=['tours'])


@cbv(router)
class FastAPITours:
app: UniverseApplication = Depends(get_app)
jdavcs marked this conversation as resolved.
Show resolved Hide resolved

@router.get('/api/tours')
def index(self) -> TourList:
"""Return list of available tours."""
return self.app.tour_registry.tours_by_id_with_description()

@router.get('/api/tours/{tour_id}')
def show(self, tour_id: str) -> TourDetails:
"""Return a tour definition."""
return self.app.tour_registry.tour_contents(tour_id)

@router.post('/api/tours/{tour_id}', dependencies=[Depends(get_admin_user)])
def update_tour(self, tour_id: str) -> TourDetails:
"""Return a tour definition."""
return self.app.tour_registry.load_tour(tour_id)


class ToursController(BaseAPIController):

def __init__(self, app):
Expand Down
10 changes: 8 additions & 2 deletions lib/galaxy/webapps/galaxy/fast_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
"name": "licenses",
"description": "Operations with [SPDX licenses](https://spdx.org/licenses/).",
},
{
"name": "tours",
"description": "Operations with interactive tours.",
},
]


Expand Down Expand Up @@ -49,12 +53,14 @@ def initialize_fast_app(gx_app):
from galaxy.webapps.galaxy.api import (
job_lock,
jobs,
licenses,
roles,
licenses
tours,
)
app.include_router(jobs.router)
app.include_router(job_lock.router)
app.include_router(roles.router)
app.include_router(licenses.router)
app.include_router(roles.router)
app.include_router(tours.router)
app.mount('/', wsgi_handler)
return app
11 changes: 11 additions & 0 deletions lib/galaxy_test/api/test_tours.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,20 @@ def test_index(self):
tours = response.json()
tour_keys = map(lambda t: t["id"], tours)
assert "core.history" in tour_keys
for tour in tours:
self._assert_has_keys(tour, "id", "name", "description", "tags")

def test_show(self):
response = self._get("tours/core.history")
self._assert_status_code_is(response, 200)
tour = response.json()
self._assert_tour(tour)

def test_update(self):
response = self._post("tours/core.history", admin=True)
self._assert_status_code_is(response, 200)
tour = response.json()
self._assert_tour(tour)

def _assert_tour(self, tour):
self._assert_has_keys(tour, "name", "description", "title_default", "steps")