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 django.utils.translation import ugettext_lazy as _
7 from slughifi import slughifi
9 from catalogue.models import Tag, Book
10 from catalogue.fields import JQueryAutoCompleteField
11 from catalogue import utils
14 class BookImportForm(forms.Form):
15 book_xml_file = forms.FileField()
17 def save(self, commit=True, **kwargs):
18 return Book.from_xml_file(self.cleaned_data['book_xml_file'], overwrite=True, **kwargs)
21 class SearchForm(forms.Form):
22 q = JQueryAutoCompleteField('/katalog/tags/', {'minChars': 2, 'selectFirst': True, 'cacheLength': 50, 'matchContains': "word"})
23 tags = forms.CharField(widget=forms.HiddenInput, required=False)
25 def __init__(self, *args, **kwargs):
26 tags = kwargs.pop('tags', [])
27 super(SearchForm, self).__init__(*args, **kwargs)
28 self.fields['q'].widget.attrs['title'] = _('title, author, theme/topic, epoch, kind, genre')
29 #self.fields['q'].widget.attrs['style'] = ''
30 self.fields['tags'].initial = '/'.join(tag.slug for tag in Tag.get_tag_list(tags))
33 class UserSetsForm(forms.Form):
34 def __init__(self, book, user, *args, **kwargs):
35 super(UserSetsForm, self).__init__(*args, **kwargs)
36 self.fields['set_ids'] = forms.ChoiceField(
37 choices=[(tag.id, tag.name) for tag in Tag.objects.filter(category='set', user=user)],
41 class ObjectSetsForm(forms.Form):
42 def __init__(self, obj, user, *args, **kwargs):
43 super(ObjectSetsForm, self).__init__(*args, **kwargs)
44 self.fields['set_ids'] = forms.MultipleChoiceField(
47 choices=[(tag.id, "%s (%s)" % (tag.name, tag.get_count())) for tag in Tag.objects.filter(category='set', user=user)],
48 initial=[tag.id for tag in obj.tags.filter(category='set', user=user)],
49 widget=forms.CheckboxSelectMultiple
53 class NewSetForm(forms.Form):
54 name = forms.CharField(max_length=50, required=True)
56 def __init__(self, *args, **kwargs):
57 super(NewSetForm, self).__init__(*args, **kwargs)
58 self.fields['name'].widget.attrs['title'] = _('Name of the new shelf')
60 def save(self, user, commit=True):
61 name = self.cleaned_data['name']
62 new_set = Tag(name=name, slug=utils.get_random_hash(name), sort_key=slughifi(name),
63 category='set', user=user)
79 class DownloadFormatsForm(forms.Form):
80 formats = forms.MultipleChoiceField(required=False, choices=FORMATS, widget=forms.CheckboxSelectMultiple)
82 def __init__(self, *args, **kwargs):
83 super(DownloadFormatsForm, self).__init__(*args, **kwargs)