search: no tag boxes + smarter pd boxes
[wolnelektury.git] / src / isbn / management / commands / eisbn_csv.py
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.
4 #
5 import csv
6 import sys
7 from django.core.management.base import BaseCommand
8 from django.utils.timezone import localtime
9
10 from catalogue.models import Book
11 from isbn.utils import isbn_data, FORMATS, FORMATS_WITH_CHILDREN
12
13
14 class Command(BaseCommand):
15
16     def handle(self, *args, **options):
17         slugs = [line.strip() for line in sys.stdin]
18         writer = csv.writer(sys.stdout)
19         all_books = Book.objects.filter(slug__in=slugs)
20         books_without_children = all_books.filter(children=None)
21         for file_format in FORMATS:
22             if file_format in FORMATS_WITH_CHILDREN:
23                 books = all_books
24             else:
25                 books = books_without_children
26             for book in books:
27                 date = localtime(book.created_at).date().isoformat()
28                 data = isbn_data(book.wldocument(), file_format)
29                 row = [
30                     data['imprint'],
31                     data['title'],
32                     data['subtitle'],
33                     data['year'],
34                     data['part_number'],
35                     date,
36                     date,
37                     data['name'],
38                     data['corporate_name'],
39                     data['edition_type'],
40                     data['edition_number'],
41                     data['product_form'],
42                     data['product_form_detail'],
43                     data['language'],
44                     book.slug,
45                     file_format,
46                 ]
47                 writer.writerow([s.encode('utf-8') for s in row])