Skip to content

Commit

Permalink
add generic errors
Browse files Browse the repository at this point in the history
  • Loading branch information
sinisaos committed Apr 10, 2024
1 parent 1157c12 commit 0872415
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 12 deletions.
25 changes: 18 additions & 7 deletions admin_ui/src/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,8 @@ export default createStore({
if (axios.isAxiosError(error)) {
console.log(error.response)
context.commit("updateApiResponseMessage", {
contents: `Problem fetching ${tableName} rows.`,
contents: `Problem fetching ${tableName} rows.
${JSON.parse(JSON.stringify(error.response?.data.detail))}.`,
type: "error"
})
}
Expand Down Expand Up @@ -234,12 +235,22 @@ export default createStore({
return response
},
async fetchSchema(context, tableName: string) {
const response = await axios.get<i.Schema>(
`${BASE_URL}tables/${tableName}/schema/`
)
context.commit("updateSchema", response.data)

return response
try {
const response = await axios.get(
`${BASE_URL}tables/${tableName}/schema/`
)
context.commit("updateSchema", response.data)
return response
} catch (error) {
if (axios.isAxiosError(error)) {
console.log(error.response)
context.commit("updateApiResponseMessage", {
contents: `Problem fetching ${tableName} rows.
${JSON.parse(JSON.stringify(error.response?.data.detail))}.`,
type: "error"
})
}
}
},
async createRow(context, config: i.CreateRow) {
const response = await axios.post(
Expand Down
9 changes: 4 additions & 5 deletions piccolo_admin/endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
from piccolo_api.crud.endpoints import OrderBy, PiccoloCRUD
from piccolo_api.crud.hooks import Hook
from piccolo_api.crud.validators import Validators
from piccolo_api.csp.middleware import CSPMiddleware, CSPConfig
from piccolo_api.csp.middleware import CSPConfig, CSPMiddleware
from piccolo_api.csrf.middleware import CSRFMiddleware
from piccolo_api.fastapi.endpoints import FastAPIKwargs, FastAPIWrapper
from piccolo_api.media.base import MediaStorage
Expand Down Expand Up @@ -671,14 +671,13 @@ def __init__(
# We apply a restrictive CSP here to mitigate SVG
# files being used maliciously when viewed by admins
private_app.mount(
path=f"/media-files/{column._meta.table._meta.tablename}/{column._meta.name}/",
# noqa: E501
path=f"/media-files/{column._meta.table._meta.tablename}/{column._meta.name}/", # noqa: E501
app=CSPMiddleware(
StaticFiles(
directory=media_storage.media_path
),
config=CSPConfig(default_src="none")
)
config=CSPConfig(default_src="none"),
),
)

#######################################################################
Expand Down
13 changes: 13 additions & 0 deletions piccolo_admin/example.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import typing as t

import targ
from fastapi import HTTPException
from hypercorn.asyncio import serve
from hypercorn.config import Config
from piccolo.apps.user.tables import BaseUser
Expand Down Expand Up @@ -45,6 +46,8 @@
from piccolo.engine.postgres import PostgresEngine
from piccolo.engine.sqlite import SQLiteEngine
from piccolo.table import Table, create_db_tables_sync, drop_db_tables_sync
from piccolo_api.crud.endpoints import PiccoloCRUD
from piccolo_api.crud.validators import Validators
from piccolo_api.media.local import LocalMediaStorage
from piccolo_api.media.s3 import S3MediaStorage
from piccolo_api.session_auth.tables import SessionsBase
Expand Down Expand Up @@ -430,6 +433,15 @@ def booking_endpoint(request: Request, data: BookingModel) -> str:
return "Booking complete"


def validator_superuser(piccolo_crud: PiccoloCRUD, request: Request):
if not request.user.user.superuser:
raise HTTPException(
detail="Only a superuser can do this",
status_code=403,
headers={"Piccolo-Admin-Error": "Only a superuser can do this"},
)


TABLE_CLASSES: t.Tuple[t.Type[Table], ...] = (
Director,
Movie,
Expand Down Expand Up @@ -488,6 +500,7 @@ def booking_endpoint(request: Request, data: BookingModel) -> str:

director_config = TableConfig(
table_class=Director,
validators=Validators(every=[validator_superuser]),
visible_columns=[
Director._meta.primary_key,
Director.name,
Expand Down

0 comments on commit 0872415

Please sign in to comment.