def tags_starting_with(request):
- prefix = request.GET['q']
+ prefix = request.GET.get('q', '')
# Prefix must have at least 2 characters
if len(prefix) < 2:
return HttpResponse('')
return render_to_response('catalogue/user_shelves.html', locals(),
context_instance=RequestContext(request))
-@login_required
@cache.never_cache
def book_sets(request, slug):
book = get_object_or_404(models.Book, slug=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>Półki zostały zapisane.</p>')
else:
models.Tag.objects.remove_tag(book, shelf)
+ shelf.book_count -= 1
+ shelf.save()
+
return HttpResponse('Usunieto')
else:
return HttpResponseRedirect('/')
- return render_to_response('catalogue/book_sets.html', locals(),
- context_instance=RequestContext(request))
+ return HttpResponseRedirect('/')
@login_required