Skip to content

Commit ad4cd6f

Browse files
Ken LippoldKen Lippold
authored andcommitted
Added admin actions for loading test data
1 parent 2481b93 commit ad4cd6f

File tree

3 files changed

+103
-4
lines changed

3 files changed

+103
-4
lines changed

hydroserver/urls.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22
from django.conf.urls.static import static
33
from django.contrib import admin
44
from django.urls import path, include
5-
from hydroserver.views import index
5+
from hydroserver.views import index, load_test_data, load_default_data
66

77
urlpatterns = [
8+
path("admin/actions/load-test-data/", load_test_data, name="load_test_data"),
9+
path("admin/actions/load-default-data/", load_default_data, name="load_default_data"),
810
path("admin/", admin.site.urls),
911
path("accounts/", include("allauth.urls")),
1012
path("api/auth/", include("iam.urls")),

hydroserver/views.py

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,42 @@
1-
from django.shortcuts import render
1+
from django.shortcuts import render, redirect
22
from django.views.decorators.csrf import csrf_exempt
3+
from django.core.management import call_command
4+
from django.contrib import messages
5+
from django.contrib.admin.views.decorators import staff_member_required
36
from django.conf import settings
47

58

69
@csrf_exempt
710
def index(request):
811

912
context = {
10-
'proxy_base_url': settings.PROXY_BASE_URL
13+
"proxy_base_url": settings.PROXY_BASE_URL
1114
}
1215

13-
return render(request, 'index.html', context)
16+
return render(request, "index.html", context)
17+
18+
19+
@staff_member_required
20+
def load_test_data(request):
21+
if request.method == "POST":
22+
if settings.DEBUG is False:
23+
messages.warning(request, "Test data is only available on development deployments.")
24+
else:
25+
try:
26+
call_command("load_iam_test_data")
27+
call_command("load_sta_test_data")
28+
messages.success(request, "Test data loaded successfully.")
29+
except Exception as e:
30+
messages.error(request, f"Error loading test data: {e}")
31+
return redirect('admin:index')
32+
33+
34+
@staff_member_required
35+
def load_default_data(request):
36+
if request.method == "POST":
37+
try:
38+
# call_command("load_default_data")
39+
messages.success(request, "Default data loaded successfully.")
40+
except Exception as e:
41+
messages.error(request, f"Error loading default data: {e}")
42+
return redirect('admin:index')

templates/admin/index.html

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
{% extends "admin/base_site.html" %}
2+
{% load i18n static %}
3+
4+
{% block extrastyle %}{{ block.super }}<link rel="stylesheet" href="{% static "admin/css/dashboard.css" %}">{% endblock %}
5+
6+
{% block coltype %}colMS{% endblock %}
7+
8+
{% block bodyclass %}{{ block.super }} dashboard{% endblock %}
9+
10+
{% block nav-breadcrumbs %}{% endblock %}
11+
12+
{% block nav-sidebar %}{% endblock %}
13+
14+
{% block content %}
15+
<div id="content-main">
16+
{% include "admin/app_list.html" with app_list=app_list show_changelinks=True %}
17+
</div>
18+
{% endblock %}
19+
20+
{% block sidebar %}
21+
<div id="content-related">
22+
<div class="module" id="admin-actions-module">
23+
<h2>{% translate 'Admin actions' %}</h2>
24+
<div class="module-content">
25+
<form method="post" action="{% url 'load_test_data' %}">
26+
{% csrf_token %}
27+
<button type="submit" class="button" style="padding: 10px 20px; margin-left: 15px;">
28+
{% trans "Load Test Data" %}
29+
</button>
30+
</form>
31+
<form method="post" action="{% url 'load_default_data' %}">
32+
{% csrf_token %}
33+
<button type="submit" class="button" style="padding: 10px 20px; margin-left: 15px;">
34+
{% trans "Load Default Data" %}
35+
</button>
36+
</form>
37+
</div>
38+
</div>
39+
<div class="module" id="recent-actions-module">
40+
<h2>{% translate 'Recent actions' %}</h2>
41+
<h3>{% translate 'My actions' %}</h3>
42+
{% load log %}
43+
{% get_admin_log 10 as admin_log for_user user %}
44+
{% if not admin_log %}
45+
<p>{% translate 'None available' %}</p>
46+
{% else %}
47+
<ul class="actionlist">
48+
{% for entry in admin_log %}
49+
<li class="{% if entry.is_addition %}addlink{% endif %}{% if entry.is_change %}changelink{% endif %}{% if entry.is_deletion %}deletelink{% endif %}">
50+
<span class="visually-hidden">{% if entry.is_addition %}{% translate 'Added:' %}{% elif entry.is_change %}{% translate 'Changed:' %}{% elif entry.is_deletion %}{% translate 'Deleted:' %}{% endif %}</span>
51+
{% if entry.is_deletion or not entry.get_admin_url %}
52+
{{ entry.object_repr }}
53+
{% else %}
54+
<a href="{{ entry.get_admin_url }}">{{ entry.object_repr }}</a>
55+
{% endif %}
56+
<br>
57+
{% if entry.content_type %}
58+
<span class="mini quiet">{% filter capfirst %}{{ entry.content_type.name }}{% endfilter %}</span>
59+
{% else %}
60+
<span class="mini quiet">{% translate 'Unknown content' %}</span>
61+
{% endif %}
62+
</li>
63+
{% endfor %}
64+
</ul>
65+
{% endif %}
66+
</div>
67+
</div>
68+
{% endblock %}

0 commit comments

Comments
 (0)