celery waiter for custompdf
[wolnelektury.git] / apps / 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
10
11 class BookImportForm(forms.Form):
12     book_xml_file = forms.FileField(required=False)
13     book_xml = forms.CharField(required=False)
14
15     def clean(self):
16         from django.core.files.base import ContentFile
17
18         if not self.cleaned_data['book_xml_file']:
19             if self.cleaned_data['book_xml']:
20                 self.cleaned_data['book_xml_file'] = \
21                         ContentFile(self.cleaned_data['book_xml'].encode('utf-8'))
22             else:
23                 raise forms.ValidationError(_("Please supply an XML."))
24         return super(BookImportForm, self).clean()
25
26     def save(self, commit=True, **kwargs):
27         return Book.from_xml_file(self.cleaned_data['book_xml_file'], overwrite=True, **kwargs)
28
29
30 FORMATS = [(f, f.upper()) for f in Book.ebook_formats]
31
32
33 class DownloadFormatsForm(forms.Form):
34     formats = forms.MultipleChoiceField(required=False, choices=FORMATS,
35             widget=forms.CheckboxSelectMultiple)
36
37     def __init__(self, *args, **kwargs):
38         super(DownloadFormatsForm, self).__init__(*args, **kwargs)
39
40
41 CUSTOMIZATION_FLAGS = (
42     ('nofootnotes', _("Don't show footnotes")),
43     ('nothemes', _("Don't disply themes")),
44     ('nowlfont', _("Don't use our custom font")),
45     )
46 CUSTOMIZATION_OPTIONS = (
47     ('leading', _("Leading"), (
48         ('defaultleading', _('Normal leading')),
49         ('onehalfleading', _('One and a half leading')),
50         ('doubleleading', _('Double leading')),
51         )),
52     ('fontsize', _("Font size"), (
53         ('11pt', _('Default')),
54         ('13pt', _('Big'))
55         )),
56 #    ('pagesize', _("Paper size"), (
57 #        ('a4paper', _('A4')),
58 #        ('a5paper', _('A5')),
59 #        )),
60     )
61
62
63 class CustomPDFForm(forms.Form):
64     def __init__(self, *args, **kwargs):
65         super(CustomPDFForm, self).__init__(*args, **kwargs)
66         for name, label in CUSTOMIZATION_FLAGS:
67             self.fields[name] = forms.BooleanField(required=False, label=label)
68         for name, label, choices in CUSTOMIZATION_OPTIONS:
69             self.fields[name] = forms.ChoiceField(choices, label=label)
70
71     @property
72     def customizations(self):
73         c = []
74         for name, label in CUSTOMIZATION_FLAGS:
75             if self.cleaned_data.get(name):
76                 c.append(name)
77         for name, label, choices in CUSTOMIZATION_OPTIONS:
78             c.append(self.cleaned_data[name])
79         c.sort()
80         return c