plwiki for books
[redakcja.git] / src / isbn / management / commands / isbn_import_wl.py
1 import json
2 from django.core.management.base import BaseCommand
3 from isbn.models import IsbnPool
4
5
6 class Command(BaseCommand):
7     def add_arguments(self, parser):
8         parser.add_argument('filename')
9     
10     def handle(self, filename, **options):
11         with open(filename) as f:
12             data = json.load(f)
13         pool_map = {}
14         for d in data:
15             f = d['fields']
16             if d['model'] == 'isbn.isbnpool':
17                 pool_map[d['pk']], created = IsbnPool.objects.get_or_create(
18                     prefix=f['prefix'],
19                     suffix_from=f['suffix_from'],
20                     suffix_to=f['suffix_to'],
21                     ref_from=f['ref_from'],
22                 )
23             if d['model'] == 'isbn.onixrecord':
24                 pool = pool_map[f['isbn_pool']]
25                 isbn, created = pool.isbn_set.get_or_create(
26                     suffix=f['suffix'],
27                 )
28                 isbn.wl_data = json.dumps(f, indent=4, ensure_ascii=False)
29                 isbn.save(update_fields=['wl_data'])