Site Viewsets

Site and Application two viewsets helps to create website user interface and navigation.

A Site could contains several applications, and each application could contains several viewsets.

from viewflow.urls import Site, Application

site = Site(title="Workforce management", items=[
    Application(
        title='Employees',
        icon=Icon('people'),
        app_name='emp',
        items=[
            EmployeeViewset(),
            DepartmentViewset(),
    ),
])

urlpatterns = [
    path('', site.urls),
]

Current site and app variables are available in the template context as {{ request.resolver_match.site }} and {{ request.resolver_match.app }} Root viewflow/base.html template aliases it as {{ site }} and {{ app }} for later usage.

It’s common to have all application views templates extended from shared base template. Viewflow build-in templates extends `{[ app.base_template_name }}` that by default points to `viewflow/base_page.html`

See also

Built-in Templates override instructions

Site and Application automatically builds user navigation menu from included viewsets. A viewset need to be included into application menu, should be mixed with viewflow.urls.AppMenuMixin

from viewflow import Icon
from viewflow.urls import AppMenuMixin, Viewset

class EmployeeViewset(AppMenuMixin, Viewset):
    title = _('Employee')
    icon = Icon('people')

If you need to create a custom menu, override .menu_template_name attribute.

class EmpApplication(Application):
    base_template_name = 'employees/base_page.html'
    menu_template_name = 'employees/menu.html'
    app_name = 'emp'
    items = [
        EmployeeViewset(),
        DepartmentViewset(),
    ]

And in ‘employees/menu.html’:

{% load viewflow_site %}
<vf-page-menu-navigation class="mdc-list">
<div class="mdc-list-group">
    <h3 class="vf-page__menu-subheader mdc-list-group__subheader">Employees</h3>
    <a class="mdc-list-item vf-page__menu-list-item"
       href="{% url 'emp:employee:index' %}">
    <i class="material-icons">account_box</i>Employees
    </a>
    <a class="mdc-list-item vf-page__menu-list-item"
       href="{% url 'emp:department:index' %}">
        <i class="material-icons">people</i>Departments
    </a>
</div>
<div class="mdc-list-divider"></div>
</vf-page-menu-navigation>

To restrict access to site or application override .has_perm method

def has_perm(request, user):
    return user.is_staff

Branding

To quickly change site colors, you can set primary_color and secondary_color attributes

site = Site(
    title="Workforce management",
    primary_color='#3949ab',
    secondary_color='#5c6bc0',
    items=[
        EmployeeViewset(),
        DepartmentViewset(),
    ]
)