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__ auto-generated as "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):
    # Inherits User's table (single table inheritance)
    # Use __tablename__ = "employees" for separate table
    employee_id: Mapped[str] = mapped_column(db.String(32), unique=True)

Learn more

  • 📚 Documentation: ReadTheDocs (automatically updated on each release)
  • 🔧 API Reference: Full API documentation and guides available online
  • 💡 Examples: The tests/ directory demonstrates filters, permissions, and pagination end-to-end
  • 🚀 Release Process: See docs/READTHEDOCS_SETUP.md for CI/CD and documentation automation

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.3.tar.gz (44.5 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.3-py3-none-any.whl (48.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: flask_more_smorest-0.2.3.tar.gz
  • Upload date:
  • Size: 44.5 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.3.tar.gz
Algorithm Hash digest
SHA256 8617c499d9185e188cc1e4c11c1ec53c0c5071e20c5471c6da2d8a056bfff421
MD5 a1e9e84347f50e8a2f852a2d994cffe1
BLAKE2b-256 6f5cc5238ba7b41c03eede93e5231d1d41087e58142b762b40ed28ae850959dc

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for flask_more_smorest-0.2.3-py3-none-any.whl
Algorithm Hash digest
SHA256 3b832509713ec6153bc915c1719c0c80d0bf63c8fcbe7dd6395793c2a0baae88
MD5 f714d99de1f8e58f02c6bcd898c3a552
BLAKE2b-256 899091bc2abc5e08b57dfaee16ba71c25488bbbb81ce5bb47d95b59783360e7c

See more details on using hashes here.

Provenance

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