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-release.git


The following commit(s) were added to refs/heads/main by this push:
     new a476ff6  Allow keys route functions to be used for URL construction
a476ff6 is described below

commit a476ff6c9446049ef868fac2408576c2cd635c4f
Author: Sean B. Palmer <[email protected]>
AuthorDate: Wed Mar 26 20:23:36 2025 +0200

    Allow keys route functions to be used for URL construction
---
 atr/routes/keys.py                  | 93 +++++++++++++------------------------
 atr/server.py                       |  6 ++-
 atr/templates/includes/sidebar.html | 12 ++---
 atr/templates/index.html            |  2 +-
 atr/templates/keys-review.html      |  6 +--
 5 files changed, 44 insertions(+), 75 deletions(-)

diff --git a/atr/routes/keys.py b/atr/routes/keys.py
index dfbd4810..2481d1b 100644
--- a/atr/routes/keys.py
+++ b/atr/routes/keys.py
@@ -31,9 +31,6 @@ import tempfile
 from collections.abc import AsyncGenerator, Sequence
 
 import asfquart as asfquart
-import asfquart.auth as auth
-import asfquart.base as base
-import asfquart.session as session
 import cryptography.hazmat.primitives.serialization as serialization
 import gnupg
 import quart
@@ -65,7 +62,7 @@ async def ephemeral_gpg_home() -> AsyncGenerator[str]:
 
 
 async def key_add_post(
-    web_session: session.ClientSession, request: quart.Request, 
user_committees: Sequence[models.Committee]
+    session: routes.CommitterSession, request: quart.Request, user_committees: 
Sequence[models.Committee]
 ) -> dict | None:
     form = await routes.get_form(request)
     public_key = form.get("public_key")
