1 # This file is part of Wolne Lektury, licensed under GNU Affero GPLv3 or later.
2 # Copyright © Fundacja Wolne Lektury. See NOTICE for more information.
5 from django.core.management.base import BaseCommand
6 from django.utils import timezone
8 from isbn.models import ONIXRecord
10 HEADER = """<?xml version="1.0" encoding="UTF-8"?>
11 <ONIXMessage release="3.0"
12 xmlns="http://ns.editeur.org/onix/3.0/reference"
13 xmlns:schemaLocation="https://e-isbn.pl/IsbnWeb/schemas/ONIX_BookProduct_3.0_reference.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
16 <SenderName>Fundacja NOWOCZESNA POLSKA</SenderName>
17 <ContactName>Paulina Choromańska</ContactName>
18 <EmailAddress>paulinachoromanska@nowoczesnapolska.org.pl</EmailAddress>
20 <SentDateTime>%s</SentDateTime>
21 <MessageNote>Opis wygenerowany przez wydawcę</MessageNote>
22 <DefaultLanguageOfText>pol</DefaultLanguageOfText>
26 <Product datestamp="%(datestamp)s">
27 <RecordReference>%(record_reference)s</RecordReference>
28 <NotificationType>03</NotificationType>
29 <RecordSourceType>01</RecordSourceType>
30 <RecordSourceName>Fundacja NOWOCZESNA POLSKA</RecordSourceName>
32 <ProductIDType>15</ProductIDType>
33 <IDValue>%(isbn)s</IDValue>
36 <ProductComposition>00</ProductComposition>
37 <ProductForm>%(product_form)s</ProductForm>%(product_form_detail)s
39 <TitleType>01</TitleType>
41 <TitleElementLevel>01</TitleElementLevel>%(part_number)s
42 <TitleText>%(title)s</TitleText>
44 </TitleDetail>%(contributors)s
45 <EditionType>%(edition_type)s</EditionType>
46 <EditionNumber>%(edition_number)s</EditionNumber>
48 <LanguageRole>01</LanguageRole>
49 <LanguageCode>%(language)s</LanguageCode>
54 <ImprintName>%(imprint)s</ImprintName>
57 <PublishingRole>01</PublishingRole>
58 <PublisherName>Fundacja NOWOCZESNA POLSKA</PublisherName>
60 <CityOfPublication>Warszawa</CityOfPublication>
61 <CountryOfPublication>PL</CountryOfPublication>
63 <PublishingDateRole>01</PublishingDateRole>
64 <Date>%(publishing_date)s</Date>
67 <PublishingDateRole>09</PublishingDateRole>
68 <Date>%(publishing_date)s</Date>
73 PRODUCT_FORM_DETAIL = """
74 <ProductFormDetail>%s</ProductFormDetail>"""
77 <PartNumber>%s</PartNumber>"""
81 <SequenceNumber>%(no)s</SequenceNumber>
82 <ContributorRole>%(role)s</ContributorRole>%(identifier)s%(name)s%(corporate_name)s%(unnamed)s%(birth_date)s%(death_date)s
87 <NameIDType>16</NameIDType>
88 <IDValue>%(isni)s</IDValue>
92 <PersonNameInverted>%s</PersonNameInverted>"""
95 <CorporateName>%s</CorporateName>"""
98 <UnnamedPersons>%s</UnnamedPersons>"""
100 CONTRIBUTOR_DATE = """
102 <ContributorDateRole>%(role)s</ContributorDateRole>
103 <Date>%(date)s</Date>
104 </ContributorDate>"""
110 class Command(BaseCommand):
111 help = "Export ONIX."
113 def handle(self, *args, **options):
114 xml = HEADER % timezone.now().strftime('%Y%m%dT%H%M%z')
115 for record in ONIXRecord.objects.all():
116 xml += self.render_product(record)
120 def render_product(self, record):
121 if record.product_form_detail:
122 product_form_detail = PRODUCT_FORM_DETAIL % record.product_form_detail
124 product_form_detail = ''
125 if record.part_number:
126 part_number = PART_NUMBER % record.part_number
130 for no, contributor in enumerate(json.loads(record.contributors), start=1):
131 contributors += self.render_contributor(no, contributor)
133 'datestamp': record.datestamp.strftime('%Y%m%d'),
134 'record_reference': record.reference(),
135 'isbn': record.isbn(),
136 'product_form': record.product_form,
137 'product_form_detail': product_form_detail,
138 'part_number': part_number,
139 'title': record.title,
140 'contributors': contributors,
141 'edition_type': record.edition_type,
142 'edition_number': record.edition_number,
143 'language': record.language,
144 'imprint': record.imprint,
145 'publishing_date': record.publishing_date.strftime('%Y%m%d'),
149 def render_contributor(no, contributor):
150 if 'isni' in contributor:
151 identifier = NAME_IDENTFIER % contributor
154 if 'name' in contributor:
155 name = NAME % contributor['name']
158 if 'corporate_name' in contributor:
159 corporate_name = CORPORATE_NAME % contributor['corporate_name']
162 if 'unnamed' in contributor:
163 unnamed = UNNAMED % contributor['unnamed']
166 if 'birth_date' in contributor:
167 birth_date = CONTRIBUTOR_DATE % {'role': '50', 'date': contributor['birth_date']}
170 if 'death_date' in contributor:
171 death_date = CONTRIBUTOR_DATE % {'role': '51', 'date': contributor['death_date']}
174 return CONTRIBUTOR % {
176 'role': contributor['role'],
177 'identifier': identifier,
179 'corporate_name': corporate_name,
181 'birth_date': birth_date,
182 'death_date': death_date,