2 from django.core.management.base import BaseCommand
3 from django.utils import timezone
5 from isbn.models import ONIXRecord
7 HEADER = """<?xml version="1.0" encoding="UTF-8"?>
8 <ONIXMessage release="3.0"
9 xmlns="http://ns.editeur.org/onix/3.0/reference"
10 xmlns:schemaLocation="https://e-isbn.pl/IsbnWeb/schemas/ONIX_BookProduct_3.0_reference.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
13 <SenderName>Fundacja NOWOCZESNA POLSKA</SenderName>
14 <ContactName>Paulina Choromańska</ContactName>
15 <EmailAddress>paulinachoromanska@nowoczesnapolska.org.pl</EmailAddress>
17 <SentDateTime>%s</SentDateTime>
18 <MessageNote>Opis wygenerowany przez wydawcę</MessageNote>
19 <DefaultLanguageOfText>pol</DefaultLanguageOfText>
23 <Product datestamp="%(datestamp)s">
24 <RecordReference>%(record_reference)s</RecordReference>
25 <NotificationType>03</NotificationType>
26 <RecordSourceType>01</RecordSourceType>
27 <RecordSourceName>Fundacja NOWOCZESNA POLSKA</RecordSourceName>
29 <ProductIDType>15</ProductIDType>
30 <IDValue>%(isbn)s</IDValue>
33 <ProductComposition>00</ProductComposition>
34 <ProductForm>%(product_form)s</ProductForm>%(product_form_detail)s
36 <TitleType>01</TitleType>
38 <TitleElementLevel>01</TitleElementLevel>%(part_number)s
39 <TitleText>%(title)s</TitleText>
41 </TitleDetail>%(contributors)s
42 <EditionType>%(edition_type)s</EditionType>
43 <EditionNumber>%(edition_number)s</EditionNumber>
45 <LanguageRole>01</LanguageRole>
46 <LanguageCode>%(language)s</LanguageCode>
51 <ImprintName>%(imprint)s</ImprintName>
54 <PublishingRole>01</PublishingRole>
55 <PublisherName>Fundacja NOWOCZESNA POLSKA</PublisherName>
57 <CityOfPublication>Warszawa</CityOfPublication>
58 <CountryOfPublication>PL</CountryOfPublication>
60 <PublishingDateRole>01</PublishingDateRole>
61 <Date>%(publishing_date)s</Date>
64 <PublishingDateRole>09</PublishingDateRole>
65 <Date>%(publishing_date)s</Date>
70 PRODUCT_FORM_DETAIL = """
71 <ProductFormDetail>%s</ProductFormDetail>"""
74 <PartNumber>%s</PartNumber>"""
78 <SequenceNumber>%(no)s</SequenceNumber>
79 <ContributorRole>%(role)s</ContributorRole>%(identifier)s%(name)s%(corporate_name)s%(unnamed)s%(birth_date)s%(death_date)s
84 <NameIDType>16</NameIDType>
85 <IDValue>%(isni)s</IDValue>
89 <PersonNameInverted>%s</PersonNameInverted>"""
92 <CorporateName>%s</CorporateName>"""
95 <UnnamedPersons>%s</UnnamedPersons>"""
97 CONTRIBUTOR_DATE = """
99 <ContributorDateRole>%(role)s</ContributorDateRole>
100 <Date>%(date)s</Date>
101 </ContributorDate>"""
107 class Command(BaseCommand):
108 help = "Export ONIX."
110 def handle(self, *args, **options):
111 xml = HEADER % timezone.now().strftime('%Y%m%dT%H%M%z')
112 for record in ONIXRecord.objects.all():
113 xml += self.render_product(record)
117 def render_product(self, record):
118 if record.product_form_detail:
119 product_form_detail = PRODUCT_FORM_DETAIL % record.product_form_detail
121 product_form_detail = ''
122 if record.part_number:
123 part_number = PART_NUMBER % record.part_number
127 for no, contributor in enumerate(json.loads(record.contributors), start=1):
128 contributors += self.render_contributor(no, contributor)
130 'datestamp': record.datestamp.strftime('%Y%m%d'),
131 'record_reference': record.reference(),
132 'isbn': record.isbn(),
133 'product_form': record.product_form,
134 'product_form_detail': product_form_detail,
135 'part_number': part_number,
136 'title': record.title,
137 'contributors': contributors,
138 'edition_type': record.edition_type,
139 'edition_number': record.edition_number,
140 'language': record.language,
141 'imprint': record.imprint,
142 'publishing_date': record.publishing_date.strftime('%Y%m%d'),
146 def render_contributor(no, contributor):
147 if 'isni' in contributor:
148 identifier = NAME_IDENTFIER % contributor
151 if 'name' in contributor:
152 name = NAME % contributor['name']
155 if 'corporate_name' in contributor:
156 corporate_name = CORPORATE_NAME % contributor['corporate_name']
159 if 'unnamed' in contributor:
160 unnamed = UNNAMED % contributor['unnamed']
163 if 'birth_date' in contributor:
164 birth_date = CONTRIBUTOR_DATE % {'role': '50', 'date': contributor['birth_date']}
167 if 'death_date' in contributor:
168 death_date = CONTRIBUTOR_DATE % {'role': '51', 'date': contributor['death_date']}
171 return CONTRIBUTOR % {
173 'role': contributor['role'],
174 'identifier': identifier,
176 'corporate_name': corporate_name,
178 'birth_date': birth_date,
179 'death_date': death_date,