Skip to content

Commit

Permalink
Refactor activity service.
Browse files Browse the repository at this point in the history
  • Loading branch information
sfermigier committed Jun 8, 2013
1 parent 20b11f6 commit 7761cc3
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 43 deletions.
2 changes: 2 additions & 0 deletions abilian/services/activity/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from .service import ActivityService
from .models import ActivityEntry
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@
from sqlalchemy.types import Integer, DateTime, Text

from abilian.core.entities import db, all_entity_classes
from abilian.core.signals import activity
from abilian.core.subjects import User

__all__ = ['ActivityEntry']


# TODO: review the design as it hits the DB with too many requests.
class ActivityEntry(db.Model):
Expand Down Expand Up @@ -48,41 +49,3 @@ def object(self):
if cls.__name__ == self.object_class:
return cls.query.get(self.object_id)
raise Exception("Unknown class: %s" % self.object_class)


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()
45 changes: 45 additions & 0 deletions abilian/services/activity/service.py
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()
37 changes: 37 additions & 0 deletions abilian/services/activity/tests.py
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)


15 changes: 11 additions & 4 deletions abilian/services/tagging/service.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
"""
"""

class Taggable(object):
"""Mixin trait for taggable objects.
"""
Mixin trait for taggable objects.
Currently not used.
"""
pass


class TagService(object):
"""The tag service.
"""
The tag service.
"""

def tag(self, object, term, user=None):
Expand All @@ -22,7 +27,9 @@ def untag(self, object, term, user=None):
"""

def get_objects_tagged_with(self, term):
"""Returns a list of objects tagged with a given term."""
"""Returns a list of objects tagged with a given term.
"""

def get_tags_applied_on(self, object):
"""Return a list of tags applied on a given document."""
"""Returns a list of tags applied on a given document.
"""
1 change: 1 addition & 0 deletions abilian/testing/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class TestConfig(object):
SQLALCHEMY_DATABASE_URI = "sqlite://"
SQLALCHEMY_ECHO = False
TESTING = True
SECRET = ""


class BaseTestCase(TestCase):
Expand Down

0 comments on commit 7761cc3

Please sign in to comment.