1
0
mirror of https://gitlab.crans.org/mediatek/med.git synced 2025-07-06 15:34:02 +02:00

Adapt media to ISBN

This commit is contained in:
Alexandre Iooss
2019-08-11 09:22:22 +02:00
parent 79ad58997a
commit 2f872eccce
20 changed files with 397 additions and 11 deletions

31
media/validators.py Normal file
View File

@ -0,0 +1,31 @@
# -*- 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.exceptions import ValidationError
from django.utils.translation import ugettext_lazy as _
from six import string_types
from stdnum import isbn
def isbn_validator(raw_isbn):
"""Check string is a valid ISBN number"""
isbn_to_check = raw_isbn.replace('-', '').replace(' ', '')
if not isinstance(isbn_to_check, string_types):
raise ValidationError(_(u'Invalid ISBN: Not a string'))
if len(isbn_to_check) != 10 and len(isbn_to_check) != 13:
raise ValidationError(_(u'Invalid ISBN: Wrong length'))
if not isbn.is_valid(isbn_to_check):
raise ValidationError(_(u'Invalid ISBN: Failed checksum'))
if isbn_to_check != isbn_to_check.upper():
raise ValidationError(_(u'Invalid ISBN: Only upper case allowed'))
return True