Merge pull request #369 from Gluejar/isbn_input

isbn input now accepts dashes and isbn10, fixes bad check digits
pull/1/head
Raymond Yee 2014-07-10 13:03:52 -07:00
commit 1a40583a44
2 changed files with 18 additions and 5 deletions

View File

@ -62,7 +62,7 @@ from regluit.core.lookups import (
EditionLookup
)
from regluit.utils.localdatetime import now
from regluit.utils.fields import EpubFileField
from regluit.utils.fields import EpubFileField, ISBNField
from regluit.mobi import Mobi
from regluit.pyepub import EPUB
@ -79,14 +79,13 @@ class EditionForm(forms.ModelForm):
allow_new=True,
)
isbn = forms.RegexField(
isbn = ISBNField(
label=_("ISBN"),
max_length=13,
regex=r'^(97[89]\d\d\d\d\d\d\d\d\d\d|delete)$',
max_length=17,
required = False,
help_text = _("13 digits, no dash."),
error_messages = {
'invalid': _("This value must be 13 digits, starting with 978 or 979."),
'invalid': _("This must be a valid ISBN-13."),
}
)
goog = forms.RegexField(

View File

@ -2,6 +2,7 @@ import zipfile
from django import forms
from django.utils.translation import ugettext_lazy as _
from django.template.defaultfilters import filesizeformat
from regluit.core.isbn import ISBN
class EpubFileField(forms.FileField):
"""
@ -12,3 +13,16 @@ class EpubFileField(forms.FileField):
if data.name and not zipfile.is_zipfile(data.file):
raise forms.ValidationError(_('%s is not a valid EPUB file' % data.name) )
return data
class ISBNField(forms.CharField):
def to_python(self, value):
value=super(ISBNField,self).to_python(value)
if value == 'delete':
return value
self.isbn=ISBN(value)
if self.isbn.error:
raise forms.ValidationError(self.isbn.error)
self.isbn.validate()
return self.isbn.to_string()