Missing logos.
[wolnelektury.git] / src / catalogue / forms.py
1 # This file is part of Wolne Lektury, licensed under GNU Affero GPLv3 or later.
2 # Copyright © Fundacja Wolne Lektury. See NOTICE for more information.
3 #
4 from django import forms
5 from django.utils.translation import gettext_lazy as _
6
7 from catalogue.models import Book
8 from waiter.models import WaitedFile
9 from django.core.exceptions import ValidationError
10 from catalogue.utils import get_customized_pdf_path
11 from catalogue.tasks import build_custom_pdf
12
13
14 class BookImportForm(forms.Form):
15     book_xml_file = forms.FileField(required=False)
16     book_xml = forms.CharField(required=False)
17     gallery_url = forms.CharField(required=False)
18     days = forms.IntegerField(required=False)
19     hidden = forms.BooleanField(required=False)
20     logo = forms.CharField(required=False)
21     logo_mono = forms.CharField(required=False)
22
23     def clean(self):
24         from django.core.files.base import ContentFile
25
26         if not self.cleaned_data['book_xml_file']:
27             if self.cleaned_data['book_xml']:
28                 self.cleaned_data['book_xml_file'] = \
29                     ContentFile(self.cleaned_data['book_xml'].encode('utf-8'))
30             else:
31                 raise forms.ValidationError("Proszę podać XML.")
32         return super(BookImportForm, self).clean()
33
34     def save(self, **kwargs):
35         return Book.from_xml_file(self.cleaned_data['book_xml_file'], overwrite=True,
36                                   remote_gallery_url=self.cleaned_data['gallery_url'],
37                                   days=self.cleaned_data['days'],
38                                   findable=not self.cleaned_data['hidden'],
39                                   logo=self.cleaned_data['logo'],
40                                   logo_mono=self.cleaned_data['logo_mono'],
41                                   **kwargs)
42
43
44 FORMATS = [(f, f.upper()) for f in Book.ebook_formats]
45
46
47 class DownloadFormatsForm(forms.Form):
48     formats = forms.MultipleChoiceField(required=False, choices=FORMATS, widget=forms.CheckboxSelectMultiple)
49
50     def __init__(self, *args, **kwargs):
51         super(DownloadFormatsForm, self).__init__(*args, **kwargs)
52
53
54 CUSTOMIZATION_FLAGS = (
55     ('nofootnotes', _("Bez przypisów")),
56     ('nothemes', _("Bez motywów")),
57     ('nowlfont', _("Bez naszego kroju pisma")),
58     ('nocover', _("Bez okładki")),
59     ('notoc', _("Bez spisu treści")),
60     )
61 CUSTOMIZATION_OPTIONS = (
62     ('leading', _("Interlinia"), (
63         ('', _('Zwykła interlinia')),
64         ('onehalfleading', _('Powiększona interlinia')),
65         ('doubleleading', _('Podwójna interlinia')),
66     )),
67     ('fontsize', _("Rozmiar tekstu"), (
68         ('', _('Domyślny')),
69         ('13pt', _('Duży')),
70         ('16pt', _('Większy')),
71     )),
72     # ('pagesize', _("Rozmiar papieru"), (
73     #     ('a4paper', _('A4')),
74     #     ('a5paper', _('A5')),
75     # )),
76 )
77
78
79 class CustomPDFForm(forms.Form):
80     def __init__(self, book, *args, **kwargs):
81         super(CustomPDFForm, self).__init__(*args, **kwargs)
82         self.book = book
83         for name, label in CUSTOMIZATION_FLAGS:
84             self.fields[name] = forms.BooleanField(required=False, label=label)
85         for name, label, choices in CUSTOMIZATION_OPTIONS:
86             self.fields[name] = forms.ChoiceField(choices=choices, required=False, label=label)
87
88     def clean(self):
89         self.cleaned_data['cust'] = self.customizations
90         self.cleaned_data['path'] = get_customized_pdf_path(self.book, self.cleaned_data['cust'])
91         if not WaitedFile.can_order(self.cleaned_data['path']):
92             raise ValidationError(_('Kolejka jest pełna. Proszę spróbować ponownie później.'))
93         return self.cleaned_data
94
95     @property
96     def customizations(self):
97         c = []
98         for name, label in CUSTOMIZATION_FLAGS:
99             if self.cleaned_data.get(name):
100                 c.append(name)
101         for name, label, choices in CUSTOMIZATION_OPTIONS:
102             option = self.cleaned_data.get(name)
103             if option:
104                 c.append(option)
105         c.sort()
106         return c
107
108     def save(self, *args, **kwargs):
109         if not self.cleaned_data['cust'] and self.book.pdf_file:
110             # Don't build with default options, just redirect to the standard file.
111             return {"redirect": self.book.pdf_url()}
112         url = WaitedFile.order(
113             self.cleaned_data['path'],
114             lambda p, waiter_id: build_custom_pdf.delay(self.book.id, self.cleaned_data['cust'], p, waiter_id),
115             self.book.pretty_title()
116         )
117         return {"redirect": url}