1. Jesper Nøhr
  2. rewsfeed

Commits

Jesper Nøhr  committed 1d34323

beginning of events

  • Participants
  • Parent commits 0618df4
  • Branches default

Comments (0)

Files changed (2)

File src/rewsfeed/models.py

View file
         self.network_name = network_name
         self.conn = redis.Redis(host=host, port=port, password=password)
 
-        curried = [ 'hmset', 'exists', 'hgetall', 'sadd', 'srem', 'smembers' ]
+        curried = [ 'hmset', 'exists', 'hgetall',
+                    'sadd', 'srem', 'smembers',
+                    'set', 'get',
+                    'zadd', 'zrem', 'zcard', 'zrange' ]
         
         for curry in curried:
             setattr(self, curry, self.curry_fun(getattr(self.conn, curry)))
     def save(self):
         self.network.hmset(self.data_key(), self.kw)
 
+        return self
+        
     def data_key(self, for_obj=None):
         if for_obj is None:
-            for_obj = self.key
+            for_obj = self.key()
         return 'data:'+for_obj
 
     # --
         Follows another user. Adds both forward and reverse
         relationships in our set.
         """
-        forward = User.FOLLOWS_KEY+self.key
-        reverse = User.FOLLOWERS_KEY+other_user.key
+        forward = User.FOLLOWS_KEY+self.key()
+        reverse = User.FOLLOWERS_KEY+other_user.key()
 
-        return self.network.sadd(forward, other_user.key) and self.network.sadd(reverse, self.key)
+        return self.network.sadd(forward, other_user.key()) and self.network.sadd(reverse, self.key())
 
     def unfollow(self, other_user):
         """
         Unfollows another user.
         """
-        forward = User.FOLLOWS_KEY+self.key
-        reverse = User.FOLLOWERS_KEY+other_user.key
+        forward = User.FOLLOWS_KEY+self.key()
+        reverse = User.FOLLOWERS_KEY+other_user.key()
 
-        return self.network.srem(forward, other_user.key) and self.network.srem(reverse, self.key)
+        return self.network.srem(forward, other_user.key()) and self.network.srem(reverse, self.key())
     
     # -- 
     
     def following(self):
-        follows = self.network.smembers(User.FOLLOWS_KEY+self.key)
-        blocked = self.network.smembers(User.BLOCKED_KEY+self.key)
+        follows = self.network.smembers(User.FOLLOWS_KEY+self.key())
+        blocked = self.network.smembers(User.BLOCKED_KEY+self.key())
 
         for user in follows.difference(blocked):
             yield User(self.network, **self.network.hgetall(self.data_key(user)))
 
     def followers(self):
-        followers = self.network.smembers(User.FOLLOWERS_KEY+self.key)
-        blocks = self.network.smembers(User.BLOCKS_KEY+self.key)
+        followers = self.network.smembers(User.FOLLOWERS_KEY+self.key())
+        blocks = self.network.smembers(User.BLOCKS_KEY+self.key())
 
         for user in followers.difference(blocks):
             yield User(self.network, **self.network.hgetall(self.data_key(user)))
         
-    @property
-    def key(self):
-        return md5(self.kw['username']).hexdigest()
+    def key(self, extra=''):
+        return md5(self.kw['username']+extra).hexdigest()
 
     def flush(self):
         return True
+
+    # -- Events
+
+    def did(self, event):
+        return self.network.zadd(self.key('events'), event.key(), event.timestamp)
+
+    def done(self, start=0, end=20):
+        for hsh in self.network.zrange(self.key('events'), start, end):
+            yield Event(self.network, **self.network.hgetall(self.data_key(hsh)))
+        
+class Event(DictAbstracted):
+    """
+    Funky events.
+    """
+    def key(self, extra=''):
+        return md5(self.kw['pk']+extra).hexdigest()
     
-class Event(DictAbstracted):
     @property
-    def key(self):
-        return md5(self.kw['pk']).hexdigest()
+    def timestamp(self):
+        return self.kw.get('timestamp')
+    
+    @classmethod
+    def add_event(cls, network, who, **kwargs):
+        if not isinstance(who, User):
+            raise ValueError("A User must've done it. You gave me '%r'" % who)
+
+        event = cls(network, **kwargs).save()
+
+        who.did(event)

File tests/tests.py

View file
 
 sys.path.insert(0, '../src')
 
-from rewsfeed import Network, User
+from rewsfeed import Network, User, Event
 
 class TestNetwork(unittest.TestCase):
     def test_network_init(self):
         self.assertEquals(self.u2.network, self.n)
 
     def test_user_keys(self):
-        self.assertEquals(self.u1.key, '24c9e15e52afc47c225b757e7bee1f9d')
-        self.assertEquals(self.u2.key, '7e58d63b60197ceb55a1c487989a3720')
+        self.assertEquals(self.u1.key(), '24c9e15e52afc47c225b757e7bee1f9d')
+        self.assertEquals(self.u2.key(), '7e58d63b60197ceb55a1c487989a3720')
 
     def test_user_flush(self):
         self.assertEquals(self.u1.flush(), True)
 
         self.assertEquals(len(list(self.u1.following())), 0)
 
+class TestEvents(unittest.TestCase):
+    def setUp(self):
+        self.n = Network('events')
+        self.n.conn.flushdb()
+
+        self.u1 = User(self.n, username='user1')
+        self.u2 = User(self.n, username='user2')        
+
+        self.u1.save()
+        self.u2.save()
+
+    def test_inject_event(self):
+        Event.add_event(self.n, self.u1, har="du", piller="ja", pk="1", timestamp=12345678901)
+
+        print list(self.u1.done())
+        
 if __name__ == "__main__":
     unittest.main()