Content features
Table of contents
Introduction
A content feature owns one content type, such as shop items, recipes or polls, with its own model and table. It renders public pages and admin screens for it. It is a full archetype, and this page walks through one, splent_feature_shop.
splent feature:create splent-io/splent_feature_shop
Each content type is its own feature, with its own typed table. Shop items, recipes and polls are three features, never one generic table. A product gets a type by installing the feature, and you compose a site by selecting features.
Writing the model
from splent_framework.db import db
class Item(db.Model):
__tablename__ = "item"
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String(255), nullable=False)
slug = db.Column(db.String(255), nullable=False, unique=True, index=True)
summary = db.Column(db.Text, default="")
description = db.Column(db.Text, default="") # rich text
price = db.Column(db.Numeric(10, 2), nullable=False, default=0)
available_from = db.Column(db.DateTime)
published = db.Column(db.Boolean, default=True)
order = db.Column(db.Integer, default=0)
Give the model the real fields of the domain as columns. A unique slug gives clean public URLs, published hides drafts, and order lets editors sort the list.
Writing the repository and the service
from __future__ import annotations
from splent_io.splent_feature_shop.models import Item
from splent_framework.repositories.BaseRepository import BaseRepository
class ShopRepository(BaseRepository):
def __init__(self):
super().__init__(Item)
def list_published(self) -> list[Item]:
return Item.query.filter_by(published=True).order_by(Item.order.asc()).all()
def get_by_slug(self, slug: str) -> Item | None:
return Item.query.filter_by(slug=slug).first()
Start every repository with
from __future__ import annotations.BaseRepositorydefines a method namedlist(), so inside a subclass an annotation like-> list[Item]would fail when the class is defined.
from splent_io.splent_feature_shop.repositories import ShopRepository
from splent_framework.services.BaseService import BaseService
class ShopService(BaseService):
def __init__(self):
super().__init__(ShopRepository())
def list_published(self):
return self.repository.list_published()
def get_by_slug(self, slug: str):
return self.repository.get_by_slug(slug)
The service is the only layer routes talk to.
Registering the feature
from splent_framework.blueprints.base_blueprint import create_blueprint
from splent_framework.nav.nav_registry import register_nav_item
from splent_framework.services.service_locator import register_service
from splent_io.splent_feature_shop.services import ShopService
shop_bp = create_blueprint(__name__)
def init_feature(app):
register_service(app, "ShopService", ShopService)
register_nav_item(key="shop", label="Shop", href="/shop", order=10)
register_service makes service_proxy("ShopService") resolve, and register_nav_item adds the public menu entry. init_feature is also where register_settings puts editorial settings in the admin panel. See Feature settings.
Serving the public pages
from flask import abort, render_template
from splent_io.splent_feature_shop import shop_bp
from splent_framework.services.service_locator import service_proxy
shop_service = service_proxy("ShopService")
@shop_bp.route("/shop", methods=["GET"])
def index():
return render_template("shop/list.html", items=shop_service.list_published())
@shop_bp.route("/shop/<slug>", methods=["GET"])
def detail(slug):
item = shop_service.get_by_slug(slug)
if item is None:
abort(404)
return render_template("shop/detail.html", item=item)
The usual pair is a list and a detail page.
Extending the theme
Templates live under templates/shop/ and extend public_base.html, the public layout the product’s theme ships. See Theme system.
{% extends "public_base.html" %}
{% block title %}{{ _('Shop') }}{% endblock %}
{% block content %}
<article class="container section">
<h1>{{ item.title }}</h1>
<div class="prose">{{ item.description | rich_text }}</div>
</article>
{% endblock %}
The styling comes from the theme, so the same feature looks native in any product. Stored HTML goes through the rich_text filter, never through | safe, which would run any script stored in it.
Adding admin screens of its own
Editors work in the admin panel. A content feature can ship its own screens under /admin/<type>, behind @login_required, rendering templates that extend base_template.html, the product’s admin shell.
@shop_bp.route("/admin/shop", methods=["GET"])
@login_required
def admin_index():
items = Item.query.order_by(Item.order.asc()).all()
return render_template("shop/admin/list.html", items=items)
Add new, edit and delete the same way. Build the saved data from Item.__table__.columns, so a column a refiner adds is saved by the same route, as collections does.
Link the screens from the admin sidebar through the menu registry.
from splent_framework.nav.menu_registry import register_menu_item
register_menu_item("shop_admin", "Shop", endpoint="shop.admin_index",
icon="shopping-bag", section="admin")
Or describe the model to the admin resource registry and let a panel draw the screens.
Shipping demo content
from splent_framework.seeders.BaseSeeder import BaseSeeder
from splent_io.splent_feature_shop.models import Item
class ShopSeeder(BaseSeeder):
def run(self):
self.seed([
Item(slug="field-guide", title="A Field Guide to Software Lines",
price=24, order=1, summary="Variability, one feature at a time."),
])
A freshly derived product then has something to show.
Checking the contract
splent feature:contract splent_feature_shop --write
Read what it inferred. The generator reads imports and service_proxy calls, not template inheritance, so nothing names the theme feature your templates extend. Put a dependency it cannot see in requires.features_manual. See Contract and dependencies.
Checklist
To build a new content feature, recipes say, copy the shape of shop.
- A typed
Recipemodel with its own table, a uniqueslug,publishedandorder. - A repository on
BaseRepository, starting withfrom __future__ import annotations. - A service on
BaseService, registered ininit_feature. - Public
indexanddetailroutes throughservice_proxy, and admin screens behind@login_required. - Public templates extending
public_base.html, admin ones extendingbase_template.html. - A menu entry for the admin screens.
- A seeder with a few real records.
splent feature:contract --write, then read what it inferred.
See also
- Archetypes, content features are
full - Contract and dependencies, the contract block
- Directory structure, where each file lives