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