Configuration¶
Custom model for tree pages¶
To build a tree, using the model from sqlalchemy_mptt.
Note
Otherwise, it will look like flat pages.
Plans to add a recursive model with only parent_id field.
Create model of tree pages. For more detail see example pyramid_pages_example.
from pyramid_pages.models import FlatPageMixin, MpttPageMixin, RedirectMixin
...
class WebPage(Base, MpttPageMixin, RedirectMixin):
__tablename__ = 'mptt_pages'
id = Column('id', Integer, primary_key=True)
class NewsPage(Base, FlatPageMixin):
__tablename__ = 'flat_news'
id = Column('id', Integer, primary_key=True)
date = Column(Date, default=func.now())
If you use pyramid_sacrud, you
can inherited from BaseSacrudMpttPage
or
BaseSacrudFlatPage
or just use
SacrudOptions
.
It’s look likes this:
Configure pyramid_pages¶
Then add settings of pyramid_pages
.
from youproject.models import WebPage, NewsPage
...
settings['pyramid_pages.models'] = {
'': WebPage,
'pages': WebPage, # available with prefix '/pages/'
'news': NewsPage
}
# pyramid_pages - put it after all routes
# and after pyramid_pages configuration.
config.include("pyramid_pages")
If you use version of pyramid >= 1.6a1, there is a possibility put
config.include("pyramid_pages")
before pyramid_pages Configuration.
from youproject.models import WebPage, NewsPage
...
config.include("pyramid_pages")
settings['pyramid_pages.models'] = {
'': WebPage,
'pages': WebPage, # available with prefix '/pages/'
'news': NewsPage
}
Custom resource¶
Base resource for pages can be found in the module pyramid_pages.routes
.
Just inherit your resource from PageResource
.
1 2 3 | class GalleryResource(BasePageResource):
model = Gallery
template = 'gallery/index.jinja2'
|
1 2 3 4 5 6 7 8 | class Gallery(BasePage, MpttPageMixin):
__tablename__ = 'mptt_gallery'
id = Column(Integer, ForeignKey('base_pages.id'), primary_key=True)
__mapper_args__ = {
'polymorphic_identity': 'gallery_page',
}
|
1 2 3 4 5 6 7 | class Photo(Base):
__tablename__ = 'photos'
id = Column('id', Integer, primary_key=True)
path = Column('path', Text)
gallery_id = Column(Integer, ForeignKey('mptt_gallery.id'))
gallery = relationship('Gallery', backref='photos')
|
And add it to config.
settings['pyramid_pages.models'] = {
'': WebPage,
'pages': WebPage, # available with prefix '/pages/'
'news': NewsPage,
'gallery': GalleryResource
}