def get(self, request, *args, **kwargs): """Render the home page for Nautobot.""" registry = dict(get_commands_registry()) logs = CommandLog.objects.all().order_by("-start_time") # Summarize the number of times each command/subcommand has been called for command_name, command_data in registry.items(): registry[command_name]["count"] = logs.filter( command=command_name).count() for subcommand_name in command_data["subcommands"]: registry[command_name]["subcommands"][subcommand_name][ "count"] = logs.filter(command=command_name, subcommand=subcommand_name).count() # Support sorting, filtering, customization, and pagination of the table queryset = CommandLogFilter(request.GET, logs).qs table = self.table(queryset, user=request.user) RequestConfig(request, paginate={ "paginator_class": EnhancedPaginator, "per_page": get_paginate_count(request) }).configure(table) return render( request, "nautobot/home.html", { "commands": registry, "table": table, "table_config_form": TableConfigForm(table) }, )
def get(self, request): plugins = [apps.get_app_config(plugin) for plugin in settings.PLUGINS] data = [] for plugin in plugins: data.append({ "name": plugin.verbose_name, "package_name": plugin.name, "author": plugin.author, "author_email": plugin.author_email, "description": plugin.description, "version": plugin.version, "actions": { "home": plugin.home_view_name, "configure": plugin.config_view_name, }, }) table = self.table(data, user=request.user) paginate = { "paginator_class": EnhancedPaginator, "per_page": get_paginate_count(request), } RequestConfig(request, paginate).configure(table) return render( request, "extras/plugins_list.html", { "table": table, "table_config_form": TableConfigForm(table=table), "filter_form": None, }, )
def post(self, request): """Update the user's table configuration. Copied from Nautobot utilities.views.ObjectListView. """ logs = CommandLog.objects.all().order_by("-start_time") table = self.table(CommandLogFilter(request.GET, logs).qs) form = TableConfigForm(table=table, data=request.POST) preference_name = f"tables.{self.table.__name__}.columns" if form.is_valid(): if "set" in request.POST: request.user.config.set(preference_name, form.cleaned_data["columns"], commit=True) elif "clear" in request.POST: request.user.config.clear(preference_name, commit=True) messages.success(request, "Your preferences have been updated.") return redirect(request.get_full_path())
def table_config_form(table, table_name=None): return { "table_name": table_name or table.__class__.__name__, "table_config_form": TableConfigForm(table=table), }