Skip to content

Commit

Permalink
fix wrong paths in explorer app
Browse files Browse the repository at this point in the history
  • Loading branch information
mmaelicke committed Jan 9, 2025
1 parent 2e938ea commit f128b4e
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 18 deletions.
8 changes: 4 additions & 4 deletions metacatalog_api/apps/explorer/create.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

@create_router.get('/create/entries.html')
def new_entry_page(request: Request):
return templates.TemplateResponse(request=request, name="add_entry.html", context={"path": server.uri_prefix})
return templates.TemplateResponse(request=request, name="add_entry.html", context={"path": server.app_prefix})


@create_router.get("/utils/leaflet_draw.html")
Expand All @@ -23,16 +23,16 @@ def leaflet_draw(request: Request, geom: str = 'marker'):

@create_router.get('/create/authors.html')
def new_author(request: Request):
return templates.TemplateResponse(request=request, name="author.html", context={"path": server.uri_prefix})
return templates.TemplateResponse(request=request, name="author.html", context={"path": server.app_prefix})


@create_router.get('/create/details.html')
def new_details(request: Request):
return templates.TemplateResponse(request=request, name="details.html", context={"path": server.uri_prefix})
return templates.TemplateResponse(request=request, name="details.html", context={"path": server.app_prefix})


@create_router.get('/create/datasources.html')
def new_datasource(request: Request):
# load the datasource types
types = core.datatypes()
return templates.TemplateResponse(request=request, name="add_datasource.html", context={"types": types, "path": server.uri_prefix})
return templates.TemplateResponse(request=request, name="add_datasource.html", context={"types": types, "path": server.app_prefix})
18 changes: 9 additions & 9 deletions metacatalog_api/apps/explorer/read.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
templates = Jinja2Templates(directory=Path(__file__).parent / 'templates')

# add static files
explorer_router.mount("/static", StaticFiles(directory=Path(__file__).parent / "templates" / "static"), name="static")
# explorer_router.mount("/static", StaticFiles(directory=Path(__file__).parent / "templates" / "static"), name="static")


