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.core.files.base import ContentFile
7 from django.utils.translation import ugettext_lazy as _
8 from slughifi import slughifi
10 from catalogue.models import Tag, Book
11 from catalogue.fields import JQueryAutoCompleteField
12 from catalogue import utils
15 class BookImportForm(forms.Form):
16 book_xml_file = forms.FileField(required=False)
17 book_xml = forms.CharField(required=False)
20 if not self.cleaned_data['book_xml_file']:
21 if self.cleaned_data['book_xml']:
22 self.cleaned_data['book_xml_file'] = \
23 ContentFile(self.cleaned_data['book_xml'].encode('utf-8'))
25 raise forms.ValidationError(_("Please supply an XML."))
26 return super(BookImportForm, self).clean()
28 def save(self, commit=True, **kwargs):
29 return Book.from_xml_file(self.cleaned_data['book_xml_file'], overwrite=True, **kwargs)
32 class SearchForm(forms.Form):
33 q = JQueryAutoCompleteField('/katalog/tags/', {'minChars': 2, 'selectFirst': True, 'cacheLength': 50, 'matchContains': "word"})
34 tags = forms.CharField(widget=forms.HiddenInput, required=False)
36 def __init__(self, *args, **kwargs):
37 tags = kwargs.pop('tags', [])
38 super(SearchForm, self).__init__(*args, **kwargs)
39 self.fields['q'].widget.attrs['title'] = _('title, author, theme/topic, epoch, kind, genre')
40 #self.fields['q'].widget.attrs['style'] = ''
41 self.fields['tags'].initial = '/'.join(tag.url_chunk for tag in Tag.get_tag_list(tags))
44 class UserSetsForm(forms.Form):
45 def __init__(self, book, user, *args, **kwargs):
46 super(UserSetsForm, self).__init__(*args, **kwargs)
47 self.fields['set_ids'] = forms.ChoiceField(
48 choices=[(tag.id, tag.name) for tag in Tag.objects.filter(category='set', user=user)],
52 class ObjectSetsForm(forms.Form):
53 def __init__(self, obj, user, *args, **kwargs):
54 super(ObjectSetsForm, self).__init__(*args, **kwargs)
55 self.fields['set_ids'] = forms.MultipleChoiceField(
58 choices=[(tag.id, "%s (%s)" % (tag.name, tag.get_count())) for tag in Tag.objects.filter(category='set', user=user)],
59 initial=[tag.id for tag in obj.tags.filter(category='set', user=user)],
60 widget=forms.CheckboxSelectMultiple
64 class NewSetForm(forms.Form):
65 name = forms.CharField(max_length=50, required=True)
67 def __init__(self, *args, **kwargs):
68 super(NewSetForm, self).__init__(*args, **kwargs)
69 self.fields['name'].widget.attrs['title'] = _('Name of the new shelf')
71 def save(self, user, commit=True):
72 name = self.cleaned_data['name']
73 new_set = Tag(name=name, slug=utils.get_random_hash(name), sort_key=name.lower(),
74 category='set', user=user)
92 class DownloadFormatsForm(forms.Form):
93 formats = forms.MultipleChoiceField(required=False, choices=FORMATS, widget=forms.CheckboxSelectMultiple)
95 def __init__(self, *args, **kwargs):
96 super(DownloadFormatsForm, self).__init__(*args, **kwargs)