Widget small UI fix.
[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()
16
17     def save(self, commit=True, **kwargs):
18         return Book.from_xml_file(self.cleaned_data['book_xml_file'], overwrite=True, **kwargs)
19
20
21 class SearchForm(forms.Form):
22     q = JQueryAutoCompleteField('/katalog/tags/', {'minChars': 2, 'selectFirst': True, 'cacheLength': 50, 'matchContains': "word"})
23     tags = forms.CharField(widget=forms.HiddenInput, required=False)
24
25     def __init__(self, *args, **kwargs):
26         tags = kwargs.pop('tags', [])
27         super(SearchForm, self).__init__(*args, **kwargs)
28         self.fields['q'].widget.attrs['title'] = _('title, author, theme/topic, epoch, kind, genre')
29             #self.fields['q'].widget.attrs['style'] = ''
30         self.fields['tags'].initial = '/'.join(tag.slug for tag in Tag.get_tag_list(tags))
31
32
33 class UserSetsForm(forms.Form):
34     def __init__(self, book, user, *args, **kwargs):
35         super(UserSetsForm, self).__init__(*args, **kwargs)
36         self.fields['set_ids'] = forms.ChoiceField(
37             choices=[(tag.id, tag.name) for tag in Tag.objects.filter(category='set', user=user)],
38         )
39
40
41 class ObjectSetsForm(forms.Form):
42     def __init__(self, obj, user, *args, **kwargs):
43         super(ObjectSetsForm, self).__init__(*args, **kwargs)
44         self.fields['set_ids'] = forms.MultipleChoiceField(
45             label=_('Shelves'),
46             required=False,
47             choices=[(tag.id, "%s (%s)" % (tag.name, tag.get_count())) for tag in Tag.objects.filter(category='set', user=user)],
48             initial=[tag.id for tag in obj.tags.filter(category='set', user=user)],
49             widget=forms.CheckboxSelectMultiple
50         )
51
52
53 class NewSetForm(forms.Form):
54     name = forms.CharField(max_length=50, required=True)
55
56     def __init__(self, *args, **kwargs):
57         super(NewSetForm, self).__init__(*args, **kwargs)
58         self.fields['name'].widget.attrs['title'] = _('Name of the new shelf')
59
60     def save(self, user, commit=True):
61         name = self.cleaned_data['name']
62         new_set = Tag(name=name, slug=utils.get_random_hash(name), sort_key=slughifi(name),
63             category='set', user=user)
64
65         new_set.save()
66         return new_set
67
68
69 FORMATS = (
70     ('mp3', 'MP3'),
71     ('ogg', 'OGG'),
72     ('pdf', 'PDF'),
73     ('odt', 'ODT'),
74     ('txt', 'TXT'),
75     ('epub', 'EPUB'),
76 )
77
78
79 class DownloadFormatsForm(forms.Form):
80     formats = forms.MultipleChoiceField(required=False, choices=FORMATS, widget=forms.CheckboxSelectMultiple)
81
82     def __init__(self, *args, **kwargs):
83          super(DownloadFormatsForm, self).__init__(*args, **kwargs)
84