Metadata-Version: 2.4
Name: flaskforge-cli
Version: 0.1.4
Summary: Django-like CLI that scaffolds and extends large Flask projects
Requires-Python: >=3.9
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: click>=8.1
Requires-Dist: Jinja2>=3.1
Requires-Dist: Flask>=3.0
Requires-Dist: python-dotenv>=1.0
Provides-Extra: dev
Requires-Dist: pytest; extra == "dev"
Dynamic: license-file

<p align="center">
  <img src="https://raw.githubusercontent.com/dvrone/flaskforge/main/logo.png" alt="flaskforge logo" width="400">
</p>

<p align="center"><b>A Django-like CLI for Flask.</b></p>

<p align="center">
  <a href="https://github.com/dvrone/flaskforge/actions/workflows/tests.yml"><img src="https://github.com/dvrone/flaskforge/actions/workflows/tests.yml/badge.svg" alt="Tests"></a>
  <a href="https://pypi.org/project/flaskforge-cli/"><img src="https://img.shields.io/pypi/v/flaskforge-cli" alt="PyPI version"></a>
</p>

flaskforge scaffolds a large, well-structured Flask project
(app factory, settings per environment, Django-style `INSTALLED_APPS`, tests, Docker)
and wires up Flask extensions with a single command.

```bash
pipx install flaskforge-cli          # or: pip install flaskforge-cli
flaskforge startproject shop --preset web
cd shop
python -m venv .venv && source .venv/bin/activate
pip install -r requirements-dev.txt
python manage.py makemigrations -m "initial" && python manage.py migrate
python manage.py createsuperuser
python manage.py runserver           # http://127.0.0.1:8000
```

The PyPI package is called **`flaskforge-cli`**; the command and the Python package are `flaskforge`.
Requires Python 3.9+.

## Commands

### Global: `flaskforge ...`

| Command | What it does |
| --- | --- |
| `startproject NAME [DIR] [--preset P] [-e EXT,...] [--install] [--force] [--dry-run]` | Create a project. Presets: `minimal`, `web`, `api`, `full` |
| `startapp NAME [--api]` | Create an app under `apps/` and register it in `INSTALLED_APPS` |
| `add EXT... [--no-install] [--force]` | `pip install` extensions and wire them into the project |
| `extensions` | List supported extensions and which are installed |

`startapp`, `add` and `extensions` work from any subdirectory of a project.

### Project-level: `python manage.py ...`

All the commands above, plus:

| Command | What it does |
| --- | --- |
| `runserver [-h HOST] [-p PORT]` | Dev server (port 8000, autoreload and debugger in development) |
| `makemigrations [-m MSG]` | Create migrations (runs `init` the first time) |
| `migrate` / `rollback` | Apply / revert migrations |
| `createsuperuser` | Create an admin user (when `login` is installed) |
| `test [LABEL...] [pytest args]` | Run the tests with `APP_ENV=testing` (see below) |
| `check` | Load the app and report blueprints and routes |
| `shell`, `routes`, `run` | Built-in Flask commands |

#### Running tests

```bash
python manage.py test                       # whole suite
python manage.py test products              # tests/test_products.py (an app or topic name)
python manage.py test products accounts     # several labels
python manage.py test products -x -k create # anything after the labels goes to pytest
python manage.py test tests/test_main.py::test_health   # paths and node ids work too
```

Labels come first. A label `x` is looked up as `tests/test_x.py`, `tests/x/`, `apps/x/tests/` and
`apps/x/tests.py`; an unknown label is reported as an error instead of silently running nothing.
Tests always run with `APP_ENV=testing` (in-memory SQLite, CSRF and rate limits off), whatever your
shell or `.env` says, so they never touch your development database. The exit code is pytest's,
which makes the command CI-friendly.

### Initialising inside an existing directory (e.g. a fresh git clone)

```bash
git clone git@github.com:you/flask-notes-app.git && cd flask-notes-app
flaskforge startproject flask_notes . --preset web
```

`startproject` works in a non-empty directory. Existing files are never clobbered:

| Existing file | What happens |
| --- | --- |
| `README.md` | kept exactly as is |
| `.gitignore` | missing entries are appended, your lines stay first |
| `.env`, `.env.example` | missing `KEY=value` pairs are appended, your values stay |
| `.git/`, `LICENSE`, any file flaskforge does not generate | untouched |
| any other file flaskforge generates (`manage.py`, `config/settings.py`, ...) | **conflict** |

