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 JQueryAutoCompleteSearchField
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 = JQueryAutoCompleteSearchField('/newsearch/hint/') # {'minChars': 2, 'selectFirst': True, 'cacheLength': 50, 'matchContains': "word"})
34 tags = forms.CharField(widget=forms.HiddenInput, required=False)
36 book = forms.IntegerField(widget=forms.HiddenInput, min_value=0, required=False)
38 def __init__(self, *args, **kwargs):
39 tags = kwargs.pop('tags', [])
40 book = kwargs.pop('book', None)
41 super(SearchForm, self).__init__(*args, **kwargs)
42 self.fields['q'].widget.attrs['title'] = _('title, author, theme/topic, epoch, kind, genre, phrase')
43 #self.fields['q'].widget.attrs['style'] = ''
44 self.fields['tags'].initial = '/'.join(tag.url_chunk for tag in Tag.get_tag_list(tags))
46 self.fields['book'].initial = book.id
49 class UserSetsForm(forms.Form):
50 def __init__(self, book, user, *args, **kwargs):
51 super(UserSetsForm, self).__init__(*args, **kwargs)
52 self.fields['set_ids'] = forms.ChoiceField(
53 choices=[(tag.id, tag.name) for tag in Tag.objects.filter(category='set', user=user)],
57 class ObjectSetsForm(forms.Form):
58 def __init__(self, obj, user, *args, **kwargs):
59 super(ObjectSetsForm, self).__init__(*args, **kwargs)
60 self.fields['set_ids'] = forms.MultipleChoiceField(
63 choices=[(tag.id, "%s (%s)" % (tag.name, tag.get_count())) for tag in Tag.objects.filter(category='set', user=user)],
64 initial=[tag.id for tag in obj.tags.filter(category='set', user=user)],
65 widget=forms.CheckboxSelectMultiple
69 class NewSetForm(forms.Form):
70 name = forms.CharField(max_length=50, required=True)
72 def __init__(self, *args, **kwargs):
73 super(NewSetForm, self).__init__(*args, **kwargs)
74 self.fields['name'].widget.attrs['title'] = _('Name of the new shelf')
76 def save(self, user, commit=True):
77 name = self.cleaned_data['name']
78 new_set = Tag(name=name, slug=utils.get_random_hash(name), sort_key=name.lower(),
79 category='set', user=user)
97 class DownloadFormatsForm(forms.Form):
98 formats = forms.MultipleChoiceField(required=False, choices=FORMATS, widget=forms.CheckboxSelectMultiple)
100 def __init__(self, *args, **kwargs):
101 super(DownloadFormatsForm, self).__init__(*args, **kwargs)