[Grok-dev] Re: SQLAlchemy integration

Santiago Videla santiago.videla at gmail.com
Wed May 21 01:37:20 EDT 2008


Hey,

Cool! Could you share a bit of code on the list?


Sure. but remember that I'm a newbie

I just try to follow what you did on your post + the doctest on
zope.sqlalchemy + the rdbexample with collective.lead from the branch

this code depends on zope.sqlalchemy, SQLAlchemy>=0.4.6, psycopg2 and a
PstgreSQL database up and running of course (make sure to override the _URL
string).

*** it was a long day for me, please let me know if this doesn't work. It's
possible that I missed something

[app.py]

import grok

from sqlalchemy import *
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import scoped_session, sessionmaker, relation
from zope.sqlalchemy import ZopeTransactionExtension, invalidate
import transaction

from models import *

_URL = 'postgres://grok:grok@localhost/grok'

engine = create_engine(_URL, convert_unicode=True)
Base.metadata.create_all(engine)

Session = scoped_session(sessionmaker(bind=engine,
                                      twophase=True,
                                      transactional=True,
                                      autoflush=True,
                                      extension=ZopeTransactionExtension()))

class RDBExample(grok.Application, grok.Model):
    def traverse(self, name):
        return Session().query(User).filter(User.name == name).first()

class Index(grok.View):
    grok.context(RDBExample)
    def render(self):
        result = "Users on grok data base \n"
        for user in Session().query(User).all():
            result += 'User: %s \n' % user.name
        return result

class UserView(grok.View):
    grok.name('index')
    grok.context(User)

    def render(self):
        return "You are over user: %s \n" % self.context.name

class AddUser(grok.AddForm):
    grok.context(RDBExample)

    @property
    def form_fields(self):
        return grok.Fields(IUser)

    @grok.action('add')
    def handle_add(self, *args, **kw):
        user = User(**kw)
        Session().save(user)


[models.py]

import grok
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import *
from sqlalchemy.orm import relation

from interfaces import IUser, IAddress

Base = declarative_base()

class User(Base,grok.Model):

    grok.implements(IUser)

    __tablename__ = 'test_users'
    id = Column(Integer, primary_key=True)
    name = Column(String(50))
    addresses = relation(backref="user")

class Address(Base, grok.Model):

    grok.implements(IAddress)

    __tablename__ = 'test_addresses'
    id = Column(Integer, primary_key=True)
    email = Column(String(50))
    user_id = Column(Integer, ForeignKey('test_users.id'))


[interfaces.py]

from zope import schema
from zope.interface import Interface

class IUser(Interface):
    """
    """
    name = schema.TextLine(title=u"Name")

class IAddress(Interface):
    """
    """

I noticed that in the rdbexample branch that I was to following, there is a
method on megrok.rdb that translate the model table to a schema to make the
addForm. I used the interfaces instead, because when I try to make a form of
a sub-class (i.e: """class SpecialUser(User)""" )
the method just shows the columns on the SpecialUser's table, but doesn't
show anything from the User's table.

Is this a good practice? or should I modify the method on megrok.rdb???


Regards


>
>
> Regards,
>
> Martijn
>
>
> _______________________________________________
> Grok-dev mailing list
> Grok-dev at zope.org
> http://mail.zope.org/mailman/listinfo/grok-dev
>



-- 
Santiago Videla
www.revolucionesweb.com.ar
http://www.linkedin.com/in/svidela

Sigue la mata dando de que hablar siempre abajo y a la izquierda donde el
pensamiento que se hace corazón resplandece con la palabra sencilla y
humilde que l at s tod at s somos.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.zope.org/pipermail/grok-dev/attachments/20080521/e613f25b/attachment-0001.html


More information about the Grok-dev mailing list