Stabilize dependencies.
[wolnelektury.git] / src / isbn / management / commands / import_onix.py
1 # -*- coding: utf-8 -*-
2 from datetime import date
3 from lxml import etree
4 from django.core.management.base import BaseCommand
5
6 from isbn.models import ISBNPool, ONIXRecord
7 from librarian import XMLNamespace
8
9 ONIXNS = XMLNamespace('http://ns.editeur.org/onix/3.0/reference')
10
11 DIRECT_FIELDS = {
12     'product_form': 'ProductForm',
13     'product_form_detail': 'ProductFormDetail',
14     'title': 'TitleText',
15     'part_number': 'PartNumber',
16     'edition_type': 'EditionType',
17     'edition_number': 'EditionNumber',
18     'language': 'LanguageCode',
19     'imprint': 'ImprintName',
20 }
21
22 UNKNOWN = u'Autor nieznany'
23
24
25 def parse_date(date_str):
26     year = int(date_str[:4])
27     month = int(date_str[4:6])
28     day = int(date_str[6:])
29     return date(year, month, day)
30
31
32 def get_descendants(element, tags):
33     if isinstance(tags, basestring):
34         tags = [tags]
35     return element.findall('.//' + '/'.join(ONIXNS(tag) for tag in tags))
36
37
38 def get_field(element, tags, allow_multiple=False):
39     sub_elements = get_descendants(element, tags)
40     if not allow_multiple:
41         assert len(sub_elements) <= 1, 'multiple elements: %s' % tags
42     return sub_elements[0].text if sub_elements else None
43
44
45 class Command(BaseCommand):
46     help = "Import data from ONIX."
47     args = 'filename'
48
49     def handle(self, filename, *args, **options):
50         tree = etree.parse(open(filename))
51         for product in get_descendants(tree, 'Product'):
52             isbn = get_field(product, ['ProductIdentifier', 'IDValue'])
53             assert len(isbn) == 13
54             pool = ISBNPool.objects.get(prefix__in=[isbn[:i] for i in xrange(8, 11)])
55             contributors = [
56                 self.parse_contributor(contributor)
57                 for contributor in get_descendants(product, 'Contributor')]
58             record_data = {
59                 'isbn_pool': pool,
60                 'suffix': int(isbn[len(pool.prefix):-1]),
61                 'publishing_date': parse_date(
62                     get_field(product, ['PublishingDate', 'Date'], allow_multiple=True)),
63                 'contributors': contributors,
64             }
65             for field, tag in DIRECT_FIELDS.iteritems():
66                 record_data[field] = get_field(product, tag) or ''
67             record = ONIXRecord.objects.create(**record_data)
68             ONIXRecord.objects.filter(pk=record.pk).update(datestamp=parse_date(product.attrib['datestamp']))
69
70     @staticmethod
71     def parse_contributor(contributor):
72         data = {
73             'isni': get_field(contributor, 'IDValue'),
74             'name': get_field(contributor, 'PersonNameInverted'),
75             'corporate_name': get_field(contributor, 'CorporateName'),
76             'unnamed': get_field(contributor, 'UnnamedPersons')
77         }
78         contributor_data = {
79             'role': get_field(contributor, 'ContributorRole'),
80         }
81         for key, value in data.iteritems():
82             if value:
83                 contributor_data[key] = value
84         if contributor_data.get('name') == UNKNOWN:
85             del contributor_data['name']
86             contributor_data['unnamed'] = '01'
87         for date_elem in get_descendants(contributor, 'ContributorDate'):
88             date_role = get_field(date_elem, 'ContributorDateRole')
89             date = get_field(date_elem, 'Date')
90             if date_role == '50':
91                 contributor_data['birth_date'] = date
92             elif date_role == '51':
93                 contributor_data['death_date'] = date
94         return contributor_data