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 the following methods for managing soft delete functionality:

soft_delete(user_id=None): Marks the record as deleted by setting a deleted_at timestamp. You can also specify the ID of the user who performed the deletion.

restore(user_id=None): Restores a soft-deleted record by resetting deleted_at. You can also specify the ID of the user who performed the restoration.

force_delete(): Permanently removes the record from the database, an action that cannot be undone.

get_active(): Retrieves all records that are not soft-deleted.

get_deleted(): Retrieves only the records that have been soft-deleted.

force_delete_all_deleted(): Permanently deletes all records that have been soft-deleted.

restore_all(): Restores all soft-deleted records.

Examples

Create a new record

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

Soft delete the record

sample.soft_delete(user_id=1)

Restore the record

sample.restore(user_id=1)

Permanently delete the record

sample.force_delete()

Retrieve All Active Records

active_records = SampleModel.get_active()

Retrieve All Deleted Records

deleted_records = SampleModel.get_deleted()

Permanently Delete All Deleted Records

SampleModel.force_delete_all_deleted()

Restore All Deleted Records

SampleModel.restore_all()

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.

This README provides a comprehensive overview of the Flask-SoftDelete module, including its installation, configuration, usage, methods, and examples. Let me know if you need any further adjustments or additions!

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.1.0.tar.gz (5.4 kB view details)

Uploaded Source

Built Distribution

flask_softdelete-2.1.0-py3-none-any.whl (5.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: flask-softdelete-2.1.0.tar.gz
  • Upload date:
  • Size: 5.4 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.1.0.tar.gz
Algorithm Hash digest
SHA256 fb0e0a04eecc136a1e00e96fc61bde4ea8d02a1c12909ae8747885d0652d53d3
MD5 05746b5df9bdd4b222cf94680026498a
BLAKE2b-256 63b466c438489b79fc164f463b44501ec198b6c998050b0e6d5ade73816dfbf8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for flask_softdelete-2.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b583fed61df85d513d45cbbe9c52ed3dbaa4e06266e055c1ade2bb071e7a7945
MD5 51819d9b176334bed756371c334f8ca8
BLAKE2b-256 fb73ee9aeee9af0c62f03bb97d5d1db113c0666338fa9a7d3f1a025b4788f086

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