1 # -*- coding: utf-8 -*-
2 from django import forms
3 from slughifi import slughifi
5 from catalogue.models import Tag, Book
6 from catalogue.fields import JQueryAutoCompleteField
7 from catalogue import utils
10 class BookImportForm(forms.Form):
11 book_xml_file = forms.FileField()
13 def save(self, commit=True):
14 return Book.from_xml_file(self.cleaned_data['book_xml_file'], overwrite=True)
17 class SearchForm(forms.Form):
18 q = JQueryAutoCompleteField('/katalog/tags/', {'minChars': 2, 'selectFirst': True, 'cacheLength': 50})
19 tags = forms.CharField(widget=forms.HiddenInput, required=False)
21 def __init__(self, *args, **kwargs):
22 tags = kwargs.pop('tags', [])
23 super(SearchForm, self).__init__(*args, **kwargs)
24 self.fields['q'].widget.attrs['title'] = u'tytuł, autor, motyw/temat, epoka, rodzaj, gatunek'
25 self.fields['tags'].initial = '/'.join(tag.slug for tag in Tag.get_tag_list(tags))
28 class UserSetsForm(forms.Form):
29 def __init__(self, book, user, *args, **kwargs):
30 super(UserSetsForm, self).__init__(*args, **kwargs)
31 self.fields['set_ids'] = forms.ChoiceField(
32 choices=[(tag.id, tag.name) for tag in Tag.objects.filter(category='set', user=user)],
36 class ObjectSetsForm(forms.Form):
37 def __init__(self, obj, user, *args, **kwargs):
38 super(ObjectSetsForm, self).__init__(*args, **kwargs)
39 self.fields['set_ids'] = forms.MultipleChoiceField(
42 choices=[(tag.id, "%s (%s)" % (tag.name, tag.book_count)) for tag in Tag.objects.filter(category='set', user=user)],
43 initial=[tag.id for tag in obj.tags.filter(category='set', user=user)],
44 widget=forms.CheckboxSelectMultiple
48 class NewSetForm(forms.Form):
49 name = forms.CharField(max_length=50, required=True)
51 def __init__(self, *args, **kwargs):
52 super(NewSetForm, self).__init__(*args, **kwargs)
53 self.fields['name'].widget.attrs['title'] = u'nazwa nowej półki'
55 def save(self, user, commit=True):
56 name = self.cleaned_data['name']
57 new_set = Tag(name=name, slug=utils.get_random_hash(name), sort_key=slughifi(name),
58 category='set', user=user)