I've tried to pull out as much cruft and redesign to get the bare
minimum model to make it easier to use the collection_class.

What I'm trying to do:

page.template.blocks[0].elements[0]

as

page.template['content'].elements[0]

Right now, I get:

page.template.blocks[0].name = 'content'
page.template.blocks[1].name = 'menu'

so my template needs to iterate through to figure out whether I need
to walk page.template.blocks[0] or page.template.blocks[1] whereas I
would rather do:

for elements in template.blocks['content']:

Thanks.

controller/cms.py:

   def index(self, id='index'):
       tmpl_context.page =
meta.Session.query(Page).filter_by(slug=id).one()
       return render(tmpl_context.page.template.name)

model/cms.py:

from sqlalchemy import *
from sqlalchemy.orm import mapper, relation, backref
from sqlalchemy import Table, ForeignKey, Column
from sqlalchemy.types import Integer, Unicode
from sqlalchemy.dialects import mysql
from sqlalchemy.orm.collections import column_mapped_collection

from pesi.model.meta import Base

Template_block = Table('m_template_block', Base.metadata,
    Column('template_id', Integer, ForeignKey('m_template.id')),
    Column('block_id', Integer, ForeignKey('m_block.id'))
)

Block_element = Table('m_block_element', Base.metadata,
    Column('block_id', Integer, ForeignKey('m_block.id')),
    Column('element_id', Integer, ForeignKey('m_element.id'))
)

class Block(Base):
   __tablename__ = 'm_block'

   id = Column(mysql.BIGINT(20, unsigned=True), primary_key=True,
autoincrement=True)
   name = Column(Unicode(80))

   elements = relation('Element', secondary=Block_element)

   def __repr__(self):
       return '<Block: id: %d name: %s>' % (self.id, self.name)

class Element(Base):
   __tablename__ = 'm_element'

   id = Column(mysql.BIGINT(20, unsigned=True), primary_key=True,
autoincrement=True)
   function = Column(Unicode(80))

class Template(Base):
   __tablename__ = 'm_template'

   id = Column(mysql.BIGINT(20, unsigned=True), primary_key=True,
autoincrement=True)
   name = Column(Unicode(80))

   blocks = relation(Block, secondary=Template_block)

class Page(Base):
   __tablename__ = 'm_page'

   id = Column(mysql.BIGINT(20, unsigned=True), primary_key=True,
autoincrement=True)
   template_id = Column(mysql.BIGINT(20, unsigned=True),
ForeignKey(Template.id))
   slug = Column(Unicode(80))

   template = relation(Template, uselist=False)

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to sqlalch...@googlegroups.com.
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.

Reply via email to