X-Git-Url: https://git.mdrn.pl/redakcja.git/blobdiff_plain/ad422879d55a62e02c71024531aa4a2277dedaf3..18948ce1d84878c6307f2d0b732a9dbd03778cfb:/src/isbn/models.py?ds=sidebyside diff --git a/src/isbn/models.py b/src/isbn/models.py index 9db0513c..5c9bb449 100644 --- a/src/isbn/models.py +++ b/src/isbn/models.py @@ -1,3 +1,4 @@ +from datetime import date from django.apps import apps from django.db import models from lxml import etree @@ -98,7 +99,7 @@ class Isbn(models.Model): max_length=32, choices=[ (form, form) for form, config in FORMS - ], null=True, blank=True + ], blank=True ) bn_data = models.TextField(blank=True) wl_data = models.TextField(blank=True) @@ -114,6 +115,39 @@ class Isbn(models.Model): def get_code(self, dashes=True): return self.pool.get_code(self.suffix, dashes=dashes) + @classmethod + def get_for_book(cls, book, form): + isbn = cls.objects.filter(book=book, form=form).first() + if isbn is None: + return cls.assign(book, form) + return isbn + + @classmethod + def assign(cls, book, form): + pool = IsbnPool.objects.filter(purpose=IsbnPool.PURPOSE_WL).first() + suffix = pool.isbn_set.aggregate(s=models.Max('suffix'))['s'] + 1 + assert suffix <= pool.suffix_to + return pool.isbn_set.create( + book=book, form=form, suffix=suffix, datestamp=date.today() + ) + + @classmethod + def formats_from_document(cls, document): + # This is a document + try: + meta = document.wldocument(librarian2=True).meta + except: + return [] + is_parent = len(meta.parts) + formats = [] + for form, config in FORMS: + if config.book and (not is_parent or config.parent): + formats.append(( + form, + getattr(meta, f'isbn_{form}') + )) + return formats + @classmethod def import_from_documents(cls): Book = apps.get_model('documents', 'Book') @@ -139,12 +173,15 @@ class Isbn(models.Model): isbn, created = pool.isbn_set.get_or_create( suffix=suffix, ) + add_note = False if isbn.book is None: isbn.book = catalogue_book - else: - assert isbn.book is catalogue_book - if isbn.form is None: + elif isbn.book != catalogue_book: + add_note = True + if not isbn.form: isbn.form = form - else: - assert isbn.form == form - isbn.save(update_fields=['book', 'form']) + elif isbn.form != form: + add_note = True + if add_note: + isbn.notes += '\n\n' + catalogue_book.slug + ' ' + form + isbn.save(update_fields=['book', 'form', 'notes'])