Switch list endpoints to jinja templates
This commit is contained in:
parent
39cfa19ebf
commit
17b602a1a5
3 changed files with 30 additions and 32 deletions
|
|
@ -14,43 +14,19 @@ templates = Jinja2Templates(directory="templates")
|
||||||
|
|
||||||
def define_routes(app: FastAPI):
|
def define_routes(app: FastAPI):
|
||||||
@app.get("/improvements", response_class=HTMLResponse)
|
@app.get("/improvements", response_class=HTMLResponse)
|
||||||
def list_improvements():
|
def list_improvements(request: Request):
|
||||||
improved = repo.get_all()
|
improved = repo.get_all()
|
||||||
return (
|
return templates.TemplateResponse(
|
||||||
"""<button hx-get="/originals" hx-target="#content">Originals</button> """
|
request=request,
|
||||||
+ "\n".join(
|
name="list_improvements.html",
|
||||||
f"""
|
context={"articles": improved},
|
||||||
<div class="card">
|
|
||||||
<div class="card-img">
|
|
||||||
<img src="{item.original.image_link if item.original.image_link else "https://placehold.co/300x200"}" width="600">
|
|
||||||
</div>
|
|
||||||
<div class="card-title">{item.title}</div>
|
|
||||||
<div class="card-summary">{item.summary}</div>
|
|
||||||
</div>"""
|
|
||||||
for item in sorted(
|
|
||||||
improved, key=lambda i: i.original.date, reverse=True
|
|
||||||
)
|
|
||||||
)
|
|
||||||
)
|
)
|
||||||
|
|
||||||
@app.get("/originals", response_class=HTMLResponse)
|
@app.get("/originals", response_class=HTMLResponse)
|
||||||
def list_originals():
|
def list_originals(request: Request):
|
||||||
improved = repo.get_all()
|
improved = repo.get_all()
|
||||||
return (
|
return templates.TemplateResponse(
|
||||||
"""<button hx-get="/improvements" hx-target="#content">Improvements</button> """
|
request=request, name="list_originals.html", context={"articles": improved}
|
||||||
+ "\n".join(
|
|
||||||
f"""
|
|
||||||
<div class="card">
|
|
||||||
<div class="card-img">
|
|
||||||
<img src="{item.original.image_link if item.original.image_link else "https://placehold.co/300x200"}" width="600">
|
|
||||||
</div>
|
|
||||||
<div class="card-title">{item.original.title}</div>
|
|
||||||
<div class="card-summary">{item.original.summary}</div>
|
|
||||||
</div>"""
|
|
||||||
for item in sorted(
|
|
||||||
improved, key=lambda i: i.original.date, reverse=True
|
|
||||||
)
|
|
||||||
)
|
|
||||||
)
|
)
|
||||||
|
|
||||||
@app.get("/", response_class=HTMLResponse)
|
@app.get("/", response_class=HTMLResponse)
|
||||||
|
|
|
||||||
11
templates/list_improvements.html
Normal file
11
templates/list_improvements.html
Normal file
|
|
@ -0,0 +1,11 @@
|
||||||
|
<button hx-get="/originals" hx-target="#content">Originals</button>
|
||||||
|
|
||||||
|
{% for article in articles %}
|
||||||
|
<div class="card">
|
||||||
|
<div class="card-title">{{article.title}}</div>
|
||||||
|
<div class="card-img">
|
||||||
|
<img src="{{ article.original.image_link }}" width="600" />
|
||||||
|
</div>
|
||||||
|
<div class="card-summary">{{article.summary}}</div>
|
||||||
|
</div>
|
||||||
|
{% endfor %}
|
||||||
11
templates/list_originals.html
Normal file
11
templates/list_originals.html
Normal file
|
|
@ -0,0 +1,11 @@
|
||||||
|
<button hx-get="/improvements" hx-target="#content">Improvements</button>
|
||||||
|
|
||||||
|
{% for article in articles %}
|
||||||
|
<div class="card">
|
||||||
|
<div class="card-title">{{article.original.title}}</div>
|
||||||
|
<div class="card-img">
|
||||||
|
<img src="{{ article.original.image_link }}" width="600" />
|
||||||
|
</div>
|
||||||
|
<div class="card-summary">{{article.original.summary}}</div>
|
||||||
|
</div>
|
||||||
|
{% endfor %}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue