mirror of
https://gitlab.crans.org/mediatek/med.git
synced 2024-12-27 07:02:22 +00:00
56 lines
1.8 KiB
Python
56 lines
1.8 KiB
Python
# -*- mode: python; coding: utf-8 -*-
|
|
# Copyright (C) 2017-2019 by BDE ENS Paris-Saclay
|
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
"""
|
|
Based on https://github.com/secnot/django-isbn-field
|
|
"""
|
|
|
|
from django.core.validators import EMPTY_VALUES
|
|
from django.db.models import CharField
|
|
from django.utils.translation import gettext_lazy as _
|
|
|
|
from .validators import isbn_validator
|
|
|
|
|
|
class ISBNField(CharField):
|
|
description = _("ISBN-10 or ISBN-13")
|
|
|
|
def __init__(self, clean_isbn=True, *args, **kwargs):
|
|
self.clean_isbn = clean_isbn
|
|
kwargs['max_length'] = kwargs[
|
|
'max_length'] if 'max_length' in kwargs else 28
|
|
kwargs['verbose_name'] = kwargs[
|
|
'verbose_name'] if 'verbose_name' in kwargs else u'ISBN'
|
|
kwargs['validators'] = [isbn_validator]
|
|
super().__init__(*args, **kwargs)
|
|
|
|
def formfield(self, **kwargs):
|
|
defaults = {
|
|
'min_length': 10,
|
|
'validators': [isbn_validator],
|
|
}
|
|
defaults.update(kwargs)
|
|
return super().formfield(**defaults)
|
|
|
|
def deconstruct(self):
|
|
name, path, args, kwargs = super().deconstruct()
|
|
# Only include clean_isbn in kwarg if it's not the default value
|
|
if not self.clean_isbn:
|
|
kwargs['clean_isbn'] = self.clean_isbn
|
|
return name, path, args, kwargs
|
|
|
|
def pre_save(self, model_instance, add):
|
|
"""
|
|
Remove dashes, spaces, and convert isbn to uppercase before saving
|
|
when clean_isbn is enabled
|
|
"""
|
|
value = getattr(model_instance, self.attname)
|
|
if self.clean_isbn and value not in EMPTY_VALUES:
|
|
cleaned_isbn = value.replace(' ', '').replace('-', '').upper()
|
|
setattr(model_instance, self.attname, cleaned_isbn)
|
|
return super().pre_save(model_instance, add)
|
|
|
|
def __unicode__(self):
|
|
return self.value
|