def vote(self, user):
from django.core.cache import cache
from biohub.core.conf import settings as biohub_settings
key = 'user_{}_vote'.format(user.id)
if cache.get(key) is not None:
raise Throttled()
cache.set(key, 1, timeout=biohub_settings.THROTTLE['vote'])
if self.author is not None and self.author.id == user.id:
return False
if not self.voted_users.filter(pk=user.id).exists():
with transaction.atomic():
self.votes += 1
self.voted_users.add(user)
self.save(update_fields=['votes'])
voted_experience_signal.send(
sender=self.__class__, instance=self,
user_voted=user, current_votes=self.votes)
return True
return False
评论列表
文章目录