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.
5 from django import forms
6 from django.utils.translation import ugettext_lazy as _
8 from catalogue.models import Book
11 class BookImportForm(forms.Form):
12 book_xml_file = forms.FileField(required=False)
13 book_xml = forms.CharField(required=False)
16 from django.core.files.base import ContentFile
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'))
23 raise forms.ValidationError(_("Please supply an XML."))
24 return super(BookImportForm, self).clean()
26 def save(self, commit=True, **kwargs):
27 return Book.from_xml_file(self.cleaned_data['book_xml_file'], overwrite=True, **kwargs)
30 FORMATS = [(f, f.upper()) for f in Book.ebook_formats]
33 class DownloadFormatsForm(forms.Form):
34 formats = forms.MultipleChoiceField(required=False, choices=FORMATS,
35 widget=forms.CheckboxSelectMultiple)
37 def __init__(self, *args, **kwargs):
38 super(DownloadFormatsForm, self).__init__(*args, **kwargs)
41 CUSTOMIZATION_FLAGS = (
42 ('nofootnotes', _("Don't show footnotes")),
43 ('nothemes', _("Don't disply themes")),
44 ('nowlfont', _("Don't use our custom font")),
46 CUSTOMIZATION_OPTIONS = (
47 ('leading', _("Leading"), (
48 ('defaultleading', _('Normal leading')),
49 ('onehalfleading', _('One and a half leading')),
50 ('doubleleading', _('Double leading')),
52 ('fontsize', _("Font size"), (
53 ('11pt', _('Default')),
56 # ('pagesize', _("Paper size"), (
57 # ('a4paper', _('A4')),
58 # ('a5paper', _('A5')),
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)
72 def customizations(self):
74 for name, label in CUSTOMIZATION_FLAGS:
75 if self.cleaned_data.get(name):
77 for name, label, choices in CUSTOMIZATION_OPTIONS:
78 c.append(self.cleaned_data[name])