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