1 from django.core.management.base import BaseCommand
2 from django.utils import timezone
4 from isbn.models import ONIXRecord
6 HEADER = """<?xml version="1.0" encoding="UTF-8"?>
7 <ONIXMessage release="3.0"
8 xmlns="http://ns.editeur.org/onix/3.0/reference"
9 xmlns:schemaLocation="https://e-isbn.pl/IsbnWeb/schemas/ONIX_BookProduct_3.0_reference.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
12 <SenderName>Fundacja NOWOCZESNA POLSKA</SenderName>
13 <ContactName>Paulina Choromańska</ContactName>
14 <EmailAddress>paulinachoromanska@nowoczesnapolska.org.pl</EmailAddress>
16 <SentDateTime>%s</SentDateTime>
17 <MessageNote>Opis wygenerowany przez wydawcę</MessageNote>
18 <DefaultLanguageOfText>pol</DefaultLanguageOfText>
22 <Product datestamp="%(datestamp)s">
23 <RecordReference>%(record_reference)s</RecordReference>
24 <NotificationType>03</NotificationType>
25 <RecordSourceType>01</RecordSourceType>
26 <RecordSourceName>Fundacja NOWOCZESNA POLSKA</RecordSourceName>
28 <ProductIDType>15</ProductIDType>
29 <IDValue>%(isbn)s</IDValue>
32 <ProductComposition>00</ProductComposition>
33 <ProductForm>%(product_form)s</ProductForm>%(product_form_detail)s
35 <TitleType>01</TitleType>
37 <TitleElementLevel>01</TitleElementLevel>%(part_number)s
38 <TitleText>%(title)s</TitleText>
40 </TitleDetail>%(contributors)s
41 <EditionType>%(edition_type)s</EditionType>
42 <EditionNumber>%(edition_number)s</EditionNumber>
44 <LanguageRole>01</LanguageRole>
45 <LanguageCode>%(language)s</LanguageCode>
50 <ImprintName>%(imprint)s</ImprintName>
53 <PublishingRole>01</PublishingRole>
54 <PublisherName>Fundacja NOWOCZESNA POLSKA</PublisherName>
56 <CityOfPublication>Warszawa</CityOfPublication>
57 <CountryOfPublication>PL</CountryOfPublication>
59 <PublishingDateRole>01</PublishingDateRole>
60 <Date>%(publishing_date)s</Date>
63 <PublishingDateRole>09</PublishingDateRole>
64 <Date>%(publishing_date)s</Date>
69 PRODUCT_FORM_DETAIL = """
70 <ProductFormDetail>%s</ProductFormDetail>"""
73 <PartNumber>%s</PartNumber>"""
77 <SequenceNumber>%(no)s</SequenceNumber>
78 <ContributorRole>%(role)s</ContributorRole>%(identifier)s%(name)s%(corporate_name)s%(unnamed)s%(birth_date)s%(death_date)s
83 <NameIDType>16</NameIDType>
84 <IDValue>%(isni)s</IDValue>
88 <PersonNameInverted>%s</PersonNameInverted>"""
91 <CorporateName>%s</CorporateName>"""
94 <UnnamedPersons>%s</UnnamedPersons>"""
96 CONTRIBUTOR_DATE = """
98 <ContributorDateRole>%(role)s</ContributorDateRole>
100 </ContributorDate>"""
106 class Command(BaseCommand):
107 help = "Export ONIX."
109 def handle(self, *args, **options):
110 xml = HEADER % timezone.now().strftime('%Y%m%dT%H%M%z')
111 for record in ONIXRecord.objects.all():
112 xml += self.render_product(record)
116 def render_product(self, record):
117 if record.product_form_detail:
118 product_form_detail = PRODUCT_FORM_DETAIL % record.product_form_detail
120 product_form_detail = ''
121 if record.part_number:
122 part_number = PART_NUMBER % record.part_number
126 for no, contributor in enumerate(record.contributors, start=1):
127 contributors += self.render_contributor(no, contributor)
129 'datestamp': record.datestamp.strftime('%Y%m%d'),
130 'record_reference': record.reference(),
131 'isbn': record.isbn(),
132 'product_form': record.product_form,
133 'product_form_detail': product_form_detail,
134 'part_number': part_number,
135 'title': record.title,
136 'contributors': contributors,
137 'edition_type': record.edition_type,
138 'edition_number': record.edition_number,
139 'language': record.language,
140 'imprint': record.imprint,
141 'publishing_date': record.publishing_date.strftime('%Y%m%d'),
145 def render_contributor(no, contributor):
146 if 'isni' in contributor:
147 identifier = NAME_IDENTFIER % contributor
150 if 'name' in contributor:
151 name = NAME % contributor['name']
154 if 'corporate_name' in contributor:
155 corporate_name = CORPORATE_NAME % contributor['corporate_name']
158 if 'unnamed' in contributor:
159 unnamed = UNNAMED % contributor['unnamed']
162 if 'birth_date' in contributor:
163 birth_date = CONTRIBUTOR_DATE % {'role': '50', 'date': contributor['birth_date']}
166 if 'death_date' in contributor:
167 death_date = CONTRIBUTOR_DATE % {'role': '51', 'date': contributor['death_date']}
170 return CONTRIBUTOR % {
172 'role': contributor['role'],
173 'identifier': identifier,
175 'corporate_name': corporate_name,
177 'birth_date': birth_date,
178 'death_date': death_date,