@explorer_router.get('/entries.html')
Expand All @@ -26,13 +26,13 @@ def get_entries_page(request: Request, offset: int = 0, limit: int = 100, search
entries = core.entries(offset, limit, search=search, full_text=full_text, title=title, variable=variable)

# check if we should return html
return templates.TemplateResponse(request=request, name="entries.html", context={"entries": entries, "path": server.uri_prefix})
return templates.TemplateResponse(request=request, name="entries.html", context={"entries": entries, "path": server.app_prefix})


@explorer_router.get('/locations.html')
def get_entries_geojson_page(request: Request):
# check if we should return html
return templates.TemplateResponse(request=request, name="map.html", context={"path": server.uri_prefix})
return templates.TemplateResponse(request=request, name="map.html", context={"path": server.app_prefix})


@explorer_router.get('/entries/{id}.html')
Expand All @@ -44,7 +44,7 @@ def get_entry_page(id: int, request: Request):
raise HTTPException(status_code=404, detail=f"Entry of <ID={id}> not found")

# check if we should return html
return templates.TemplateResponse(request=request, name="entry.html", context={"entry": entries[0], "path": server.uri_prefix})
return templates.TemplateResponse(request=request, name="entry.html", context={"entry": entries[0], "path": server.app_prefix})


@explorer_router.get('/entries/{id}.xml')
Expand All @@ -56,7 +56,7 @@ def get_entry_radar_xml(id: int, request: Request):
if len(entries) == 0:
raise HTTPException(status_code=404, detail=f"Entry of <ID={id}> not found")

return templates.TemplateResponse(request=request, name="entry.radar.xml", context={"entry": entries[0], "path": server.uri_prefix}, media_type='application/xml')
return templates.TemplateResponse(request=request, name="entry.radar.xml", context={"entry": entries[0], "path": server.app_prefix}, media_type='application/xml')


@explorer_router.get('/licenses.html')
Expand All @@ -71,7 +71,7 @@ def get_licenses_page(request: Request, license_id: int | None = None):
if license_id is not None:
return templates.TemplateResponse(request=request, name="license.html", context={"license": licenses.model_dump()})
else:
return templates.TemplateResponse(request=request, name="licenses.html", context={"licenses": licenses, "path": server.uri_prefix})
return templates.TemplateResponse(request=request, name="licenses.html", context={"licenses": licenses, "path": server.app_prefix})


@explorer_router.get('/authors.html')
Expand All @@ -87,13 +87,13 @@ def get_authors_page(request: Request, entry_id: int | None = None, author_id: i
return templates.TemplateResponse(
request=request,
name="author.html",
context={"author": authors, 'variant': 'fixed', 'target': target, "path": server.uri_prefix}
context={"author": authors, 'variant': 'fixed', 'target': target, "path": server.app_prefix}
)

return templates.TemplateResponse(
request=request,
name="authors.html",
context={"authors": authors, 'variant': 'select' if entry_id is None else 'list', 'target': target, "path": server.uri_prefix}
context={"authors": authors, 'variant': 'select' if entry_id is None else 'list', 'target': target, "path": server.app_prefix, "root_path": server.uri_prefix}
)

@explorer_router.get('/variables')
Expand All @@ -107,5 +107,5 @@ def get_variables_page(request: Request, offset: int = None, limit: int = None):
return templates.TemplateResponse(
request=request,
name="variables.html",
context={"variables": variables, "path": server.uri_prefix}
context={"variables": variables, "path": server.app_prefix}
)
2 changes: 1 addition & 1 deletion metacatalog_api/apps/explorer/templates/authors.html
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ <h3>Debug Info:</h3>

async fetchExistingAuthors() {
try {
const response = await fetch('{{ path }}authors.json');
const response = await fetch('{{ root_path }}authors.json');
const data = await response.json();
this.existingAuthors = data;
} catch (err) {
Expand Down
5 changes: 4 additions & 1 deletion metacatalog_api/default_server.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from fastapi import Request
from fastapi.staticfiles import StaticFiles
from pathlib import Path
from starlette.middleware.cors import CORSMiddleware

from metacatalog_api.server import app, server
Expand Down Expand Up @@ -27,14 +29,15 @@ def index(request: Request):
This example app includes the explorer read and create routes
which are powered by the api route
"""
return templates.TemplateResponse(request=request, name="index.html", context={"path": server.uri_prefix})
return templates.TemplateResponse(request=request, name="index.html", context={"path": server.app_prefix})


# add all api routes - currently this is only splitted into read and create
app.include_router(api_read_router)
app.include_router(api_create_router)

# add the default explorer application (the HTML)
app.mount(f"{server.app_prefix}static", StaticFiles(directory=Path(__file__).parent / "apps" / "explorer" / "templates" / "static"), name="static")
app.include_router(explorer_router, prefix=f"/{server.app_name}")
app.include_router(explorer_create, prefix=f"/{server.app_name}")

Expand Down
4 changes: 2 additions & 2 deletions metacatalog_api/router/api/read.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@ def get_variables(only_available: bool = False, offset: int = None, limit: int =

return variables

@read_router.get('/variable/{id}')
@read_router.get('/variable/{id}.json')
@read_router.get('/variables/{id}')
@read_router.get('/variables/{id}.json')
def get_variable(id: int):
try:
variable = core.variables(id=id)
Expand Down
7 changes: 6 additions & 1 deletion metacatalog_api/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ def uri_prefix(self):

if not path.endswith('/'):
path += '/'
return path

@property
def app_prefix(self):
path = self.uri_prefix

if self.app_name.startswith('/'):
path += self.app_name.strip('/')
Expand All @@ -50,7 +55,7 @@ def cli_cmd(self, asgi_app: str):

# create the server object
server = Server()
logger.info(server.uri_prefix, server.root_path, server.app_name)
logger.info(server.app_prefix, server.root_path, server.app_name)


# before we initialize the app, we check that the database is installed and up to date
Expand Down

0 comments on commit f128b4e

Please sign in to comment.