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.
7 from django.core.management.base import BaseCommand
8 from django.utils.timezone import localtime
10 from catalogue.models import Book
11 from librarian import RDFNS, DCNS
14 FORMATS = ('PDF', 'HTML', 'TXT', 'EPUB', 'MOBI')
16 FORMATS_WITH_CHILDREN = ('PDF', 'EPUB', 'MOBI')
36 def is_institution(name):
37 return name.startswith(u'Zgromadzenie Ogólne')
40 VOLUME_SEPARATORS = (u'. część ', u', część ', u', tom ', u'. der tragödie ')
43 def get_volume(title):
44 for volume_separator in VOLUME_SEPARATORS:
45 if volume_separator in title.lower():
46 vol_idx = title.lower().index(volume_separator)
47 stripped = title[:vol_idx]
48 vol_name = title[vol_idx + 2:]
49 return stripped, vol_name
53 class Command(BaseCommand):
55 def dc_values(desc, tag):
56 return [e.text for e in desc.findall('.//' + DCNS(tag))]
58 def handle(self, *args, **options):
59 slugs = [line.strip() for line in sys.stdin]
60 writer = csv.writer(sys.stdout)
61 all_books = Book.objects.filter(slug__in=slugs)
62 books_without_children = all_books.filter(children=None)
63 for file_format in FORMATS:
64 if file_format in FORMATS_WITH_CHILDREN:
67 books = books_without_children
69 desc = book.wldocument().edoc.find('.//' + RDFNS('Description'))
70 imprint = '; '.join(self.dc_values(desc, 'publisher'))
71 title, volume = get_volume(book.title)
74 publication_date = localtime(book.created_at).date().isoformat()
75 info_date = publication_date
76 author = '; '.join(author.strip() for author in self.dc_values(desc, 'creator'))
77 author_person = author if not is_institution(author) else ''
78 author_institution = author if is_institution(author) else ''
79 publication_type = 'DGO'
81 product_form1 = PRODUCT_FORMS_1[file_format]
82 product_form2 = PRODUCT_FORMS_2[file_format]
83 language = self.dc_values(desc, 'language')[0]
102 writer.writerow([s.encode('utf-8') for s in row])