391e3e4299e755cac14aa6141dff99a3af55b99d
[wolnelektury.git] / apps / 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 from slughifi import slughifi
8
9 from catalogue.models import Tag, Book
10 from catalogue.fields import JQueryAutoCompleteField
11 from catalogue import utils
12
13
14 class BookImportForm(forms.Form):
15     book_xml_file = forms.FileField(required=False)
16     book_xml = forms.CharField(required=False)
17
18     def clean(self):
19         from django.core.files.base import ContentFile
20
21         if not self.cleaned_data['book_xml_file']:
22             if self.cleaned_data['book_xml']:
23                 self.cleaned_data['book_xml_file'] = \
24                         ContentFile(self.cleaned_data['book_xml'].encode('utf-8'))
25             else:
26                 raise forms.ValidationError(_("Please supply an XML."))
27         return super(BookImportForm, self).clean()
28
29     def save(self, commit=True, **kwargs):
30         return Book.from_xml_file(self.cleaned_data['book_xml_file'], overwrite=True, **kwargs)
31
32
33 class SearchForm(forms.Form):
34     q = JQueryAutoCompleteField('/katalog/tags/', {'minChars': 2, 'selectFirst': True, 'cacheLength': 50, 'matchContains': "word"})
35     tags = forms.CharField(widget=forms.HiddenInput, required=False)
36
37     def __init__(self, *args, **kwargs):
38         tags = kwargs.pop('tags', [])
39         super(SearchForm, self).__init__(*args, **kwargs)
40         self.fields['q'].widget.attrs['title'] = _('title, author, theme/topic, epoch, kind, genre')
41             #self.fields['q'].widget.attrs['style'] = ''
42         self.fields['tags'].initial = '/'.join(tag.url_chunk for tag in Tag.get_tag_list(tags))
43
44
45 class UserSetsForm(forms.Form):
46     def __init__(self, book, user, *args, **kwargs):
47         super(UserSetsForm, self).__init__(*args, **kwargs)
48         self.fields['set_ids'] = forms.ChoiceField(
49             choices=[(tag.id, tag.name) for tag in Tag.objects.filter(category='set', user=user)],
50         )
51
52
53 class ObjectSetsForm(forms.Form):
54     def __init__(self, obj, user, *args, **kwargs):
55         super(ObjectSetsForm, self).__init__(*args, **kwargs)
56         self.fields['set_ids'] = forms.MultipleChoiceField(
57             label=_('Shelves'),
58             required=False,
59             choices=[(tag.id, "%s (%s)" % (tag.name, tag.get_count())) for tag in Tag.objects.filter(category='set', user=user)],
60             initial=[tag.id for tag in obj.tags.filter(category='set', user=user)],
61             widget=forms.CheckboxSelectMultiple
62         )
63
64
65 class NewSetForm(forms.Form):
66     name = forms.CharField(max_length=50, required=True)
67
68     def __init__(self, *args, **kwargs):
69         super(NewSetForm, self).__init__(*args, **kwargs)
70         self.fields['name'].widget.attrs['title'] = _('Name of the new shelf')
71
72     def save(self, user, commit=True):
73         name = self.cleaned_data['name']
74         new_set = Tag(name=name, slug=utils.get_random_hash(name), sort_key=name.lower(),
75             category='set', user=user)
76
77         new_set.save()
78         return new_set
79
80
81 FORMATS = (
82     ('mp3', 'MP3'),
83     ('ogg', 'OGG'),
84     ('pdf', 'PDF'),
85     ('odt', 'ODT'),
86     ('txt', 'TXT'),
87     ('epub', 'EPUB'),
88     ('daisy', 'DAISY'),
89     ('mobi', 'MOBI'),
90 )
91
92
93 class DownloadFormatsForm(forms.Form):
94     formats = forms.MultipleChoiceField(required=False, choices=FORMATS, widget=forms.CheckboxSelectMultiple)
95
96     def __init__(self, *args, **kwargs):
97          super(DownloadFormatsForm, self).__init__(*args, **kwargs)
98