Membership View

Next we’ll add the views for the membership page. Add a new views file:

web/views/member_views.pypython
from fastapi import APIRouter, Depends, Request, status
from fastapi.responses import HTMLResponse, RedirectResponse
from fastapi.templating import Jinja2Templates
from sqlmodel import Session

from api.auth_apis import is_user
from api.payment_apis import create_membership_checkout_api, fulfil_membership_api, get_checkout_api
from db.db import get_session_db

router = APIRouter()

templates = Jinja2Templates('web/templates')

def member_page(request: Request, user, title: str, message: str | None = None):
    context = {
        "page": "member",
        "user": user,
        "title": title,
        "message": message,
    }
    return templates.TemplateResponse(request, 'member.html', {'context': context})

@router.get('/member', response_class=HTMLResponse, status_code=status.HTTP_200_OK)
def member_view(*, request: Request, session: Session = Depends(get_session_db)):
    current_user = is_user(request, session)
    if current_user is None:
        return RedirectResponse(url='/auth/login', status_code=status.HTTP_303_SEE_OTHER)
    return member_page(request, current_user, "Membership")

Only logged-in users can access the page, because the purchase is linked to their account. member_page is used by all the membership views, which share one template, with a title and an optional message:

web/templates/member.htmljinja
{% extends "_base.html" %}

{% block content %}

    <!-- Page Header -->
    <header class="intro-header" style="background-image: url('/static/img/about-bg.jpg')">
        <div class="container">
            <div class="row">
                <div class="col-lg-8 col-lg-offset-2 col-md-10 col-md-offset-1">
                    <div class="page-heading">
                        <h1>{{ context.title }}</h1>
                    </div>
                </div>
            </div>
        </div>
    </header>

    <!-- Main Content -->
    <div class="container">
        <div class="row">
            <div class="col-lg-8 col-lg-offset-2 col-md-10 col-md-offset-1">
                {% if context.message %}<p>{{ context.message }}</p>{% endif %}
                {% if context.user.is_member %}
                <p>You are a premium member, thank you for your support.</p>
                {% else %}
                <p>Membership is a £5 one-off payment. You can enter a promotion code on the payment page.</p>
                <form action="/member/purchase" method="post">
                    <button type="submit" class="btn btn-default">Checkout</button>
                </form>
                {% endif %}
            </div>
        </div>
    </div>

{% endblock %}

Members see a thank-you; everyone else sees a Checkout button.

This new members views router needs including in main.py, along with the payments router (for the webhook, in the next section). Both are part of the website, so they go outside the CONF_ENABLE_APIS check:

main.pypython
from api import health_api, blog_apis, captcha_apis, auth_apis, admin_apis, payment_apis  # UPDATED
from web.views import home_view, blog_views, contact_view, auth_views, admin_views, profile_views, member_views  # UPDATED
main.pypython
    main_app.include_router(member_views.router, tags=["web_views"])  # NEW
    main_app.include_router(payment_apis.router, tags=["payments"])  # NEW

Add a Join link to the navigation in _base.html, for logged-in users who aren’t members yet:

web/templates/_base.htmljinja
                    {% if not context.user.is_member %}
                    <li{% if context.page == 'member' %} class="active"{% endif %}>
                        <a href="/member">Join</a>
                    </li>
                    {% endif %}