workaround for ie ignoring everything
[wolnelektury.git] / apps / social / 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
8 from catalogue.models import Tag
9 from catalogue import utils
10 from social.utils import get_set, set_sets
11
12
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)],
18         )
19
20
21 class ObjectSetsForm(forms.Form):
22     tags = forms.CharField(label=_('Tags (comma-separated)'), required=False,
23                            widget=forms.Textarea())
24
25     def __init__(self, obj, user, *args, **kwargs):
26         self._obj = obj
27         self._user = user
28         data = kwargs.setdefault('data', {})
29         if 'tags' not in data and user.is_authenticated():
30             data['tags'] = ', '.join(t.name
31                 for t in obj.tags.filter(category='set', user=user) if t.name)
32         super(ObjectSetsForm, self).__init__(*args, **kwargs)
33
34     def save(self, request):
35         tags = [get_set(self._user, tag_name.strip())
36                     for tag_name in self.cleaned_data['tags'].split(',')]
37         set_sets(self._user, self._obj, tags)
38         return {"like": True}
39
40
41 class NewSetForm(forms.Form):
42     name = forms.CharField(max_length=50, required=True)
43
44     def __init__(self, *args, **kwargs):
45         super(NewSetForm, self).__init__(*args, **kwargs)
46         self.fields['name'].widget.attrs['title'] = _('Name of the new shelf')
47
48     def save(self, user, commit=True):
49         name = self.cleaned_data['name']
50         new_set = Tag(name=name, slug=utils.get_random_hash(name), sort_key=name.lower(),
51             category='set', user=user)
52
53         new_set.save()
54         return new_set