Skip to content

Commit cbaff74

Browse files
Merge pull request #104 from Promptly-Technologies-LLC/89-use-inheritance-to-define-data-models
Refactor to use a more traditional CRUD module structure
2 parents 1f48b72 + be36999 commit cbaff74

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+3173
-2431
lines changed

.github/workflows/publish.yml

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,14 @@ jobs:
6262
6363
- name: Set required env variables
6464
run: |
65-
echo "DB_USER=postgres" >> $GITHUB_ENV
66-
echo "DB_PASSWORD=postgres" >> $GITHUB_ENV
67-
echo "DB_HOST=127.0.0.1" >> $GITHUB_ENV
68-
echo "DB_PORT=5432" >> $GITHUB_ENV
69-
echo "DB_NAME=test_db" >> $GITHUB_ENV
70-
echo "SECRET_KEY=$(openssl rand -base64 32)" >> $GITHUB_ENV
71-
echo "BASE_URL=http://localhost:8000" >> $GITHUB_ENV
72-
echo "RESEND_API_KEY=resend_api_key" >> $GITHUB_ENV
65+
echo "DB_USER=postgres" > _environment
66+
echo "DB_PASSWORD=postgres" >> _environment
67+
echo "DB_HOST=127.0.0.1" >> _environment
68+
echo "DB_PORT=5432" >> _environment
69+
echo "DB_NAME=test_db" >> _environment
70+
echo "SECRET_KEY=$(openssl rand -base64 32)" >> _environment
71+
echo "BASE_URL=http://localhost:8000" >> _environment
72+
echo "RESEND_API_KEY=resend_api_key" >> _environment
7373
7474
- name: Setup Graphviz
7575
uses: ts-graphviz/setup-graphviz@v2

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,17 @@
22
__pycache__
33
*.pyc
44
.env
5+
_environment
56
/.quarto/
67
_docs/
8+
*.ipynb
79
.pytest_cache/
810
.mypy_cache/
911
node_modules
1012
package-lock.json
1113
package.json
1214
.specstory
1315
.cursorrules
16+
.cursor
17+
repomix-output.txt
18+
artifacts/

.python-version

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
3.13

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,15 +164,15 @@ Make sure the development database is running and tables and default
164164
permissions/roles are created first.
165165

166166
``` bash
167-
uvicorn main:app --host 0.0.0.0 --port 8000 --reload
167+
uv run uvicorn main:app --host 0.0.0.0 --port 8000 --reload
168168
```
169169

170170
Navigate to http://localhost:8000/
171171

172172
### Lint types with mypy
173173

174174
``` bash
175-
mypy .
175+
uv run mypy .
176176
```
177177

178178
## Developing with LLMs

docs/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/.quarto/

docs/architecture.qmd

Lines changed: 4 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ with dot.subgraph(name='cluster_client') as client:
2525
# Create server subgraph below
2626
with dot.subgraph(name='cluster_server') as server:
2727
server.attr(label='Server')
28-
server.node('C', 'Convert to Pydantic model', fillcolor='lightgreen', style='rounded,filled')
29-
server.node('D', 'Optional custom validation', fillcolor='lightgreen', style='rounded,filled')
28+
server.node('C', 'FastAPI request validation in route signature', fillcolor='lightgreen', style='rounded,filled')
29+
server.node('D', 'Business logic validation in route function body', fillcolor='lightgreen', style='rounded,filled')
3030
server.node('E', 'Update database', fillcolor='lightgreen', style='rounded,filled')
3131
server.node('F', 'Middleware error handler', fillcolor='lightgreen', style='rounded,filled')
3232
server.node('G', 'Render error template', fillcolor='lightgreen', style='rounded,filled')
@@ -59,103 +59,6 @@ dot.render('static/data_flow', format='png', cleanup=True)
5959

6060
![Data flow diagram](static/data_flow.png)
6161

62-
The advantage of the PRG pattern is that it is very straightforward to implement and keeps most of the rendering logic on the server side. The disadvantage is that it requires an extra round trip to the database to fetch the updated data, and re-rendering the entire page template may be less efficient than a partial page update on the client side.
62+
The advantage of the PRG pattern is that it is very straightforward to implement and keeps most of the rendering logic on the server side. One disadvantage is that it requires an extra round trip to the database to fetch the updated data, and re-rendering the entire page template may be less efficient than a partial page update on the client side. Another disadvantage is that it if the user makes an invalid form submission, they will see an error page and will have to click the browser's "back" button to get back to the form with their original form inputs.
6363

