FIX: Problem z zapisywaniem MP3 i OGG oraz odświeżaniem short_html przy uaktualnianiu...
[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, Book
6 from catalogue.fields import JQueryAutoCompleteField
7 from catalogue import utils
8
9
10 class BookImportForm(forms.Form):
11     book_xml_file = forms.FileField()
12
13     def save(self, commit=True):
14         return Book.from_xml_file(self.cleaned_data['book_xml_file'], overwrite=True)
15
16
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)
20     
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))
26
27
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)],
33         )
34
35
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(
40             label=u'Półki',
41             required=False,
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
45         )
46         
47
48 class NewSetForm(forms.Form):
49     name = forms.CharField(max_length=50, required=True)
50     
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'
54         
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)
59         
60         new_set.save()
61         return new_set
62