1 from django.apps import apps
2 from django.db import models
5 from .product_forms import FORMS
8 class IsbnPool(models.Model):
9 PURPOSE_GENERAL = 'GENERAL'
12 (PURPOSE_WL, 'Wolne Lektury'),
13 (PURPOSE_GENERAL, 'Ogólne'),
16 prefix = models.CharField(max_length=10)
17 suffix_from = models.IntegerField()
18 suffix_to = models.IntegerField()
19 ref_from = models.IntegerField()
20 purpose = models.CharField(max_length=8, choices=PURPOSE_CHOICES)
27 'X' * (12 - len(self.prefix)),
32 def check_digit(prefix12):
33 digits = [int(d) for d in prefix12]
34 return str((-sum(digits[0::2]) + 7 * sum(digits[1::2])) % 10)
36 def get_code(self, suffix, dashes=False):
37 suffix_length = 12 - len(self.prefix)
38 suffix_str = f'{suffix:0{suffix_length}d}'
39 prefix12 = self.prefix + suffix_str
40 check_digit = self.check_digit(prefix12)
58 return self.suffix_to - self.suffix_from + 1
62 return self.isbn_set.count()
65 def fill_percentage(self):
66 return 100 * self.entries / self.size
68 def bn_record_id_for(self, suffix):
69 return self.ref_from + suffix
71 def import_all_bn_data(self):
72 for suffix in range(self.suffix_from, self.suffix_to + 1):
74 self.import_bn_data_for(suffix)
76 def import_bn_data_for(self, suffix):
77 record_id = self.bn_record_id_for(suffix)
78 content = requests.get(
79 f'https://e-isbn.pl/IsbnWeb/record/export_onix.xml?record_id={record_id}').content
80 elem = etree.fromstring(content)
81 product = elem.find('{http://ns.editeur.org/onix/3.0/reference}Product')
82 if product is not None:
83 isbn, created = self.isbn_set.get_or_create(
86 isbn.bn_data = etree.tostring(product, pretty_print=True, encoding='unicode')
87 isbn.save(update_fields=['bn_data'])
90 class Isbn(models.Model):
91 pool = models.ForeignKey(IsbnPool, models.PROTECT)
92 suffix = models.IntegerField()
93 datestamp = models.DateField(blank=True, null=True)
94 book = models.ForeignKey(
95 'catalogue.Book', models.PROTECT, null=True, blank=True
97 form = models.CharField(
98 max_length=32, choices=[
100 for form, config in FORMS
103 bn_data = models.TextField(blank=True)
104 wl_data = models.TextField(blank=True)
105 notes = models.TextField(blank=True)
108 ordering = ['pool', 'suffix']
109 unique_together = ['pool', 'suffix']
112 return self.get_code(True)
114 def get_code(self, dashes=True):
115 return self.pool.get_code(self.suffix, dashes=dashes)
118 def import_from_documents(cls):
119 Book = apps.get_model('documents', 'Book')
120 for book in Book.objects.all():
122 catalogue_book = book.catalogue_book
123 if catalogue_book is None:
128 meta = book.wldocument(publishable=False, librarian2=True).meta
131 for form in ('html', 'txt', 'pdf', 'epub', 'mobi'):
132 isbn = getattr(meta, f'isbn_{form}')
134 parts = isbn.split('-')
135 assert parts[0] == 'ISBN'
136 suffix = int(parts[-2])
137 prefix = ''.join(parts[1:-2])
138 pool = IsbnPool.objects.get(prefix=prefix)
139 isbn, created = pool.isbn_set.get_or_create(
143 if isbn.book is None:
144 isbn.book = catalogue_book
145 elif isbn.book is not catalogue_book:
149 elif isbn.form != form:
152 isbn.notes += '\n\n' + catalogue_book.slug + ' ' + form
153 isbn.save(update_fields=['book', 'form', 'notes'])