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