get weekdays

This commit is contained in:
Bernhard Radermacher (imuthes)
2025-08-09 17:39:27 +02:00
parent 38111fd594
commit f92d20dc89
7 changed files with 69 additions and 4 deletions

View File

@@ -1,5 +1,6 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="vue01" type="Python.FastAPI">
<option name="additionalOptions" value="--reload --host 0.0.0.0 --port 4433" />
<option name="file" value="$PROJECT_DIR$/backend/main.py" />
<module name="vue01" />
<option name="ENV_FILES" value="" />

View File

@@ -1,6 +1,27 @@
from fastapi import FastAPI
from contextlib import asynccontextmanager
app = FastAPI()
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from backend.src import router
@asynccontextmanager
async def lifespan(app: FastAPI):
yield
app = FastAPI(lifespan=lifespan)
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
app.include_router(router.calendar)
@app.get("/")

0
backend/src/__init__.py Normal file
View File

View File

@@ -0,0 +1 @@
from .calendar import calendar

View File

@@ -0,0 +1,41 @@
import datetime
from dateutil.relativedelta import relativedelta
from fastapi import APIRouter, HTTPException
calendar = APIRouter(
prefix="/calendar",
tags=["calendar"],
)
@calendar.get("/", response_model=list[datetime.date])
async def get_calendar(
year: int = None,
month: int = None,
):
"""Get a list of weekday dates for a month.
By default, the current month is used.
If **year** is provided, **month** must be provided, too.
"""
if year is not None:
if month is None:
raise HTTPException(status_code=422,
detail=[
dict(type="missing parameter",
loc=["query", "month"],
msg="month must be provided when year is provided")])
else:
today = datetime.date.today()
month = today.month
year = today.year
date = datetime.date(year, month, 1)
end_date = date + relativedelta(day=31)
result = []
while date <= end_date:
if date.isoweekday() < 6:
result.append(date)
date += relativedelta(days=1)
return result

View File

@@ -1,11 +1,11 @@
# Test your FastAPI endpoints
GET http://127.0.0.1:8000/
GET http://127.0.0.1:4433/
Accept: application/json
###
GET http://127.0.0.1:8000/hello/User
GET http://127.0.0.1:4433/hello/User
Accept: application/json
###

View File

@@ -5,5 +5,6 @@ description = "Add your description here"
requires-python = ">=3.13"
dependencies = [
"fastapi>=0.116.1",
"python-dateutil>=2.9.0.post0",
"uvicorn>=0.35.0",
]