Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6a6e6716b1 |
File diff suppressed because one or more lines are too long
@@ -38,7 +38,6 @@ def field(
|
||||
example="",
|
||||
description="",
|
||||
options=None,
|
||||
nullable=False,
|
||||
):
|
||||
spec = {
|
||||
"name": name,
|
||||
@@ -47,7 +46,6 @@ def field(
|
||||
"required": required,
|
||||
"example": example,
|
||||
"description": description,
|
||||
"nullable": nullable,
|
||||
}
|
||||
if options:
|
||||
spec["options"] = list(options)
|
||||
|
||||
@@ -39,15 +39,7 @@ def _unwrap_optional(annotation):
|
||||
return annotation
|
||||
|
||||
|
||||
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
|
||||
def _value(name, annotation, stack):
|
||||
annotation = _unwrap_optional(annotation)
|
||||
origin = get_origin(annotation)
|
||||
if origin in (list, set, tuple):
|
||||
@@ -79,8 +71,7 @@ def _from_model(model, stack):
|
||||
stack = stack | {model}
|
||||
example = {}
|
||||
for name, info in model.model_fields.items():
|
||||
is_nullable = _is_optional(info.annotation)
|
||||
example[name] = _value(name, info.annotation, stack, is_nullable)
|
||||
example[name] = _value(name, info.annotation, stack)
|
||||
return example
|
||||
|
||||
|
||||
|
||||
@@ -46,6 +46,7 @@ from devplacepy.utils import (
|
||||
track_action,
|
||||
build_achievements,
|
||||
)
|
||||
from devplacepy.utils.rewards import LEVEL_XP
|
||||
from devplacepy.responses import respond, action_result, wants_json
|
||||
from devplacepy.schemas import ProfileOut
|
||||
from devplacepy.avatar import avatar_url, avatar_seed
|
||||
@@ -380,6 +381,10 @@ async def profile_page(
|
||||
],
|
||||
)
|
||||
|
||||
xp = profile_user.get("xp") or 0
|
||||
xp_progress_pct = xp % LEVEL_XP
|
||||
xp_next_level = ((xp // LEVEL_XP) + 1) * LEVEL_XP
|
||||
|
||||
return respond(
|
||||
request,
|
||||
"profile.html",
|
||||
@@ -439,6 +444,8 @@ async def profile_page(
|
||||
"awards_pagination": awards_pagination,
|
||||
"awards_count": awards_count,
|
||||
"prominent_award": prominent_award,
|
||||
"xp_progress_pct": xp_progress_pct,
|
||||
"xp_next_level": xp_next_level,
|
||||
"can_give_award": can_give,
|
||||
},
|
||||
model=ProfileOut,
|
||||
|
||||
@@ -80,6 +80,8 @@ class ProfileOut(_Out):
|
||||
awards_count: int = 0
|
||||
prominent_award: Optional[AwardOut] = None
|
||||
can_give_award: bool = False
|
||||
xp_progress_pct: Optional[int] = None
|
||||
xp_next_level: Optional[int] = None
|
||||
|
||||
|
||||
class TelegramPairOut(_Out):
|
||||
|
||||
@@ -218,16 +218,6 @@
|
||||
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,7 +139,6 @@ 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
|
||||
|
||||
@@ -267,6 +267,37 @@ def test_activity_comment_card_renders_overlay_link(app_server):
|
||||
assert f'class="card-link" href="/posts/{post_slug}#comment-{comment_uid}"' in r.text
|
||||
|
||||
|
||||
def test_profile_json_contains_xp_progress(app_server):
|
||||
from devplacepy.database import get_table, refresh_snapshot
|
||||
import time
|
||||
|
||||
name = f"xp{int(time.time() * 1000)}"
|
||||
session = requests.Session()
|
||||
session.post(
|
||||
f"{BASE_URL}/auth/signup",
|
||||
data={
|
||||
"username": name,
|
||||
"email": f"{name}@t.dev",
|
||||
"password": "secret123",
|
||||
"confirm_password": "secret123",
|
||||
},
|
||||
allow_redirects=True,
|
||||
)
|
||||
|
||||
user = get_table("users").find_one(username=name)
|
||||
assert user is not None
|
||||
get_table("users").update({"uid": user["uid"], "xp": 250}, ["uid"])
|
||||
refresh_snapshot()
|
||||
|
||||
r = session.get(
|
||||
f"{BASE_URL}/profile/{name}", headers={"Accept": "application/json"}
|
||||
)
|
||||
assert r.status_code == 200, f"status {r.status_code}: {r.text[:200]}"
|
||||
body = r.json()
|
||||
assert body["xp_progress_pct"] == 50
|
||||
assert body["xp_next_level"] == 300
|
||||
|
||||
|
||||
def test_profile_json_exposes_online_presence(app_server):
|
||||
import time
|
||||
|
||||
|
||||
@@ -1,59 +0,0 @@
|
||||
# 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