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