64-
## Form validation flow
65-
66-
We've experimented with several approaches to validating form inputs in the FastAPI endpoints.
67-
68-
### Objectives
69-
70-
Ideally, on an invalid input, we would redirect the user back to the form, preserving their inputs and displaying an error message about which input was invalid.
71-
72-
This would keep the error handling consistent with the PRG pattern described in the [Architecture](https://promptlytechnologies.com/fastapi-jinja2-postgres-webapp/docs/architecture) section of this documentation.
73-
74-
To keep the code DRY, we'd also like to handle such validation with Pydantic dependencies, Python exceptions, and exception-handling middleware as much as possible.
75-
76-
### Obstacles
77-
78-
One challenge is that if we redirect back to the page with the form, the page is re-rendered with empty form fields.
79-
80-
This can be overcome by passing the inputs from the request as context variables to the template.
81-
82-
But that's a bit clunky, because then we have to support form-specific context variables in every form page and corresponding GET endpoint.
83-
84-
Also, we have to:
85-
86-
1. access the request object (which is not by default available to our middleware), and
87-
2. extract the form inputs (at least one of which is invalid in this error case), and
88-
3. pass the form inputs to the template (which is a bit challenging to do in a DRY way since there are different sets of form inputs for different forms).
89-
90-
Solving these challenges is possible, but gets high-complexity pretty quickly.
91-
92-
### Approaches
93-
94-
The best solution, I think, is to use really robust client-side form validation to prevent invalid inputs from being sent to the server in the first place. That makes it less important what we do on the server side, although we still need to handle the server-side error case as a backup in the event that something slips past our validation on the client side.
95-
96-
Here are some patterns we've considered for server-side error handling:
97-
98-
<style>
99-
.styled-table, .styled-table th, .styled-table td {
100-
border: 1px solid black;
101-
padding: 8px;
102-
border-collapse: collapse;
103-
}
104-
105-
.styled-table th:nth-child(1) { width: 50%; }
106-
.styled-table th:nth-child(2),
107-
.styled-table th:nth-child(3),
108-
.styled-table th:nth-child(4) { width: 15%; }
109-
.styled-table th:nth-child(5) { width: 10%; }
110-
</style>
111-
112-
<table class="styled-table">
113-
<thead>
114-
<tr>
115-
<th>Approach</th>
116-
<th>Returns to same page</th>
117-
<th>Preserves form inputs</th>
118-
<th>Follows PRG pattern</th>
119-
<th>Complexity</th>
120-
</tr>
121-
</thead>
122-
<tbody>
123-
<tr>
124-
<td>Validate with Pydantic dependency, catch and redirect from middleware (with exception message as context) to an error page with "go back" button</td>
125-
<td>No</td>
126-
<td>Yes</td>
127-
<td>Yes</td>
128-
<td>Low</td>
129-
</tr>
130-
<tr>
131-
<td>Validate in FastAPI endpoint function body, redirect to origin page with error message query param</td>
132-
<td>Yes</td>
133-
<td>No</td>
134-
<td>Yes</td>
135-
<td>Medium</td>
136-
</tr>
137-
<tr>
138-
<td>Validate in FastAPI endpoint function body, redirect to origin page with error message query param and form inputs as context so we can re-render page with original form inputs</td>
139-
<td>Yes</td>
140-
<td>Yes</td>
141-
<td>Yes</td>
142-
<td>High</td>
143-
</tr>
144-
<tr>
145-
<td>Validate with Pydantic dependency, use session context to get form inputs from request, redirect to origin page from middleware with exception message and form inputs as context so we can re-render page with original form inputs</td>
146-
<td>Yes</td>
147-
<td>Yes</td>
148-
<td>Yes</td>
149-
<td>High</td>
150-
</tr>
151-
<tr>
152-
<td>Validate in either Pydantic dependency or function endpoint body and directly return error message or error toast HTML partial in JSON, then mount error toast with HTMX or some simple layout-level Javascript</td>
153-
<td>Yes</td>
154-
<td>Yes</td>
155-
<td>No</td>
156-
<td>Low</td>
157-
</tr>
158-
</tbody>
159-
</table>
160-
161-
Presently this template primarily uses option 1 but also supports option 2. Ultimately, I think option 5 will be preferable; support for that [is planned](https://github.com/Promptly-Technologies-LLC/fastapi-jinja2-postgres-webapp/issues/5) for a future update or fork of this template.
64+
A future iteration of this application will use HTMX to update the page in place, so that on an invalid submission an error toast is displayed without a page reload (thus preserving the user's scroll position and form inputs).

docs/contributing.qmd

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,17 @@ To contribute code to the project:
4343

4444
### Rendering the documentation
4545

46-
The README and documentation website are rendered with [Quarto](https://quarto.org/docs/). If you ,make changes to the `.qmd` files in the root folder and the `docs` folder, run the following commands to re-render the docs:
46+
The README and documentation website are rendered with [Quarto](https://quarto.org/docs/). If you make changes to the `.qmd` files in the root folder and the `docs` folder, you will need to re-render the docs with Quarto.
47+
48+
Quarto expects environment variables to be set in a file called `_environment`, so before running Quarto render commands, you should copy your `.env` file to `_environment`.
4749

4850
``` bash
51+
# To copy the .env file to _environment
52+
cp .env _environment
4953
# To render the documentation website
50-
quarto render
54+
uv run quarto render
5155
# To render the README
52-
quarto render index.qmd --output-dir . --output README.md --to gfm
56+
uv run quarto render index.qmd --output-dir . --output README.md --to gfm
5357
```
5458

5559
Due to a quirk of Quarto, an unnecessary `index.html` file is created in the root folder when the README is rendered. This file can be safely deleted.
@@ -74,5 +78,5 @@ When creating new features,
7478
To publish the documentation to GitHub Pages, run the following command:
7579

7680
``` bash
77-
quarto publish gh-pages
81+
uv run quarto publish gh-pages
7882
```

0 commit comments

Comments
 (0)