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