1 # This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later.
2 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
4 from django import forms
6 from catalogue.models import Book, Tag
7 from social.utils import get_set
10 class UserSetsForm(forms.Form):
11 def __init__(self, book, user, *args, **kwargs):
12 super(UserSetsForm, self).__init__(*args, **kwargs)
13 self.fields['set_ids'] = forms.ChoiceField(
14 choices=[(tag.id, tag.name) for tag in Tag.objects.filter(category='set', user=user).iterator()],
18 class AddSetForm(forms.Form):
19 name = forms.CharField(max_length=50)
20 book = forms.IntegerField()
23 name = self.cleaned_data['name'].strip()
25 tag = get_set(user, name)
27 book = Book.objects.get(id=self.cleaned_data['book'])
28 except Book.DoesNotExist:
32 book.tag_relations.create(tag=tag)
39 class RemoveSetForm(forms.Form):
40 slug = forms.CharField(max_length=50)
41 book = forms.IntegerField()
44 slug = self.cleaned_data['slug']
46 tag = Tag.objects.get(user=user, slug=slug)
47 except Tag.DoesNotExist:
50 book = Book.objects.get(id=self.cleaned_data['book'])
51 except Book.DoesNotExist:
55 book.tag_relations.filter(tag=tag).delete()