Only formalities.
[wolnelektury.git] / src / isbn / management / commands / eisbn_csv.py
1 # This file is part of Wolne Lektury, licensed under GNU Affero GPLv3 or later.
2 # Copyright © Fundacja Wolne Lektury. See NOTICE for more information.
3 #
4 import csv
5 import sys
6 from django.core.management.base import BaseCommand
7 from django.utils.timezone import localtime
8
9 from catalogue.models import Book
10 from isbn.utils import isbn_data, FORMATS, FORMATS_WITH_CHILDREN
11
12
13 class Command(BaseCommand):
14
15     def handle(self, *args, **options):
16         slugs = [line.strip() for line in sys.stdin]
17         writer = csv.writer(sys.stdout)
18         all_books = Book.objects.filter(slug__in=slugs)
19         books_without_children = all_books.filter(children=None)
20         for file_format in FORMATS:
21             if file_format in FORMATS_WITH_CHILDREN:
22                 books = all_books
23             else:
24                 books = books_without_children
25             for book in books:
26                 date = localtime(book.created_at).date().isoformat()
27                 data = isbn_data(book.wldocument(), file_format)
28                 row = [
29                     data['imprint'],
30                     data['title'],
31                     data['subtitle'],
32                     data['year'],
33                     data['part_number'],
34                     date,
35                     date,
36                     data['name'],
37                     data['corporate_name'],
38                     data['edition_type'],
39                     data['edition_number'],
40                     data['product_form'],
41                     data['product_form_detail'],
42                     data['language'],
43                     book.slug,
44                     file_format,
45                 ]
46                 writer.writerow([s.encode('utf-8') for s in row])