migration and oth. minor fixes
[wolnelektury.git] / apps / catalogue / forms.py
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.
4 #
5 from django import forms
6 from django.utils.translation import ugettext_lazy as _
7 from slughifi import slughifi
8
9 from catalogue.models import Tag, Book
10 from catalogue.fields import JQueryAutoCompleteField
11 from catalogue import utils
12
13
14 class BookImportForm(forms.Form):
15     book_xml_file = forms.FileField()
16
17     def save(self, commit=True):
18         return Book.from_xml_file(self.cleaned_data['book_xml_file'], overwrite=True)
19
20
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)
24     
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))
30
31
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)],
37         )
38
39
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(
44             label=_('Shelves'),
45             required=False,
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
49         )
50         
51
52 class NewSetForm(forms.Form):
53     name = forms.CharField(max_length=50, required=True)
54     
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')
58         
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)
63         
64         new_set.save()
65         return new_set
66
67
68 FORMATS = (
69     ('mp3', 'MP3'),
70     ('ogg', 'OGG'),
71     ('pdf', 'PDF'),
72     ('odt', 'ODT'),
73     ('txt', 'TXT'),
74     ('epub', 'EPUB'),
75 )
76
77
78 class DownloadFormatsForm(forms.Form):
79     formats = forms.MultipleChoiceField(required=False, choices=FORMATS, widget=forms.CheckboxSelectMultiple)
80     
81     def __init__(self, *args, **kwargs):
82          super(DownloadFormatsForm, self).__init__(*args, **kwargs)
83