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
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
15 class BookImportForm(forms.Form):
16 book_xml_file = forms.FileField(required=False)
17 book_xml = forms.CharField(required=False)
20 from django.core.files.base import ContentFile
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'))
27 raise forms.ValidationError(_("Please supply an XML."))
28 return super(BookImportForm, self).clean()
30 def save(self, commit=True, **kwargs):
31 return Book.from_xml_file(self.cleaned_data['book_xml_file'], overwrite=True, **kwargs)
34 FORMATS = [(f, f.upper()) for f in Book.ebook_formats]
37 class DownloadFormatsForm(forms.Form):
38 formats = forms.MultipleChoiceField(required=False, choices=FORMATS,
39 widget=forms.CheckboxSelectMultiple)
41 def __init__(self, *args, **kwargs):
42 super(DownloadFormatsForm, self).__init__(*args, **kwargs)
45 CUSTOMIZATION_FLAGS = (
46 ('nofootnotes', _("Don't show footnotes")),
47 ('nothemes', _("Don't disply themes")),
48 ('nowlfont', _("Don't use our custom font")),
49 ('no-cover', _("Without cover")),
51 CUSTOMIZATION_OPTIONS = (
52 ('leading', _("Leading"), (
53 ('', _('Normal leading')),
54 ('onehalfleading', _('One and a half leading')),
55 ('doubleleading', _('Double leading')),
57 ('fontsize', _("Font size"), (
61 # ('pagesize', _("Paper size"), (
62 # ('a4paper', _('A4')),
63 # ('a5paper', _('A5')),
68 class CustomPDFForm(forms.Form):
69 def __init__(self, book, *args, **kwargs):
70 super(CustomPDFForm, self).__init__(*args, **kwargs)
72 for name, label in CUSTOMIZATION_FLAGS:
73 self.fields[name] = forms.BooleanField(required=False, label=label)
74 for name, label, choices in CUSTOMIZATION_OPTIONS:
75 self.fields[name] = forms.ChoiceField(choices, required=False, label=label)
78 self.cleaned_data['cust'] = self.customizations
79 self.cleaned_data['path'] = get_customized_pdf_path(self.book,
80 self.cleaned_data['cust'])
81 if not WaitedFile.can_order(self.cleaned_data['path']):
82 raise ValidationError(_('Queue is full. Please try again later.'))
83 return self.cleaned_data
86 def customizations(self):
88 for name, label in CUSTOMIZATION_FLAGS:
89 if self.cleaned_data.get(name):
91 for name, label, choices in CUSTOMIZATION_OPTIONS:
92 option = self.cleaned_data.get(name)
98 def save(self, *args, **kwargs):
99 if not self.cleaned_data['cust'] and self.book.pdf_file:
100 # Don't build with default options, just redirect to the standard file.
101 return {"redirect": self.book.pdf_file.url}
102 url = WaitedFile.order(self.cleaned_data['path'],
103 lambda p: build_custom_pdf.delay(self.book.id,
104 self.cleaned_data['cust'], p),
105 self.book.pretty_title()
107 #return redirect(url)
108 return {"redirect": url}