2013-08-27 03:54:19 +00:00
|
|
|
import zipfile
|
|
|
|
from django import forms
|
|
|
|
from django.utils.translation import ugettext_lazy as _
|
|
|
|
from django.template.defaultfilters import filesizeformat
|
2014-07-05 00:29:42 +00:00
|
|
|
from regluit.core.isbn import ISBN
|
2013-08-27 03:54:19 +00:00
|
|
|
|
|
|
|
class EpubFileField(forms.FileField):
|
|
|
|
"""
|
|
|
|
does some epub checking; currently only checks its a zip:
|
|
|
|
"""
|
|
|
|
def clean(self, data, initial=None):
|
|
|
|
data = super(EpubFileField, self).clean(data, initial)
|
|
|
|
if data.name and not zipfile.is_zipfile(data.file):
|
|
|
|
raise forms.ValidationError(_('%s is not a valid EPUB file' % data.name) )
|
|
|
|
return data
|
2014-07-05 00:29:42 +00:00
|
|
|
|
|
|
|
class ISBNField(forms.CharField):
|
|
|
|
def to_python(self, value):
|
|
|
|
value=super(ISBNField,self).to_python(value)
|
2014-08-06 14:59:16 +00:00
|
|
|
if not value:
|
|
|
|
return ''
|
|
|
|
elif value == 'delete':
|
2014-07-05 00:29:42 +00:00
|
|
|
return value
|
|
|
|
self.isbn=ISBN(value)
|
|
|
|
if self.isbn.error:
|
|
|
|
raise forms.ValidationError(self.isbn.error)
|
|
|
|
self.isbn.validate()
|
|
|
|
return self.isbn.to_string()
|
|
|
|
|
|
|
|
|