Permalink
Browse files

Send signal when a user is linked to a response

  • Loading branch information...
1 parent cacf5ca commit 6b45021e99e0ad763c4532839e68aea889e464f3 @paltman paltman committed Dec 20, 2012
Showing with 29 additions and 3 deletions.
  1. +1 −3 anafero/middleware.py
  2. +7 −0 anafero/models.py
  3. +4 −0 anafero/signals.py
  4. +1 −0 docs/index.rst
  5. +16 −0 docs/signals.rst
View
@@ -14,9 +14,7 @@ def process_request(self, request):
try:
referral = Referral.objects.get(code=code)
- for response in referral.responses.filter(session_key=session_key, user__isnull=True):
- response.user = request.user
- response.save()
+ referral.link_responses_to_user(request.user, session_key)
except Referral.DoesNotExist:
pass
View
@@ -8,6 +8,7 @@
from django.contrib.sites.models import Site
from anafero.conf import settings
+from anafero.signals import user_linked_to_response
class Referral(models.Model):
@@ -93,6 +94,12 @@ def referral_for_request(cls, request):
except IndexError:
pass
+ def link_responses_to_user(self, user, session_key):
+ for response in self.responses.filter(session_key=session_key, user__isnull=True):
+ response.user = user
+ response.save()
+ user_linked_to_response.send(sender=self, response=response)
+
def respond(self, request, action_string, user=None, target=None):
if user is None:
if request.user.is_authenticated():
View
@@ -0,0 +1,4 @@
+import django.dispatch
+
+
+user_linked_to_response = django.dispatch.Signal(providing_args=["response"])
View
@@ -31,6 +31,7 @@ Contents
installation
usage
settings
+ signals
templates
templatetags
changelog
View
@@ -0,0 +1,16 @@
+.. _signals:
+
+Signals
+=======
+
+``user_linked_to_response`` is a signal that provides the single argument of a ``response``
+object that has just been linked to a user. You can use this to provide further authomatic
+processing within your site, such as adding permissions, etc. to users that signup as a result
+of a referral.
+
+For example:
+
+ @receiver(user_linked_to_response)
+ def handle_user_linked_to_response(sender, response, **kwargs):
+ if response.action == "SOME_SPECIAL_ACTION":
+ pass # do something with response.user and response.referral.target (object that referral was linked to)

0 comments on commit 6b45021

Please sign in to comment.