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)],
21 class ObjectSetsForm(forms.Form):
22 tags = forms.CharField(label=_('Tags (comma-separated)'), required=False)
24 def __init__(self, obj, user, *args, **kwargs):
27 data = kwargs.setdefault('data', {})
28 if 'tags' not in data and user.is_authenticated():
29 data['tags'] = ', '.join(t.name
30 for t in obj.tags.filter(category='set', user=user) if t.name)
31 super(ObjectSetsForm, self).__init__(*args, **kwargs)
33 def save(self, request):
34 tags = [get_set(self._user, tag_name.strip())
35 for tag_name in self.cleaned_data['tags'].split(',')]
36 set_sets(self._user, self._obj, tags)
40 class NewSetForm(forms.Form):
41 name = forms.CharField(max_length=50, required=True)
43 def __init__(self, *args, **kwargs):
44 super(NewSetForm, self).__init__(*args, **kwargs)
45 self.fields['name'].widget.attrs['title'] = _('Name of the new shelf')
47 def save(self, user, commit=True):
48 name = self.cleaned_data['name']
49 new_set = Tag(name=name, slug=utils.get_random_hash(name), sort_key=name.lower(),
50 category='set', user=user)