Skip to content

Commit

Permalink
Implement admin button for refreshing hyperparams
Browse files Browse the repository at this point in the history
  • Loading branch information
olzhasar-reef committed May 4, 2024
1 parent dad9bd2 commit c3db9db
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 1 deletion.
31 changes: 30 additions & 1 deletion app/src/bittensor_panel/core/admin.py
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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"
Expand Down
38 changes: 38 additions & 0 deletions app/src/bittensor_panel/core/tests/test_admin.py
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{% extends "admin/change_list.html" %} {% load i18n %}
{% block object-tools-items %}
{{ block.super }}
<div>
<form action="{% url 'admin:refresh_hyperparams' %}" method="POST">
{% csrf_token %}
<button type="submit" onclick="return confirm('Are you sure want to refresh hyperparams from subtensor?\nExisting data will be overwritten')">Refresh from subtensor</button>
</form>
</div>
{% endblock %}

0 comments on commit c3db9db

Please sign in to comment.