Current version: 0.1.0

Welcome to django-simple-page#

TODO


Api reference#

Models#

class simple_page.models.Page(*args, **kwargs)#

Bases: mptt.models.MPTTModel

This is the base model for all pages. The only thing a subclass has to do is to setup the regions it wants to use. Since the database layout of the base class is fully functional, it is totally sufficient to define your page model as a proxy. Still you are free to use a concrete child model with all the additional fields and logic you whish for.

The sections of a page are available as a region specific queryset using the region’s name as an object attribute.

The base model takes care of the following things: - As a mptt model it allows to arrange pages in treelike structures. - It has a region specific many-to-many relationship to the Section model. - It has a slug field which can be used in your url path. - It has a ContentType relation that points to the Page’s child class. - It provides some logic to handle the regions a subclass sets.

REGIONS = []#

REGIONS must be set by a subclass as a list of tuples holding the region’s name and its title. Something like:

REGIONS = [
    ('main', 'Main Region'),
    ('sidebar', 'Sidebar'),
    ('footer', 'Footer'),
]
classmethod get_regions()#

Return the regions for this page. This method can be customized by child classes to return different regions.

resolve_obj()#

Return the instance of the child class.

class simple_page.models.Section(*args, **kwargs)#

Bases: django.db.models.base.Model

This model is the base model for what ever content you want to see on your website. It does not has any fields on its own but provides a many-to-many relationship to the Page model.

ModelAdmin#

class simple_page.admin.BasePageAdmin(model, admin_site)#

Bases: simple_page.admin.SetPageTypeMixin, simple_page.admin.RenderPageRegionsMixin, django.contrib.admin.options.ModelAdmin

Use this base class for your own concrete page modeladmin. It will take care of rendering an inline formset for each region. And set the appropriate value for the page_type field.

Renderer#

simple_page.renderer.register(renderer_cls, model_cls=None, context=None)#

Register an Renderer class for a page or section model. This function can also be used as a decorator for your model class:

@renderer.register(FancyPageRenderer)
class FancyPage(Page):
    ...

For Section classes it is possible to define a context in which a renderer should be used. A context can be a page type, a region name or a tuple of page typen and region name. You can register multiple renderer classes with different contexts for a section:

@renderer.register(MainSectionRenderer, context='main')
@renderer.register(FancySectionRenderer, context=(FancyPage, 'main'))
class FancySection(Section):
    ...

In this example the FancySectionRenderer will be used when the section appears on a FancyPage in a ‘main’ region. The MainSectionRenderer will be used for all ‘main’ regions on all other pages. And in all other contexts SectionRenderer will be used. See also get_section_renderer().

Parameters
  • renderer_cls (Renderer) – the renderer class to be registered

  • model_cls (Page or Section) – the model for which the renderer should be used

  • context (Page or str or tuple of both) – a context in which a section should be rendered with the renderer class

simple_page.renderer.get_page_renderer(page)#

Return the registered renderer for the page or PageRenderer.

Parameters

page (Page) – page instance to be rendered

Returns

renderer class

Return type

PageRenderer

simple_page.renderer.get_section_renderer(section, page=None, region=None)#

Return a renderer instance for the section.

We look for a registered renderer in this order:

  • page-type and region specific

  • region specific

  • page-type specific

  • neither page-type nor region specific

The first one found will be returned. Otherwise the SectionRenderer is used as fallback.

Parameters
  • obj (Section) – section instance

  • page (Page) – page the section will be rendered for

  • region (str) – region the section will be rendered in

Returns

renderer class

Return type

SectionRenderer

class simple_page.renderer.Renderer(obj, request=None, **kwargs)#

Bases: object

Base renderer class. This class provides the basic functionality to render a Page or Section instance. It uses the proven triad of get_template_name, get_context and render methods. But can be customized to any extend. All a child class has to provide is a render method returning valid HTML.

get_template_name()#

Return the name of the template. If template_name is set it will be returned. Otherwise the template name will be constructed as follows:

  • Using ‘pages’ or ‘sections’ as folder - depending on the object’s type.

  • And converting the object’s class name to snake case with a html suffix as file name.

For example the template name for a MyTextSection class would be ‘sections/my_text_section.html’.

get_context()#

Return the context to use when rendering the template. By default the context will contain the object being rendered as ‘page’ or ‘section’ - depending on the object’s type.

render()#

Return the rendered HTML using the template and context returned by get_template_name() and get_context() methods.

class simple_page.renderer.SectionRenderer(obj, request=None, **kwargs)#

Bases: simple_page.renderer.Renderer

Renderer for Section instances.

Parameters
  • obj (Section) – section object

  • request (HttpRequest) – request object (default: None)

  • kwargs – Additional data about the rendering context as keyword arguments

class simple_page.renderer.PageRenderer(obj, request=None, **kwargs)#

Bases: simple_page.renderer.Renderer

Renderer for Page instances.

Parameters
  • obj (Page) – page object

  • request (HttpRequest) – request object (default: None)

  • kwargs – Additional data about the rendering context as keyword arguments

render_section(section, region)#

Return a section rendered as html.

get_section_data(section, region)#

Return a dictonary holding the section as object and as rendered html using ‘obj’ and ‘html’ as keys.

get_region_data(name, title)#

Return a dictonary with the name, the title and the sections of a region. The sections will be a dictonary of the section object and its rendered html. See get_section_data().

get_assets(region)#

Return an Assets instance which holding all the assets from the sections of a given region.

get_context()#

Add regions and media assets to the context.

Regions will be added as template variables on their own and as a list named ‘regions’. Each region is a dictonary of its name, title and its sections. See get_region_data().

Media assets will be the merged assets of the page and all its sections. See get_assets().

Media Assets#

simple_page.assets.register(assets_cls, model_cls=None, context=None)#

Register an Assets class for a page or section model. This function can also be used as a decorator for the model class:

@assets.register(FancySectionAssets):
class FancySection(Section):
    ...

With section models assets could be registered context specific. This works exactly the same way as it does for the renderer register() function.

Parameters
  • assets_cls (Assets) – the assets class to be registered

  • model_cls (Page or Section) – the model for which the renderer should be used

  • context (Page or str or tuple of both) – a context in which a section should be rendered with the renderer class

simple_page.assets.get_page_assets(page)#

Return an assets instance for the page. Check for a registered assets class. Use Assets as a fallback.

Parameters

page (Page) – page instance to be rendered

Returns

assets class

Return type

Assets

simple_page.assets.get_section_assets(section, page=None, region=None)#

Return a assets instance for the section.

We look for a registered assets in this order:

  • page-type and region specific

  • region specific

  • page-type specific

  • neither page-type nor region specific

The first one found will be returned. Otherwise the Assets is used as fallback.

Parameters
  • obj (Section) – section instance

  • page (Page) – page the section will be rendered for

  • region (str) – region the section will be rendered in

Returns

assets class

Return type

Assets

class simple_page.assets.Assets#

Bases: django.forms.widgets.Media

Base class for css and javascript media definitions. It works exactly as the Media class for django forms: Simply set the css and js class attributes and register your Assets for your pages or sections.


Indices and tables#