Ticket #161: rwall_livequery.patch

File rwall_livequery.patch, 3.0 kB (added by rwall, 3 years ago)

A very basic start at using louie (http://louie.berlios.de/) for livequery

  • /home/richard/lib/Divmod/trunk/Axiom/axiom/test/test_livequery.py

    old new  
     1from louie import dispatcher 
     2 
     3from twisted.trial.unittest import TestCase 
     4 
     5from axiom.item import Item 
     6from axiom.store import Store 
     7from axiom.attributes import text 
     8 
     9class MyItem(Item): 
     10    name = text(allowNone = True) 
     11     
     12class TabularDataModel: 
     13    results = [] 
     14    def __init__(self, store, queryItem): 
     15        self.store = store 
     16        self.queryItem = queryItem 
     17         
     18    def updateResults(self): 
     19        self.results = list(self.store.query(self.queryItem)) 
     20     
     21    def onStoreItemAdd(self): 
     22        self.updateResults() 
     23 
     24class LiveQuery(TestCase): 
     25    def testLiveQueryItemAddObserver(self): 
     26         
     27        s = Store() 
     28        tdm = TabularDataModel(s, MyItem) 
     29        dispatcher.connect(tdm.onStoreItemAdd, "itemadd", MyItem) 
     30        def transaction():           
     31            MyItem(store=s, name=u"a") 
     32 
     33        count1 = len(tdm.results) 
     34        s.transact(transaction) 
     35        count2 = len(tdm.results) 
     36 
     37        self.assertEquals(count1, 0) 
     38        self.assertEquals(count2, 1) 
     39        s.close() 
     40 
  • /home/richard/lib/Divmod/trunk/Axiom/axiom/item.py

    old new  
    11# -*- test-case-name: axiom.test -*- 
    22__metaclass__ = type 
     3from louie import dispatcher 
    34 
    45from twisted.python.reflect import qual 
    56from twisted.application.service import IService, IServiceCollection, MultiService 
     
    438439                # TODO: need to measure the performance impact of this, then do 
    439440                # it to make sure things are in fact deleted: 
    440441                # self.store.executeSQL(_schema.APP_VACUUM) 
     442                dispatcher.send("itemdelete", self.__class__) 
    441443            else: 
    442444                assert self.__legacy__ 
    443445 
     
    449451                # transaction; just don't do anything. 
    450452                return 
    451453            self.store.executeSQL(*self._updateSQL()) 
     454            dispatcher.send("itemupdate", self.__class__) 
    452455        else: 
    453456            # case 2: we are in the middle of creating the object, we've never 
    454457            # been inserted into the db before 
     
    459462                [self.storeID] + 
    460463                [self.__dirty__.get(a[1].attrname, (None, a[1].default,))[1] for a in attrs]) 
    461464            self.__everInserted = True 
     465            dispatcher.send("itemadd", self.__class__) 
    462466        # In case 1, we're dirty but we did an update, synchronizing the 
    463467        # database, in case 2, we haven't been created but we issue an insert. 
    464468        # In either case, the code in attributes.py sets the attribute *as well 
jethro@divmod.org