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