Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/wooyek/django-model-cleanup
Mixins for model cleanup methods and validation error concatenations
https://github.com/wooyek/django-model-cleanup
Last synced: about 1 month ago
JSON representation
Mixins for model cleanup methods and validation error concatenations
- Host: GitHub
- URL: https://github.com/wooyek/django-model-cleanup
- Owner: wooyek
- License: mit
- Created: 2017-10-04T16:54:20.000Z (about 7 years ago)
- Default Branch: develop
- Last Pushed: 2019-09-05T13:03:33.000Z (over 5 years ago)
- Last Synced: 2024-10-12T07:33:58.128Z (2 months ago)
- Language: Python
- Size: 69.3 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.rst
- Changelog: HISTORY.rst
- Contributing: CONTRIBUTING.rst
- License: LICENSE
Awesome Lists containing this project
README
====================
Django Model Cleanup
====================.. image:: https://img.shields.io/pypi/v/django-model-cleanup.svg
:target: https://pypi.python.org/pypi/django-model-cleanup.. image:: https://img.shields.io/travis/wooyek/django-model-cleanup.svg
:target: https://travis-ci.org/wooyek/django-model-cleanup.. image:: https://readthedocs.org/projects/django-model-cleanup/badge/?version=latest
:target: https://django-model-cleanup.readthedocs.io/en/latest/?badge=latest
:alt: Documentation Status
.. image:: https://coveralls.io/repos/github/wooyek/django-model-cleanup/badge.svg?branch=develop
:target: https://coveralls.io/github/wooyek/django-model-cleanup?branch=develop
:alt: Coveralls.io coverage.. image:: https://codecov.io/gh/wooyek/django-model-cleanup/branch/develop/graph/badge.svg
:target: https://codecov.io/gh/wooyek/django-model-cleanup
:alt: CodeCov coverage.. image:: https://api.codeclimate.com/v1/badges/0e7992f6259bc7fd1a1a/maintainability
:target: https://codeclimate.com/github/wooyek/django-model-cleanup/maintainability
:alt: Maintainability.. image:: https://img.shields.io/github/license/wooyek/django-model-cleanup.svg
:target: https://github.com/wooyek/django-model-cleanup/blob/develop/LICENSE
:alt: License.. image:: https://img.shields.io/twitter/url/https/github.com/wooyek/django-model-cleanup.svg?style=social
:target: https://twitter.com/intent/tweet?text=Wow:&url=https://github.com/wooyek/django-model-cleanup
:alt: Tweet about this project.. image:: https://img.shields.io/badge/Say%20Thanks-!-1EAEDB.svg
:target: https://saythanks.io/to/wooyekMixins for model cleanup methods and validation error concatenations
* Free software: MIT license
* Documentation: https://django-model-cleanup.readthedocs.io.Features of CleanMixin
----------------------* Provides `clean` method implementation
* Call to `full_clean` will result in call to all `clean_*` methods
* All methods will get called regardless of validation errors - get all errors at once
* Auto mapping of errors to field names based on clean method names, if errors have no error_dictQuickstart
----------Install Django Model Cleanup::
pip install django-model-cleanup
Add mixin in your models and enjoy `clean_` method collection and error concatenation when `full_clean` is called:
.. code-block:: python
from django.core.exceptions import ValidationError
from django.db import models
from django.utils.translation import ugettext_lazy as _
from django_model_cleanup import CleanMixin, ExtensibleValidationErrorclass SomeModel(CleanMixin, models.Model):
lorem = models.CharField(max_length=10, blank=True)def clean_foo(self):
raise ValidationError('Foo is bad')def clean_bar(self):
raise ExtensibleValidationError({'bar': _('Bar is wrong cause %s > %s!')}, code='bar', params=(2, 1))def clean_legacy(self):
# We can't init ValidationError as one-liner, cause dict + params are not compatible
# We need to wrap a message in ValidationError and put that in dict indicating a field
msg = _('Bar legacy error %s > %s!')
error = ValidationError(msg, code='bar', params=(7, 5))
raise ValidationError({'bar': error})Each error handling and concatenation is no longer required:
.. code-block:: python
# This is not longer required:
def clean(self):
errors = []
try:
self.clean_foo()
except ValidationError as ex:
errors.append(ex)
errors = []
try:
self.clean_bar()
except ValidationError as ex:
errors.append(ex)
errors = []
try:
self.clean_legacy()
except ValidationError as ex:
errors.append(ex)
if errors:
raise ValidationError(errors)Running Tests
-------------Does the code actually work?
::
$ pipenv install --dev
$ pipenv shell
$ toxWe recommend using pipenv_ but a legacy approach to creating virtualenv and installing requirements should also work.
Please install `requirements/development.txt` to setup virtual env for testing and development.Credits
-------This package was created with Cookiecutter_ and the `wooyek/cookiecutter-django-app`_ project template.
.. _Cookiecutter: https://github.com/audreyr/cookiecutter
.. _`wooyek/cookiecutter-django-app`: https://github.com/wooyek/cookiecutter-django-app
.. _`pipenv`: https://docs.pipenv.org/install/#fancy-installation-of-pipenv