From f29c40d25ec747dd5169576401136b5cb78d2773 Mon Sep 17 00:00:00 2001 From: "Waylon S. Walker" Date: Sat, 6 Apr 2024 11:26:46 -0500 Subject: [PATCH] 0.1.0 boosted --- CHANGELOG.md | 4 +++ htmx_patterns/__about__.py | 2 +- htmx_patterns/app.py | 4 ++- htmx_patterns/boosted/models.py | 24 +++++++++++++ htmx_patterns/boosted/router.py | 37 +++++++++++++++++++ htmx_patterns/config.py | 6 ---- htmx_patterns/infinite/router.py | 2 +- templates/base.html | 28 +++++++++++---- templates/boosted/person.html | 61 ++++++++++++++++++++++++++++++++ templates/index.html | 29 +++++++++++---- templates/infinite/persons.html | 6 +++- 11 files changed, 181 insertions(+), 22 deletions(-) create mode 100644 htmx_patterns/boosted/models.py create mode 100644 htmx_patterns/boosted/router.py create mode 100644 templates/boosted/person.html diff --git a/CHANGELOG.md b/CHANGELOG.md index 9178848..d966ca5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # HTMX-PATTERNS CHANGELOG +## 0.1.0 + +* feat boosted links + ## 0.0.4 * fix simulated load need asyncio.sleep diff --git a/htmx_patterns/__about__.py b/htmx_patterns/__about__.py index a2ba126..3dc5651 100644 --- a/htmx_patterns/__about__.py +++ b/htmx_patterns/__about__.py @@ -1,4 +1,4 @@ # SPDX-FileCopyrightText: 2024-present Waylon S. Walker # # SPDX-License-Identifier: MIT -__version__ = "0.0.4" +__version__ = "0.1.0" diff --git a/htmx_patterns/app.py b/htmx_patterns/app.py index de0884a..fcd4483 100644 --- a/htmx_patterns/app.py +++ b/htmx_patterns/app.py @@ -2,6 +2,7 @@ from fastapi import Depends, FastAPI, Request from fastapi.responses import FileResponse from htmx_patterns.__about__ import __version__ +from htmx_patterns.boosted.router import boosted_router from htmx_patterns.config import get_config from htmx_patterns.infinite.router import infinite_router @@ -36,10 +37,11 @@ app = FastAPI( config = get_config() app.include_router(infinite_router) +app.include_router(boosted_router) @app.get("/") -async def read_main(request: Request): +async def index(request: Request): return config.templates.TemplateResponse("index.html", {"request": request}) diff --git a/htmx_patterns/boosted/models.py b/htmx_patterns/boosted/models.py new file mode 100644 index 0000000..a5ad575 --- /dev/null +++ b/htmx_patterns/boosted/models.py @@ -0,0 +1,24 @@ +from datetime import date, datetime +from typing import List, Union + +from faker import Faker +from polyfactory.factories.pydantic_factory import ModelFactory +from pydantic import UUID4, BaseModel + +faker = Faker() + + +class Person(BaseModel): + id: UUID4 + name: str + birthday: Union[datetime, date] + phone_number: str + + +class PersonFactory(ModelFactory): + name = faker.name + phone_number = faker.phone_number + __model__ = Person + + +# result = PersonFactory.build() diff --git a/htmx_patterns/boosted/router.py b/htmx_patterns/boosted/router.py new file mode 100644 index 0000000..87b8a90 --- /dev/null +++ b/htmx_patterns/boosted/router.py @@ -0,0 +1,37 @@ +import asyncio +import time + +from fastapi import APIRouter +from fastapi.requests import Request + +boosted_router = APIRouter(prefix="/boosted", tags=["Shots Methods"]) + +from htmx_patterns.boosted.models import PersonFactory +from htmx_patterns.config import get_config + +config = get_config() + + +@boosted_router.get("/") +@boosted_router.get("") +async def boosted(request: Request, id: int = 0): + # simulate getting a person by id + person = PersonFactory.build() + + if id > 0: + prev_id = id - 1 + next_id = id + 1 + else: + prev_id = None + next_id = id + 1 + + return config.templates.TemplateResponse( + "boosted/person.html", + { + "request": request, + "person": person, + "person_id": id, + "prev_id": prev_id, + "next_id": next_id, + }, + ) diff --git a/htmx_patterns/config.py b/htmx_patterns/config.py index aa2ffe7..642a226 100644 --- a/htmx_patterns/config.py +++ b/htmx_patterns/config.py @@ -32,11 +32,6 @@ class ApiServer(BaseModel): proxy_headers: bool = True -# @pass_context -# def https_url_for(context: dict, name: str, **params: Any) -> str: -# http_url = url_for_query(context, name, **params) -# return str(http_url).replace("http", "https", 1) - @pass_context def url_for_query(context: dict, name: str, **params: dict) -> str: @@ -71,7 +66,6 @@ def url_for_query(context: dict, name: str, **params: dict) -> str: if os.environ.get("ENV") in ["dev", "qa", "prod"]: updated_url = updated_url.replace("http", "https", 1) - return updated_url diff --git a/htmx_patterns/infinite/router.py b/htmx_patterns/infinite/router.py index 030debc..e1096bb 100644 --- a/htmx_patterns/infinite/router.py +++ b/htmx_patterns/infinite/router.py @@ -23,7 +23,7 @@ async def infinite(request: Request, page: int = 1, n: int = 10): persons = [PersonFactory.build() for _ in range(n)] if request.state.prefers_partial: - await asyncio.sleep(0.5) + await asyncio.sleep(1) return config.templates.TemplateResponse( "infinite/persons_partial.html", { diff --git a/templates/base.html b/templates/base.html index 833867e..95e7bd4 100644 --- a/templates/base.html +++ b/templates/base.html @@ -12,18 +12,34 @@ + {% if DEBUG %} + {{ hot_reload.script(url_for('hot-reload') ) | safe }} + {% endif %} {% endblock %} +{% set links = { +"HTMX-PATTERNS": url_for("index"), +"Boosted Links": url_for('boosted'), +"Infinite Scroll": url_for('infinite'), +} %} +
-
- {% if DEBUG %} - {{ hot_reload.script(url_for('hot-reload') ) | safe }} - {% endif %} + class="absolute top-0 right-0 bottom-0 left-0 justify-center items-center min-w-full bg-repeat bg-cover" + style="background-image: url(https://fokais.com/grit.svg), url(https://fokais.com/grit-light.svg); animation: pulse 10s cubic-bezier(0.4, 0, 0.6, 1) infinite; pointer-events: none"> +
+
+ {% block content %} {{ body | safe }} {% endblock %} diff --git a/templates/boosted/person.html b/templates/boosted/person.html new file mode 100644 index 0000000..75a934a --- /dev/null +++ b/templates/boosted/person.html @@ -0,0 +1,61 @@ +{% extends "base.html" %} +{% block title %}Contact - {{ person_id }} - {{ person.name }}{% endblock %} +{% block content %} +

+ HTMX PATTERNS - BOOSTED +

+ +

+ Contact - {{ person_id }} +

+ +

+ {{ person.name }} - + {{ person.phone_number }} +

+ +

+ Boosted Links +

+ +
+ {% if prev_id is not none %} + + Previous + + {% else %} + + Previous + + {% endif %} + + + Next + +
+ +

+ Normal Links +

+ +
+ {% if prev_id is not none %} + + Previous + + {% else %} + + Previous + + {% endif %} + + + Next + +
+{% endblock %} diff --git a/templates/index.html b/templates/index.html index b683019..36911b6 100644 --- a/templates/index.html +++ b/templates/index.html @@ -5,17 +5,34 @@ HTMX PATTERNS -

+

A collection of HTMX patterns

-
    -
  • - - Infinite Scroll + {% endblock %} diff --git a/templates/infinite/persons.html b/templates/infinite/persons.html index 33689e7..f7099bc 100644 --- a/templates/infinite/persons.html +++ b/templates/infinite/persons.html @@ -1,4 +1,5 @@ {% extends "base.html" %} +{% block title %}Contacts List{% endblock %} {% block content %}

    @@ -14,7 +15,10 @@

- loading more contacts +

loading more contacts

+

+ with simulated slow down +

{% endblock %}