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 a number of enhancements and goodies, with the sole goal of drastically reducing boilerplate and complexity when creating a new REST API.

Highlights

  • Automatic CRUD endpoints with filtering and pagination
  • SQLAlchemy base model with Marshmallow schemas
  • User management
  • Resource-based permission management

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

critters = CRUDBlueprint(
    "critters",
    __name__,
    model="Critter",        # resolved from your models module
    schema="CritterSchema",  # resolved from your schemas module
    url_prefix="/api/critters/",
)

api.register_blueprint(critters)

The blueprint above exposes the usual REST operations (GET, POST, PATCH, DELETE) plus automatic filtering (/api/critters/?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
critters = CRUDBlueprint(
    "critters",
    __name__,
    model="Critter",
    schema="CritterSchema",
)

# Enable only specific methods (whitelist)
critters = CRUDBlueprint(
    "critters",
    __name__,
    model="Critter",
    schema="CritterSchema",
    methods=[CRUDMethod.INDEX, CRUDMethod.GET],
)

# Customize or disable specific methods (all enabled by default with dict)
critters = CRUDBlueprint(
    "critters",
    __name__,
    model="Critter",
    schema="CritterSchema",
    methods={
        CRUDMethod.POST: {"schema": "CritterWriteSchema"},  # 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 Critter(BaseModel):
    # __tablename__ auto-generated as "critter"
    
    name: Mapped[str] = mapped_column(db.String(100), nullable=False)
    species: Mapped[str] = mapped_column(db.String(50), nullable=False)
    cuteness_level: Mapped[int] = mapped_column(db.Integer, default=10)

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

Critter.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 (stable) or latest (dev)
  • 🔧 API Reference: Full API documentation and guides available online
  • 💡 Examples: The tests/ directory demonstrates filters, permissions, and pagination end-to-end

Release Process

Creating a new release:

./scripts/bump_version.sh [patch|minor|major]  # Updates version and provides next steps
# Then: update CHANGELOG.md, commit, tag, and create GitHub release

GitHub Actions automatically publishes to PyPI and updates ReadTheDocs.

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.4.tar.gz (45.4 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.4-py3-none-any.whl (49.4 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: flask_more_smorest-0.2.4.tar.gz
  • Upload date:
  • Size: 45.4 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.4.tar.gz
Algorithm Hash digest
SHA256 86d32e2031e48efb04a9ba7853d60fa721830d0a3dffa0fcb3bdfdc574baa699
MD5 ebe8c12e91731f2848bd4e13fffdab9f
BLAKE2b-256 3e483e1078e6a735eade1c28db59b4eab8c7114378bee0324eff3ffbf8035b66

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for flask_more_smorest-0.2.4-py3-none-any.whl
Algorithm Hash digest
SHA256 9e918bd2c350f94f80d8d7cb647ec5c5230faeed077940c2f8e5d1719965ac7e
MD5 220c34fdfb952047d9c84b4fb2bb161a
BLAKE2b-256 8e9ab60602cbbbfabb2e0307b2784833c8a5ae273a02d3acff79e46b6123e7d0

See more details on using hashes here.

Provenance

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