websauna.system.core.sitemap module

Sitemap generation helpers.

class websauna.system.core.sitemap.ReflectiveSitemapBuilder(request)[source]

Bases: object

Scan all registered routes and traversable resources and build sitemap from them automatically.

This will read route configuration and build sitemap for

This method might not yet work for more advanced view configuration use cases. Check websauna.tests.sitemapsamples for covered use cases.

See Sitemap for examples.

add_route_item(name, pattern, view_data)[source]

Add one route item to the table.

Override for custom sitemap parameters (changefreq, etc.).

add_traverse_item(context, view_name)[source]

Add one traverse item to the table.

Override for custom sitemap parameters (changefreq, etc.).

build()[source]

Iterate through all public routes and traversable items and add them to the sitemap.

build_routes()[source]

Build all routes without parameters and permissions.

build_traverse_trees()[source]

Build all traversed hierarchies.

enumerate_available_views(route, context)[source]

Get list of available views for a given resource.

Return type

Iterable[Introspectable]

get_mapper()[source]
get_sitemap()[source]

Get ready sitemap after build.

Return type

Sitemap

get_traverse_endpoint_context(router, route)[source]

Get root object for a traversable route.

E.g. resolve /container/* to a SampleContainer context.

Return type

Resource

has_public_view_acl(context)[source]

Check if ACL for the resource is publicly viewable.

View permission must be either missing or pyramid.security.Everyone.

is_anonymous(view_data)[source]
is_get_requestable(view_data)[source]
is_good_route_item(name, pattern, view_data)[source]

Check conditions if routed view can be added in the sitemap.

is_included(view_data, context, request)[source]

Check if sitemap conditions allow to include this item.

is_parameter_free_route(route_spec)[source]
is_public_get_view(view_data)[source]

Check if view can be publicly accessed

Parameters

view_data (dict) – Introspected view data as dict

is_static(view_data)[source]
is_traversable_sitemap_route(route)[source]

Is this route such that it 1) is traversable 2) can be added to sitemap

recurse_traversable(router, route, context)[source]

Walk through traversable hierarchy.

For each context iterate available views and add to sitemap.

classmethod render(context, request)[source]

Render the sitemap.

TODO: I don’t like that Sitemap.render is little bit different from this one.

Returns

dict of information for the templates {urlset: SitemapItem iterator}

class websauna.system.core.sitemap.RouteItem(route_name, changefreq=None, priority=None, lastmod=None, **kwargs)[source]

Bases: websauna.system.core.sitemap.SitemapItem

Add a static Pyramid URL dispatched route to the sitemap.

location(request)[source]

Resolve the full URL of the this item in the sitemap.

Returns

URL as a string

class websauna.system.core.sitemap.Sitemap[source]

Bases: object

Sitemap helper. See Sitemap.

add_generator(generator)[source]
Parameters

generator – Yields SitemapItem instances

add_item(item)[source]
render(context, request)[source]

Render the sitemap.

Returns

dict of information for the templates {urlset: SitemapItem iterator}

urls()[source]

Return an iterable which goes through all SitemapItem objects in this Sitemap.

class websauna.system.core.sitemap.SitemapItem(changefreq=None, priority=None, lastmod=None)[source]

Bases: abc.ABC

Present an item apperearing in the sitemap.

Pass information about the sitemap entry for the template.

changefreq(request)[source]

Return sitemap changefreq string or None if tag not present.

lastmod(request)[source]

Return sitemap lastmod string or None if tag not present.

abstract location(request)[source]

Resolve the full URL of the this item in the sitemap.

Returns

URL as a string

priority(request)[source]

Return sitemap priority string or None if priority tag string not present.

class websauna.system.core.sitemap.TraverseItem(context, view_name, changefreq=None, priority=None, lastmod=None, **kwargs)[source]

Bases: websauna.system.core.sitemap.SitemapItem

Add a travered resource view to the sitemap.

location(request)[source]

Resolve the full URL of the this item in the sitemap.

Returns

URL as a string

websauna.system.core.sitemap.include_in_sitemap(include=None, condition=None)[source]

A function decorator to determine if a view should be included in the automatically generated sitemap.

You need to give either include argument or condition. If this view decorator is not present the view is always included.

Example of hardcoded condition for an URL dispatch view:

from websauna.system.core.sitemap import include_in_sitemap
from websauna.system.core.route import simple_route


@simple_route("/skipped_route", route_name="skipped_route")
@include_in_sitemap(False)
def skipped_route(request: Request):
    return Response()

Example of dynamic condition for a traverse view:

from websauna.system.core.sitemap import include_in_sitemap
from pyramid.view import view_config


def skipped_condition(context, request):
    return False


@view_config(context=SampleResource, name="skipped_conditional", route_name="sitemap_test")
@include_in_sitemap(condition=skipped_condition)
def skipped_conditional(sample_resource: SampleResource, request: Request):
    return Response()

More complex example where you dynamically look up from the database if the content exist and then have the item in the sitemap:

# We reuse this helper function in sitemap condition and actual view
def get_insight_content(asset_resource):
    asset = asset_resource.asset
    content = asset.asset_content.filter_by(content_type=AssetContentType.insight).one_or_none()
    return content


@view_config(context=AssetDescription, route_name="network", name="insight", renderer="network/asset_insight.html")
@include_in_sitemap(lambda c, r: get_insight_content(c) is not None)
def insight(asset_resource: AssetDescription, request: Request):
    breadcrumbs = get_breadcrumbs(asset_resource, request, current_view_name="Asset insight", current_view_url=request.resource_url(asset_resource, "insight"))
    content = get_insight_content(asset_resource)

    if content:
        body = markdown.markdown(content.text)
    else:
        body = "This research has not yet been published"

    return locals()
Parameters
  • include (Optional[bool]) – Either True or False

  • condition (Optional[Callable]) – callback function (context, request) to called to every item. Traversable views get both route and request, URL dispatch views get only request. Return true if the item should be included in the sitemap.