7472eb420c6cf913ef792024179c4800c9685ea3
[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     tags = forms.CharField(widget=forms.HiddenInput)
12     
13     def __init__(self, *args, **kwargs):
14         super(SearchForm, self).__init__(*args, **kwargs)
15         self.fields['q'].widget.attrs['title'] = u'tytuł utworu, motyw lub kategoria'
16
17
18 class UserSetsForm(forms.Form):
19     def __init__(self, book, user, *args, **kwargs):
20         super(UserSetsForm, self).__init__(*args, **kwargs)
21         self.fields['set_ids'] = forms.ChoiceField(
22             choices=[(tag.id, tag.name) for tag in Tag.objects.filter(category='set', user=user)],
23         )
24
25
26 class BookSetsForm(forms.Form):
27     def __init__(self, book, user, *args, **kwargs):        
28         super(BookSetsForm, self).__init__(*args, **kwargs)
29         self.fields['set_ids'] = forms.MultipleChoiceField(
30             label=u'Zestawy',
31             required=False,
32             choices=[(tag.id, tag.name) for tag in Tag.objects.filter(category='set', user=user)],
33             initial=[tag.id for tag in book.tags.filter(category='set', user=user)],
34             widget=forms.CheckboxSelectMultiple
35         )
36         
37
38 class NewSetForm(forms.Form):
39     name = forms.CharField(max_length=50, required=True)
40         
41     def save(self, user, commit=True):
42         name = self.cleaned_data['name']
43         new_set = Tag(name=name, slug=slughifi(name), sort_key=slughifi(name),
44             category='set', user=user)
45         
46         new_set.save()
47         return new_set
48