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):
18 return Book.from_xml_file(self.cleaned_data['book_xml_file'], overwrite=True)
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['tags'].initial = '/'.join(tag.slug for tag in Tag.get_tag_list(tags))
32 class UserSetsForm(forms.Form):
33 def __init__(self, book, user, *args, **kwargs):
34 super(UserSetsForm, self).__init__(*args, **kwargs)
35 self.fields['set_ids'] = forms.ChoiceField(
36 choices=[(tag.id, tag.name) for tag in Tag.objects.filter(category='set', user=user)],
40 class ObjectSetsForm(forms.Form):
41 def __init__(self, obj, user, *args, **kwargs):
42 super(ObjectSetsForm, self).__init__(*args, **kwargs)
43 self.fields['set_ids'] = forms.MultipleChoiceField(
46 choices=[(tag.id, "%s (%s)" % (tag.name, tag.book_count)) for tag in Tag.objects.filter(category='set', user=user)],
47 initial=[tag.id for tag in obj.tags.filter(category='set', user=user)],
48 widget=forms.CheckboxSelectMultiple
52 class NewSetForm(forms.Form):
53 name = forms.CharField(max_length=50, required=True)
55 def __init__(self, *args, **kwargs):
56 super(NewSetForm, self).__init__(*args, **kwargs)
57 self.fields['name'].widget.attrs['title'] = _('Name of the new shelf')
59 def save(self, user, commit=True):
60 name = self.cleaned_data['name']
61 new_set = Tag(name=name, slug=utils.get_random_hash(name), sort_key=slughifi(name),
62 category='set', user=user)
78 class DownloadFormatsForm(forms.Form):
79 formats = forms.MultipleChoiceField(required=False, choices=FORMATS, widget=forms.CheckboxSelectMultiple)
81 def __init__(self, *args, **kwargs):
82 super(DownloadFormatsForm, self).__init__(*args, **kwargs)