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

Control which CRUD routes are created using the methods parameter:

from flask_more_smorest.crud.crud_blueprint import CRUDMethod

# All methods enabled by default
users = CRUDBlueprint(
    "users",
    __name__,
    model="User",
    schema="UserSchema",
)

# Enable only specific methods (whitelist)
users = CRUDBlueprint(
    "users",
    __name__,
    model="User",
    schema="UserSchema",
    methods=[CRUDMethod.INDEX, CRUDMethod.GET],
)

# Customize or disable specific methods (all enabled by default with dict)
users = CRUDBlueprint(
    "users",
    __name__,
    model="User",
    schema="UserSchema",
    methods={
        CRUDMethod.POST: {"schema": "UserWriteSchema"},  # Custom schema
        CRUDMethod.DELETE: {"admin_only": True},         # Admin-only endpoint
        CRUDMethod.PATCH: False,                         # Disable this method
        # INDEX and GET not mentioned → enabled with defaults
    },
)

When using a dict, all methods are enabled by default. Specify a method to customize it, or set it to False to disable.

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.2.tar.gz (42.9 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.2-py3-none-any.whl (48.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: flask_more_smorest-0.2.2.tar.gz
  • Upload date:
  • Size: 42.9 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.2.tar.gz
Algorithm Hash digest
SHA256 897bad7da75d116853fca18d21ad3c244bdc4ffbb3f9f21d38919719f7aa5e96
MD5 e9058b14ee9cb5d833f9c68d1e6db715
BLAKE2b-256 a8b627b09c911021ca98c629c3ac26895fb3f302cedbc04dedb8db1a928b1f68

See more details on using hashes here.

Provenance

The following attestation bundles were made for flask_more_smorest-0.2.2.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.2-py3-none-any.whl.

File metadata

File hashes

Hashes for flask_more_smorest-0.2.2-py3-none-any.whl
Algorithm Hash digest
SHA256 a6db83843a32cb596cc50e023847f775544fc7a3160a64d1037b1a09f093cbde
MD5 4ba7b23c563249c5a148ddd34581fa05
BLAKE2b-256 487e0a3e6bb783a8764a30e266324e05f2771dd9c479bda1e0d06d66195c0bea

See more details on using hashes here.

Provenance

The following attestation bundles were made for flask_more_smorest-0.2.2-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