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