3 from django.core.management import BaseCommand
4 from slugify import slugify
6 from catalogue.models import Book, Author
9 class Command(BaseCommand):
10 def add_arguments(self, parser):
11 parser.add_argument('path')
13 def handle(self, path, **kwargs):
17 if item['model'] == 'pdcounter.bookstub':
20 slug = item['fields']['slug']
21 book, created = Book.objects.get_or_create(slug=slug)
22 if item['fields']['translator'] and not book.translators.exists():
23 notes.append('tłum.: ' + item['fields']['translator'])
24 book.title = book.title or item['fields']['title']
25 book.pd_year = book.pd_year or item['fields']['pd']
26 notes = '\n'.join(notes)
27 if notes and notes not in book.notes:
28 book.notes = '\n'.join([notes, book.notes])
31 if not book.authors.exists():
32 author_name = item['fields']['author']
33 name_pieces = author_name.rsplit(' ', 1)
34 if len(name_pieces) == 1:
35 first_name, last_name = name_pieces, ''
37 first_name, last_name = name_pieces
39 author, created = Author.objects.get_or_create(first_name=first_name, last_name=last_name)
41 print(author.slug, author_name)
42 author.slug = slugify(author_name)
44 book.authors.set([author])
45 elif item['model'] == 'pdcounter.author':
46 slug = item['fields']['slug']
47 author, created = Author.objects.get_or_create(slug=slug)
48 if not author.first_name and not author.last_name:
49 author_name = item['fields']['name']
50 name_pieces = author_name.rsplit(' ', 1)
51 if len(name_pieces) == 1:
52 author.first_name, author.last_name = name_pieces, ''
54 author.first_name, author.last_name = name_pieces
55 author.year_of_death = author.year_of_death or item['fields']['death']
56 author.notes = author.notes or item['fields']['description']
57 author.gazeta_link = author.gazeta_link or item['fields']['gazeta_link']
58 wiki_link = item['fields']['wiki_link']
59 assert not wiki_link # Welp