If you hit the actstream_follow view multiple times, multiple Follow records get created.
I could check for existing follow records... but i'm lazy.
I am fairly new to Python/Django, but I altered your models.py on my machine and added a unique_together clause in the Meta class of Follow. Then i catch an "IntegrityError" in the follow() method. If an IntegrityError is raised the follow method still returns a Follow object but it returns the existing object instead of creating a new one and it DOESNT fire the signal.
I don't know if this fix is very Python'ish though.
I also wasn't able to get the tests to pass either but I think that's because I dont' know how to get them to run for a standalone django app without a project.
diff --git a/actstream/models.py b/actstream/models.py
index 699ebf5..0f1bd0a 100644
--- a/actstream/models.py
+++ b/actstream/models.py
@@ -8,6 +8,7 @@ from django.utils.timesince import timesince as timesince_
from django.contrib.contenttypes import generic
from django.contrib.contenttypes.models import ContentType
from django.contrib.auth.models import User
+from django.db import IntegrityError
from actstream.signals import action
@@ -36,7 +37,10 @@ class Follow(models.Model):
actor = generic.GenericForeignKey()
objects = FollowManager()
-
+
+ class Meta:
+ unique_together = (("user", "content_type", "object_id"),)
+
def __unicode__(self):
return u'%s -> %s' % (self.user, self.actor)
@@ -157,10 +161,15 @@ def follow(user, actor, send_action=True):
follow(request.user, group)
"""
- if send_action:
- action.send(user, verb=_('started following'), target=actor)
- return Follow.objects.create(user = user, object_id = actor.pk,
- content_type = ContentType.objects.get_for_model(actor))
+ try:
+ follow = Follow.objects.create(user = user, object_id = actor.pk,
+ content_type = ContentType.objects.get_for_model(actor))
+ if send_action:
+ action.send(user, verb=_('started following'), target=actor)
+ return follow
+ except IntegrityError:
+ return Follow.objects.filter(user = user, object_id = actor.pk,
+ content_type = ContentType.objects.get_for_model(actor)).all()[0]
def unfollow(user, actor, send_action=False):
"""
diff --git a/setup.py b/setup.py
old mode 100644
new mode 100755