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


Release history Release notifications

This version
History Node

0.1.1

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Filename, size & hash SHA256 hash help File type Python version Upload date
django_model_cleanup-0.1.1-py2.py3-none-any.whl (8.6 kB) Copy SHA256 hash SHA256 Wheel 3.5
django-model-cleanup-0.1.1.tar.gz (30.1 kB) Copy SHA256 hash SHA256 Source None

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN SignalFx SignalFx Supporter DigiCert DigiCert EV certificate StatusPage StatusPage Status page