Return CSS "class" attributes for <body>.

    Allows to provide a CSS namespace using urlpatterns namespace (as ``.ns-*``) and view name (as ``.vw-*``).

    Usage: ``{% body_class %}``
    Example: ``ns-my-app vw-my-view`` or ``ns-contacts vw-list``
    Requires: ``apps.core.middlewares.CoreMiddleware``.

def body_class_tag(context):
    """
    Return CSS "class" attributes for <body>.

    Allows to provide a CSS namespace using urlpatterns namespace (as ``.ns-*``) and view name (as ``.vw-*``).

    Usage: ``{% body_class %}``
    Example: ``ns-my-app vw-my-view`` or ``ns-contacts vw-list``
    Requires: ``apps.core.middlewares.CoreMiddleware``.
    """
    request = context.get("request")
    if not hasattr(request, "ROUTE"):
        return ""

    css_classes = []

    namespace = request.ROUTE["namespace"]
    if namespace:
        namespace = RE_CLEAN_CSS_NAME.sub("-", namespace.lower())
        css_classes.append("ns-{}".format(namespace))

    view = request.ROUTE["url_name"]  # Use ``url_name`` as ``view_name`` includes the namespace.
    if view:
        view = RE_CLEAN_CSS_NAME.sub("-", view.lower())
        css_classes.append("vw-{}".format(view))

    return " ".join(css_classes)