forked from retoor/devplacepy
Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
38c2bab503 |
File diff suppressed because one or more lines are too long
@@ -38,6 +38,7 @@ def field(
|
||||
example="",
|
||||
description="",
|
||||
options=None,
|
||||
nullable=False,
|
||||
):
|
||||
spec = {
|
||||
"name": name,
|
||||
@@ -46,6 +47,7 @@ def field(
|
||||
"required": required,
|
||||
"example": example,
|
||||
"description": description,
|
||||
"nullable": nullable,
|
||||
}
|
||||
if options:
|
||||
spec["options"] = list(options)
|
||||
|
||||
@@ -39,7 +39,15 @@ def _unwrap_optional(annotation):
|
||||
return annotation
|
||||
|
||||
|
||||
def _value(name, annotation, stack):
|
||||
def _is_optional(annotation):
|
||||
if get_origin(annotation) is Union:
|
||||
return type(None) in get_args(annotation)
|
||||
return False
|
||||
|
||||
|
||||
def _value(name, annotation, stack, is_nullable=False):
|
||||
if is_nullable:
|
||||
return None
|
||||
annotation = _unwrap_optional(annotation)
|
||||
origin = get_origin(annotation)
|
||||
if origin in (list, set, tuple):
|
||||
@@ -71,7 +79,8 @@ def _from_model(model, stack):
|
||||
stack = stack | {model}
|
||||
example = {}
|
||||
for name, info in model.model_fields.items():
|
||||
example[name] = _value(name, info.annotation, stack)
|
||||
is_nullable = _is_optional(info.annotation)
|
||||
example[name] = _value(name, info.annotation, stack, is_nullable)
|
||||
return example
|
||||
|
||||
|
||||
|
||||
@@ -44,7 +44,6 @@ from devplacepy.templating import templates, jinja_unread_count
|
||||
from devplacepy.cache import TTLCache
|
||||
from devplacepy.responses import respond, wants_json, json_error
|
||||
from devplacepy.schemas import LandingOut, ValidationErrorOut
|
||||
from devplacepy.attachments import get_attachments_batch
|
||||
from fastapi.responses import JSONResponse
|
||||
from devplacepy.utils import get_current_user, time_ago, safe_next, client_ip
|
||||
from devplacepy.seo import base_seo_context, site_url, website_schema
|
||||
@@ -672,7 +671,6 @@ def _landing_recent_posts(blocked):
|
||||
authors = get_users_by_uids(author_uids)
|
||||
comment_counts = get_comment_counts_by_post_uids(post_uids)
|
||||
upvotes, downvotes = get_vote_counts(post_uids)
|
||||
attachments_map = get_attachments_batch("post", post_uids)
|
||||
for p in raw_posts:
|
||||
posts.append(
|
||||
{
|
||||
@@ -682,7 +680,6 @@ def _landing_recent_posts(blocked):
|
||||
"comment_count": comment_counts.get(p["uid"], 0),
|
||||
"stars": upvotes.get(p["uid"], 0) - downvotes.get(p["uid"], 0),
|
||||
"slug": p.get("slug", "") or p["uid"],
|
||||
"attachments": attachments_map.get(p["uid"], []),
|
||||
}
|
||||
)
|
||||
if not blocked:
|
||||
|
||||
@@ -7,6 +7,7 @@ from devplacepy.models import ProfileForm
|
||||
from fastapi.responses import HTMLResponse, JSONResponse
|
||||
from devplacepy.database import (
|
||||
get_table,
|
||||
db,
|
||||
get_customization_prefs,
|
||||
get_notification_prefs,
|
||||
get_user_stars,
|
||||
@@ -47,7 +48,6 @@ from devplacepy.utils import (
|
||||
)
|
||||
from devplacepy.responses import respond, action_result, wants_json
|
||||
from devplacepy.schemas import ProfileOut
|
||||
from devplacepy.attachments import get_attachments_batch
|
||||
from devplacepy.avatar import avatar_url, avatar_seed
|
||||
from devplacepy.seo import (
|
||||
base_seo_context,
|
||||
@@ -188,10 +188,8 @@ async def profile_page(
|
||||
else set()
|
||||
)
|
||||
polls_map = get_polls_by_post_uids(post_uids, current_user)
|
||||
attachments_map = get_attachments_batch("post", post_uids)
|
||||
for item in posts:
|
||||
uid = item["post"]["uid"]
|
||||
item["attachments"] = attachments_map.get(uid, [])
|
||||
item["reactions"] = reactions_map.get(uid, {"counts": {}, "mine": []})
|
||||
item["bookmarked"] = uid in bookmark_set
|
||||
item["poll"] = polls_map.get(uid)
|
||||
|
||||
@@ -5,7 +5,7 @@ from __future__ import annotations
|
||||
from typing import Any, Optional
|
||||
|
||||
from devplacepy.schemas.base import _Out
|
||||
from devplacepy.schemas.content import AttachmentOut, UserOut
|
||||
from devplacepy.schemas.content import UserOut
|
||||
|
||||
|
||||
class AuthPageOut(_Out):
|
||||
@@ -38,7 +38,6 @@ class LandingPostOut(_Out):
|
||||
comment_count: int = 0
|
||||
stars: int = 0
|
||||
slug: str = ""
|
||||
attachments: list[AttachmentOut] = []
|
||||
|
||||
|
||||
class TrendingTopicOut(_Out):
|
||||
|
||||
@@ -218,6 +218,16 @@
|
||||
font-weight: 700;
|
||||
}
|
||||
|
||||
.param-nullable {
|
||||
font-size: 0.625rem;
|
||||
text-transform: uppercase;
|
||||
letter-spacing: 0.05em;
|
||||
color: var(--text-muted);
|
||||
border: 1px dashed var(--border);
|
||||
border-radius: 999px;
|
||||
padding: 0.05rem 0.4rem;
|
||||
}
|
||||
|
||||
.param-loc {
|
||||
font-size: 0.625rem;
|
||||
text-transform: uppercase;
|
||||
|
||||
@@ -139,6 +139,7 @@ export class ApiTester {
|
||||
const label = this.el("div", { class: "param-label" }, [
|
||||
this.el("span", { class: "param-name", text: param.name }),
|
||||
param.required ? this.el("span", { class: "param-required", text: "*" }) : null,
|
||||
param.nullable ? this.el("span", { class: "param-nullable", text: "nullable" }) : null,
|
||||
this.el("span", { class: "param-loc param-loc-" + param.location, text: param.location }),
|
||||
]);
|
||||
const allowed = param.type === "enum" && param.options && param.options.length
|
||||
|
||||
@@ -0,0 +1,59 @@
|
||||
# retoor <retoor@molodetz.nl>
|
||||
|
||||
from typing import Optional
|
||||
|
||||
from pydantic import BaseModel
|
||||
|
||||
from devplacepy.docs_api._shared import field
|
||||
from devplacepy.docs_examples import _is_optional, schema_example
|
||||
|
||||
|
||||
def test_field_nullable_parameter():
|
||||
f = field("bio", "query", "string", False, "", "User biography", nullable=True)
|
||||
assert f["nullable"] is True
|
||||
|
||||
f2 = field("username", "path", "string", True, "alice", "Target username")
|
||||
assert f2["nullable"] is False
|
||||
|
||||
|
||||
def test_is_optional_optional_type():
|
||||
assert _is_optional(Optional[str]) is True
|
||||
assert _is_optional(Optional[int]) is True
|
||||
assert _is_optional(Optional[list[str]]) is True
|
||||
|
||||
|
||||
def test_is_optional_non_optional():
|
||||
assert _is_optional(str) is False
|
||||
assert _is_optional(int) is False
|
||||
assert _is_optional(list[str]) is False
|
||||
assert _is_optional(dict) is False
|
||||
|
||||
|
||||
def test_schema_example_nullable_fields():
|
||||
class NullableModel(BaseModel):
|
||||
name: str
|
||||
bio: Optional[str] = None
|
||||
age: int
|
||||
score: Optional[int] = None
|
||||
tags: Optional[list[str]] = None
|
||||
|
||||
result = schema_example(NullableModel)
|
||||
|
||||
assert result["name"] == "string"
|
||||
assert result["age"] == 0
|
||||
assert result["bio"] is None
|
||||
assert result["score"] is None
|
||||
assert result["tags"] is None
|
||||
|
||||
|
||||
def test_schema_example_non_nullable_fields_unaffected():
|
||||
class StrictModel(BaseModel):
|
||||
x: str
|
||||
y: int
|
||||
z: bool
|
||||
|
||||
result = schema_example(StrictModel)
|
||||
|
||||
assert result["x"] == "string"
|
||||
assert result["y"] == 0
|
||||
assert result["z"] is False
|
||||
Reference in New Issue
Block a user