1 # -*- coding: utf-8 -*-
2 # This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later.
3 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
5 from django import forms
6 from slughifi import slughifi
8 from catalogue.models import Tag, Book
9 from catalogue.fields import JQueryAutoCompleteField
10 from catalogue import utils
13 class BookImportForm(forms.Form):
14 book_xml_file = forms.FileField()
16 def save(self, commit=True):
17 return Book.from_xml_file(self.cleaned_data['book_xml_file'], overwrite=True)
20 class SearchForm(forms.Form):
21 q = JQueryAutoCompleteField('/katalog/tags/', {'minChars': 2, 'selectFirst': True, 'cacheLength': 50, 'matchContains': "word"})
22 tags = forms.CharField(widget=forms.HiddenInput, required=False)
24 def __init__(self, *args, **kwargs):
25 tags = kwargs.pop('tags', [])
26 super(SearchForm, self).__init__(*args, **kwargs)
27 self.fields['q'].widget.attrs['title'] = u'tytuł, autor, motyw/temat, epoka, rodzaj, gatunek'
28 self.fields['tags'].initial = '/'.join(tag.slug for tag in Tag.get_tag_list(tags))
31 class UserSetsForm(forms.Form):
32 def __init__(self, book, user, *args, **kwargs):
33 super(UserSetsForm, self).__init__(*args, **kwargs)
34 self.fields['set_ids'] = forms.ChoiceField(
35 choices=[(tag.id, tag.name) for tag in Tag.objects.filter(category='set', user=user)],
39 class ObjectSetsForm(forms.Form):
40 def __init__(self, obj, user, *args, **kwargs):
41 super(ObjectSetsForm, self).__init__(*args, **kwargs)
42 self.fields['set_ids'] = forms.MultipleChoiceField(
45 choices=[(tag.id, "%s (%s)" % (tag.name, tag.book_count)) for tag in Tag.objects.filter(category='set', user=user)],
46 initial=[tag.id for tag in obj.tags.filter(category='set', user=user)],
47 widget=forms.CheckboxSelectMultiple
51 class NewSetForm(forms.Form):
52 name = forms.CharField(max_length=50, required=True)
54 def __init__(self, *args, **kwargs):
55 super(NewSetForm, self).__init__(*args, **kwargs)
56 self.fields['name'].widget.attrs['title'] = u'nazwa nowej półki'
58 def save(self, user, commit=True):
59 name = self.cleaned_data['name']
60 new_set = Tag(name=name, slug=utils.get_random_hash(name), sort_key=slughifi(name),
61 category='set', user=user)
76 class DownloadFormatsForm(forms.Form):
77 formats = forms.MultipleChoiceField(required=False, choices=FORMATS, widget=forms.CheckboxSelectMultiple)
79 def __init__(self, *args, **kwargs):
80 super(DownloadFormatsForm, self).__init__(*args, **kwargs)