Plain is headed towards 1.0! Subscribe for development updates →

 1from typing import Any
 2
 3from plain.http import Response, ResponseRedirect
 4from plain.urls import Router, include, path
 5
 6from .impersonate.urls import ImpersonateRouter
 7from .views.base import AdminView
 8from .views.registry import registry
 9
10
11class AdminIndexView(AdminView):
12    template_name = "admin/index.html"
13    title = "Dashboard"
14
15    def get(self) -> Response:
16        # Slight hack to redirect to the first view that doesn't
17        # require any url params...
18        if views := registry.get_searchable_views():
19            return ResponseRedirect(list(views)[0].get_view_url())
20
21        return super().get()
22
23
24class AdminSearchView(AdminView):
25    template_name = "admin/search.html"
26    title = "Search"
27
28    def get_template_context(self) -> dict[str, Any]:
29        context = super().get_template_context()
30        context["searchable_views"] = registry.get_searchable_views()
31        context["global_search_query"] = self.request.query_params.get("query", "")
32        return context
33
34
35class AdminRouter(Router):
36    namespace = "admin"
37    urls = [
38        path("search/", AdminSearchView, name="search"),
39        include("impersonate/", ImpersonateRouter),
40        include("", registry.get_urls()),
41        path("", AdminIndexView, name="index"),
42    ]