change workshop form title
[wolnelektury.git] / src / 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 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
13
14
15 class BookImportForm(forms.Form):
16     book_xml_file = forms.FileField(required=False)
17     book_xml = forms.CharField(required=False)
18     gallery_url = forms.CharField(required=False)
19
20     def clean(self):
21         from django.core.files.base import ContentFile
22
23         if not self.cleaned_data['book_xml_file']:
24             if self.cleaned_data['book_xml']:
25                 self.cleaned_data['book_xml_file'] = \
26                     ContentFile(self.cleaned_data['book_xml'].encode('utf-8'))
27             else:
28                 raise forms.ValidationError(_("Please supply an XML."))
29         return super(BookImportForm, self).clean()
30
31     def save(self, **kwargs):
32         return Book.from_xml_file(self.cleaned_data['book_xml_file'], overwrite=True,
33                                   remote_gallery_url=self.cleaned_data['gallery_url'], **kwargs)
34
35
36 FORMATS = [(f, f.upper()) for f in Book.ebook_formats]
37
38
39 class DownloadFormatsForm(forms.Form):
40     formats = forms.MultipleChoiceField(required=False, choices=FORMATS, widget=forms.CheckboxSelectMultiple)
41
42     def __init__(self, *args, **kwargs):
43         super(DownloadFormatsForm, self).__init__(*args, **kwargs)
44
45
46 CUSTOMIZATION_FLAGS = (
47     ('nofootnotes', _("Don't show footnotes")),
48     ('nothemes', _("Don't disply themes")),
49     ('nowlfont', _("Don't use our custom font")),
50     ('nocover', _("Without cover")),
51     )
52 CUSTOMIZATION_OPTIONS = (
53     ('leading', _("Leading"), (
54         ('', _('Normal leading')),
55         ('onehalfleading', _('One and a half leading')),
56         ('doubleleading', _('Double leading')),
57     )),
58     ('fontsize', _("Font size"), (
59         ('', _('Default')),
60         ('13pt', _('Big'))
61     )),
62     # ('pagesize', _("Paper size"), (
63     #     ('a4paper', _('A4')),
64     #     ('a5paper', _('A5')),
65     # )),
66 )
67
68
69 class CustomPDFForm(forms.Form):
70     def __init__(self, book, *args, **kwargs):
71         super(CustomPDFForm, self).__init__(*args, **kwargs)
72         self.book = book
73         for name, label in CUSTOMIZATION_FLAGS:
74             self.fields[name] = forms.BooleanField(required=False, label=label)
75         for name, label, choices in CUSTOMIZATION_OPTIONS:
76             self.fields[name] = forms.ChoiceField(choices, required=False, label=label)
77
78     def clean(self):
79         self.cleaned_data['cust'] = self.customizations
80         self.cleaned_data['path'] = get_customized_pdf_path(self.book, 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
84
85     @property
86     def customizations(self):
87         c = []
88         for name, label in CUSTOMIZATION_FLAGS:
89             if self.cleaned_data.get(name):
90                 c.append(name)
91         for name, label, choices in CUSTOMIZATION_OPTIONS:
92             option = self.cleaned_data.get(name)
93             if option:
94                 c.append(option)
95         c.sort()
96         return c
97
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(
103             self.cleaned_data['path'],
104             lambda p, waiter_id: build_custom_pdf.delay(self.book.id, self.cleaned_data['cust'], p, waiter_id),
105             self.book.pretty_title()
106         )
107         return {"redirect": url}