From c2637e02e9822235015b683c9712e5cc7ec250d2 Mon Sep 17 00:00:00 2001 From: Alejandro Alonso Date: Thu, 13 Oct 2016 11:18:28 +0200 Subject: [PATCH] Fixing upvote downvote api --- taiga/projects/votes/services.py | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/taiga/projects/votes/services.py b/taiga/projects/votes/services.py index 6d80c20d..5c99efa9 100644 --- a/taiga/projects/votes/services.py +++ b/taiga/projects/votes/services.py @@ -23,6 +23,8 @@ from django.db import transaction as tx from django.apps import apps from django.contrib.auth import get_user_model +from django_pglocks import advisory_lock + from .models import Votes, Vote @@ -37,14 +39,15 @@ def add_vote(obj, user): :param user: User adding the vote. :class:`~taiga.users.models.User` instance. """ obj_type = apps.get_model("contenttypes", "ContentType").objects.get_for_model(obj) - vote, created = Vote.objects.get_or_create(content_type=obj_type, object_id=obj.id, user=user) - if not created: - return + with advisory_lock("vote-{}-{}".format(obj_type.id, obj.id)): + vote, created = Vote.objects.get_or_create(content_type=obj_type, object_id=obj.id, user=user) + if not created: + return - votes, _ = Votes.objects.get_or_create(content_type=obj_type, object_id=obj.id) - votes.count = F('count') + 1 - votes.save() - return vote + votes, _ = Votes.objects.get_or_create(content_type=obj_type, object_id=obj.id) + votes.count = F('count') + 1 + votes.save() + return vote @tx.atomic @@ -58,9 +61,10 @@ def remove_vote(obj, user): :param user: User removing her vote. :class:`~taiga.users.models.User` instance. """ obj_type = apps.get_model("contenttypes", "ContentType").objects.get_for_model(obj) - qs = Vote.objects.filter(content_type=obj_type, object_id=obj.id, user=user) - if not qs.exists(): - return + with advisory_lock("vote-{}-{}".format(obj_type.id, obj.id)): + qs = Vote.objects.filter(content_type=obj_type, object_id=obj.id, user=user) + if not qs.exists(): + return qs.delete()