# 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](#installation)
- [Configuration](#configuration)
- [Usage](#usage)
- [Base Model](#base-model)
- [Record Management Methods](#record-management-methods)
- [Examples](#examples)
- [Logging](#logging)
- [Contributing](#contributing)
- [License](#license)
## Installation
To install Flask-Softdelete, use pip:
```bash
pip install Flask-Softdelete
```
## Configuration
```python
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
```python
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
```python
sample = SampleModel(name="Example")
db.session.add(sample)
db.session.commit()
```
# Soft delete the record
```python
sample.soft_delete(user_id=1)
```
# Restore the record
```python
sample.restore(user_id=1)
```
# Permanently delete the record
```python
sample.force_delete()
```
# Retrieve All Active Records
```python
active_records = SampleModel.get_active()
```
# Retrieve All Deleted Records
```python
deleted_records = SampleModel.get_deleted()
```
# Permanently Delete All Deleted Records
```python
SampleModel.force_delete_all_deleted()
```
# Restore All Deleted Records
```python
SampleModel.restore_all()
```
## Logging
```python
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.
```vbnet
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!
```
Raw data
{
"_id": null,
"home_page": "https://github.com/Moesthetics-code/flask-softdelete",
"name": "flask-softdelete",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.6",
"maintainer_email": null,
"keywords": null,
"author": "Mohamed Ndiaye",
"author_email": "mintok2000@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/63/b4/66c438489b79fc164f463b44501ec198b6c998050b0e6d5ade73816dfbf8/flask-softdelete-2.1.0.tar.gz",
"platform": null,
"description": "# Flask-Softdelete\r\n\r\nFlask-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.\r\n\r\n## Table of Contents\r\n\r\n- [Installation](#installation)\r\n- [Configuration](#configuration)\r\n- [Usage](#usage)\r\n - [Base Model](#base-model)\r\n - [Record Management Methods](#record-management-methods)\r\n- [Examples](#examples)\r\n- [Logging](#logging)\r\n- [Contributing](#contributing)\r\n- [License](#license)\r\n\r\n## Installation\r\n\r\nTo install Flask-Softdelete, use pip:\r\n\r\n```bash\r\npip install Flask-Softdelete\r\n```\r\n\r\n## Configuration\r\n\r\n```python\r\nfrom flask import Flask\r\nfrom flask_sqlalchemy import SQLAlchemy\r\nfrom flask_softdelete import SoftDeleteMixin\r\n\r\napp = Flask(__name__)\r\napp.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///your_database.db'\r\ndb = SQLAlchemy(app)\r\n```\r\n\r\n## Usage\r\n\r\n## Base Model\r\n\r\n```python\r\nclass SampleModel(db.Model, SoftDeleteMixin):\r\n __tablename__ = 'sample_model'\r\n\r\n id = db.Column(db.Integer, primary_key=True)\r\n name = db.Column(db.String(50))\r\n```\r\n\r\n## Record Management Methods\r\n\r\nFlask-Softdelete provides the following methods for managing soft delete functionality:\r\n\r\nsoft_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.\r\n\r\nrestore(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.\r\n\r\nforce_delete(): Permanently removes the record from the database, an action that cannot be undone.\r\n\r\nget_active(): Retrieves all records that are not soft-deleted.\r\n\r\nget_deleted(): Retrieves only the records that have been soft-deleted.\r\n\r\nforce_delete_all_deleted(): Permanently deletes all records that have been soft-deleted.\r\n\r\nrestore_all(): Restores all soft-deleted records.\r\n\r\n## Examples\r\n\r\n# Create a new record\r\n\r\n```python\r\nsample = SampleModel(name=\"Example\")\r\ndb.session.add(sample)\r\ndb.session.commit()\r\n```\r\n\r\n# Soft delete the record\r\n\r\n```python\r\nsample.soft_delete(user_id=1)\r\n```\r\n\r\n# Restore the record\r\n\r\n```python\r\nsample.restore(user_id=1)\r\n```\r\n\r\n# Permanently delete the record\r\n\r\n```python\r\nsample.force_delete()\r\n```\r\n\r\n# Retrieve All Active Records\r\n\r\n```python\r\nactive_records = SampleModel.get_active()\r\n```\r\n\r\n# Retrieve All Deleted Records\r\n\r\n```python\r\ndeleted_records = SampleModel.get_deleted()\r\n```\r\n\r\n# Permanently Delete All Deleted Records\r\n\r\n```python\r\nSampleModel.force_delete_all_deleted()\r\n```\r\n\r\n# Restore All Deleted Records\r\n\r\n```python\r\nSampleModel.restore_all()\r\n```\r\n\r\n## Logging\r\n\r\n```python\r\nimport logging\r\n\r\nlogging.basicConfig(level=logging.INFO)\r\nlogger = logging.getLogger(__name__)\r\n\r\n# Log soft delete action\r\nlogger.info(f\"Soft deleted record with ID: {sample.id}\")\r\n```\r\n\r\n## Contributing\r\n\r\nIf you would like to contribute to Flask-Softdelete, please fork the repository and submit a pull request. You can find the repository on GitHub.\r\n\r\n## Reporting Issues\r\n\r\nIf 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.\r\n\r\n## License\r\n\r\nMIT License\r\n\r\nCopyright (c) 2024 Mohamed Ndiaye\r\n\r\nPermission is hereby granted, free of charge, to any person obtaining a copy\r\nof this software and associated documentation files (the \"Software\"), to deal\r\nin the Software without restriction, including without limitation the rights\r\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\r\ncopies of the Software, and to permit persons to whom the Software is\r\nfurnished to do so, subject to the following conditions:\r\n\r\n1. The above copyright notice and this permission notice shall be included in\r\n all copies or substantial portions of the Software.\r\n\r\n2. THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r\n IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\r\n FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\r\n AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\r\n LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\r\n OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\r\n SOFTWARE.\r\n\r\n```vbnet\r\nThis 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!\r\n```\r\n",
"bugtrack_url": null,
"license": null,
"summary": "A simple mixin for adding soft delete functionality to Flask-SQLAlchemy models",
"version": "2.1.0",
"project_urls": {
"Homepage": "https://github.com/Moesthetics-code/flask-softdelete"
},
"split_keywords": [],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "fb73ee9aeee9af0c62f03bb97d5d1db113c0666338fa9a7d3f1a025b4788f086",
"md5": "51819d9b176334bed756371c334f8ca8",
"sha256": "b583fed61df85d513d45cbbe9c52ed3dbaa4e06266e055c1ade2bb071e7a7945"
},
"downloads": -1,
"filename": "flask_softdelete-2.1.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "51819d9b176334bed756371c334f8ca8",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.6",
"size": 5523,
"upload_time": "2024-10-21T13:31:24",
"upload_time_iso_8601": "2024-10-21T13:31:24.221370Z",
"url": "https://files.pythonhosted.org/packages/fb/73/ee9aeee9af0c62f03bb97d5d1db113c0666338fa9a7d3f1a025b4788f086/flask_softdelete-2.1.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "63b466c438489b79fc164f463b44501ec198b6c998050b0e6d5ade73816dfbf8",
"md5": "05746b5df9bdd4b222cf94680026498a",
"sha256": "fb0e0a04eecc136a1e00e96fc61bde4ea8d02a1c12909ae8747885d0652d53d3"
},
"downloads": -1,
"filename": "flask-softdelete-2.1.0.tar.gz",
"has_sig": false,
"md5_digest": "05746b5df9bdd4b222cf94680026498a",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.6",
"size": 5376,
"upload_time": "2024-10-21T13:31:25",
"upload_time_iso_8601": "2024-10-21T13:31:25.576649Z",
"url": "https://files.pythonhosted.org/packages/63/b4/66c438489b79fc164f463b44501ec198b6c998050b0e6d5ade73816dfbf8/flask-softdelete-2.1.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-10-21 13:31:25",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "Moesthetics-code",
"github_project": "flask-softdelete",
"github_not_found": true,
"lcname": "flask-softdelete"
}