All conflicts are detected before anything is written. If there are any, the command lists them
and exits without changing a single file. Move the files away, or pass `--force` to overwrite
them (`README.md`, `.gitignore` and `.env` files are still preserved, and directories are never
replaced). Use `--dry-run` to preview the result without writing anything.

## Generated layout

```
config/settings.py   Base / Development / Testing / Production configs, INSTALLED_APPS
core/__init__.py     create_app() factory; loads every app in INSTALLED_APPS
core/extensions.py   extension instances (db, login_manager, ...), filled in by `add`
apps/<name>/         views.py (bp), models.py, templates/<name>/, tests in tests/
templates/ static/   shared templates (base.html) and assets
tests/               pytest, with app/client fixtures
wsgi.py Dockerfile .env
```

Each app in `INSTALLED_APPS` is auto-loaded:

| File | Purpose |
| --- | --- |
| `models.py` | Imported so the ORM and migrations see your models |
| `events.py` | Imported (e.g. Flask-SocketIO handlers) |
| `admin.py` | Imported (Flask-Admin model views, like Django's `admin.py`) |
| `views.py` | Must define `bp` (or a `blueprints` list) |
| `commands.py` | `commands = [...]` are added to the CLI |

Apps are named in the plural and classes in the singular: `startapp products` creates
`apps/products/` (URL `/products/`, templates in `products/`) with `Product`, `ProductForm` and
`ProductSchema`. Only the last word of a snake_case name is singularized (`blog_categories` ->
`BlogCategory`); irregular plurals (`people` -> `Person`) and uncountable words (`news` -> `News`)
are handled.

Blueprints mounted under `/api` are exempt from CSRF (token-authenticated JSON APIs).

## Supported extensions

| Key | Package | Notes |
| --- | --- | --- |
| `sqlalchemy` | Flask-SQLAlchemy | `db`, `TimestampMixin` in `core/mixins.py` |
| `migrate` | Flask-Migrate | Requires `sqlalchemy` |
| `login` | Flask-Login | Scaffolds `apps/accounts` (User, register, login, logout, `createsuperuser`) |
| `admin` | Flask-Admin | Panel at `/admin`, **restricted to `is_admin` users**; requires `login` |
| `wtf` | Flask-WTF | Forms and global CSRF protection |
| `mail` | Flask-Mail | |
| `cors` | Flask-CORS | Applied to `/api/*` |
| `jwt` | Flask-JWT-Extended | |
| `cache` | Flask-Caching | |
| `limiter` | Flask-Limiter | |
| `babel` | Flask-Babel | |
| `marshmallow` | Flask-Marshmallow | Requires `sqlalchemy` |
| `socketio` | Flask-SocketIO | `runserver` uses `socketio.run()` automatically |
| `debugtoolbar` | Flask-DebugToolbar | Dev dependency, active only when `DEBUG=True` |
| `talisman` | Flask-Talisman | HTTPS forced in production |
| `compress` | Flask-Compress | |

Dependencies are resolved automatically (`add admin` also adds `login` and `sqlalchemy`).
Add a new extension by appending one `Extension(...)` entry to `src/flaskforge/registry.py`.

### Admin panel

```bash
python manage.py add admin
python manage.py createsuperuser
python manage.py runserver
# sign in at /accounts/login, then open /admin/
```

Register models of your own apps in `apps/<name>/admin.py` (generated automatically by
`startapp` when `admin` is installed). Always use `SecureModelView` from `core/admin_views.py`:
a bare Flask-Admin view has no access control. Anonymous visitors are redirected to the login
page and signed-in non-admins get `403`.

If Flask-Babel is installed in your environment, also run `add babel`: Flask-Admin uses it
automatically and it must be initialised.

## How it works

`add` inserts code above the `# forge:...` marker comments in `core/extensions.py` and
`config/settings.py` - please keep those comments. Operations are idempotent (running the same
command twice changes nothing) and existing files are never overwritten without `--force`.
Project metadata lives in `.flaskforge.json`.

## Development

```bash
git clone https://github.com/dvrone/flaskforge.git && cd flaskforge
python -m venv .venv && source .venv/bin/activate
pip install -e ".[dev]"
pytest
```

CI runs the test suite on Python 3.9-3.13, builds and installs the package in a clean
environment, and scaffolds a full-preset project with its own tests on every push and pull request.

### Releasing

```bash
# bump the version in pyproject.toml and src/flaskforge/__init__.py, then:
rm -rf dist && python -m build && twine upload dist/*
git commit -am "Release X.Y.Z" && git tag vX.Y.Z && git push origin main --tags
```

## License

See [LICENSE](https://github.com/dvrone/flaskforge/blob/main/LICENSE).
