Django 2.0
[wolnelektury.git] / src / isbn / management / commands / export_onix.py
1 import json
2 from django.core.management.base import BaseCommand
3 from django.utils import timezone
4
5 from isbn.models import ONIXRecord
6
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">
11     <Header>
12         <Sender>
13             <SenderName>Fundacja NOWOCZESNA POLSKA</SenderName>
14             <ContactName>Paulina Choromańska</ContactName>
15             <EmailAddress>paulinachoromanska@nowoczesnapolska.org.pl</EmailAddress>
16         </Sender>
17         <SentDateTime>%s</SentDateTime>
18         <MessageNote>Opis wygenerowany przez wydawcę</MessageNote>
19         <DefaultLanguageOfText>pol</DefaultLanguageOfText>
20     </Header>"""
21
22 PRODUCT = """
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>
28         <ProductIdentifier>
29             <ProductIDType>15</ProductIDType>
30             <IDValue>%(isbn)s</IDValue>
31         </ProductIdentifier>
32         <DescriptiveDetail>
33             <ProductComposition>00</ProductComposition>
34             <ProductForm>%(product_form)s</ProductForm>%(product_form_detail)s
35             <TitleDetail>
36                 <TitleType>01</TitleType>
37                 <TitleElement>
38                     <TitleElementLevel>01</TitleElementLevel>%(part_number)s
39                     <TitleText>%(title)s</TitleText>
40                 </TitleElement>
41             </TitleDetail>%(contributors)s
42             <EditionType>%(edition_type)s</EditionType>
43             <EditionNumber>%(edition_number)s</EditionNumber>
44             <Language>
45                 <LanguageRole>01</LanguageRole>
46                 <LanguageCode>%(language)s</LanguageCode>
47             </Language>
48         </DescriptiveDetail>
49         <PublishingDetail>
50             <Imprint>
51                 <ImprintName>%(imprint)s</ImprintName>
52             </Imprint>
53             <Publisher>
54                 <PublishingRole>01</PublishingRole>
55                 <PublisherName>Fundacja NOWOCZESNA POLSKA</PublisherName>
56             </Publisher>
57             <CityOfPublication>Warszawa</CityOfPublication>
58             <CountryOfPublication>PL</CountryOfPublication>
59             <PublishingDate>
60                 <PublishingDateRole>01</PublishingDateRole>
61                 <Date>%(publishing_date)s</Date>
62             </PublishingDate>
63             <PublishingDate>
64                 <PublishingDateRole>09</PublishingDateRole>
65                 <Date>%(publishing_date)s</Date>
66             </PublishingDate>
67         </PublishingDetail>
68     </Product>"""
69
70 PRODUCT_FORM_DETAIL = """
71             <ProductFormDetail>%s</ProductFormDetail>"""
72
73 PART_NUMBER = """
74                     <PartNumber>%s</PartNumber>"""
75
76 CONTRIBUTOR = """
77             <Contributor>
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
80             </Contributor>"""
81
82 NAME_IDENTFIER = """
83                 <NameIdentifier>
84                     <NameIDType>16</NameIDType>
85                     <IDValue>%(isni)s</IDValue>
86                 </NameIdentifier>"""
87
88 NAME = """
89                 <PersonNameInverted>%s</PersonNameInverted>"""
90
91 CORPORATE_NAME = """
92                 <CorporateName>%s</CorporateName>"""
93
94 UNNAMED = """
95                 <UnnamedPersons>%s</UnnamedPersons>"""
96
97 CONTRIBUTOR_DATE = """
98                 <ContributorDate>
99                     <ContributorDateRole>%(role)s</ContributorDateRole>
100                     <Date>%(date)s</Date>
101                 </ContributorDate>"""
102
103 FOOTER = """
104 </ONIXMessage>"""
105
106
107 class Command(BaseCommand):
108     help = "Export ONIX."
109
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)
114         xml += FOOTER
115         print(xml)
116
117     def render_product(self, record):
118         if record.product_form_detail:
119             product_form_detail = PRODUCT_FORM_DETAIL % record.product_form_detail
120         else:
121             product_form_detail = ''
122         if record.part_number:
123             part_number = PART_NUMBER % record.part_number
124         else:
125             part_number = ''
126         contributors = ''
127         for no, contributor in enumerate(json.loads(record.contributors), start=1):
128             contributors += self.render_contributor(no, contributor)
129         return PRODUCT % {
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'),
143         }
144
145     @staticmethod
146     def render_contributor(no, contributor):
147         if 'isni' in contributor:
148             identifier = NAME_IDENTFIER % contributor
149         else:
150             identifier = ''
151         if 'name' in contributor:
152             name = NAME % contributor['name']
153         else:
154             name = ''
155         if 'corporate_name' in contributor:
156             corporate_name = CORPORATE_NAME % contributor['corporate_name']
157         else:
158             corporate_name = ''
159         if 'unnamed' in contributor:
160             unnamed = UNNAMED % contributor['unnamed']
161         else:
162             unnamed = ''
163         if 'birth_date' in contributor:
164             birth_date = CONTRIBUTOR_DATE % {'role': '50', 'date': contributor['birth_date']}
165         else:
166             birth_date = ''
167         if 'death_date' in contributor:
168             death_date = CONTRIBUTOR_DATE % {'role': '51', 'date': contributor['death_date']}
169         else:
170             death_date = ''
171         return CONTRIBUTOR % {
172             'no': no,
173             'role': contributor['role'],
174             'identifier': identifier,
175             'name': name,
176             'corporate_name': corporate_name,
177             'unnamed': unnamed,
178             'birth_date': birth_date,
179             'death_date': death_date,
180         }