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 = ('HTML', 'PDF', 'TXT', 'EPUB', 'MOBI')
 
  16 FORMATS_WITH_CHILDREN = ('PDF', 'EPUB', 'MOBI')
 
  51 def is_institution(name):
 
  52     return name.startswith(u'Zgromadzenie Ogólne')
 
  55 VOLUME_SEPARATOR = ', tom '
 
  58 def get_volume(title):
 
  59     if VOLUME_SEPARATOR not in title:
 
  62         vol_idx = title.index(VOLUME_SEPARATOR)
 
  63         stripped = title[:vol_idx]
 
  64         vol_name = title[vol_idx + len(VOLUME_SEPARATOR):]
 
  65         if vol_name in VOLUME_NUMBERS:
 
  66             return stripped, VOLUME_NUMBERS[vol_name]
 
  71 class Command(BaseCommand):
 
  73     def dc_values(desc, tag):
 
  74         return [e.text for e in desc.findall('.//' + DCNS(tag))]
 
  76     def handle(self, *args, **options):
 
  77         writer = csv.writer(sys.stdout)
 
  78         for book in Book.objects.all():
 
  79             desc = book.wldocument().edoc.find('.//' + RDFNS('Description'))
 
  80             formats = FORMATS_WITH_CHILDREN if book.children.exists() else FORMATS
 
  81             for file_format in formats:
 
  82                 # imprint = u'Fundacja Nowoczesna Polska'
 
  83                 imprint = '; '.join(self.dc_values(desc, 'publisher'))
 
  84                 title, volume = get_volume(book.title)
 
  87                 publication_date = localtime(book.created_at).date().isoformat()
 
  88                 info_date = publication_date
 
  89                 author = '; '.join(self.dc_values(desc, 'creator'))
 
  90                 author_person = author if not is_institution(author) else ''
 
  91                 author_institution = author if is_institution(author) else ''
 
  92                 publication_type = 'DGO'
 
  94                 product_form1 = PRODUCT_FORMS_1[file_format]
 
  95                 product_form2 = PRODUCT_FORMS_2[file_format]
 
  96                 language = self.dc_values(desc, 'language')[0]
 
 113                 writer.writerow([s.encode('utf-8') for s in row])