fix duplicated shelves
authorJan Szejko <janek37@gmail.com>
Thu, 19 Jul 2018 09:54:08 +0000 (11:54 +0200)
committerJan Szejko <janek37@gmail.com>
Thu, 19 Jul 2018 09:54:08 +0000 (11:54 +0200)
src/catalogue/models/book.py
src/newtagging/models.py
src/social/utils.py
src/social/views.py

index 7cd6386..b71926e 100644 (file)
@@ -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:
index 83aabe4..9d12111 100644 (file)
@@ -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
index f8fb241..52fd4e4 100755 (executable)
@@ -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
 
 
index 15f01bf..757b5a2 100644 (file)
@@ -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)