From a2f5e89d46881be85c38c664ffd30dcfe39ad512 Mon Sep 17 00:00:00 2001 From: Jan Szejko Date: Mon, 16 Apr 2018 10:28:06 +0200 Subject: [PATCH 1/1] book abstracts - backend --- lib/librarian | 2 +- .../management/commands/load_abstracts.py | 15 +++++++++++++++ .../migrations/0023_book_abstract.py | 19 +++++++++++++++++++ src/catalogue/models/book.py | 11 +++++++++++ 4 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 src/catalogue/management/commands/load_abstracts.py create mode 100644 src/catalogue/migrations/0023_book_abstract.py diff --git a/lib/librarian b/lib/librarian index 09b0a0456..1df9b49c8 160000 --- a/lib/librarian +++ b/lib/librarian @@ -1 +1 @@ -Subproject commit 09b0a045683b6a965e83c1512f0cc99907f2c65c +Subproject commit 1df9b49c8a7378b1fb300c413c2c438b1d41b969 diff --git a/src/catalogue/management/commands/load_abstracts.py b/src/catalogue/management/commands/load_abstracts.py new file mode 100644 index 000000000..b828974a2 --- /dev/null +++ b/src/catalogue/management/commands/load_abstracts.py @@ -0,0 +1,15 @@ +# -*- coding: utf-8 -*- +# This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later. +# Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information. +# +from django.core.management.base import BaseCommand + +from catalogue.models import Book + + +class Command(BaseCommand): + def handle(self, *args, **options): + for b in Book.objects.order_by('slug'): + print b.slug + b.load_abstract() + b.save() diff --git a/src/catalogue/migrations/0023_book_abstract.py b/src/catalogue/migrations/0023_book_abstract.py new file mode 100644 index 000000000..6fed3562a --- /dev/null +++ b/src/catalogue/migrations/0023_book_abstract.py @@ -0,0 +1,19 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('catalogue', '0022_auto_20180226_1503'), + ] + + operations = [ + migrations.AddField( + model_name='book', + name='abstract', + field=models.TextField(verbose_name='abstract', blank=True), + ), + ] diff --git a/src/catalogue/models/book.py b/src/catalogue/models/book.py index e5ab5e480..dc6ba5099 100644 --- a/src/catalogue/models/book.py +++ b/src/catalogue/models/book.py @@ -18,6 +18,8 @@ from django.utils.deconstruct import deconstructible import jsonfield from fnpdjango.storage import BofhFileSystemStorage from ssify import flush_ssi_includes + +from librarian.html import transform_abstrakt from newtagging import managers from catalogue import constants from catalogue.fields import EbookField @@ -60,6 +62,7 @@ class Book(models.Model): common_slug = models.SlugField(_('slug'), max_length=120, db_index=True) language = models.CharField(_('language code'), max_length=3, db_index=True, default=app_settings.DEFAULT_LANGUAGE) description = models.TextField(_('description'), blank=True) + abstract = models.TextField(_('abstract'), blank=True) created_at = models.DateTimeField(_('creation date'), auto_now_add=True, db_index=True) changed_at = models.DateTimeField(_('change date'), auto_now=True, db_index=True) parent_number = models.IntegerField(_('parent number'), default=0) @@ -345,6 +348,13 @@ class Book(models.Model): ilustr_path = os.path.join(gallery_path, ilustr_src) urllib.urlretrieve('%s/%s' % (remote_gallery_url, ilustr_src), ilustr_path) + def load_abstract(self): + abstract = self.wldocument().edoc.getroot().find('.//abstrakt') + if abstract is not None: + self.abstract = transform_abstrakt(abstract) + else: + self.abstract = '' + @classmethod def from_xml_file(cls, xml_file, **kwargs): from django.core.files import File @@ -403,6 +413,7 @@ class Book(models.Model): else: book.common_slug = book.slug book.extra_info = book_info.to_dict() + book.load_abstract() book.save() meta_tags = Tag.tags_from_info(book_info) -- 2.20.1