wyszukiwanie książek działa - narazie bez agregowania fragmentów.
[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 JQueryAutoCompleteField
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 = JQueryAutoCompleteField('/katalog/tags/', {'minChars': 2, 'selectFirst': True, 'cacheLength': 50, 'matchContains': "word"})
34     tags = forms.CharField(widget=forms.HiddenInput, required=False)
35
36     def __init__(self, *args, **kwargs):
37         tags = kwargs.pop('tags', [])
38         super(SearchForm, self).__init__(*args, **kwargs)
39         self.fields['q'].widget.attrs['title'] = _('title, author, theme/topic, epoch, kind, genre')
40             #self.fields['q'].widget.attrs['style'] = ''
41         self.fields['tags'].initial = '/'.join(tag.url_chunk for tag in Tag.get_tag_list(tags))
42
43
44 class UserSetsForm(forms.Form):
45     def __init__(self, book, user, *args, **kwargs):
46         super(UserSetsForm, self).__init__(*args, **kwargs)
47         self.fields['set_ids'] = forms.ChoiceField(
48             choices=[(tag.id, tag.name) for tag in Tag.objects.filter(category='set', user=user)],
49         )
50
51
52 class ObjectSetsForm(forms.Form):
53     def __init__(self, obj, user, *args, **kwargs):
54         super(ObjectSetsForm, self).__init__(*args, **kwargs)
55         self.fields['set_ids'] = forms.MultipleChoiceField(
56             label=_('Shelves'),
57             required=False,
58             choices=[(tag.id, "%s (%s)" % (tag.name, tag.get_count())) for tag in Tag.objects.filter(category='set', user=user)],
59             initial=[tag.id for tag in obj.tags.filter(category='set', user=user)],
60             widget=forms.CheckboxSelectMultiple
61         )
62
63
64 class NewSetForm(forms.Form):
65     name = forms.CharField(max_length=50, required=True)
66
67     def __init__(self, *args, **kwargs):
68         super(NewSetForm, self).__init__(*args, **kwargs)
69         self.fields['name'].widget.attrs['title'] = _('Name of the new shelf')
70
71     def save(self, user, commit=True):
72         name = self.cleaned_data['name']
73         new_set = Tag(name=name, slug=utils.get_random_hash(name), sort_key=name.lower(),
74             category='set', user=user)
75
76         new_set.save()
77         return new_set
78
79
80 FORMATS = (
81     ('mp3', 'MP3'),
82     ('ogg', 'OGG'),
83     ('pdf', 'PDF'),
84     ('odt', 'ODT'),
85     ('txt', 'TXT'),
86     ('epub', 'EPUB'),
87     ('daisy', 'DAISY'),
88     ('mobi', 'MOBI'),
89 )
90
91
92 class DownloadFormatsForm(forms.Form):
93     formats = forms.MultipleChoiceField(required=False, choices=FORMATS, widget=forms.CheckboxSelectMultiple)
94
95     def __init__(self, *args, **kwargs):
96          super(DownloadFormatsForm, self).__init__(*args, **kwargs)
97