A possible approach with Nevow and Storm/twisted-integration

(thanks to these very nice guys in mailing list)

Set up pool

from storm.databases.sqlite import SQLite
from storm.uri import URI
from storm.twisted.store import StorePool

database = SQLite(URI('sqlite:///test.db'))
pool = StorePool(database, 5, 10)

pool.start()

Create models

from storm.locals import *
from storm.twisted.wrapper import DeferredReference

class People(object):
    __storm_table__ = 'people'

    id                      =       Int(primary=True)
    email           =       Unicode()
    nickname        =       Unicode()
    passwd          =       Unicode()
    avatar          =       Unicode()


class Topic(object):
    __storm_table__ = 'topic'

    id                      =       Int(primary=True)
    topic_title     =       Unicode()
    keyword         =       Unicode()
    content         =       Unicode()
    people_id       =       Int()
    people          =       DeferredReference(people_id, People.id)


    posted          =       DateTime()

and have fun

from twisted.internet.defer import inlineCallbacks, returnValue

from db import pool
from db.models import Topic


    def getTopics(self):
            @inlineCallbacks
            def transaction(store):
                    dr = yield store.find(Topic, Topic.topic_title==self.topic, Topic.keyword==self.keyword)

                    dr.order_by(Desc(Topic.posted))
                    items = yield dr.all()

                    for item in items:
                            #do whatever.
                            people = yield item.people
                            #do whatever else.

                    #this can recycle stores in pool
                    yield store.commit()

                    returnValue(   ) #return whatever you want.

            return pool.transact(transaction)

Project Versions

Table Of Contents

Previous topic

Tutorial: Using Storm with Nevow

Next topic

Divmod Athena

This Page