Skip to main content

Mixins for model cleanup methods and validation error concatenations

Project description

https://img.shields.io/pypi/v/django-model-cleanup.svg https://img.shields.io/travis/wooyek/django-model-cleanup.svg Documentation Status Coveralls.io coverage CodeCov coverage Maintainability License Tweet about this project https://img.shields.io/badge/Say%20Thanks-!-1EAEDB.svg

Mixins for model cleanup methods and validation error concatenations

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_dict

Quickstart

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:

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, ExtensibleValidationError


class 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:

# 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 $ tox

We 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.

History

0.1.0 (2017-12-05)

  • First release on PyPI.

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

django-model-cleanup-0.1.1.tar.gz (30.1 kB view details)

Uploaded Source

Built Distribution

django_model_cleanup-0.1.1-py2.py3-none-any.whl (8.6 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file django-model-cleanup-0.1.1.tar.gz.

File metadata

File hashes

Hashes for django-model-cleanup-0.1.1.tar.gz
Algorithm Hash digest
SHA256 0fd7a92543e69e1d6007c064855e5705220409f74582859332639f268eb55e2a
MD5 19bb04e31708d431e514dcf5f6ecc0ea
BLAKE2b-256 6b13b49e114de028082ee14db160e46f7db0ee52d0da1f730b0b9bd3c0d121a2

See more details on using hashes here.

File details

Details for the file django_model_cleanup-0.1.1-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for django_model_cleanup-0.1.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 788909682e4a572059f8b568eb3ddd1d4bababa18c750637d3cb8de8b1453166
MD5 aa313385bab9b96d42d498b575614050
BLAKE2b-256 f02b44efc0669610cb3b5613075501dd587ee0f58ff044baeb00455cf8ce4062

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