Skip to main content

Enhanced Flask-Smorest blueprints with automatic CRUD operations

Project description

Flask-More-Smorest

PyPI version Python Support License: MIT

Flask-More-Smorest extends Flask-Smorest with batteries-included CRUD blueprints, SQLAlchemy helpers, and an opinionated user/permission system.

Highlights

  • Automatic CRUD endpoints with filtering and pagination
  • Blueprint mixins for public/admin annotations and operation IDs
  • SQLAlchemy base model with Marshmallow schemas and permission hooks
  • Optional JWT-powered user, role, and token models

Quick Start

from flask import Flask
from flask_more_smorest import init_db
from flask_more_smorest.perms import Api, CRUDBlueprint

app = Flask(__name__)
app.config.update(
    API_TITLE="Example API",
    API_VERSION="v1",
    OPENAPI_VERSION="3.0.2",
    SQLALCHEMY_DATABASE_URI="sqlite:///example.db",
    SECRET_KEY="change-me",
    JWT_SECRET_KEY="change-me-too",
)

init_db(app)          # sets up SQLAlchemy
api = Api(app)        # registers JWT + permission hooks

users = CRUDBlueprint(
    "users",
    __name__,
    model="User",        # resolved from your models module
    schema="UserSchema",  # resolved from your schemas module
    url_prefix="/api/users/",
)

api.register_blueprint(users)

The blueprint above exposes the usual REST operations (GET, POST, PATCH, DELETE) plus automatic filtering (/api/users/?created_at__from=...).

Controlling generated endpoints

Use the methods and skip_methods parameters to control which CRUD routes are created:

from flask_more_smorest.crud.crud_blueprint import CRUDMethod

users = CRUDBlueprint(
    "users",
    __name__,
    model="User",
    schema="UserSchema",
    methods={
        CRUDMethod.INDEX: True,            # defaults to GET list
        CRUDMethod.POST: {"schema": "UserWriteSchema"},
        CRUDMethod.DELETE: {"admin_only": True},
    },
    skip_methods=[CRUDMethod.PATCH],
)

methods accepts either a list of CRUDMethod enums or a mapping of enum → config. Each config can override the request/response schema or mark the route as admin_only. skip_methods is evaluated after methods, making it useful for turning off a default route.

Working with models

Use BaseModel to get UUID keys, timestamp fields, and auto-generated Marshmallow schemas:

from flask_more_smorest import BaseModel
from flask_more_smorest.sqla import db
from sqlalchemy.orm import Mapped, mapped_column

class Article(BaseModel):
    __tablename__ = "article"

    title: Mapped[str] = mapped_column(db.String(200), nullable=False)
    body: Mapped[str] = mapped_column(db.Text, nullable=False)

    def _can_write(self) -> bool:  # optional permission hook
        return self.is_current_user_admin()

Article.Schema() instantly provides a Marshmallow schema (including an is_writable field) ready for use in blueprints.

Built-in user system

Import the ready-made models when you need authentication, roles, or tokens:

from flask_more_smorest.perms import DefaultUserRole, User, UserRole

user = User(email="admin@example.com")
user.set_password("secret")
user.save()

UserRole(user=user, role=DefaultUserRole.ADMIN).save()

Extending the default user is straightforward—inherit from User and add your fields or mixins:

from flask_more_smorest.perms import ProfileMixin, TimestampMixin, User
from flask_more_smorest.sqla import db
from sqlalchemy.orm import Mapped, mapped_column

class Employee(User, ProfileMixin, TimestampMixin):
    __tablename__ = "employee"
    employee_id: Mapped[str] = mapped_column(db.String(32), unique=True)

Learn more

  • API reference and guides: project documentation
  • Examples and tests under the tests/ directory demonstrate filters, permissions, and pagination end-to-end.

Contributions and feedback are welcome—see CONTRIBUTING.md for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

flask_more_smorest-0.2.1.tar.gz (41.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

flask_more_smorest-0.2.1-py3-none-any.whl (46.8 kB view details)

Uploaded Python 3

File details

Details for the file flask_more_smorest-0.2.1.tar.gz.

File metadata

  • Download URL: flask_more_smorest-0.2.1.tar.gz
  • Upload date:
  • Size: 41.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for flask_more_smorest-0.2.1.tar.gz
Algorithm Hash digest
SHA256 a449c50cd1bbb68811059ffeac69bd77e69e33e8662c6391f6861c1a8b091acf
MD5 0be8d17b6bf7ff699f64a010fb86acc2
BLAKE2b-256 f4d866c42c54e5fa4210b7bb1fb2cd1073e99e59dd814ca0ca115841c81e9bdb

See more details on using hashes here.

Provenance

The following attestation bundles were made for flask_more_smorest-0.2.1.tar.gz:

Publisher: ci-cd.yml on qualisero/flask-more-smorest

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file flask_more_smorest-0.2.1-py3-none-any.whl.

File metadata

File hashes

Hashes for flask_more_smorest-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 a0b80c291e455dc0ee0c2cc6f1028f43f2b24553b5f0b396ae6ca0631e91ddb6
MD5 ee41494872205a42c20b8dc5a87b5172
BLAKE2b-256 db0c3402290f774434a5eaa3e485e1f93ee26c5848ae684f5ee2cb17b8b9d6b5

See more details on using hashes here.

Provenance

The following attestation bundles were made for flask_more_smorest-0.2.1-py3-none-any.whl:

Publisher: ci-cd.yml on qualisero/flask-more-smorest

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page