Serve partner audiobooks with isbns
[wolnelektury.git] / src / catalogue / forms.py
1 # This file is part of Wolne Lektury, licensed under GNU Affero GPLv3 or later.
2 # Copyright © Fundacja Wolne Lektury. See NOTICE for more information.
3 #
4 from django import forms
5 from django.utils.translation import gettext_lazy as _
6
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
12
13
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)
20     logo = forms.CharField(required=False)
21     logo_mono = forms.CharField(required=False)
22     logo_alt = forms.CharField(required=False)
23     can_sell = forms.BooleanField(required=False)
24     isbn_mp3 = forms.CharField(required=False)
25
26     def clean(self):
27         from django.core.files.base import ContentFile
28
29         if not self.cleaned_data['book_xml_file']:
30             if self.cleaned_data['book_xml']:
31                 self.cleaned_data['book_xml_file'] = \
32                     ContentFile(self.cleaned_data['book_xml'].encode('utf-8'))
33             else:
34                 raise forms.ValidationError("Proszę podać XML.")
35         return super(BookImportForm, self).clean()
36
37     def save(self, **kwargs):
38         return Book.from_xml_file(self.cleaned_data['book_xml_file'], overwrite=True,
39                                   remote_gallery_url=self.cleaned_data['gallery_url'],
40                                   days=self.cleaned_data['days'],
41                                   findable=not self.cleaned_data['hidden'],
42                                   logo=self.cleaned_data['logo'],
43                                   logo_mono=self.cleaned_data['logo_mono'],
44                                   logo_alt=self.cleaned_data['logo_alt'],
45                                   can_sell=self.cleaned_data['can_sell'], 
46                                   isbn_mp3=self.cleaned_data['isbn_mp3'],
47                                  **kwargs)
48
49
50 FORMATS = [(f, f.upper()) for f in Book.ebook_formats]
51
52
53 class DownloadFormatsForm(forms.Form):
54     formats = forms.MultipleChoiceField(required=False, choices=FORMATS, widget=forms.CheckboxSelectMultiple)
55
56     def __init__(self, *args, **kwargs):
57         super(DownloadFormatsForm, self).__init__(*args, **kwargs)
58
59
60 CUSTOMIZATION_FLAGS = (
61     ('nofootnotes', _("Bez przypisów")),
62     ('nothemes', _("Bez motywów")),
63     ('nowlfont', _("Bez naszego kroju pisma")),
64     ('nocover', _("Bez okładki")),
65     ('notoc', _("Bez spisu treści")),
66     )
67 CUSTOMIZATION_OPTIONS = (
68     ('leading', _("Interlinia"), (
69         ('', _('Zwykła interlinia')),
70         ('onehalfleading', _('Powiększona interlinia')),
71         ('doubleleading', _('Podwójna interlinia')),
72     )),
73     ('fontsize', _("Rozmiar tekstu"), (
74         ('', _('Domyślny')),
75         ('13pt', _('Duży')),
76         ('16pt', _('Większy')),
77     )),
78     # ('pagesize', _("Rozmiar papieru"), (
79     #     ('a4paper', _('A4')),
80     #     ('a5paper', _('A5')),
81     # )),
82 )
83
84
85 class CustomPDFForm(forms.Form):
86     def __init__(self, book, *args, **kwargs):
87         super(CustomPDFForm, self).__init__(*args, **kwargs)
88         self.book = book
89         for name, label in CUSTOMIZATION_FLAGS:
90             self.fields[name] = forms.BooleanField(required=False, label=label)
91         for name, label, choices in CUSTOMIZATION_OPTIONS:
92             self.fields[name] = forms.ChoiceField(choices=choices, required=False, label=label)
93
94     def clean(self):
95         self.cleaned_data['cust'] = self.customizations
96         self.cleaned_data['path'] = get_customized_pdf_path(self.book, self.cleaned_data['cust'])
97         if not WaitedFile.can_order(self.cleaned_data['path']):
98             raise ValidationError(_('Kolejka jest pełna. Proszę spróbować ponownie później.'))
99         return self.cleaned_data
100
101     @property
102     def customizations(self):
103         c = []
104         for name, label in CUSTOMIZATION_FLAGS:
105             if self.cleaned_data.get(name):
106                 c.append(name)
107         for name, label, choices in CUSTOMIZATION_OPTIONS:
108             option = self.cleaned_data.get(name)
109             if option:
110                 c.append(option)
111         c.sort()
112         return c
113
114     def save(self, *args, **kwargs):
115         if not self.cleaned_data['cust'] and self.book.pdf_file:
116             # Don't build with default options, just redirect to the standard file.
117             return {"redirect": self.book.pdf_url()}
118         url = WaitedFile.order(
119             self.cleaned_data['path'],
120             lambda p, waiter_id: build_custom_pdf.delay(self.book.id, self.cleaned_data['cust'], p, waiter_id),
121             self.book.pretty_title()
122         )
123         return {"redirect": url}