1 # -*- coding: utf-8 -*-
2 from __future__ import unicode_literals
3 from django.core.management.base import BaseCommand
4 from django.utils import timezone
6 from isbn.models import ONIXRecord
8 HEADER = """<?xml version="1.0" encoding="UTF-8"?>
9 <ONIXMessage release="3.0"
10 xmlns="http://ns.editeur.org/onix/3.0/reference"
11 xmlns:schemaLocation="https://e-isbn.pl/IsbnWeb/schemas/ONIX_BookProduct_3.0_reference.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
14 <SenderName>Fundacja NOWOCZESNA POLSKA</SenderName>
15 <ContactName>Paulina Choromańska</ContactName>
16 <EmailAddress>paulinachoromanska@nowoczesnapolska.org.pl</EmailAddress>
18 <SentDateTime>%s</SentDateTime>
19 <MessageNote>Opis wygenerowany przez wydawcę</MessageNote>
20 <DefaultLanguageOfText>pol</DefaultLanguageOfText>
24 <Product datestamp="%(datestamp)s">
25 <RecordReference>%(record_reference)s</RecordReference>
26 <NotificationType>03</NotificationType>
27 <RecordSourceType>01</RecordSourceType>
28 <RecordSourceName>Fundacja NOWOCZESNA POLSKA</RecordSourceName>
30 <ProductIDType>15</ProductIDType>
31 <IDValue>%(isbn)s</IDValue>
34 <ProductComposition>00</ProductComposition>
35 <ProductForm>%(product_form)s</ProductForm>%(product_form_detail)s
37 <TitleType>01</TitleType>
39 <TitleElementLevel>01</TitleElementLevel>%(part_number)s
40 <TitleText>%(title)s</TitleText>
42 </TitleDetail>%(contributors)s
43 <EditionType>%(edition_type)s</EditionType>
44 <EditionNumber>%(edition_number)s</EditionNumber>
46 <LanguageRole>01</LanguageRole>
47 <LanguageCode>%(language)s</LanguageCode>
52 <ImprintName>%(imprint)s</ImprintName>
55 <PublishingRole>01</PublishingRole>
56 <PublisherName>Fundacja NOWOCZESNA POLSKA</PublisherName>
58 <CityOfPublication>Warszawa</CityOfPublication>
59 <CountryOfPublication>PL</CountryOfPublication>
61 <PublishingDateRole>01</PublishingDateRole>
62 <Date>%(publishing_date)s</Date>
65 <PublishingDateRole>09</PublishingDateRole>
66 <Date>%(publishing_date)s</Date>
71 PRODUCT_FORM_DETAIL = """
72 <ProductFormDetail>%s</ProductFormDetail>"""
75 <PartNumber>%s</PartNumber>"""
79 <SequenceNumber>%(no)s</SequenceNumber>
80 <ContributorRole>%(role)s</ContributorRole>%(identifier)s%(name)s%(corporate_name)s%(unnamed)s%(birth_date)s%(death_date)s
85 <NameIDType>16</NameIDType>
86 <IDValue>%(isni)s</IDValue>
90 <PersonNameInverted>%s</PersonNameInverted>"""
93 <CorporateName>%s</CorporateName>"""
96 <UnnamedPersons>%s</UnnamedPersons>"""
98 CONTRIBUTOR_DATE = """
100 <ContributorDateRole>%(role)s</ContributorDateRole>
101 <Date>%(date)s</Date>
102 </ContributorDate>"""
108 class Command(BaseCommand):
109 help = "Export ONIX."
111 def handle(self, *args, **options):
112 xml = HEADER % timezone.now().strftime('%Y%m%dT%H%M%z')
113 for record in ONIXRecord.objects.all():
114 xml += self.render_product(record)
116 print(xml.encode('utf-8'))
118 def render_product(self, record):
119 if record.product_form_detail:
120 product_form_detail = PRODUCT_FORM_DETAIL % record.product_form_detail
122 product_form_detail = ''
123 if record.part_number:
124 part_number = PART_NUMBER % record.part_number
128 for no, contributor in enumerate(record.contributors, start=1):
129 contributors += self.render_contributor(no, contributor)
131 'datestamp': record.datestamp.strftime('%Y%m%d'),
132 'record_reference': record.reference(),
133 'isbn': record.isbn(),
134 'product_form': record.product_form,
135 'product_form_detail': product_form_detail,
136 'part_number': part_number,
137 'title': record.title,
138 'contributors': contributors,
139 'edition_type': record.edition_type,
140 'edition_number': record.edition_number,
141 'language': record.language,
142 'imprint': record.imprint,
143 'publishing_date': record.publishing_date.strftime('%Y%m%d'),
147 def render_contributor(no, contributor):
148 if 'isni' in contributor:
149 identifier = NAME_IDENTFIER % contributor
152 if 'name' in contributor:
153 name = NAME % contributor['name']
156 if 'corporate_name' in contributor:
157 corporate_name = CORPORATE_NAME % contributor['corporate_name']
160 if 'unnamed' in contributor:
161 unnamed = UNNAMED % contributor['unnamed']
164 if 'birth_date' in contributor:
165 birth_date = CONTRIBUTOR_DATE % {'role': '50', 'date': contributor['birth_date']}
168 if 'death_date' in contributor:
169 death_date = CONTRIBUTOR_DATE % {'role': '51', 'date': contributor['death_date']}
172 return CONTRIBUTOR % {
174 'role': contributor['role'],
175 'identifier': identifier,
177 'corporate_name': corporate_name,
179 'birth_date': birth_date,
180 'death_date': death_date,