-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
20b11f6
commit 7761cc3
Showing
6 changed files
with
98 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
from .service import ActivityService | ||
from .models import ActivityEntry |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
from abilian.core.extensions import db | ||
from abilian.core.signals import activity | ||
|
||
from .models import ActivityEntry | ||
|
||
|
||
__all__ = ['ActivityService'] | ||
|
||
|
||
class ActivityService(object): | ||
|
||
def __init__(self, app=None): | ||
self.running = False | ||
if app: | ||
self.init_app(app) | ||
|
||
def init_app(self, app): | ||
self.app = app | ||
|
||
def start(self): | ||
assert not self.running | ||
activity.connect(self.log_activity) | ||
self.running = True | ||
|
||
def stop(self): | ||
assert self.running | ||
activity.disconnect(self.log_activity) | ||
self.running = False | ||
|
||
def log_activity(self, sender, actor, verb, object, subject=None): | ||
assert self.running | ||
entry = ActivityEntry() | ||
entry.actor = actor | ||
entry.verb = verb | ||
entry.object_id = object.id | ||
entry.object_class = object.__class__.__name__ | ||
if subject: | ||
entry.subject_id = subject.id | ||
entry.subject_class = subject.__class__.__name__ | ||
|
||
db.session.add(entry) | ||
|
||
@staticmethod | ||
def entries_for_actor(actor, limit=50): | ||
return ActivityEntry.query.filter(ActivityEntry.actor_id == actor.id).limit(limit).all() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
from abilian.core.entities import Entity | ||
from abilian.core.extensions import db | ||
from abilian.core.subjects import User | ||
from abilian.testing import BaseTestCase | ||
|
||
from .service import ActivityService | ||
|
||
|
||
class Message1(Entity): | ||
pass | ||
|
||
class ActivityTestCase(BaseTestCase): | ||
|
||
def setUp(self): | ||
BaseTestCase.setUp(self) | ||
self.activity_service = ActivityService() | ||
self.activity_service.start() | ||
|
||
def test(self): | ||
service = self.activity_service | ||
user = User(email=u'test@example.com') | ||
message = Message1() | ||
|
||
db.session.add(user) | ||
db.session.add(message) | ||
db.session.flush() | ||
|
||
service.log_activity(None, user, "post", message) | ||
db.session.flush() | ||
|
||
entries = service.entries_for_actor(user, 10) | ||
self.assertEquals(len(entries), 1) | ||
entry = entries[0] | ||
self.assertEquals(entry.actor, user) | ||
self.assertEquals(entry.object, message) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters