flaskforge-cli 0.1.0__tar.gz → 0.1.2__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (73) hide show
  1. flaskforge_cli-0.1.2/PKG-INFO +149 -0
  2. flaskforge_cli-0.1.2/README.md +134 -0
  3. {flaskforge_cli-0.1.0 → flaskforge_cli-0.1.2}/pyproject.toml +1 -1
  4. {flaskforge_cli-0.1.0 → flaskforge_cli-0.1.2}/src/flaskforge/__init__.py +1 -1
  5. {flaskforge_cli-0.1.0 → flaskforge_cli-0.1.2}/src/flaskforge/generators.py +5 -1
  6. flaskforge_cli-0.1.2/src/flaskforge/naming.py +136 -0
  7. {flaskforge_cli-0.1.0 → flaskforge_cli-0.1.2}/src/flaskforge/registry.py +20 -5
  8. flaskforge_cli-0.1.2/src/flaskforge/templates/app/admin.py.tpl +16 -0
  9. {flaskforge_cli-0.1.0 → flaskforge_cli-0.1.2}/src/flaskforge/templates/app/index.html.tpl +2 -2
  10. flaskforge_cli-0.1.2/src/flaskforge/templates/ext/admin/accounts_admin.py.tpl +14 -0
  11. flaskforge_cli-0.1.2/src/flaskforge/templates/ext/admin/admin_views.py.tpl +31 -0
  12. flaskforge_cli-0.1.2/src/flaskforge/templates/ext/admin/test_admin.py.tpl +30 -0
  13. flaskforge_cli-0.1.2/src/flaskforge/templates/project/README.md.tpl +54 -0
  14. {flaskforge_cli-0.1.0 → flaskforge_cli-0.1.2}/src/flaskforge/templates/project/core/__init__.py.tpl +2 -1
  15. flaskforge_cli-0.1.2/src/flaskforge/templates/project/requirements-dev.txt.tpl +3 -0
  16. flaskforge_cli-0.1.2/src/flaskforge_cli.egg-info/PKG-INFO +149 -0
  17. {flaskforge_cli-0.1.0 → flaskforge_cli-0.1.2}/src/flaskforge_cli.egg-info/SOURCES.txt +7 -1
  18. {flaskforge_cli-0.1.0 → flaskforge_cli-0.1.2}/tests/test_flaskforge.py +65 -0
  19. flaskforge_cli-0.1.2/tests/test_naming.py +80 -0
  20. flaskforge_cli-0.1.0/PKG-INFO +0 -77
  21. flaskforge_cli-0.1.0/README.md +0 -62
  22. flaskforge_cli-0.1.0/src/flaskforge/templates/project/README.md.tpl +0 -40
  23. flaskforge_cli-0.1.0/src/flaskforge/templates/project/requirements-dev.txt.tpl +0 -3
  24. flaskforge_cli-0.1.0/src/flaskforge_cli.egg-info/PKG-INFO +0 -77
  25. {flaskforge_cli-0.1.0 → flaskforge_cli-0.1.2}/LICENSE +0 -0
  26. {flaskforge_cli-0.1.0 → flaskforge_cli-0.1.2}/setup.cfg +0 -0
  27. {flaskforge_cli-0.1.0 → flaskforge_cli-0.1.2}/src/flaskforge/__main__.py +0 -0
  28. {flaskforge_cli-0.1.0 → flaskforge_cli-0.1.2}/src/flaskforge/cli.py +0 -0
  29. {flaskforge_cli-0.1.0 → flaskforge_cli-0.1.2}/src/flaskforge/commands.py +0 -0
  30. {flaskforge_cli-0.1.0 → flaskforge_cli-0.1.2}/src/flaskforge/management.py +0 -0
  31. {flaskforge_cli-0.1.0 → flaskforge_cli-0.1.2}/src/flaskforge/scaffold.py +0 -0
  32. {flaskforge_cli-0.1.0 → flaskforge_cli-0.1.2}/src/flaskforge/templates/app/__init__.py.tpl +0 -0
  33. {flaskforge_cli-0.1.0 → flaskforge_cli-0.1.2}/src/flaskforge/templates/app/forms.py.tpl +0 -0
  34. {flaskforge_cli-0.1.0 → flaskforge_cli-0.1.2}/src/flaskforge/templates/app/models.py.tpl +0 -0
  35. {flaskforge_cli-0.1.0 → flaskforge_cli-0.1.2}/src/flaskforge/templates/app/schemas.py.tpl +0 -0
  36. {flaskforge_cli-0.1.0 → flaskforge_cli-0.1.2}/src/flaskforge/templates/app/test_app.py.tpl +0 -0
  37. {flaskforge_cli-0.1.0 → flaskforge_cli-0.1.2}/src/flaskforge/templates/app/views.py.tpl +0 -0
  38. {flaskforge_cli-0.1.0 → flaskforge_cli-0.1.2}/src/flaskforge/templates/ext/login/__init__.py.tpl +0 -0
  39. {flaskforge_cli-0.1.0 → flaskforge_cli-0.1.2}/src/flaskforge/templates/ext/login/_csrf.html.tpl +0 -0
  40. {flaskforge_cli-0.1.0 → flaskforge_cli-0.1.2}/src/flaskforge/templates/ext/login/commands.py.tpl +0 -0
  41. {flaskforge_cli-0.1.0 → flaskforge_cli-0.1.2}/src/flaskforge/templates/ext/login/login.html.tpl +0 -0
  42. {flaskforge_cli-0.1.0 → flaskforge_cli-0.1.2}/src/flaskforge/templates/ext/login/models.py.tpl +0 -0
  43. {flaskforge_cli-0.1.0 → flaskforge_cli-0.1.2}/src/flaskforge/templates/ext/login/profile.html.tpl +0 -0
  44. {flaskforge_cli-0.1.0 → flaskforge_cli-0.1.2}/src/flaskforge/templates/ext/login/register.html.tpl +0 -0
  45. {flaskforge_cli-0.1.0 → flaskforge_cli-0.1.2}/src/flaskforge/templates/ext/login/test_accounts.py.tpl +0 -0
  46. {flaskforge_cli-0.1.0 → flaskforge_cli-0.1.2}/src/flaskforge/templates/ext/login/views.py.tpl +0 -0
  47. {flaskforge_cli-0.1.0 → flaskforge_cli-0.1.2}/src/flaskforge/templates/ext/sqlalchemy/mixins.py.tpl +0 -0
  48. {flaskforge_cli-0.1.0 → flaskforge_cli-0.1.2}/src/flaskforge/templates/project/Dockerfile.tpl +0 -0
  49. {flaskforge_cli-0.1.0 → flaskforge_cli-0.1.2}/src/flaskforge/templates/project/apps/__init__.py.tpl +0 -0
  50. {flaskforge_cli-0.1.0 → flaskforge_cli-0.1.2}/src/flaskforge/templates/project/apps/main/__init__.py.tpl +0 -0
  51. {flaskforge_cli-0.1.0 → flaskforge_cli-0.1.2}/src/flaskforge/templates/project/apps/main/templates/main/index.html.tpl +0 -0
  52. {flaskforge_cli-0.1.0 → flaskforge_cli-0.1.2}/src/flaskforge/templates/project/apps/main/views.py.tpl +0 -0
  53. {flaskforge_cli-0.1.0 → flaskforge_cli-0.1.2}/src/flaskforge/templates/project/config/__init__.py.tpl +0 -0
  54. {flaskforge_cli-0.1.0 → flaskforge_cli-0.1.2}/src/flaskforge/templates/project/config/settings.py.tpl +0 -0
  55. {flaskforge_cli-0.1.0 → flaskforge_cli-0.1.2}/src/flaskforge/templates/project/core/errors.py.tpl +0 -0
  56. {flaskforge_cli-0.1.0 → flaskforge_cli-0.1.2}/src/flaskforge/templates/project/core/extensions.py.tpl +0 -0
  57. {flaskforge_cli-0.1.0 → flaskforge_cli-0.1.2}/src/flaskforge/templates/project/dot_env.example.tpl +0 -0
  58. {flaskforge_cli-0.1.0 → flaskforge_cli-0.1.2}/src/flaskforge/templates/project/dot_env.tpl +0 -0
  59. {flaskforge_cli-0.1.0 → flaskforge_cli-0.1.2}/src/flaskforge/templates/project/dot_gitignore.tpl +0 -0
  60. {flaskforge_cli-0.1.0 → flaskforge_cli-0.1.2}/src/flaskforge/templates/project/manage.py.tpl +0 -0
  61. {flaskforge_cli-0.1.0 → flaskforge_cli-0.1.2}/src/flaskforge/templates/project/pytest.ini.tpl +0 -0
  62. {flaskforge_cli-0.1.0 → flaskforge_cli-0.1.2}/src/flaskforge/templates/project/requirements.txt.tpl +0 -0
  63. {flaskforge_cli-0.1.0 → flaskforge_cli-0.1.2}/src/flaskforge/templates/project/static/css/style.css.tpl +0 -0
  64. {flaskforge_cli-0.1.0 → flaskforge_cli-0.1.2}/src/flaskforge/templates/project/templates/base.html.tpl +0 -0
  65. {flaskforge_cli-0.1.0 → flaskforge_cli-0.1.2}/src/flaskforge/templates/project/templates/errors/error.html.tpl +0 -0
  66. {flaskforge_cli-0.1.0 → flaskforge_cli-0.1.2}/src/flaskforge/templates/project/tests/__init__.py.tpl +0 -0
  67. {flaskforge_cli-0.1.0 → flaskforge_cli-0.1.2}/src/flaskforge/templates/project/tests/conftest.py.tpl +0 -0
  68. {flaskforge_cli-0.1.0 → flaskforge_cli-0.1.2}/src/flaskforge/templates/project/tests/test_main.py.tpl +0 -0
  69. {flaskforge_cli-0.1.0 → flaskforge_cli-0.1.2}/src/flaskforge/templates/project/wsgi.py.tpl +0 -0
  70. {flaskforge_cli-0.1.0 → flaskforge_cli-0.1.2}/src/flaskforge_cli.egg-info/dependency_links.txt +0 -0
  71. {flaskforge_cli-0.1.0 → flaskforge_cli-0.1.2}/src/flaskforge_cli.egg-info/entry_points.txt +0 -0
  72. {flaskforge_cli-0.1.0 → flaskforge_cli-0.1.2}/src/flaskforge_cli.egg-info/requires.txt +0 -0
  73. {flaskforge_cli-0.1.0 → flaskforge_cli-0.1.2}/src/flaskforge_cli.egg-info/top_level.txt +0 -0
@@ -0,0 +1,149 @@
1
+ Metadata-Version: 2.4
2
+ Name: flaskforge-cli
3
+ Version: 0.1.2
4
+ Summary: Django-like CLI that scaffolds and extends large Flask projects
5
+ Requires-Python: >=3.9
6
+ Description-Content-Type: text/markdown
7
+ License-File: LICENSE
8
+ Requires-Dist: click>=8.1
9
+ Requires-Dist: Jinja2>=3.1
10
+ Requires-Dist: Flask>=3.0
11
+ Requires-Dist: python-dotenv>=1.0
12
+ Provides-Extra: dev
13
+ Requires-Dist: pytest; extra == "dev"
14
+ Dynamic: license-file
15
+
16
+ # flaskforge
17
+
18
+ A Django-like CLI for Flask. It scaffolds a large, well-structured Flask project
19
+ (app factory, settings per environment, Django-style `INSTALLED_APPS`, tests, Docker)
20
+ and wires up Flask extensions with a single command.
21
+
22
+ ```bash
23
+ pipx install flaskforge-cli # or: pip install flaskforge-cli
24
+ flaskforge startproject shop --preset web
25
+ cd shop
26
+ python -m venv .venv && source .venv/bin/activate
27
+ pip install -r requirements-dev.txt
28
+ python manage.py makemigrations -m "initial" && python manage.py migrate
29
+ python manage.py createsuperuser
30
+ python manage.py runserver # http://127.0.0.1:8000
31
+ ```
32
+
33
+ The PyPI package is called **`flaskforge-cli`**; the command and the Python package are `flaskforge`.
34
+ Requires Python 3.9+.
35
+
36
+ ## Commands
37
+
38
+ Global (`flaskforge ...`):
39
+
40
+ | Command | What it does |
41
+ | --- | --- |
42
+ | `startproject NAME [DIR] [--preset P] [-e EXT,...] [--install]` | Create a project. Presets: `minimal`, `web`, `api`, `full` |
43
+ | `startapp NAME [--api]` | Create an app under `apps/` and register it in `INSTALLED_APPS` |
44
+ | `add EXT... [--no-install] [--force]` | `pip install` extensions and wire them into the project |
45
+ | `extensions` | List supported extensions and which are installed |
46
+
47
+ `startapp`, `add` and `extensions` work from any subdirectory of a project.
48
+
49
+ Project-level (`python manage.py ...`) - the commands above plus:
50
+
51
+ | Command | What it does |
52
+ | --- | --- |
53
+ | `runserver [-h HOST] [-p PORT]` | Dev server (port 8000, autoreload and debugger in development) |
54
+ | `makemigrations [-m MSG]` | Create migrations (runs `init` the first time) |
55
+ | `migrate` / `rollback` | Apply / revert migrations |
56
+ | `createsuperuser` | Create an admin user (when `login` is installed) |
57
+ | `check` | Load the app and report blueprints and routes |
58
+ | `shell`, `routes`, `run` | Built-in Flask commands |
59
+
60
+ ## Generated layout
61
+
62
+ ```
63
+ config/settings.py Base / Development / Testing / Production configs, INSTALLED_APPS
64
+ core/__init__.py create_app() factory; loads every app in INSTALLED_APPS
65
+ core/extensions.py extension instances (db, login_manager, ...), filled in by `add`
66
+ apps/<name>/ views.py (bp), models.py, templates/<name>/, tests in tests/
67
+ templates/ static/ shared templates (base.html) and assets
68
+ tests/ pytest, with app/client fixtures
69
+ wsgi.py Dockerfile .env
70
+ ```
71
+
72
+ Each app in `INSTALLED_APPS` is auto-loaded:
73
+
74
+ | File | Purpose |
75
+ | --- | --- |
76
+ | `models.py` | Imported so the ORM and migrations see your models |
77
+ | `events.py` | Imported (e.g. Flask-SocketIO handlers) |
78
+ | `admin.py` | Imported (Flask-Admin model views, like Django's `admin.py`) |
79
+ | `views.py` | Must define `bp` (or a `blueprints` list) |
80
+ | `commands.py` | `commands = [...]` are added to the CLI |
81
+
82
+ Apps are named in the plural and classes in the singular: `startapp products` creates
83
+ `apps/products/` (URL `/products/`, templates in `products/`) with `Product`, `ProductForm` and
84
+ `ProductSchema`; only the last word of a snake_case name is singularized (`blog_categories` ->
85
+ `BlogCategory`), and irregular plurals (`people` -> `Person`) and uncountable words (`news` -> `News`)
86
+ are handled.
87
+
88
+ Blueprints mounted under `/api` are exempt from CSRF (token-authenticated JSON APIs).
89
+
90
+ ## Supported extensions
91
+
92
+ | Key | Package | Notes |
93
+ | --- | --- | --- |
94
+ | `sqlalchemy` | Flask-SQLAlchemy | `db`, `TimestampMixin` in `core/mixins.py` |
95
+ | `migrate` | Flask-Migrate | Requires `sqlalchemy` |
96
+ | `login` | Flask-Login | Scaffolds `apps/accounts` (User, register, login, logout, `createsuperuser`) |
97
+ | `admin` | Flask-Admin | Panel at `/admin`, **restricted to `is_admin` users**; requires `login` |
98
+ | `wtf` | Flask-WTF | Forms and global CSRF protection |
99
+ | `mail` | Flask-Mail | |
100
+ | `cors` | Flask-CORS | Applied to `/api/*` |
101
+ | `jwt` | Flask-JWT-Extended | |
102
+ | `cache` | Flask-Caching | |
103
+ | `limiter` | Flask-Limiter | |
104
+ | `babel` | Flask-Babel | |
105
+ | `marshmallow` | Flask-Marshmallow | Requires `sqlalchemy` |
106
+ | `socketio` | Flask-SocketIO | `runserver` uses `socketio.run()` automatically |
107
+ | `debugtoolbar` | Flask-DebugToolbar | Dev dependency, active only when `DEBUG=True` |
108
+ | `talisman` | Flask-Talisman | HTTPS forced in production |
109
+ | `compress` | Flask-Compress | |
110
+
111
+ Dependencies are resolved automatically (`add admin` also adds `login` and `sqlalchemy`).
112
+ Add a new extension by appending one `Extension(...)` entry to `src/flaskforge/registry.py`.
113
+
114
+ ### Admin panel
115
+
116
+ ```bash
117
+ python manage.py add admin
118
+ python manage.py createsuperuser
119
+ python manage.py runserver
120
+ # sign in at /accounts/login, then open /admin/
121
+ ```
122
+
123
+ Register models of your own apps in `apps/<name>/admin.py` (generated automatically by
124
+ `startapp` when `admin` is installed). Always use `SecureModelView` from `core/admin_views.py`:
125
+ a bare Flask-Admin view has no access control. Anonymous visitors are redirected to the login
126
+ page and signed-in non-admins get `403`.
127
+
128
+ If Flask-Babel is installed in your environment, also run `add babel`: Flask-Admin uses it
129
+ automatically and it must be initialised.
130
+
131
+ ## How it works
132
+
133
+ `add` inserts code above the `# forge:...` marker comments in `core/extensions.py` and
134
+ `config/settings.py` - please keep those comments. Operations are idempotent (running the same
135
+ command twice changes nothing) and existing files are never overwritten without `--force`.
136
+ Project metadata lives in `.flaskforge.json`.
137
+
138
+ ## Development
139
+
140
+ ```bash
141
+ git clone https://github.com/dvrone/flaskforge.git && cd flaskforge
142
+ pip install -e ".[dev]"
143
+ pytest
144
+ python -m build && twine upload dist/* # release (bump the version first)
145
+ ```
146
+
147
+ ## License
148
+
149
+ See [LICENSE](LICENSE).
@@ -0,0 +1,134 @@
1
+ # flaskforge
2
+
3
+ A Django-like CLI for Flask. It scaffolds a large, well-structured Flask project
4
+ (app factory, settings per environment, Django-style `INSTALLED_APPS`, tests, Docker)
5
+ and wires up Flask extensions with a single command.
6
+
7
+ ```bash
8
+ pipx install flaskforge-cli # or: pip install flaskforge-cli
9
+ flaskforge startproject shop --preset web
10
+ cd shop
11
+ python -m venv .venv && source .venv/bin/activate
12
+ pip install -r requirements-dev.txt
13
+ python manage.py makemigrations -m "initial" && python manage.py migrate
14
+ python manage.py createsuperuser
15
+ python manage.py runserver # http://127.0.0.1:8000
16
+ ```
17
+
18
+ The PyPI package is called **`flaskforge-cli`**; the command and the Python package are `flaskforge`.
19
+ Requires Python 3.9+.
20
+
21
+ ## Commands
22
+
23
+ Global (`flaskforge ...`):
24
+
25
+ | Command | What it does |
26
+ | --- | --- |
27
+ | `startproject NAME [DIR] [--preset P] [-e EXT,...] [--install]` | Create a project. Presets: `minimal`, `web`, `api`, `full` |
28
+ | `startapp NAME [--api]` | Create an app under `apps/` and register it in `INSTALLED_APPS` |
29
+ | `add EXT... [--no-install] [--force]` | `pip install` extensions and wire them into the project |
30
+ | `extensions` | List supported extensions and which are installed |
31
+
32
+ `startapp`, `add` and `extensions` work from any subdirectory of a project.
33
+
34
+ Project-level (`python manage.py ...`) - the commands above plus:
35
+
36
+ | Command | What it does |
37
+ | --- | --- |
38
+ | `runserver [-h HOST] [-p PORT]` | Dev server (port 8000, autoreload and debugger in development) |
39
+ | `makemigrations [-m MSG]` | Create migrations (runs `init` the first time) |
40
+ | `migrate` / `rollback` | Apply / revert migrations |
41
+ | `createsuperuser` | Create an admin user (when `login` is installed) |
42
+ | `check` | Load the app and report blueprints and routes |
43
+ | `shell`, `routes`, `run` | Built-in Flask commands |
44
+
45
+ ## Generated layout
46
+
47
+ ```
48
+ config/settings.py Base / Development / Testing / Production configs, INSTALLED_APPS
49
+ core/__init__.py create_app() factory; loads every app in INSTALLED_APPS
50
+ core/extensions.py extension instances (db, login_manager, ...), filled in by `add`
51
+ apps/<name>/ views.py (bp), models.py, templates/<name>/, tests in tests/
52
+ templates/ static/ shared templates (base.html) and assets
53
+ tests/ pytest, with app/client fixtures
54
+ wsgi.py Dockerfile .env
55
+ ```
56
+
57
+ Each app in `INSTALLED_APPS` is auto-loaded:
58
+
59
+ | File | Purpose |
60
+ | --- | --- |
61
+ | `models.py` | Imported so the ORM and migrations see your models |
62
+ | `events.py` | Imported (e.g. Flask-SocketIO handlers) |
63
+ | `admin.py` | Imported (Flask-Admin model views, like Django's `admin.py`) |
64
+ | `views.py` | Must define `bp` (or a `blueprints` list) |
65
+ | `commands.py` | `commands = [...]` are added to the CLI |
66
+
67
+ Apps are named in the plural and classes in the singular: `startapp products` creates
68
+ `apps/products/` (URL `/products/`, templates in `products/`) with `Product`, `ProductForm` and
69
+ `ProductSchema`; only the last word of a snake_case name is singularized (`blog_categories` ->
70
+ `BlogCategory`), and irregular plurals (`people` -> `Person`) and uncountable words (`news` -> `News`)
71
+ are handled.
72
+
73
+ Blueprints mounted under `/api` are exempt from CSRF (token-authenticated JSON APIs).
74
+
75
+ ## Supported extensions
76
+
77
+ | Key | Package | Notes |
78
+ | --- | --- | --- |
79
+ | `sqlalchemy` | Flask-SQLAlchemy | `db`, `TimestampMixin` in `core/mixins.py` |
80
+ | `migrate` | Flask-Migrate | Requires `sqlalchemy` |
81
+ | `login` | Flask-Login | Scaffolds `apps/accounts` (User, register, login, logout, `createsuperuser`) |
82
+ | `admin` | Flask-Admin | Panel at `/admin`, **restricted to `is_admin` users**; requires `login` |
83
+ | `wtf` | Flask-WTF | Forms and global CSRF protection |
84
+ | `mail` | Flask-Mail | |
85
+ | `cors` | Flask-CORS | Applied to `/api/*` |
86
+ | `jwt` | Flask-JWT-Extended | |
87
+ | `cache` | Flask-Caching | |
88
+ | `limiter` | Flask-Limiter | |
89
+ | `babel` | Flask-Babel | |
90
+ | `marshmallow` | Flask-Marshmallow | Requires `sqlalchemy` |
91
+ | `socketio` | Flask-SocketIO | `runserver` uses `socketio.run()` automatically |
92
+ | `debugtoolbar` | Flask-DebugToolbar | Dev dependency, active only when `DEBUG=True` |
93
+ | `talisman` | Flask-Talisman | HTTPS forced in production |
94
+ | `compress` | Flask-Compress | |
95
+
96
+ Dependencies are resolved automatically (`add admin` also adds `login` and `sqlalchemy`).
97
+ Add a new extension by appending one `Extension(...)` entry to `src/flaskforge/registry.py`.
98
+
99
+ ### Admin panel
100
+
101
+ ```bash
102
+ python manage.py add admin
103
+ python manage.py createsuperuser
104
+ python manage.py runserver
105
+ # sign in at /accounts/login, then open /admin/
106
+ ```
107
+
108
+ Register models of your own apps in `apps/<name>/admin.py` (generated automatically by
109
+ `startapp` when `admin` is installed). Always use `SecureModelView` from `core/admin_views.py`:
110
+ a bare Flask-Admin view has no access control. Anonymous visitors are redirected to the login
111
+ page and signed-in non-admins get `403`.
112
+
113
+ If Flask-Babel is installed in your environment, also run `add babel`: Flask-Admin uses it
114
+ automatically and it must be initialised.
115
+
116
+ ## How it works
117
+
118
+ `add` inserts code above the `# forge:...` marker comments in `core/extensions.py` and
119
+ `config/settings.py` - please keep those comments. Operations are idempotent (running the same
120
+ command twice changes nothing) and existing files are never overwritten without `--force`.
121
+ Project metadata lives in `.flaskforge.json`.
122
+
123
+ ## Development
124
+
125
+ ```bash
126
+ git clone https://github.com/dvrone/flaskforge.git && cd flaskforge
127
+ pip install -e ".[dev]"
128
+ pytest
129
+ python -m build && twine upload dist/* # release (bump the version first)
130
+ ```
131
+
132
+ ## License
133
+
134
+ See [LICENSE](LICENSE).
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "flaskforge-cli"
7
- version = "0.1.0"
7
+ version = "0.1.2"
8
8
  description = "Django-like CLI that scaffolds and extends large Flask projects"
9
9
  readme = "README.md"
10
10
  requires-python = ">=3.9"
@@ -1,3 +1,3 @@
1
1
  """flaskforge - a Django-like CLI for large Flask projects."""
2
2
 
3
- __version__ = "0.1.0"
3
+ __version__ = "0.1.2"
@@ -11,6 +11,7 @@ from pathlib import Path
11
11
  import click
12
12
 
13
13
  from . import __version__
14
+ from .naming import class_name_for, title_for
14
15
  from .registry import EXTENSIONS, PRESETS, Extension, resolve_key
15
16
  from .scaffold import (
16
17
  MANIFEST,
@@ -140,7 +141,8 @@ def create_app(project: Project, name: str, api: bool = False, force: bool = Fal
140
141
  ctx = _ctx(
141
142
  project,
142
143
  app_name=name,
143
- class_name="".join(p.capitalize() for p in name.split("_")),
144
+ class_name=class_name_for(name), # singular: products -> Product
145
+ app_title=title_for(name), # plural heading: products -> Products
144
146
  api=api,
145
147
  )
146
148
  click.secho(f"Creating {'API ' if api else ''}app '{name}'", bold=True)
@@ -157,6 +159,8 @@ def create_app(project: Project, name: str, api: bool = False, force: bool = Fal
157
159
  files.append(("app/forms.py.tpl", f"apps/{name}/forms.py"))
158
160
  elif "marshmallow" in exts:
159
161
  files.append(("app/schemas.py.tpl", f"apps/{name}/schemas.py"))
162
+ if "admin" in exts and "sqlalchemy" in exts:
163
+ files.append(("app/admin.py.tpl", f"apps/{name}/admin.py"))
160
164
  _render_files(project, files, ctx, force)
161
165
  register_app(project, f"apps.{name}")
162
166
  project.save()
@@ -0,0 +1,136 @@
1
+ """Naming helpers for generated code.
2
+
3
+ Apps are named in the plural (``products``, ``blog_posts``) while Python classes are
4
+ singular (``Product``, ``BlogPost``). The singularizer is a small rule-based English
5
+ implementation with an irregular-word table, so flaskforge needs no extra dependency.
6
+ Only the last ``snake_case`` word is singularized: ``order_items`` -> ``OrderItem``.
7
+ """
8
+
9
+ from __future__ import annotations
10
+
11
+ # Plural -> singular, checked first (exact match on the whole word).
12
+ IRREGULAR = {
13
+ "people": "person",
14
+ "men": "man",
15
+ "women": "woman",
16
+ "children": "child",
17
+ "feet": "foot",
18
+ "teeth": "tooth",
19
+ "mice": "mouse",
20
+ "geese": "goose",
21
+ "oxen": "ox",
22
+ "criteria": "criterion",
23
+ "phenomena": "phenomenon",
24
+ "indices": "index",
25
+ "matrices": "matrix",
26
+ "vertices": "vertex",
27
+ "appendices": "appendix",
28
+ "analyses": "analysis",
29
+ "bases": "basis",
30
+ "theses": "thesis",
31
+ "crises": "crisis",
32
+ "diagnoses": "diagnosis",
33
+ "hypotheses": "hypothesis",
34
+ "axes": "axis",
35
+ "leaves": "leaf",
36
+ "wolves": "wolf",
37
+ "knives": "knife",
38
+ "lives": "life",
39
+ "wives": "wife",
40
+ "shelves": "shelf",
41
+ "halves": "half",
42
+ "calves": "calf",
43
+ "thieves": "thief",
44
+ "loaves": "loaf",
45
+ "selves": "self",
46
+ "quizzes": "quiz",
47
+ }
48
+
49
+ # Same form in singular and plural (or not safely singularizable).
50
+ UNCHANGED = {
51
+ "news", "series", "species", "sheep", "fish", "deer", "moose", "aircraft",
52
+ "data", "media", "metadata", "info", "information", "equipment", "software",
53
+ "hardware", "feedback", "money", "analytics", "physics", "mathematics",
54
+ "economics", "politics", "statistics", "ethics", "athletics", "linguistics",
55
+ "means", "lens", "headquarters", "scissors", "pants", "jeans", "chaos",
56
+ "cosmos", "kudos", "bias", "atlas", "canvas", "gas", "plus", "alias",
57
+ "status", "bonus", "campus", "virus", "focus", "census", "corpus", "genus",
58
+ "bus", "lotus", "sinus", "apparatus", "radius", "cactus", "chorus", "iris",
59
+ "nexus", "stimulus", "syllabus", "fungus", "thesaurus", "octopus", "platypus",
60
+ "prospectus", "consensus", "nucleus", "terminus", "mucus", "surplus",
61
+ "tennis", "pelvis", "metropolis", "debris", "chassis",
62
+ }
63
+
64
+ # Singular words that end in "s" and form their plural with "-es" (buses, statuses).
65
+ _S_SINGULARS = {
66
+ "bus", "status", "bonus", "campus", "virus", "focus", "census", "corpus",
67
+ "genus", "lotus", "sinus", "alias", "atlas", "canvas", "gas", "plus", "bias",
68
+ "apparatus", "radius", "cactus", "chorus", "iris", "lens",
69
+ }
70
+
71
+ # "-ches" words whose singular is "-che" (caches -> cache), not "-ch" (batches -> batch).
72
+ _CHE_SINGULARS = {
73
+ "cache", "niche", "headache", "moustache", "mustache", "avalanche", "quiche",
74
+ "psyche", "creche", "brioche", "apache", "attache",
75
+ }
76
+
77
+ # "-ies" words whose singular is "-ie" (movies -> movie), not "-y".
78
+ _IE_SINGULARS = {
79
+ "movie", "cookie", "zombie", "pie", "tie", "selfie", "brownie", "rookie",
80
+ "hoodie", "genie", "lie", "die", "calorie", "smoothie", "birdie", "prairie",
81
+ }
82
+
83
+ # "-oes" words whose singular is "-oe" (shoes -> shoe), not "-o" (heroes -> hero).
84
+ _OE_SINGULARS = {"shoe", "toe", "canoe", "oboe", "foe", "hoe", "doe", "horseshoe"}
85
+
86
+
87
+ def singularize(word: str) -> str:
88
+ """Return the singular form of a lowercase English noun.
89
+
90
+ Already-singular words are returned unchanged (``product`` -> ``product``).
91
+ """
92
+ w = word.lower()
93
+ if len(w) < 2 or w in UNCHANGED:
94
+ return w
95
+ if w in IRREGULAR:
96
+ return IRREGULAR[w]
97
+
98
+ if w.endswith("ies") and len(w) > 4:
99
+ stem = w[:-1] # movies -> movie
100
+ return stem if stem in _IE_SINGULARS else w[:-3] + "y" # categories -> category
101
+ if w.endswith("ies"): # "ties", "pies", "lies": too short to be "-y" words
102
+ return w[:-1]
103
+ if w.endswith("sses"): # addresses -> address, classes -> class
104
+ return w[:-2]
105
+ if w.endswith("ses"):
106
+ with_s = w[:-2] # buses -> bus, statuses -> status
107
+ return with_s if with_s in _S_SINGULARS else w[:-1] # cases -> case, databases -> database
108
+ if w.endswith("ches"):
109
+ stem = w[:-1] # caches -> cache
110
+ return stem if stem in _CHE_SINGULARS else w[:-2] # batches -> batch
111
+ if w.endswith(("shes", "xes")): # dishes -> dish, boxes -> box
112
+ return w[:-2]
113
+ if w.endswith("zzes"): # buzzes -> buzz
114
+ return w[:-2]
115
+ if w.endswith("oes"):
116
+ stem = w[:-1] # shoes -> shoe
117
+ return stem if stem in _OE_SINGULARS else w[:-2] # heroes -> hero
118
+ if w.endswith("ves"): # archives -> archive, drives -> drive
119
+ return w[:-1]
120
+ if w.endswith(("ss", "sis")): # already singular: class, analysis, basis
121
+ return w
122
+ if w.endswith("s"):
123
+ return w[:-1]
124
+ return w
125
+
126
+
127
+ def class_name_for(app_name: str) -> str:
128
+ """``blog_posts`` -> ``BlogPost``; only the last word is singularized."""
129
+ parts = app_name.split("_")
130
+ parts[-1] = singularize(parts[-1])
131
+ return "".join(p.capitalize() for p in parts if p)
132
+
133
+
134
+ def title_for(app_name: str) -> str:
135
+ """Human-friendly plural heading: ``blog_posts`` -> ``Blog Posts``."""
136
+ return app_name.replace("_", " ").title()
@@ -222,12 +222,27 @@ _ALL = [
222
222
  Extension(
223
223
  key="admin",
224
224
  title="Flask-Admin",
225
- description="Auto-generated admin panel at /admin (admin)",
226
- packages=("Flask-Admin",),
227
- imports=("from flask_admin import Admin",),
228
- instances=('admin = Admin(name="Admin")',),
225
+ description="Admin panel at /admin, restricted to is_admin users (needs login)",
226
+ packages=("Flask-Admin>=2.0",),
227
+ imports=(
228
+ "from flask_admin import Admin",
229
+ "",
230
+ "from .admin_views import SecureAdminIndexView",
231
+ ),
232
+ instances=('admin = Admin(name="Admin", index_view=SecureAdminIndexView())',),
229
233
  init=("admin.init_app(app)",),
230
- notes="Register model views: admin.add_view(ModelView(Model, db.session))",
234
+ requires=("sqlalchemy", "login"),
235
+ files=(
236
+ ("ext/admin/admin_views.py.tpl", "core/admin_views.py"),
237
+ ("ext/admin/accounts_admin.py.tpl", "apps/accounts/admin.py"),
238
+ ("ext/admin/test_admin.py.tpl", "tests/test_admin.py"),
239
+ ),
240
+ notes=(
241
+ "Create an admin user with `manage.py createsuperuser`, sign in at /accounts/login, "
242
+ "open /admin/. Register models in apps/<name>/admin.py with SecureModelView. "
243
+ "If Flask-Babel is installed in this environment, also run `add babel` "
244
+ "(Flask-Admin uses it automatically and it must be initialised)."
245
+ ),
231
246
  ),
232
247
  Extension(
233
248
  key="debugtoolbar",
@@ -0,0 +1,16 @@
1
+ from core.admin_views import SecureModelView
2
+ from core.extensions import admin, db
3
+
4
+ from .models import [[ class_name ]]
5
+
6
+ # The `admin_` endpoint prefix keeps Flask-Admin's blueprint name from clashing
7
+ # with this app's own blueprint ("[[ app_name ]]").
8
+ admin.add_view(
9
+ SecureModelView(
10
+ [[ class_name ]],
11
+ db,
12
+ name="[[ app_title ]]",
13
+ endpoint="admin_[[ app_name ]]",
14
+ url="[[ app_name ]]",
15
+ )
16
+ )
@@ -1,9 +1,9 @@
1
1
  {% extends "base.html" %}
2
- {% block title %}[[ class_name ]]{% endblock %}
2
+ {% block title %}[[ app_title ]]{% endblock %}
3
3
 
4
4
  {% block content %}
5
5
  <section class="card">
6
- <h1>[[ class_name ]]</h1>
6
+ <h1>[[ app_title ]]</h1>
7
7
  <p>The <code>[[ app_name ]]</code> app works. Edit <code>apps/[[ app_name ]]/templates/[[ app_name ]]/index.html</code>.</p>
8
8
  </section>
9
9
  {% endblock %}
@@ -0,0 +1,14 @@
1
+ from core.admin_views import SecureModelView
2
+ from core.extensions import admin, db
3
+
4
+ from .models import User
5
+
6
+
7
+ class UserAdmin(SecureModelView):
8
+ column_list = ["id", "email", "is_admin", "created_at"]
9
+ column_searchable_list = ["email"]
10
+ form_columns = ["email", "is_admin"]
11
+ can_create = False # passwords are set via /accounts/register or `manage.py createsuperuser`
12
+
13
+
14
+ admin.add_view(UserAdmin(User, db, name="Users", endpoint="admin_users", url="users"))
@@ -0,0 +1,31 @@
1
+ """Access-controlled Flask-Admin views.
2
+
3
+ Only authenticated users with `is_admin = True` may open the admin panel:
4
+ * anonymous visitors are redirected to the login page,
5
+ * signed-in non-admins get 403.
6
+
7
+ Always use these classes instead of the bare Flask-Admin ones:
8
+ admin.add_view(SecureModelView(Model, db))
9
+ """
10
+ from flask import abort, redirect, request, url_for
11
+ from flask_admin import AdminIndexView
12
+ from flask_admin.contrib.sqla import ModelView
13
+ from flask_login import current_user
14
+
15
+
16
+ class AdminAccessMixin:
17
+ def is_accessible(self):
18
+ return current_user.is_authenticated and bool(getattr(current_user, "is_admin", False))
19
+
20
+ def inaccessible_callback(self, name, **kwargs):
21
+ if current_user.is_authenticated:
22
+ abort(403)
23
+ return redirect(url_for("accounts.login", next=request.path))
24
+
25
+
26
+ class SecureAdminIndexView(AdminAccessMixin, AdminIndexView):
27
+ pass
28
+
29
+
30
+ class SecureModelView(AdminAccessMixin, ModelView):
31
+ pass
@@ -0,0 +1,30 @@
1
+ from apps.accounts.models import User
2
+ from core.extensions import db
3
+
4
+ PASSWORD = "s3cret-pass"
5
+
6
+
7
+ def _login_as(client, email, is_admin):
8
+ user = User(email=email, is_admin=is_admin)
9
+ user.set_password(PASSWORD)
10
+ db.session.add(user)
11
+ db.session.commit()
12
+ client.post("/accounts/login", data={"email": email, "password": PASSWORD})
13
+
14
+
15
+ def test_admin_redirects_anonymous_to_login(client):
16
+ response = client.get("/admin/")
17
+ assert response.status_code == 302
18
+ assert "/accounts/login" in response.headers["Location"]
19
+
20
+
21
+ def test_admin_forbidden_for_regular_user(client):
22
+ _login_as(client, "user@example.com", is_admin=False)
23
+ assert client.get("/admin/").status_code == 403
24
+ assert client.get("/admin/users/").status_code == 403
25
+
26
+
27
+ def test_admin_open_for_admin_user(client):
28
+ _login_as(client, "boss@example.com", is_admin=True)
29
+ assert client.get("/admin/").status_code == 200
30
+ assert client.get("/admin/users/").status_code == 200
@@ -0,0 +1,54 @@
1
+ # [[ project ]]
2
+
3
+ A Flask project generated with [flaskforge](https://pypi.org/project/flaskforge-cli/).
4
+
5
+ ## Getting started
6
+
7
+ ```bash
8
+ python -m venv .venv && source .venv/bin/activate
9
+ pip install -r requirements-dev.txt
10
+ # .env was generated with a random SECRET_KEY (never commit it)
11
+ python manage.py runserver # http://127.0.0.1:8000
12
+ ```
13
+
14
+ If the project uses a database, create the tables first:
15
+
16
+ ```bash
17
+ python manage.py makemigrations -m "initial"
18
+ python manage.py migrate
19
+ ```
20
+
21
+ ## Layout
22
+
23
+ ```
24
+ config/settings.py settings (Base / Development / Testing / Production), INSTALLED_APPS
25
+ core/__init__.py create_app() - the application factory
26
+ core/extensions.py Flask extension instances (db, login_manager, ...)
27
+ apps/<name>/ one package per app: views.py (bp), models.py, templates/<name>/
28
+ templates/ shared templates (base.html)
29
+ tests/ pytest
30
+ ```
31
+
32
+ ## Commands
33
+
34
+ ```bash
35
+ python manage.py startapp blog # new app in apps/blog
36
+ python manage.py startapp posts --api # JSON API app at /api/posts
37
+ python manage.py add sqlalchemy migrate # install and wire up extensions
38
+ python manage.py extensions # list available extensions
39
+ python manage.py makemigrations -m "msg" # create a migration
40
+ python manage.py migrate # apply migrations
41
+ python manage.py routes # list URL rules
42
+ python manage.py shell
43
+ pytest
44
+ ```
45
+
46
+ Do not remove the `# forge:...` comments in `config/settings.py` and `core/extensions.py`:
47
+ flaskforge uses them to know where to insert code.
48
+
49
+ ## Deployment
50
+
51
+ ```bash
52
+ APP_ENV=production SECRET_KEY=... gunicorn wsgi:app
53
+ # or: docker build -t [[ project_slug ]] . && docker run -e SECRET_KEY=... -p 8000:8000 [[ project_slug ]]
54
+ ```