This is an automated email from the ASF dual-hosted git repository.

sbp pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/tooling-trusted-releases.git


The following commit(s) were added to refs/heads/main by this push:
     new 4454290  Make the forms to cache and delete user data more type safe
4454290 is described below

commit 4454290fef715070111e58885cf9f33ee4131ae5
Author: Sean B. Palmer <[email protected]>
AuthorDate: Mon Nov 10 14:15:24 2025 +0000

    Make the forms to cache and delete user data more type safe
---
 atr/form.py        |  2 +-
 atr/get/user.py    | 23 +++++++++--------------
 atr/post/user.py   | 25 ++++++++++---------------
 atr/shared/user.py | 20 +++++++++++++++-----
 4 files changed, 35 insertions(+), 35 deletions(-)

diff --git a/atr/form.py b/atr/form.py
index 6635104..2652858 100644
--- a/atr/form.py
+++ b/atr/form.py
@@ -208,7 +208,7 @@ async def render(
     if action is None:
         action = quart.request.path
 
-    is_empty_form = model_cls is Empty
+    is_empty_form = isinstance(model_cls, type) and issubclass(model_cls, 
Empty)
     if is_empty_form and (form_classes == ".atr-canary"):
         form_classes = ""
 
diff --git a/atr/get/user.py b/atr/get/user.py
index 539afa2..0a247bf 100644
--- a/atr/get/user.py
+++ b/atr/get/user.py
@@ -15,10 +15,8 @@
 # specific language governing permissions and limitations
 # under the License.
 
-import quart
-
 import atr.blueprints.get as get
-import atr.forms as forms
+import atr.form as form
 import atr.htm as htm
 import atr.shared as shared
 import atr.template as template
@@ -28,9 +26,6 @@ import atr.web as web
 
 @get.committer("/user/cache")
 async def cache_get(session: web.Committer) -> str:
-    cache_form = await shared.user.CacheForm.create_form()
-    delete_cache_form = await shared.user.DeleteCacheForm.create_form()
-
     cache_data = await util.session_cache_read()
     user_cached = session.uid in cache_data
 
@@ -66,23 +61,23 @@ async def cache_get(session: web.Committer) -> str:
 
         block.h3["Delete cache"]
         block.p["Remove your cached session information:"]
-        delete_form_element = forms.render_simple(
-            delete_cache_form,
-            action=quart.request.path,
+        delete_cache_form = await form.render(
+            model_cls=shared.user.DeleteCacheForm,
+            submit_label="Delete my cache",
             submit_classes="btn-danger",
         )
-        block.append(delete_form_element)
+        block.append(delete_cache_form)
     else:
         block.h2["No cached session"]
         block.p["Your session is not currently cached."]
 
         block.h3["Cache current session"]
         block.p["Press the button below to cache your current session 
information:"]
-        cache_form_element = forms.render_simple(
-            cache_form,
-            action=quart.request.path,
+        cache_form = await form.render(
+            model_cls=shared.user.CacheUserForm,
+            submit_label="Cache me!",
             submit_classes="btn-primary",
         )
-        block.append(cache_form_element)
+        block.append(cache_form)
 
     return await template.blank("Session cache management", 
content=block.collect())
diff --git a/atr/post/user.py b/atr/post/user.py
index aaf149a..a414c10 100644
--- a/atr/post/user.py
+++ b/atr/post/user.py
@@ -15,7 +15,6 @@
 # specific language governing permissions and limitations
 # under the License.
 
-
 import quart
 
 import atr.blueprints.post as post
@@ -26,20 +25,16 @@ import atr.web as web
 
 
 @post.committer("/user/cache")
-async def session_post(session: web.Committer) -> web.WerkzeugResponse:
-    form_data = await quart.request.form
-
-    cache_form = await shared.user.CacheForm.create_form(data=form_data)
-    delete_cache_form = await 
shared.user.DeleteCacheForm.create_form(data=form_data)
-
-    if cache_form.cache_submit.data:
-        await _cache_session(session)
-        await quart.flash("Your session has been cached successfully", 
"success")
-    elif delete_cache_form.delete_submit.data:
-        await _delete_session_cache(session)
-        await quart.flash("Your cached session has been deleted", "success")
-    else:
-        await quart.flash("Invalid form submission", "error")
[email protected](shared.user.UserCacheForm)
+async def session_post(session: web.Committer, user_cache_form: 
shared.user.UserCacheForm) -> web.WerkzeugResponse:
+    match user_cache_form:
+        case shared.user.CacheUserForm():
+            await _cache_session(session)
+            await quart.flash("Your session has been cached successfully", 
"success")
+
+        case shared.user.DeleteCacheForm():
+            await _delete_session_cache(session)
+            await quart.flash("Your cached session has been deleted", 
"success")
 
     return await session.redirect(get.user.cache_get)
 
diff --git a/atr/shared/user.py b/atr/shared/user.py
index e9dbeff..8e321f8 100644
--- a/atr/shared/user.py
+++ b/atr/shared/user.py
@@ -15,13 +15,23 @@
 # specific language governing permissions and limitations
 # under the License.
 
+from typing import Annotated, Literal
 
-import atr.forms as forms
+import atr.form as form
 
+type CACHE = Literal["cache"]
+type DELETE = Literal["delete"]
 
-class CacheForm(forms.Typed):
-    cache_submit = forms.submit("Cache me!")
 
+class CacheUserForm(form.Empty):
+    variant: CACHE = form.value(CACHE)
 
-class DeleteCacheForm(forms.Typed):
-    delete_submit = forms.submit("Delete my cache")
+
+class DeleteCacheForm(form.Empty):
+    variant: DELETE = form.value(DELETE)
+
+
+type UserCacheForm = Annotated[
+    CacheUserForm | DeleteCacheForm,
+    form.DISCRIMINATOR,
+]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to