1 # This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later.
2 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
4 from django.db import models
5 from django.db.models import F
6 from jsonfield import JSONField
9 class ISBNPool(models.Model):
13 (PURPOSE_WL, 'Wolne Lektury'),
14 (PURPOSE_FNP, 'Fundacja Nowoczesna Polska'),
17 prefix = models.CharField(max_length=10)
18 suffix_from = models.IntegerField()
19 suffix_to = models.IntegerField()
20 ref_from = models.IntegerField()
21 next_suffix = models.IntegerField()
22 purpose = models.CharField(max_length=4, choices=PURPOSE_CHOICES)
28 def active_pool(cls, purpose):
29 pools = cls.objects.filter(purpose=purpose)
30 pools = pools.exclude(next_suffix__gt=F('suffix_to'))
34 pools.exclude(next_suffix=F('suffix_from'))
38 def check_digit(prefix12):
39 digits = [int(d) for d in prefix12]
40 return str((-sum(digits[0::2]) + 7 * sum(digits[1::2])) % 10)
42 def isbn(self, suffix, dashes=False):
43 prefix_length = len(self.prefix)
44 suffix_length = 12 - prefix_length
45 suffix_str = ('%%0%dd' % suffix_length) % suffix
46 prefix12 = self.prefix + suffix_str
48 prefix12_final = '%s-%s-%s-%s-' % (self.prefix[:3], self.prefix[3:5], self.prefix[5:], suffix_str)
50 prefix12_final = prefix12
51 return prefix12_final + self.check_digit(prefix12)
54 class ONIXRecord(models.Model):
55 isbn_pool = models.ForeignKey(ISBNPool, models.PROTECT)
56 datestamp = models.DateField(auto_now=True)
57 suffix = models.IntegerField()
58 product_form = models.CharField(max_length=4)
59 product_form_detail = models.CharField(max_length=8, blank=True)
60 title = models.CharField(max_length=256)
61 part_number = models.CharField(max_length=64, blank=True)
62 contributors = JSONField() # roles, names, optional: ISNI, date of birth/death
63 edition_type = models.CharField(max_length=4)
64 edition_number = models.IntegerField(default=1)
65 language = models.CharField(max_length=4)
66 imprint = models.CharField(max_length=256)
67 publishing_date = models.DateField()
68 dc_slug = models.CharField(max_length=256, default='', db_index=True)
71 ordering = ['isbn_pool__id', 'suffix']
72 unique_together = ['isbn_pool', 'suffix']
75 def new_record(cls, purpose, data):
76 pool = ISBNPool.active_pool(purpose)
79 'suffix': pool.next_suffix,
83 'product_form_detail',
94 for field in fields_to_copy:
96 fields[field] = data[field]
97 cls.objects.create(**fields)
101 def isbn(self, dashes=False):
102 return self.isbn_pool.isbn(self.suffix, dashes=dashes)
105 return 'pl-eisbn-%s' % (self.isbn_pool.ref_from + self.suffix)