diff --git a/app/src/bittensor_panel/core/admin.py b/app/src/bittensor_panel/core/admin.py index 9ea2d7f..b2bf429 100644 --- a/app/src/bittensor_panel/core/admin.py +++ b/app/src/bittensor_panel/core/admin.py @@ -1,8 +1,17 @@ from django.contrib import admin, messages from django.contrib.admin import register +from django.http.response import ( + HttpResponseRedirect, +) +from django.urls import path, reverse from .models import HyperParameter -from .services import HyperParameterUpdateFailed, update_hyperparam +from .services import ( + HyperParameterRefreshFailed, + HyperParameterUpdateFailed, + refresh_hyperparams, + update_hyperparam, +) @register(HyperParameter) @@ -21,6 +30,26 @@ def save_model(self, request, obj, form, change): except HyperParameterUpdateFailed as e: messages.error(request, str(e)) + def get_urls(self): + urls = [ + path( + "refresh-hyperparams/", + self.admin_site.admin_view(self.refresh_hyperparams_view), + name="refresh_hyperparams", + ), + ] + urls += super().get_urls() + return urls + + def refresh_hyperparams_view(self, request): + if request.method == "POST": + try: + refresh_hyperparams() + except HyperParameterRefreshFailed as e: + messages.error(request, str(e)) + + return HttpResponseRedirect(reverse("admin:core_hyperparameter_changelist")) + admin.site.site_header = "Bittensor Administration Panel" admin.site.site_title = "Bittensor Administration Panel" diff --git a/app/src/bittensor_panel/core/tests/test_admin.py b/app/src/bittensor_panel/core/tests/test_admin.py new file mode 100644 index 0000000..9f355b0 --- /dev/null +++ b/app/src/bittensor_panel/core/tests/test_admin.py @@ -0,0 +1,38 @@ +from unittest.mock import MagicMock + +import pytest +from django.contrib.messages import get_messages +from django.test import Client +from pytest_mock import MockerFixture + +from bittensor_panel.core.services import HyperParameterRefreshFailed + + +@pytest.fixture +def mock_refresh_hyperparams(mocker: MockerFixture): + return mocker.patch("bittensor_panel.core.admin.refresh_hyperparams") + + +def test_refresh_hyperparams_view( + admin_client: Client, mock_refresh_hyperparams: MagicMock +): + response = admin_client.post("/admin/core/hyperparameter/refresh-hyperparams/") + + mock_refresh_hyperparams.assert_called_once() + + assert response.status_code == 302 + assert response.url == "/admin/core/hyperparameter/" + + +def test_refresh_hyperparams_view_exception( + admin_client: Client, mock_refresh_hyperparams: MagicMock +): + mock_refresh_hyperparams.side_effect = HyperParameterRefreshFailed("error") + + response = admin_client.post("/admin/core/hyperparameter/refresh-hyperparams/") + + assert response.status_code == 302 + assert response.url == "/admin/core/hyperparameter/" + + messages = list(get_messages(response.wsgi_request)) + assert len(messages) == 1 diff --git a/app/src/bittensor_panel/templates/admin/core/hyperparameter/change_list.html b/app/src/bittensor_panel/templates/admin/core/hyperparameter/change_list.html new file mode 100644 index 0000000..cf7c3ab --- /dev/null +++ b/app/src/bittensor_panel/templates/admin/core/hyperparameter/change_list.html @@ -0,0 +1,10 @@ +{% extends "admin/change_list.html" %} {% load i18n %} +{% block object-tools-items %} + {{ block.super }} +