fix
[redakcja.git] / src / wlxml / views.py
1 from io import BytesIO
2 import json
3 from django.http import HttpResponse
4 from django.views.generic import TemplateView, ListView, DetailView, View
5 from . import models
6 from librarian.dcparser import BookInfo
7 from librarian.document import WLDocument
8 from librarian.builders import StandaloneHtmlBuilder
9 from librarian.meta.types.wluri import WLURI
10 from librarian.meta.types.text import LegimiCategory, Epoch, Kind, Genre, Audience, ThemaCategory, MainThemaCategory
11 from depot.publishers.legimi import Legimi
12
13
14 class XslView(TemplateView):
15     template_name = 'wlxml/wl2html.xsl'
16     content_type = 'application/xslt+xml'
17
18     def get_context_data(self):
19         ctx = super().get_context_data()
20         tags = {}
21         for t in models.Tag.objects.all():
22             tags.setdefault(t.type, []).append(t.name)
23         ctx['tags'] = tags
24         ctx['namespaces'] = {
25             "http://www.w3.org/1999/02/22-rdf-syntax-ns#": "rdf",
26             "http://purl.org/dc/elements/1.1/": "dc",
27             "http://www.w3.org/XML/1998/namespace": "xml",
28             "": "wl",
29         }
30         return ctx
31
32
33 class EditorCSS(ListView):
34     template_name = 'wlxml/editor.css'
35     content_type = 'text/css'
36     queryset = models.Tag.objects.all()
37         
38
39 class TagsView(ListView):
40     queryset = models.Tag.objects.all()
41
42
43 class TagView(DetailView):
44     queryset = models.Tag.objects.all()
45     slug_field = 'name'
46
47
48 VALUE_TYPES = {
49     LegimiCategory: {
50         'widget': 'select',
51         'options': [''] + list(Legimi.CATEGORIES.keys()),
52     },
53     Audience: {
54         'autocomplete': {
55             'source': '/catalogue/terms/audience/',
56         }
57     },
58     ThemaCategory: {
59         'autocomplete': {
60             'source': '/catalogue/terms/thema/',
61         },
62         'chooser': {
63             'source': '/catalogue/chooser/thema/',
64         },
65     },
66     MainThemaCategory: {
67         'autocomplete': {
68             'source': '/catalogue/terms/thema-main/',
69         },
70         'chooser': {
71             'source': '/catalogue/chooser/thema-main/',
72         },
73     },
74     Epoch: {
75         'autocomplete': {
76             'source': '/catalogue/terms/epoch/',
77         }
78     },
79     Kind: {
80         'autocomplete': {
81             'source': '/catalogue/terms/kind/',
82         }
83     },
84     Genre: {
85         'autocomplete': {
86             'source': '/catalogue/terms/genre/',
87         }
88     },
89     WLURI: {
90         "autocomplete": {
91             "source": "/catalogue/terms/wluri/",
92         }
93     },
94     "authors": {
95         "autocomplete": {
96             "source": "/catalogue/terms/author/",
97         }
98     },
99     "translators": {
100         "autocomplete": {
101             "source": "/catalogue/terms/author/",
102         }
103     },
104     "editors": {
105         "autocomplete": {
106             "source": "/catalogue/terms/editor/",
107         }
108     },
109     "technical_editors": {
110         "autocomplete": {
111             "source": "/catalogue/terms/editor/",
112         }
113     },
114     "type": {
115         "autocomplete": {
116             "source": ["text"]
117         }
118     },
119     "title": {
120         "autocomplete": {
121             "source": "/catalogue/terms/book_title/",
122         }
123     },
124
125     "language": {
126         'widget': 'select',
127         'options': [
128             '',
129             'pol',
130             'eng',
131             'fre',
132             'ger',
133             'lit',
134         ],
135     },
136     "publisher": {
137         "autocomplete": {
138             "source": ["Fundacja Wolne Lektury"]
139         }
140     },
141
142 }
143
144
145
146 class MetaTagsView(View):
147     def get(self, request):
148         fields = []
149         for f in BookInfo.FIELDS:
150             d = {
151                 'name': f.name,
152                 'required': f.required,
153                 'multiple': f.multiple,
154                 'uri': f.uri,
155                 'value_type': {
156                     'hasLanguage': f.value_type.has_language,
157                     'name': f.value_type.__name__,
158                 }
159             }
160             d['value_type'].update(
161                 VALUE_TYPES.get(
162                     f.value_type,
163                     VALUE_TYPES.get(
164                         f.name,
165                         {}
166                     )
167                 )
168             )
169             fields.append(d)
170
171         return HttpResponse(
172             'let META_FIELDS = ' + json.dumps(fields),
173             content_type='text/javascript')
174