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