Cutting stuff out.
[wolnelektury.git] / src / catalogue / forms.py
1 # This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later.
2 # Copyright © Fundacja Nowoczesna Polska. 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
21     def clean(self):
22         from django.core.files.base import ContentFile
23
24         if not self.cleaned_data['book_xml_file']:
25             if self.cleaned_data['book_xml']:
26                 self.cleaned_data['book_xml_file'] = \
27                     ContentFile(self.cleaned_data['book_xml'].encode('utf-8'))
28             else:
29                 raise forms.ValidationError(_("Please supply an XML."))
30         return super(BookImportForm, self).clean()
31
32     def save(self, **kwargs):
33         return Book.from_xml_file(self.cleaned_data['book_xml_file'], overwrite=True,
34                                   remote_gallery_url=self.cleaned_data['gallery_url'],
35                                   days=self.cleaned_data['days'],
36                                   findable=not self.cleaned_data['hidden'],
37                                   **kwargs)
38
39
40 FORMATS = [(f, f.upper()) for f in Book.ebook_formats]
41
42
43 class DownloadFormatsForm(forms.Form):
44     formats = forms.MultipleChoiceField(required=False, choices=FORMATS, widget=forms.CheckboxSelectMultiple)
45
46     def __init__(self, *args, **kwargs):
47         super(DownloadFormatsForm, self).__init__(*args, **kwargs)
48
49
50 CUSTOMIZATION_FLAGS = (
51     ('nofootnotes', _("Don't show footnotes")),
52     ('nothemes', _("Don't disply themes")),
53     ('nowlfont', _("Don't use our custom font")),
54     ('nocover', _("Without cover")),
55     ('notoc', _("Without table of contents")),
56     )
57 CUSTOMIZATION_OPTIONS = (
58     ('leading', _("Leading"), (
59         ('', _('Normal leading')),
60         ('onehalfleading', _('One and a half leading')),
61         ('doubleleading', _('Double leading')),
62     )),
63     ('fontsize', _("Font size"), (
64         ('', _('Default')),
65         ('13pt', _('Big')),
66         ('16pt', _('Bigger')),
67     )),
68     # ('pagesize', _("Paper size"), (
69     #     ('a4paper', _('A4')),
70     #     ('a5paper', _('A5')),
71     # )),
72 )
73
74
75 class CustomPDFForm(forms.Form):
76     def __init__(self, book, *args, **kwargs):
77         super(CustomPDFForm, self).__init__(*args, **kwargs)
78         self.book = book
79         for name, label in CUSTOMIZATION_FLAGS:
80             self.fields[name] = forms.BooleanField(required=False, label=label)
81         for name, label, choices in CUSTOMIZATION_OPTIONS:
82             self.fields[name] = forms.ChoiceField(choices=choices, required=False, label=label)
83
84     def clean(self):
85         self.cleaned_data['cust'] = self.customizations
86         self.cleaned_data['path'] = get_customized_pdf_path(self.book, self.cleaned_data['cust'])
87         if not WaitedFile.can_order(self.cleaned_data['path']):
88             raise ValidationError(_('Queue is full. Please try again later.'))
89         return self.cleaned_data
90
91     @property
92     def customizations(self):
93         c = []
94         for name, label in CUSTOMIZATION_FLAGS:
95             if self.cleaned_data.get(name):
96                 c.append(name)
97         for name, label, choices in CUSTOMIZATION_OPTIONS:
98             option = self.cleaned_data.get(name)
99             if option:
100                 c.append(option)
101         c.sort()
102         return c
103
104     def save(self, *args, **kwargs):
105         if not self.cleaned_data['cust'] and self.book.pdf_file:
106             # Don't build with default options, just redirect to the standard file.
107             return {"redirect": self.book.pdf_url()}
108         url = WaitedFile.order(
109             self.cleaned_data['path'],
110             lambda p, waiter_id: build_custom_pdf.delay(self.book.id, self.cleaned_data['cust'], p, waiter_id),
111             self.book.pretty_title()
112         )
113         return {"redirect": url}