From 141f3bea77fdb337069c198ffeee870696f4554e Mon Sep 17 00:00:00 2001 From: Jan Szejko Date: Thu, 19 Jul 2018 11:54:08 +0200 Subject: [PATCH] fix duplicated shelves --- src/catalogue/models/book.py | 11 +++++++++++ src/newtagging/models.py | 16 ++++++++++++---- src/social/utils.py | 9 +++++++++ src/social/views.py | 34 ++++++++++++++++------------------ 4 files changed, 48 insertions(+), 22 deletions(-) diff --git a/src/catalogue/models/book.py b/src/catalogue/models/book.py index 7cd6386ba..b71926ec3 100644 --- a/src/catalogue/models/book.py +++ b/src/catalogue/models/book.py @@ -770,6 +770,17 @@ class Book(models.Model): def ridero_link(self): return 'https://ridero.eu/%s/books/wl_%s/' % (get_language(), self.slug.replace('-', '_')) + def like(self, user): + from social.utils import likes, get_set, set_sets + if not likes(user, self): + tag = get_set(user, '') + set_sets(user, self, [tag]) + + def unlike(self, user): + from social.utils import likes, set_sets + if likes(user, self): + set_sets(user, self, []) + def add_file_fields(): for format_ in Book.formats: diff --git a/src/newtagging/models.py b/src/newtagging/models.py index 83aabe4fc..9d12111d2 100644 --- a/src/newtagging/models.py +++ b/src/newtagging/models.py @@ -62,11 +62,9 @@ class TagManager(models.Manager): object_id=obj.pk, tag__in=tags_for_removal).delete() # Add new tags - tags_to_add = [tag for tag in updated_tags - if tag not in current_tags] + tags_to_add = [tag for tag in updated_tags if tag not in current_tags] for tag in tags_to_add: - if tag not in current_tags: - self.intermediary_table_model.objects.create(tag=tag, content_object=obj) + self.intermediary_table_model.objects.create(tag=tag, content_object=obj) tags_updated.send(sender=type(obj), instance=obj, affected_tags=tags_to_add + tags_for_removal) @@ -78,6 +76,16 @@ class TagManager(models.Manager): self.intermediary_table_model.objects.filter( content_type__pk=content_type.pk, object_id=obj.pk, tag=tag).delete() + def add_tag(self, obj, tag): + """ + Add tag to an object. + """ + content_type = ContentType.objects.get_for_model(obj) + relations = self.intermediary_table_model.objects.filter( + content_type__pk=content_type.pk, object_id=obj.pk, tag=tag) + if not relations: + self.intermediary_table_model.objects.create(tag=tag, content_object=obj) + def get_for_object(self, obj): """ Create a queryset matching all tags associated with the given diff --git a/src/social/utils.py b/src/social/utils.py index f8fb2410a..52fd4e443 100755 --- a/src/social/utils.py +++ b/src/social/utils.py @@ -52,6 +52,15 @@ def get_set(user, name): except Tag.DoesNotExist: tag = Tag.objects.create( category='set', user=user, name=name, slug=utils.get_random_hash(name), sort_key=name.lower()) + except Tag.MultipleObjectsReturned: + # fix duplicated noname shelf + tags = list(Tag.objects.filter(category='set', user=user, name=name)) + tag = tags[0] + for other_tag in tags[1:]: + for item in other_tag.items.all(): + Tag.objects.remove_tag(item, other_tag) + Tag.objects.add_tag(item, tag) + other_tag.delete() return tag diff --git a/src/social/views.py b/src/social/views.py index 15f01bf45..757b5a26d 100644 --- a/src/social/views.py +++ b/src/social/views.py @@ -13,7 +13,6 @@ from catalogue.models import Book from ssify import ssi_included from social import forms from .models import Cite -from social.utils import get_set, likes, set_sets # ==================== @@ -26,9 +25,8 @@ def like_book(request, slug): if not request.user.is_authenticated(): return HttpResponseForbidden('Login required.') book = get_object_or_404(Book, slug=slug) - if not likes(request.user, book): - tag = get_set(request.user, '') - set_sets(request.user, book, [tag]) + + book.like(request.user) if request.is_ajax(): return JsonResponse({"success": True, "msg": "ok", "like": True}) @@ -36,6 +34,20 @@ def like_book(request, slug): return redirect(book) +@require_POST +def unlike_book(request, slug): + if not request.user.is_authenticated(): + return HttpResponseForbidden('Login required.') + book = get_object_or_404(Book, slug=slug) + + book.unlike(request.user) + + if request.is_ajax(): + return JsonResponse({"success": True, "msg": "ok", "like": False}) + else: + return redirect(book) + + @login_required def my_shelf(request): return render(request, 'social/my_shelf.html', { @@ -60,20 +72,6 @@ class ObjectSetsFormView(AjaxableFormView): return (obj, request.user), {} -@require_POST -def unlike_book(request, slug): - if not request.user.is_authenticated(): - return HttpResponseForbidden('Login required.') - book = get_object_or_404(Book, slug=slug) - if likes(request.user, book): - set_sets(request.user, book, []) - - if request.is_ajax(): - return JsonResponse({"success": True, "msg": "ok", "like": False}) - else: - return redirect(book) - - @ssi_included def cite(request, pk, main=False): cite = get_object_or_404(Cite, pk=pk) -- 2.20.1