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 _
8 from catalogue.models import Tag
9 from catalogue import utils
10 from social.utils import get_set, set_sets
13 class UserSetsForm(forms.Form):
14 def __init__(self, book, user, *args, **kwargs):
15 super(UserSetsForm, self).__init__(*args, **kwargs)
16 self.fields['set_ids'] = forms.ChoiceField(
17 choices=[(tag.id, tag.name) for tag in Tag.objects.filter(category='set', user=user).iterator()],
21 class ObjectSetsForm(forms.Form):
22 tags = forms.CharField(label=_('Tags (comma-separated)'), required=False,
23 widget=forms.Textarea())
25 def __init__(self, obj, user, *args, **kwargs):
28 data = kwargs.setdefault('data', {})
29 if 'tags' not in data and user.is_authenticated():
30 data['tags'] = ', '.join(t.name for t in obj.tags.filter(category='set', user=user).iterator() if t.name)
31 super(ObjectSetsForm, self).__init__(*args, **kwargs)
33 def save(self, request):
34 tags = [get_set(self._user, tag_name.strip()) for tag_name in self.cleaned_data['tags'].split(',')]
35 set_sets(self._user, self._obj, tags)
39 class NewSetForm(forms.Form):
40 name = forms.CharField(max_length=50, required=True)
42 def __init__(self, *args, **kwargs):
43 super(NewSetForm, self).__init__(*args, **kwargs)
44 self.fields['name'].widget.attrs['title'] = _('Name of the new shelf')
46 def save(self, user, commit=True):
47 name = self.cleaned_data['name']
48 new_set = Tag(name=name, slug=utils.get_random_hash(name), sort_key=name.lower(), category='set', user=user)