[entities] minor refactoring: next_entity and previous_entity actually share 99% of the code
authorAdrien Di Mascio <Adrien.DiMascio@logilab.fr>
Fri, 06 Nov 2009 14:38:33 +0100
changeset 144 265e1ad99d07
parent 143 3ebe83f66291
child 145 629233488671
[entities] minor refactoring: next_entity and previous_entity actually share 99% of the code
entities.py
--- a/entities.py	Thu Nov 05 14:41:47 2009 -0800
+++ b/entities.py	Fri Nov 06 14:38:33 2009 +0100
@@ -98,31 +98,22 @@
         return []
 
     # IPrevNext interface #####################################################
-
-    def next_entity(self):
+    def _sibling_entry(self, order, operator):
         if self.entry_of:
-            rql = ('Any B ORDERBY B ASC LIMIT 1 '
-                   'WHERE B is BlogEntry, B entry_of BL, BL eid %(blog)s, '
-                   'B eid > %(eid)s')
-            rset = self.req.execute(rql, {'blog': self.entry_of[0].eid, 'eid': self.eid})
+            rql = ('Any B ORDERBY B %s LIMIT 1 '
+                   'WHERE B is BlogEntry, B entry_of BL, BL eid %%(blog)s, '
+                   'B eid %s %%(eid)s')
+            rset = self.req.execute(rql % (order, operator),
+                                    {'blog': self.entry_of[0].eid, 'eid': self.eid})
         else:
-            rql = ('Any B ORDERBY B ASC LIMIT 1 '
-                   'WHERE B is BlogEntry, B eid > %(eid)s')
-            rset = self.req.execute(rql, {'eid': self.eid})
-
+            rql = ('Any B ORDERBY B %s LIMIT 1 '
+                   'WHERE B is BlogEntry, B eid %s %%(eid)s')
+            rset = self.req.execute(rql % (order, operator), {'eid': self.eid})
         if rset:
             return rset.get_entity(0,0)
 
+    def next_entity(self):
+        return self._sibling_entry('ASC', '>')
+
     def previous_entity(self):
-        if self.entry_of:
-            rql = ('Any B ORDERBY B DESC LIMIT 1 '
-                   'WHERE B is BlogEntry, B entry_of BL, BL eid %(blog)s, '
-                   'B eid < %(eid)s')
-            rset = self.req.execute(rql, {'blog': self.entry_of[0].eid, 'eid': self.eid})
-        else:
-            rql = ('Any B ORDERBY B DESC LIMIT 1 '
-                   'WHERE B is BlogEntry, B eid < %(eid)s')
-            rset = self.req.execute(rql, {'eid': self.eid})
-        if rset:
-            return rset.get_entity(0,0)
-
+        return self._sibling_entry('DESC', '<')