1 # This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later.
2 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
4 from django import forms
5 from django.utils.translation import gettext_lazy as _
7 from catalogue.models import Book
8 from waiter.models import WaitedFile
9 from django.core.exceptions import ValidationError
10 from catalogue.utils import get_customized_pdf_path
11 from catalogue.tasks import build_custom_pdf
14 class BookImportForm(forms.Form):
15 book_xml_file = forms.FileField(required=False)
16 book_xml = forms.CharField(required=False)
17 gallery_url = forms.CharField(required=False)
18 days = forms.IntegerField(required=False)
19 hidden = forms.BooleanField(required=False)
22 from django.core.files.base import ContentFile
24 if not self.cleaned_data['book_xml_file']:
25 if self.cleaned_data['book_xml']:
26 self.cleaned_data['book_xml_file'] = \
27 ContentFile(self.cleaned_data['book_xml'].encode('utf-8'))
29 raise forms.ValidationError("Proszę podać XML.")
30 return super(BookImportForm, self).clean()
32 def save(self, **kwargs):
33 return Book.from_xml_file(self.cleaned_data['book_xml_file'], overwrite=True,
34 remote_gallery_url=self.cleaned_data['gallery_url'],
35 days=self.cleaned_data['days'],
36 findable=not self.cleaned_data['hidden'],
40 FORMATS = [(f, f.upper()) for f in Book.ebook_formats]
43 class DownloadFormatsForm(forms.Form):
44 formats = forms.MultipleChoiceField(required=False, choices=FORMATS, widget=forms.CheckboxSelectMultiple)
46 def __init__(self, *args, **kwargs):
47 super(DownloadFormatsForm, self).__init__(*args, **kwargs)
50 CUSTOMIZATION_FLAGS = (
51 ('nofootnotes', _("Bez przypisów")),
52 ('nothemes', _("Bez motywów")),
53 ('nowlfont', _("Bez naszego kroju pisma")),
54 ('nocover', _("Bez okładki")),
55 ('notoc', _("Bez spisu treści")),
57 CUSTOMIZATION_OPTIONS = (
58 ('leading', _("Interlinia"), (
59 ('', _('Zwykła interlinia')),
60 ('onehalfleading', _('Powiększona interlinia')),
61 ('doubleleading', _('Podwójna interlinia')),
63 ('fontsize', _("Rozmiar tekstu"), (
66 ('16pt', _('Większy')),
68 # ('pagesize', _("Rozmiar papieru"), (
69 # ('a4paper', _('A4')),
70 # ('a5paper', _('A5')),
75 class CustomPDFForm(forms.Form):
76 def __init__(self, book, *args, **kwargs):
77 super(CustomPDFForm, self).__init__(*args, **kwargs)
79 for name, label in CUSTOMIZATION_FLAGS:
80 self.fields[name] = forms.BooleanField(required=False, label=label)
81 for name, label, choices in CUSTOMIZATION_OPTIONS:
82 self.fields[name] = forms.ChoiceField(choices=choices, required=False, label=label)
85 self.cleaned_data['cust'] = self.customizations
86 self.cleaned_data['path'] = get_customized_pdf_path(self.book, self.cleaned_data['cust'])
87 if not WaitedFile.can_order(self.cleaned_data['path']):
88 raise ValidationError(_('Kolejka jest pełna. Proszę spróbować ponownie później.'))
89 return self.cleaned_data
92 def customizations(self):
94 for name, label in CUSTOMIZATION_FLAGS:
95 if self.cleaned_data.get(name):
97 for name, label, choices in CUSTOMIZATION_OPTIONS:
98 option = self.cleaned_data.get(name)
104 def save(self, *args, **kwargs):
105 if not self.cleaned_data['cust'] and self.book.pdf_file:
106 # Don't build with default options, just redirect to the standard file.
107 return {"redirect": self.book.pdf_url()}
108 url = WaitedFile.order(
109 self.cleaned_data['path'],
110 lambda p, waiter_id: build_custom_pdf.delay(self.book.id, self.cleaned_data['cust'], p, waiter_id),
111 self.book.pretty_title()
113 return {"redirect": url}