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)
 
  18     gallery_url = forms.CharField(required=False)
 
  19     days = forms.IntegerField(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(_("Please supply an 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'], **kwargs)
 
  38 FORMATS = [(f, f.upper()) for f in Book.ebook_formats]
 
  41 class DownloadFormatsForm(forms.Form):
 
  42     formats = forms.MultipleChoiceField(required=False, choices=FORMATS, widget=forms.CheckboxSelectMultiple)
 
  44     def __init__(self, *args, **kwargs):
 
  45         super(DownloadFormatsForm, self).__init__(*args, **kwargs)
 
  48 CUSTOMIZATION_FLAGS = (
 
  49     ('nofootnotes', _("Don't show footnotes")),
 
  50     ('nothemes', _("Don't disply themes")),
 
  51     ('nowlfont', _("Don't use our custom font")),
 
  52     ('nocover', _("Without cover")),
 
  54 CUSTOMIZATION_OPTIONS = (
 
  55     ('leading', _("Leading"), (
 
  56         ('', _('Normal leading')),
 
  57         ('onehalfleading', _('One and a half leading')),
 
  58         ('doubleleading', _('Double leading')),
 
  60     ('fontsize', _("Font size"), (
 
  64     # ('pagesize', _("Paper size"), (
 
  65     #     ('a4paper', _('A4')),
 
  66     #     ('a5paper', _('A5')),
 
  71 class CustomPDFForm(forms.Form):
 
  72     def __init__(self, book, *args, **kwargs):
 
  73         super(CustomPDFForm, self).__init__(*args, **kwargs)
 
  75         for name, label in CUSTOMIZATION_FLAGS:
 
  76             self.fields[name] = forms.BooleanField(required=False, label=label)
 
  77         for name, label, choices in CUSTOMIZATION_OPTIONS:
 
  78             self.fields[name] = forms.ChoiceField(choices, required=False, label=label)
 
  81         self.cleaned_data['cust'] = self.customizations
 
  82         self.cleaned_data['path'] = get_customized_pdf_path(self.book, self.cleaned_data['cust'])
 
  83         if not WaitedFile.can_order(self.cleaned_data['path']):
 
  84             raise ValidationError(_('Queue is full. Please try again later.'))
 
  85         return self.cleaned_data
 
  88     def customizations(self):
 
  90         for name, label in CUSTOMIZATION_FLAGS:
 
  91             if self.cleaned_data.get(name):
 
  93         for name, label, choices in CUSTOMIZATION_OPTIONS:
 
  94             option = self.cleaned_data.get(name)
 
 100     def save(self, *args, **kwargs):
 
 101         if not self.cleaned_data['cust'] and self.book.pdf_file:
 
 102             # Don't build with default options, just redirect to the standard file.
 
 103             return {"redirect": self.book.pdf_url()}
 
 104         url = WaitedFile.order(
 
 105             self.cleaned_data['path'],
 
 106             lambda p, waiter_id: build_custom_pdf.delay(self.book.id, self.cleaned_data['cust'], p, waiter_id),
 
 107             self.book.pretty_title()
 
 109         return {"redirect": url}