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
All routes without parameter
All traversable endpoints that implement
websauna.system.core.interfaces.IContainer
protocolGET accessible views
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.
-
enumerate_available_views
(route, context)[source]¶ Get list of available views for a given resource.
- Return type
Iterable
[Introspectable
]
-
get_traverse_endpoint_context
(router, route)[source]¶ Get root object for a traversable route.
E.g. resolve /container/* to a SampleContainer context.
- Return type
-
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_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_public_get_view
(view_data)[source]¶ Check if view can be publicly accessed
- Parameters
view_data (
dict
) – Introspected view data as dict
-
is_traversable_sitemap_route
(route)[source]¶ Is this route such that it 1) is traversable 2) can be added to sitemap
-
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.
-
class
websauna.system.core.sitemap.
Sitemap
[source]¶ Bases:
object
Sitemap helper. See 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.
-
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.
-
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 orcondition
. 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()