Skip to main content

A simple mixin for adding soft delete functionality to Flask-SQLAlchemy models

Project description

Flask-Softdelete

Flask-SoftDelete is a simple extension for Flask applications that adds soft delete functionality to Flask-SQLAlchemy models. Instead of permanently deleting records, soft deleting allows you to mark records as "deleted" without actually removing them from the database. This is useful for keeping a history of deleted records or allowing for easy restoration.

Table of Contents

Installation

To install Flask-Softdelete, use pip:

pip install Flask-Softdelete

Configuration

from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_softdelete import SoftDeleteMixin

app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///your_database.db'
db = SQLAlchemy(app)

Usage

Base Model

class SampleModel(db.Model, SoftDeleteMixin):
    __tablename__ = 'sample_model'

    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(50))

Record Management Methods

Flask-Softdelete provides methods for managing soft delete functionality:

soft_delete(): Marks the record as deleted, but keeps it in the database. restore(): Restores a soft-deleted record, making it active again. force_delete(): Permanently removes the record from the database, which cannot be undone.

Examples

Create a new record

sample = SampleModel(name="Example")
db.session.add(sample)
db.session.commit()

# Soft delete the record
sample.soft_delete()

# Restore the record
sample.restore()

# Permanently delete the record
sample.force_delete()

Logging

import logging

logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

# Log soft delete action
logger.info(f"Soft deleted record with ID: {sample.id}")

Contributing

If you would like to contribute to Flask-Softdelete, please fork the repository and submit a pull request. You can find the repository on GitHub.

Reporting Issues

If you encounter any issues, please report them in the Issues section of the GitHub repository. This helps improve the package and assists others who might face similar issues.

License

MIT License

Copyright (c) 2024 Mohamed Ndiaye

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

  1. The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

  2. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

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-softdelete-2.0.0.tar.gz (5.1 kB view details)

Uploaded Source

Built Distribution

flask_softdelete-2.0.0-py3-none-any.whl (5.1 kB view details)

Uploaded Python 3

File details

Details for the file flask-softdelete-2.0.0.tar.gz.

File metadata

  • Download URL: flask-softdelete-2.0.0.tar.gz
  • Upload date:
  • Size: 5.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.6

File hashes

Hashes for flask-softdelete-2.0.0.tar.gz
Algorithm Hash digest
SHA256 d515b382a57d19fc7b395880cfc911c2866fc9fe1c976aa288a37ec1ea3d5b5c
MD5 74f67253ed2cc76676fe364afe8bc014
BLAKE2b-256 689c53c8bb40ce183a43bbbf7fc0ce080baa13c1657e347f13131785c13cc5f7

See more details on using hashes here.

File details

Details for the file flask_softdelete-2.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for flask_softdelete-2.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 194edd812a5146fcab47b6e88f08ce80467359f5836a7aefc5380e2b28e4c7f4
MD5 2bdffcb7f9a97618fe9aeb4f5088ce7d
BLAKE2b-256 f7c7e0f49a8bad51d55a05176c30d5212b3bf06d5dabb52e8b472d3cca835ce0

See more details on using hashes here.

Supported by

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