fnp
/
wolnelektury.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
Simplify transitional files a bit.
[wolnelektury.git]
/
src
/
isbn
/
management
/
commands
/
import_onix.py
diff --git
a/src/isbn/management/commands/import_onix.py
b/src/isbn/management/commands/import_onix.py
index
796b7aa
..
1a1fe9e
100644
(file)
--- a/
src/isbn/management/commands/import_onix.py
+++ b/
src/isbn/management/commands/import_onix.py
@@
-1,4
+1,6
@@
-# -*- coding: utf-8 -*-
+# This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later.
+# Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
+#
from datetime import date
from lxml import etree
from django.core.management.base import BaseCommand
from datetime import date
from lxml import etree
from django.core.management.base import BaseCommand
@@
-6,6
+8,7
@@
from django.core.management.base import BaseCommand
from isbn.models import ISBNPool, ONIXRecord
from librarian import XMLNamespace
from isbn.models import ISBNPool, ONIXRecord
from librarian import XMLNamespace
+
ONIXNS = XMLNamespace('http://ns.editeur.org/onix/3.0/reference')
DIRECT_FIELDS = {
ONIXNS = XMLNamespace('http://ns.editeur.org/onix/3.0/reference')
DIRECT_FIELDS = {
@@
-19,7
+22,7
@@
DIRECT_FIELDS = {
'imprint': 'ImprintName',
}
'imprint': 'ImprintName',
}
-UNKNOWN =
u
'Autor nieznany'
+UNKNOWN = 'Autor nieznany'
def parse_date(date_str):
def parse_date(date_str):
@@
-30,7
+33,7
@@
def parse_date(date_str):
def get_descendants(element, tags):
def get_descendants(element, tags):
- if isinstance(tags,
basestring
):
+ if isinstance(tags,
str
):
tags = [tags]
return element.findall('.//' + '/'.join(ONIXNS(tag) for tag in tags))
tags = [tags]
return element.findall('.//' + '/'.join(ONIXNS(tag) for tag in tags))
@@
-44,14
+47,17
@@
def get_field(element, tags, allow_multiple=False):
class Command(BaseCommand):
help = "Import data from ONIX."
class Command(BaseCommand):
help = "Import data from ONIX."
- args = 'filename'
- def handle(self, filename, *args, **options):
+ def add_arguments(self, parser):
+ parser.add_argument('filename')
+
+ def handle(self, **options):
+ filename = options['filename']
tree = etree.parse(open(filename))
for product in get_descendants(tree, 'Product'):
isbn = get_field(product, ['ProductIdentifier', 'IDValue'])
assert len(isbn) == 13
tree = etree.parse(open(filename))
for product in get_descendants(tree, 'Product'):
isbn = get_field(product, ['ProductIdentifier', 'IDValue'])
assert len(isbn) == 13
- pool = ISBNPool.objects.get(prefix__in=[isbn[:i] for i in
x
range(8, 11)])
+ pool = ISBNPool.objects.get(prefix__in=[isbn[:i] for i in range(8, 11)])
contributors = [
self.parse_contributor(contributor)
for contributor in get_descendants(product, 'Contributor')]
contributors = [
self.parse_contributor(contributor)
for contributor in get_descendants(product, 'Contributor')]
@@
-62,7
+68,7
@@
class Command(BaseCommand):
get_field(product, ['PublishingDate', 'Date'], allow_multiple=True)),
'contributors': contributors,
}
get_field(product, ['PublishingDate', 'Date'], allow_multiple=True)),
'contributors': contributors,
}
- for field, tag in DIRECT_FIELDS.ite
rite
ms():
+ for field, tag in DIRECT_FIELDS.items():
record_data[field] = get_field(product, tag) or ''
record = ONIXRecord.objects.create(**record_data)
ONIXRecord.objects.filter(pk=record.pk).update(datestamp=parse_date(product.attrib['datestamp']))
record_data[field] = get_field(product, tag) or ''
record = ONIXRecord.objects.create(**record_data)
ONIXRecord.objects.filter(pk=record.pk).update(datestamp=parse_date(product.attrib['datestamp']))
@@
-78,7
+84,7
@@
class Command(BaseCommand):
contributor_data = {
'role': get_field(contributor, 'ContributorRole'),
}
contributor_data = {
'role': get_field(contributor, 'ContributorRole'),
}
- for key, value in data.ite
rite
ms():
+ for key, value in data.items():
if value:
contributor_data[key] = value
if contributor_data.get('name') == UNKNOWN:
if value:
contributor_data[key] = value
if contributor_data.get('name') == UNKNOWN: