5708d897b6cf14a9038f783024170a033afecd63
[wolnelektury.git] / catalogue / forms.py
1 # -*- coding: utf-8 -*-
2 from django import forms
3
4 from catalogue.models import Tag
5 from catalogue.fields import JQueryAutoCompleteField
6 from catalogue.lib.slughifi import slughifi
7
8
9 class SearchForm(forms.Form):
10     q = JQueryAutoCompleteField('/katalog/tags/', {'minChars': 2, 'selectFirst': True, 'cacheLength': 50})
11
12     def __init__(self, *args, **kwargs):
13         super(SearchForm, self).__init__(*args, **kwargs)
14         self.fields['q'].widget.attrs['title'] = u'tytuł utworu, motyw lub kategoria'
15
16
17 class UserSetsForm(forms.Form):
18     def __init__(self, book, user, *args, **kwargs):
19         super(UserSetsForm, self).__init__(*args, **kwargs)
20         self.fields['set_ids'] = forms.ChoiceField(
21             choices=[(tag.id, tag.name) for tag in Tag.objects.filter(category='set', user=user)],
22         )
23
24
25 class BookSetsForm(forms.Form):
26     def __init__(self, book, user, *args, **kwargs):        
27         super(BookSetsForm, self).__init__(*args, **kwargs)
28         self.fields['set_ids'] = forms.MultipleChoiceField(
29             label=u'Zestawy',
30             required=False,
31             choices=[(tag.id, tag.name) for tag in Tag.objects.filter(category='set', user=user)],
32             initial=[tag.id for tag in book.tags.filter(category='set', user=user)],
33             widget=forms.CheckboxSelectMultiple
34         )
35         
36
37 class NewSetForm(forms.Form):
38     name = forms.CharField(max_length=50, required=True)
39         
40     def save(self, user, commit=True):
41         name = self.cleaned_data['name']
42         new_set = Tag(name=name, slug=slughifi(name), sort_key=slughifi(name),
43             category='set', user=user)
44         
45         new_set.save()
46         return new_set
47