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