@@ -81,12 +78,12 @@ async def key_add_post(
     invalid_committees = [
         committee
         for committee in selected_committees
-        if (committee not in web_session.committees) and (committee not in 
web_session.projects)
+        if (committee not in session.committees) and (committee not in 
session.projects)
     ]
     if invalid_committees:
         raise routes.FlashError(f"Invalid PMC selection: {', 
'.join(invalid_committees)}")
 
-    return await key_user_add(util.unwrap(web_session.uid), public_key, 
selected_committees)
+    return await key_user_add(session.uid, public_key, selected_committees)
 
 
 def key_ssh_fingerprint(ssh_key_string: str) -> str:
@@ -222,24 +219,19 @@ async def key_user_session_add(
     }
 
 
[email protected]_route("/keys/add", methods=["GET", "POST"])
[email protected](auth.Requirements.committer)
-async def root_keys_add() -> str:
[email protected]_route("/keys/add", methods=["GET", "POST"])
+async def add(session: routes.CommitterSession) -> str:
     """Add a new public signing key to the user's account."""
-    web_session = await session.read()
-    if web_session is None:
-        raise base.ASFQuartException("Not authenticated", errorcode=401)
-
     key_info = None
 
     # Get committees for all projects the user is a member of
     async with db.session() as data:
-        project_list = web_session.committees + web_session.projects
+        project_list = session.committees + session.projects
         user_committees = await data.committee(name_in=project_list).all()
 
     if quart.request.method == "POST":
         try:
-            key_info = await key_add_post(web_session, quart.request, 
user_committees)
+            key_info = await key_add_post(session, quart.request, 
user_committees)
         except routes.FlashError as e:
             logging.exception("FlashError:")
             await quart.flash(str(e), "error")
@@ -249,71 +241,59 @@ async def root_keys_add() -> str:
 
     return await quart.render_template(
         "keys-add.html",
-        asf_id=web_session.uid,
+        asf_id=session.uid,
         user_committees=user_committees,
         key_info=key_info,
         algorithms=routes.algorithms,
     )
 
 
[email protected]_route("/keys/delete", methods=["POST"])
[email protected](auth.Requirements.committer)
-async def root_keys_delete() -> response.Response:
[email protected]_route("/keys/delete", methods=["POST"])
+async def delete(session: routes.CommitterSession) -> response.Response:
     """Delete a public signing key from the user's account."""
-    web_session = await session.read()
-    if web_session is None:
-        raise base.ASFQuartException("Not authenticated", errorcode=401)
-    uid = util.unwrap(web_session.uid)
-
     form = await routes.get_form(quart.request)
     fingerprint = form.get("fingerprint")
     if not fingerprint:
         await quart.flash("No key fingerprint provided", "error")
-        return quart.redirect(quart.url_for("root_keys_review"))
+        return quart.redirect(util.as_url(review))
 
     async with db.session() as data:
         async with data.begin():
             # Try to get a GPG key first
-            key = await data.public_signing_key(fingerprint=fingerprint, 
apache_uid=uid).get()
+            key = await data.public_signing_key(fingerprint=fingerprint, 
apache_uid=session.uid).get()
             if key:
                 # Delete the GPG key
                 await data.delete(key)
                 await quart.flash("GPG key deleted successfully", "success")
-                return quart.redirect(quart.url_for("root_keys_review"))
+                return quart.redirect(util.as_url(review))
 
             # If not a GPG key, try to get an SSH key
-            ssh_key = await data.ssh_key(fingerprint=fingerprint, 
asf_uid=uid).get()
+            ssh_key = await data.ssh_key(fingerprint=fingerprint, 
asf_uid=session.uid).get()
             if ssh_key:
                 # Delete the SSH key
                 await data.delete(ssh_key)
                 await quart.flash("SSH key deleted successfully", "success")
-                return quart.redirect(quart.url_for("root_keys_review"))
+                return quart.redirect(util.as_url(review))
 
             # No key was found
             await quart.flash("Key not found or not owned by you", "error")
-            return quart.redirect(quart.url_for("root_keys_review"))
+            return quart.redirect(util.as_url(review))
 
 
[email protected]_route("/keys/review")
[email protected](auth.Requirements.committer)
-async def root_keys_review() -> str:
[email protected]_route("/keys/review")
+async def review(session: routes.CommitterSession) -> str:
     """Show all keys associated with the user's account."""
-    web_session = await session.read()
-    if web_session is None:
-        raise base.ASFQuartException("Not authenticated", errorcode=401)
-    uid = util.unwrap(web_session.uid)
-
     # Get all existing keys for the user
     async with db.session() as data:
-        user_keys = await data.public_signing_key(apache_uid=uid, 
_committees=True).all()
-        user_ssh_keys = await data.ssh_key(asf_uid=uid).all()
+        user_keys = await data.public_signing_key(apache_uid=session.uid, 
_committees=True).all()
+        user_ssh_keys = await data.ssh_key(asf_uid=session.uid).all()
 
     status_message = quart.request.args.get("status_message")
     status_type = quart.request.args.get("status_type")
 
     return await quart.render_template(
         "keys-review.html",
-        asf_id=web_session.uid,
+        asf_id=session.uid,
         user_keys=user_keys,
         user_ssh_keys=user_ssh_keys,
         algorithms=routes.algorithms,
@@ -323,20 +303,14 @@ async def root_keys_review() -> str:
     )
 
 
[email protected]_route("/keys/ssh/add", methods=["GET", "POST"])
[email protected](auth.Requirements.committer)
-async def root_keys_ssh_add() -> response.Response | str:
[email protected]_route("/keys/ssh/add", methods=["GET", "POST"])
+async def ssh_add(session: routes.CommitterSession) -> response.Response | str:
     """Add a new SSH key to the user's account."""
     # TODO: Make an auth.require wrapper that gives the session automatically
     # And the form if it's a POST handler? Might be hard to type
     # But we can use variants of the function
     # GET, POST, GET_POST are all we need
     # We could even include auth in the function names
-    web_session = await session.read()
-    if web_session is None:
-        raise base.ASFQuartException("Not authenticated", errorcode=401)
-    uid = util.unwrap(web_session.uid)
-
     form = await AddSSHKeyForm.create_form()
     fingerprint = None
     if await form.validate_on_submit():
@@ -344,29 +318,24 @@ async def root_keys_ssh_add() -> response.Response | str:
         fingerprint = await asyncio.to_thread(key_ssh_fingerprint, key)
         async with db.session() as data:
             async with data.begin():
-                data.add(models.SSHKey(fingerprint=fingerprint, key=key, 
asf_uid=uid))
+                data.add(models.SSHKey(fingerprint=fingerprint, key=key, 
asf_uid=session.uid))
         await quart.flash(f"SSH key added successfully: {fingerprint}", 
"success")
-        return quart.redirect(quart.url_for("root_keys_review"))
+        return quart.redirect(util.as_url(review))
 
     return await quart.render_template(
         "keys-ssh-add.html",
-        asf_id=web_session.uid,
+        asf_id=session.uid,
         form=form,
         fingerprint=fingerprint,
     )
 
 
[email protected]_route("/keys/upload", methods=["GET", "POST"])
[email protected](auth.Requirements.committer)
-async def root_keys_upload() -> str:
[email protected]_route("/keys/upload", methods=["GET", "POST"])
+async def upload(session: routes.CommitterSession) -> str:
     """Upload a KEYS file containing multiple GPG keys."""
-    web_session = await session.read()
-    if web_session is None:
-        raise base.ASFQuartException("Not authenticated", errorcode=401)
-
     # Get committees for all projects the user is a member of
     async with db.session() as data:
-        project_list = web_session.committees + web_session.projects
+        project_list = session.committees + session.projects
         user_committees = await data.committee(name_in=project_list).all()
 
     class UploadKeyForm(util.QuartFormTyped):
@@ -383,7 +352,7 @@ async def root_keys_upload() -> str:
             await quart.flash(error, "error")
         return await quart.render_template(
             "keys-upload.html",
-            asf_id=web_session.uid,
+            asf_id=session.uid,
             user_committees=user_committees,
             form=form,
             results=results,
@@ -407,7 +376,7 @@ async def root_keys_upload() -> str:
             return await render(error="You must select at least one committee")
 
         # Ensure that the selected committee is one of which the user is 
actually a member
-        if selected_committee not in (web_session.committees + 
web_session.projects):
+        if selected_committee not in (session.committees + session.projects):
             return await render(error=f"You are not a member of 
{selected_committee}")
 
         # Process each key block
diff --git a/atr/server.py b/atr/server.py
index cf1b35c..568ecf5 100644
--- a/atr/server.py
+++ b/atr/server.py
@@ -136,13 +136,17 @@ def app_setup_context(app: base.QuartApp) -> None:
         import atr.routes as routes
         import atr.util as util
 
+        modules = routes.modules()
+        # In Jinja, keys is a keyword when used in the form .keys
+        modules["_keys"] = modules["keys"]
+        del modules["keys"]
         return {
             "as_url": util.as_url,
             "commit": metadata.commit,
             "current_user": await asfquart.session.read(),
             "is_admin_fn": util.is_admin,
             "is_project_lead_fn": service.is_project_lead,
-            "routes": routes.modules(),
+            "routes": modules,
             "version": metadata.version,
         }
 
diff --git a/atr/templates/includes/sidebar.html 
b/atr/templates/includes/sidebar.html
index 5966a09..40216e3 100644
--- a/atr/templates/includes/sidebar.html
+++ b/atr/templates/includes/sidebar.html
@@ -62,20 +62,16 @@
       <ul>
         <li>
           <i class="fa-solid fa-key"></i>
-          <a href="{{ url_for('root_keys_review') }}"
-             {% if request.endpoint == 'root_keys_review' %}class="active"{% 
endif %}>Your signing keys</a>
+          <a href="{{ as_url(routes._keys.review) }}">Your signing keys</a>
         </li>
         <li>
-          <a href="{{ url_for('root_keys_add') }}"
-             {% if request.endpoint == 'root_keys_add' %}class="active"{% 
endif %}>Add your signing key</a>
+          <a href="{{ as_url(routes._keys.add) }}">Add your signing key</a>
         </li>
         <li>
-          <a href="{{ url_for('root_keys_upload') }}"
-             {% if request.endpoint == 'root_keys_upload' %}class="active"{% 
endif %}>Upload a KEYS file</a>
+          <a href="{{ as_url(routes._keys.upload) }}">Upload a KEYS file</a>
         </li>
         <li>
-          <a href="{{ url_for('root_keys_ssh_add') }}"
-             {% if request.endpoint == 'root_keys_ssh_add' %}class="active"{% 
endif %}>Add SSH key</a>
+          <a href="{{ as_url(routes._keys.ssh_add) }}">Add SSH key</a>
         </li>
       </ul>
 
diff --git a/atr/templates/index.html b/atr/templates/index.html
index c9d8da9..6b888c3 100644
--- a/atr/templates/index.html
+++ b/atr/templates/index.html
@@ -23,7 +23,7 @@
 
   <h2>Getting Started</h2>
   <p>
-    To submit a release candidate, you must be a PMC member of the target 
project. First, <a href="{{ url_for('root_keys_add') }}">add your signing 
key</a>, then <a href="{{ as_url(routes.candidate.create) }}">create a release 
candidate</a>.
+    To submit a release candidate, you must be a PMC member of the target 
project. First, <a href="{{ as_url(ks.add) }}">add your signing key</a>, then 
<a href="{{ as_url(routes.candidate.create) }}">create a release candidate</a>.
   </p>
 
   <h2>Documentation</h2>
diff --git a/atr/templates/keys-review.html b/atr/templates/keys-review.html
index 610cd58..e2fe50e 100644
--- a/atr/templates/keys-review.html
+++ b/atr/templates/keys-review.html
@@ -83,7 +83,7 @@
               <pre class="mt-3">{{ key.ascii_armored_key }}</pre>
             </details>
 
-            <form method="post" action="{{ url_for('root_keys_delete') }}" 
class="mt-3">
+            <form method="post" action="{{ as_url(routes._keys.delete) }}" 
class="mt-3">
               <input type="hidden" name="fingerprint" value="{{ 
key.fingerprint }}" />
               <button type="submit" class="btn btn-danger">Delete key</button>
             </form>
@@ -97,7 +97,7 @@
       <strong>You haven't added any signing keys yet.</strong>
     </p>
     <p>
-      <a href="{{ url_for('root_keys_add') }}">Add a key</a>
+      <a href="{{ as_url(routes._keys.add) }}">Add a key</a>
     </p>
   {% endif %}
 
@@ -125,7 +125,7 @@
               <pre class="mt-3">{{ key.key }}</pre>
             </details>
 
-            <form method="post" action="{{ url_for('root_keys_delete') }}" 
class="mt-3">
+            <form method="post" action="{{ as_url(routes._keys.delete) }}" 
class="mt-3">
               <input type="hidden" name="fingerprint" value="{{ 
key.fingerprint }}" />
               <button type="submit" class="btn btn-danger">Delete key</button>
             </form>


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

Reply via email to