X-Git-Url: https://git.mdrn.pl/wolnelektury.git/blobdiff_plain/4e361d55331351f56077e5f9999ecb4ee7e93819..a9226097c8eea0591fd3249f62a71becc6d2be93:/src/social/forms.py?ds=sidebyside diff --git a/src/social/forms.py b/src/social/forms.py index 5c0973c19..8527915a6 100644 --- a/src/social/forms.py +++ b/src/social/forms.py @@ -2,9 +2,9 @@ # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information. # from django import forms -from django.utils.translation import ugettext_lazy as _ +from django.utils.translation import gettext_lazy as _ -from catalogue.models import Tag +from catalogue.models import Book, Tag from catalogue import utils from social.utils import get_set, set_sets @@ -49,3 +49,47 @@ class NewSetForm(forms.Form): new_set.save() return new_set + + +class AddSetForm(forms.Form): + name = forms.CharField(max_length=50) + book = forms.IntegerField() + + def save(self, user): + name = self.cleaned_data['name'].strip() + if not name: return + tag = get_set(user, name) + try: + book = Book.objects.get(id=self.cleaned_data['book']) + except Book.DoesNotExist: + return + + try: + book.tag_relations.create(tag=tag) + except: + pass + + return book, tag + + +class RemoveSetForm(forms.Form): + slug = forms.CharField(max_length=50) + book = forms.IntegerField() + + def save(self, user): + slug = self.cleaned_data['slug'] + try: + tag = Tag.objects.get(user=user, slug=slug) + except Tag.DoesNotExist: + return + try: + book = Book.objects.get(id=self.cleaned_data['book']) + except Book.DoesNotExist: + return + + try: + book.tag_relations.filter(tag=tag).delete() + except: + pass + + return book, tag