Started i18n.
[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 slughifi import slughifi
7
8 from catalogue.models import Tag, Book
9 from catalogue.fields import JQueryAutoCompleteField
10 from catalogue import utils
11
12
13 class BookImportForm(forms.Form):
14     book_xml_file = forms.FileField()
15
16     def save(self, commit=True):
17         return Book.from_xml_file(self.cleaned_data['book_xml_file'], overwrite=True)
18
19
20 class SearchForm(forms.Form):
21     q = JQueryAutoCompleteField('/katalog/tags/', {'minChars': 2, 'selectFirst': True, 'cacheLength': 50})
22     tags = forms.CharField(widget=forms.HiddenInput, required=False)
23     
24     def __init__(self, *args, **kwargs):
25         tags = kwargs.pop('tags', [])
26         super(SearchForm, self).__init__(*args, **kwargs)
27         self.fields['q'].widget.attrs['title'] = u'tytuł, autor, motyw/temat, epoka, rodzaj, gatunek'
28         self.fields['tags'].initial = '/'.join(tag.slug for tag in Tag.get_tag_list(tags))
29
30
31 class UserSetsForm(forms.Form):
32     def __init__(self, book, user, *args, **kwargs):
33         super(UserSetsForm, self).__init__(*args, **kwargs)
34         self.fields['set_ids'] = forms.ChoiceField(
35             choices=[(tag.id, tag.name) for tag in Tag.objects.filter(category='set', user=user)],
36         )
37
38
39 class ObjectSetsForm(forms.Form):
40     def __init__(self, obj, user, *args, **kwargs):        
41         super(ObjectSetsForm, self).__init__(*args, **kwargs)
42         self.fields['set_ids'] = forms.MultipleChoiceField(
43             label=u'Półki',
44             required=False,
45             choices=[(tag.id, "%s (%s)" % (tag.name, tag.book_count)) for tag in Tag.objects.filter(category='set', user=user)],
46             initial=[tag.id for tag in obj.tags.filter(category='set', user=user)],
47             widget=forms.CheckboxSelectMultiple
48         )
49         
50
51 class NewSetForm(forms.Form):
52     name = forms.CharField(max_length=50, required=True)
53     
54     def __init__(self, *args, **kwargs):
55         super(NewSetForm, self).__init__(*args, **kwargs)
56         self.fields['name'].widget.attrs['title'] = u'nazwa nowej półki'
57         
58     def save(self, user, commit=True):
59         name = self.cleaned_data['name']
60         new_set = Tag(name=name, slug=utils.get_random_hash(name), sort_key=slughifi(name),
61             category='set', user=user)
62         
63         new_set.save()
64         return new_set
65
66
67 FORMATS = (
68     ('mp3', 'MP3'),
69     ('ogg', 'OGG'),
70     ('pdf', 'PDF'),
71     ('odt', 'ODT'),
72     ('txt', 'TXT'),
73 )
74
75
76 class DownloadFormatsForm(forms.Form):
77     formats = forms.MultipleChoiceField(required=False, choices=FORMATS, widget=forms.CheckboxSelectMultiple)
78     
79     def __init__(self, *args, **kwargs):
80          super(DownloadFormatsForm, self).__init__(*args, **kwargs)
81