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.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     },
63     MainThemaCategory: {
64         'autocomplete': {
65             'source': '/catalogue/terms/thema-main/',
66         }
67     },
68     Epoch: {
69         'autocomplete': {
70             'source': '/catalogue/terms/epoch/',
71         }
72     },
73     Kind: {
74         'autocomplete': {
75             'source': '/catalogue/terms/kind/',
76         }
77     },
78     Genre: {
79         'autocomplete': {
80             'source': '/catalogue/terms/genre/',
81         }
82     },
83     WLURI: {
84         "autocomplete": {
85             "source": "/catalogue/terms/wluri/",
86         }
87     },
88     "authors": {
89         "autocomplete": {
90             "source": "/catalogue/terms/author/",
91         }
92     },
93     "translators": {
94         "autocomplete": {
95             "source": "/catalogue/terms/author/",
96         }
97     },
98     "editors": {
99         "autocomplete": {
100             "source": "/catalogue/terms/editor/",
101         }
102     },
103     "technical_editors": {
104         "autocomplete": {
105             "source": "/catalogue/terms/editor/",
106         }
107     },
108     "type": {
109         "autocomplete": {
110             "source": ["text"]
111         }
112     },
113     "title": {
114         "autocomplete": {
115             "source": "/catalogue/terms/book_title/",
116         }
117     },
118
119     "language": {
120         'widget': 'select',
121         'options': [
122             '',
123             'pol',
124             'eng',
125             'fre',
126             'ger',
127             'lit',
128         ],
129     },
130     "publisher": {
131         "autocomplete": {
132             "source": ["Fundacja Wolne Lektury"]
133         }
134     },
135
136 }
137
138
139
140 class MetaTagsView(View):
141     def get(self, request):
142         fields = []
143         for f in BookInfo.FIELDS:
144             d = {
145                 'name': f.name,
146                 'required': f.required,
147                 'multiple': f.multiple,
148                 'uri': f.uri,
149                 'value_type': {
150                     'hasLanguage': f.value_type.has_language,
151                     'name': f.value_type.__name__,
152                 }
153             }
154             d['value_type'].update(
155                 VALUE_TYPES.get(
156                     f.value_type,
157                     VALUE_TYPES.get(
158                         f.name,
159                         {}
160                     )
161                 )
162             )
163             fields.append(d)
164
165         return HttpResponse(
166             'let META_FIELDS = ' + json.dumps(fields),
167             content_type='text/javascript')
168