From: Marek Stępniowski Date: Thu, 25 Sep 2008 15:37:03 +0000 (+0200) Subject: Tracking book counts for shelves. X-Git-Url: https://git.mdrn.pl/wolnelektury.git/commitdiff_plain/4dc763d30ce6b8065c316731e2ed06220a74813c?ds=inline Tracking book counts for shelves. --- diff --git a/apps/catalogue/views.py b/apps/catalogue/views.py index 9ae50515d..c9d6ab227 100644 --- a/apps/catalogue/views.py +++ b/apps/catalogue/views.py @@ -188,8 +188,18 @@ def book_sets(request, slug): if request.method == 'POST': form = forms.ObjectSetsForm(book, request.user, request.POST) if form.is_valid(): - book.tags = ([models.Tag.objects.get(pk=id) for id in form.cleaned_data['set_ids']] + - list(book.tags.filter(~Q(category='set') | ~Q(user=request.user)))) + old_shelves = list(book.tags.filter(category='set')) + new_shelves = [models.Tag.objects.get(pk=id) for id in form.cleaned_data['set_ids']] + + for shelf in [shelf for shelf in old_shelves if shelf not in new_shelves]: + shelf.book_count -= 1 + shelf.save() + + for shelf in [shelf for shelf in new_shelves if shelf not in old_shelves]: + shelf.book_count += 1 + shelf.save() + + book.tags = new_shelves + list(book.tags.filter(~Q(category='set') | ~Q(user=request.user))) if request.is_ajax(): return HttpResponse('

Półki zostały zapisane.

') else: @@ -211,6 +221,9 @@ def remove_from_shelf(request, shelf, book): models.Tag.objects.remove_tag(book, shelf) + shelf.book_count -= 1 + shelf.save() + return HttpResponse('Usunieto')