mirror of
https://github.com/mealie-recipes/mealie.git
synced 2026-02-05 15:33:10 -05:00
feat: added "last-modified" header to supported record types (#1379)
* fixed type error * exposed created/updated timestamps to shopping list schema * added custom route to mix in "last-modified" header when available in CRUD routes * mixed in MealieCrudRoute to APIRouters * added HEAD route for shopping lists/list-items * replaced default serializer with FastAPI's
This commit is contained in:
@@ -1,6 +1,11 @@
|
||||
from typing import Optional
|
||||
import json
|
||||
from collections.abc import Callable
|
||||
from enum import Enum
|
||||
from json.decoder import JSONDecodeError
|
||||
from typing import Optional, Union
|
||||
|
||||
from fastapi import APIRouter, Depends
|
||||
from fastapi import APIRouter, Depends, Request, Response
|
||||
from fastapi.routing import APIRoute
|
||||
|
||||
from mealie.core.dependencies import get_admin_user, get_current_user
|
||||
|
||||
@@ -8,20 +13,34 @@ from mealie.core.dependencies import get_admin_user, get_current_user
|
||||
class AdminAPIRouter(APIRouter):
|
||||
"""Router for functions to be protected behind admin authentication"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
tags: Optional[list[str]] = None,
|
||||
prefix: str = "",
|
||||
):
|
||||
super().__init__(tags=tags, prefix=prefix, dependencies=[Depends(get_admin_user)])
|
||||
def __init__(self, tags: Optional[list[Union[str, Enum]]] = None, prefix: str = "", **kwargs):
|
||||
super().__init__(tags=tags, prefix=prefix, dependencies=[Depends(get_admin_user)], **kwargs)
|
||||
|
||||
|
||||
class UserAPIRouter(APIRouter):
|
||||
"""Router for functions to be protected behind user authentication"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
tags: Optional[list[str]] = None,
|
||||
prefix: str = "",
|
||||
):
|
||||
super().__init__(tags=tags, prefix=prefix, dependencies=[Depends(get_current_user)])
|
||||
def __init__(self, tags: Optional[list[Union[str, Enum]]] = None, prefix: str = "", **kwargs):
|
||||
super().__init__(tags=tags, prefix=prefix, dependencies=[Depends(get_current_user)], **kwargs)
|
||||
|
||||
|
||||
class MealieCrudRoute(APIRoute):
|
||||
"""Route class to include the last-modified header when returning a MealieModel, when available"""
|
||||
|
||||
def get_route_handler(self) -> Callable:
|
||||
original_route_handler = super().get_route_handler()
|
||||
|
||||
async def custom_route_handler(request: Request) -> Response:
|
||||
try:
|
||||
response = await original_route_handler(request)
|
||||
response_body = json.loads(response.body)
|
||||
if type(response_body) == dict:
|
||||
if last_modified := response_body.get("updateAt"):
|
||||
response.headers["last-modified"] = last_modified
|
||||
|
||||
except JSONDecodeError:
|
||||
pass
|
||||
|
||||
return response
|
||||
|
||||
return custom_route_handler
|
||||
|
||||
Reference in New Issue
Block a user