Skip to content

Commit

Permalink
[Bug 1083423] Add actstream hooks to Questions and Answers.
Browse files Browse the repository at this point in the history
This causes Questions and Answers to create Actions objects when
created. These objects don't do anything yet, and aren't visible, but
they will exist in the database. The UI and APIs for this will come
later.
  • Loading branch information
mythmon committed Feb 2, 2015
1 parent bf39a13 commit 880c7eb
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 0 deletions.
5 changes: 5 additions & 0 deletions kitsune/notifications/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
from django.contrib.auth.models import User
from django.db import models

import actstream.registry

from kitsune.sumo.models import ModelBase


Expand All @@ -11,3 +13,6 @@ class PushNotificationRegistration(ModelBase):
creator = models.ForeignKey(User, db_index=True)
created = models.DateTimeField(default=datetime.now)
push_url = models.CharField(max_length=256)


actstream.registry.register(User)
26 changes: 26 additions & 0 deletions kitsune/questions/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,15 @@
from django.core.cache import cache
from django.core.urlresolvers import resolve
from django.conf import settings
from django.dispatch import receiver
from django.db import models, connection, close_old_connections
from django.db.models import Q
from django.db.models.signals import post_save, pre_save
from django.db.utils import IntegrityError
from django.http import Http404

import actstream
import actstream.registry
from product_details import product_details
from statsd import statsd
from taggit.models import Tag, TaggedItem
Expand Down Expand Up @@ -1332,3 +1335,26 @@ def _content_parsed(obj, locale):
html = wiki_to_html(obj.content, locale)
cache.add(cache_key, html, CACHE_TIMEOUT)
return html


actstream.registry.register(Question)
actstream.registry.register(Answer)


@receiver(post_save, sender=Question, dispatch_uid='question_create_actionstream')
def add_action_for_new_question(sender, instance, created, **kwargs):
if created:
actstream.action.send(
instance.creator,
verb='asked',
action_object=instance)


@receiver(post_save, sender=Answer, dispatch_uid='answer_create_actionstream')
def add_action_for_new_answer(sender, instance, created, **kwargs):
if created:
actstream.action.send(
instance.creator,
verb='answered',
action_object=instance,
target=instance.question)
34 changes: 34 additions & 0 deletions kitsune/questions/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from django.db.models import Q

import mock
from actstream.models import Action
from nose.tools import eq_, ok_, raises
from taggit.models import Tag

Expand Down Expand Up @@ -581,3 +582,36 @@ def test_add_metadata_over_1000_chars(self):
qv.add_metadata('test1', 'a'*1001)
metadata = VoteMetadata.objects.all()[0]
eq_('a'*1000, metadata.value)


class TestSignals(TestCase):
def test_question_create_action(self):
"""When a question is created, an Action is created too."""
q = question(save=True)
a = Action.objects.action_object(q).get()
eq_(a.actor, q.creator)
eq_(a.verb, 'asked')
eq_(a.target, None)

def test_answer_create_action(self):
"""When an answer is created, an Action is created too."""
q = question(save=True)
ans = answer(question=q, save=True)
act = Action.objects.action_object(ans).get()
eq_(act.actor, ans.creator)
eq_(act.verb, 'answered')
eq_(act.target, q)

def test_question_change_no_action(self):
"""When a question is changed, no action should be created."""
q = question(save=True)
Action.objects.all().delete()
q.save() # trigger another post_save hook
eq_(Action.objects.count(), 0)

def test_answer_change_no_action(self):
"""When an answer is changed, no action should be created."""
a = question(save=True)
Action.objects.all().delete()
a.save() # trigger another post_save hook
eq_(Action.objects.count(), 0)

0 comments on commit 880c7eb

Please sign in to comment.