Fixes.
[redakcja.git] / src / catalogue / management / commands / import_catalogue_from_wl_dump.py
1 import json
2 import sys
3 from django.core.management import BaseCommand
4 from slugify import slugify
5 import wikidata
6 from catalogue.models import Book, Author
7
8
9 class Command(BaseCommand):
10     def add_arguments(self, parser):
11         parser.add_argument('path')
12
13     def handle(self, path, **kwargs):
14         with open(path) as f:
15             data = json.load(f)
16         for item in data:
17             if item['model'] == 'pdcounter.bookstub':
18                 notes = []
19                 slug = item['fields']['slug']
20                 book, created = Book.objects.get_or_create(slug=slug)
21                 if item['fields']['translator'] and not book.translators.exists():
22                     notes.append('tłum.: ' + item['fields']['translator'])
23                 book.title = book.title or item['fields']['title']
24                 book.pd_year = book.pd_year or item['fields']['pd']
25                 notes = '\n'.join(notes)
26                 if notes and notes not in book.notes:
27                     book.notes = '\n'.join([notes, book.notes])
28                 book.save()
29
30                 if not book.authors.exists():
31                     author_name = item['fields']['author']
32                     name_pieces = author_name.rsplit(' ', 1)
33                     if len(name_pieces) == 1:
34                         first_name, last_name = name_pieces, ''
35                     else:
36                         first_name, last_name = name_pieces
37
38                     author, created = Author.objects.get_or_create(first_name=first_name, last_name=last_name)
39                     if not author.slug:
40                         print(author.slug, author_name)
41                         author.slug = slugify(author_name)
42                         author.save()
43                     book.authors.set([author])
44             elif item['model'] == 'pdcounter.author':
45                 slug = item['fields']['slug']
46                 author, created = Author.objects.get_or_create(slug=slug)
47                 if not author.first_name and not author.last_name:
48                     author_name = item['fields']['name']
49                     name_pieces = author_name.rsplit(' ', 1)
50                     if len(name_pieces) == 1:
51                         author.first_name, author.last_name = name_pieces, ''
52                     else:
53                         author.first_name, author.last_name = name_pieces
54                     author.year_of_death = author.year_of_death or item['fields']['death']
55                     author.notes = author.notes or item['fields']['description']
56                     author.gazeta_link = author.gazeta_link or item['fields']['gazeta_link']
57                     author.save()
58                     wiki_link = item['fields']['wiki_link']
59                     assert not wiki_link # Welp
60             else:
61                 print(item)
62                 break
63