e20adbadca3501747c5af83dd782ea432c5a096e
[wolnelektury.git] / apps / catalogue / forms.py
1 # -*- coding: utf-8 -*-
2 from django import forms
3 from slughifi import slughifi
4
5 from catalogue.models import Tag
6 from catalogue.fields import JQueryAutoCompleteField
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, required=False)
12     
13     def __init__(self, *args, **kwargs):
14         tags = kwargs.pop('tags', [])
15         super(SearchForm, self).__init__(*args, **kwargs)
16         self.fields['q'].widget.attrs['title'] = u'tytuł utworu, motyw lub kategoria'
17         self.fields['tags'].initial = '/'.join(tag.slug for tag in Tag.get_tag_list(tags))
18
19
20 class UserSetsForm(forms.Form):
21     def __init__(self, book, user, *args, **kwargs):
22         super(UserSetsForm, self).__init__(*args, **kwargs)
23         self.fields['set_ids'] = forms.ChoiceField(
24             choices=[(tag.id, tag.name) for tag in Tag.objects.filter(category='set', user=user)],
25         )
26
27
28 class ObjectSetsForm(forms.Form):
29     def __init__(self, obj, user, *args, **kwargs):        
30         super(ObjectSetsForm, self).__init__(*args, **kwargs)
31         self.fields['set_ids'] = forms.MultipleChoiceField(
32             label=u'Półki',
33             required=False,
34             choices=[(tag.id, tag.name) for tag in Tag.objects.filter(category='set', user=user)],
35             initial=[tag.id for tag in obj.tags.filter(category='set', user=user)],
36             widget=forms.CheckboxSelectMultiple
37         )
38         
39
40 class NewSetForm(forms.Form):
41     name = forms.CharField(max_length=50, required=True)
42     
43     def __init__(self, *args, **kwargs):
44         super(NewSetForm, self).__init__(*args, **kwargs)
45         self.fields['name'].widget.attrs['title'] = u'nazwa nowej półki'
46         
47     def save(self, user, commit=True):
48         name = self.cleaned_data['name']
49         new_set = Tag(name=name, slug=slughifi(name), sort_key=slughifi(name),
50             category='set', user=user)
51         
52         new_set.save()
53         return new_set
54