Fixed a bug in saving shelves for each bug (beware of lazy evaluation of querysets!).
[wolnelektury.git] / apps / catalogue / views.py
index 9ae5051..c9d6ab2 100644 (file)
@@ -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():
     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>Półki zostały zapisane.</p>')
             else:
             if request.is_ajax():
                 return HttpResponse('<p>Półki zostały zapisane.</p>')
             else:
@@ -211,6 +221,9 @@ def remove_from_shelf(request, shelf, book):
     
     models.Tag.objects.remove_tag(book, shelf)
     
     
     models.Tag.objects.remove_tag(book, shelf)
     
+    shelf.book_count -= 1
+    shelf.save()
+    
     return HttpResponse('Usunieto')
 
 
     return HttpResponse('Usunieto')