75d9ab997b18f6ff84aef215ec4f99cdc6cbf01d
[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 PDF_PAGE_SIZES = (
42     ('a4paper', _('A4')),
43     ('a5paper', _('A5')),
44 )
45
46
47 PDF_LEADINGS = (
48     ('', _('Normal leading')),
49     ('onehalfleading', _('One and a half leading')),
50     ('doubleleading', _('Double leading')),
51     )
52
53 PDF_FONT_SIZES = (
54     ('11pt', _('Default')),
55     ('13pt', _('Big'))
56     )
57
58
59 class CustomPDFForm(forms.Form):
60     nofootnotes = forms.BooleanField(required=False, label=_("Don't show footnotes"))
61     nothemes = forms.BooleanField(required=False, label=_("Don't disply themes"))
62     nowlfont = forms.BooleanField(required=False, label=_("Don't use our custom font"))
63     ##    pagesize = forms.ChoiceField(PDF_PAGE_SIZES, required=True, label=_("Paper size"))
64     leading = forms.ChoiceField(PDF_LEADINGS, required=False, label=_("Leading"))
65     fontsize = forms.ChoiceField(PDF_FONT_SIZES, required=True, label=_("Font size"))
66
67     @property
68     def customizations(self):
69         c = []
70         if self.cleaned_data['nofootnotes']:
71             c.append('nofootnotes')
72             
73         if self.cleaned_data['nothemes']:
74             c.append('nothemes')
75             
76         if self.cleaned_data['nowlfont']:
77             c.append('nowlfont')
78         
79             ##  c.append(self.cleaned_data['pagesize'])
80         c.append(self.cleaned_data['fontsize'])
81
82         if self.cleaned_data['leading']:
83             c.append(self.cleaned_data['leading'])
84
85         c.sort()
86
87         return c
88