Skip to content

Commit 67dbb1b

Browse files
committed
add custom metrics to admin view
1 parent 8c96b42 commit 67dbb1b

File tree

9 files changed

+166
-3
lines changed

9 files changed

+166
-3
lines changed

api/config/admin.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
from django.contrib import admin
2+
from django.template.response import TemplateResponse
3+
from django.urls import path
4+
5+
from skills_matcher_db.core.views import AdminMetricsView
6+
7+
8+
class CustomAdminSite(admin.AdminSite):
9+
index_template = "admin/custom_index.html"
10+
11+
def get_urls(
12+
self,
13+
):
14+
return [
15+
path(
16+
"metrics/",
17+
AdminMetricsView.as_view(),
18+
name="view_metrics",
19+
),
20+
] + super().get_urls()

api/config/urls.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,16 @@
88

99
from rest_framework.authtoken.views import obtain_auth_token
1010

11+
from config.admin import CustomAdminSite
1112
from skills_matcher_db.users.api import jwt_views
1213
from skills_matcher_db.users.api.views import Ping
1314

15+
# custom admin site class
16+
admin.site.__class__ = CustomAdminSite
17+
admin.site.site_header = "WHOI Skills Matcher Administration"
18+
admin.site.index_title = "Site administration"
19+
admin.site.site_title = "WHOI Skills Matcher Administration"
20+
1421
urlpatterns = [
1522
path("", TemplateView.as_view(template_name="pages/home.html"), name="home"),
1623
path(

api/local.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ services:
1616
volumes:
1717
- .:/app:z
1818
env_file:
19-
- ./.envs/.local/.django
19+
- ./.env
2020
- ./.envs/.local/.postgres
2121
ports:
2222
- "8000:8000"
Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,47 @@
11
from django.urls import reverse
2-
from django.views.generic.base import RedirectView
2+
from django.http import HttpResponseRedirect
3+
from django.views.generic.base import RedirectView, TemplateView
4+
from django.db.models.functions import TruncMonth
5+
from django.db.models import Count
6+
7+
from skills_matcher_db.users.models import User
8+
from skills_matcher_db.project_owners.models import Project
39

410

511
class AdminLoginView(RedirectView):
612
def get(self, request, *args, **kwargs):
713
"""
8-
Assuming the name of the external system's login url is "login"
14+
Assuming the name of the external system's login url is "login
915
"""
1016
return HttpResponseRedirect(reverse("login"))
17+
18+
19+
class AdminMetricsView(TemplateView):
20+
template_name = "admin/core/metrics.html"
21+
22+
def get_context_data(self, **kwargs):
23+
context = super(AdminMetricsView, self).get_context_data(**kwargs)
24+
# get user metric data
25+
user_metrics_qs = (
26+
User.objects.annotate(month=TruncMonth("date_joined"))
27+
.values("month")
28+
.annotate(user_count=Count("id"))
29+
.order_by("month")
30+
)
31+
32+
# get user metric data
33+
projects_metrics_qs = (
34+
Project.objects.annotate(month=TruncMonth("date_created"))
35+
.values("month")
36+
.annotate(project_count=Count("id"))
37+
.order_by("month")
38+
)
39+
40+
context.update(
41+
{
42+
"node_type": "assemblies",
43+
"user_metrics": user_metrics_qs,
44+
"project_metrics": projects_metrics_qs,
45+
}
46+
)
47+
return context
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{% extends "admin/base.html" %}
2+
3+
{% load static %}
4+
5+
{% block title %}{{ title }} | {{ site_title|default:_('Django site admin') }}{% endblock %}
6+
7+
{% block extrastyle %}{{ block.super }}<link rel="stylesheet" type="text/css" href="{% static "css/admin-extra.css" %}" />{% endblock %}
8+
9+
{% block branding %}
10+
<h1 id="site-name"><a href="{% url 'admin:index' %}">{{ site_header|default:_('Django administration') }}</a></h1>
11+
{% endblock %}
12+
13+
{% block nav-global %}{% endblock %}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
{# templates/admin/custom_page.html #}
2+
3+
{% extends 'admin/change_list.html' %}
4+
{% load crispy_forms_tags %}
5+
6+
{% block pagination %}
7+
{% endblock %}
8+
{% block filters %}{% endblock filters %}
9+
{% block object-tools %}{% endblock object-tools %}
10+
{% block search %}{% endblock %}
11+
12+
{% block breadcrumbs %}
13+
<div class="breadcrumbs">
14+
<a href="{% url 'admin:index' %}">Home</a>
15+
{% if page_name %} &rsaquo; {{ page_name }}{% endif %}
16+
</div>
17+
{% endblock %}
18+
19+
{% block result_list %}
20+
<div class="content">
21+
<h1>Application Metrics</h1>
22+
23+
<h2>New Users by Month</h2>
24+
<ul>
25+
{% for item in user_metrics %}
26+
<li>{{ item.month|date:"Y-m" }} - {{ item.user_count }} user</li>
27+
{% empty %}
28+
<li>No items found.</li>
29+
{% endfor %}
30+
</ul>
31+
32+
33+
<h2>New Projects by Month</h2>
34+
<ul>
35+
{% for item in projects_metrics_qs %}
36+
<li>{{ item.month|date:"Y-m" }} - {{ item.project_count }} project</li>
37+
{% empty %}
38+
<li>No items found.</li>
39+
{% endfor %}
40+
</ul>
41+
42+
43+
44+
</div>
45+
{% endblock result_list %}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<div id="extra_links_wrapper" class="module">
2+
<table>
3+
<caption>
4+
<a class="section" title="Metrics">Metrics</a>
5+
</caption>
6+
<tr>
7+
<th scope="row">
8+
<a href="{% url 'admin:view_metrics' %}"> View Metrics </a>
9+
</th>
10+
<td></td>
11+
</tr>
12+
</table>
13+
</div>
14+
15+
{% include 'admin/app_list.html' %}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{% extends "admin/index.html" %}
2+
3+
{% block content %}
4+
<div id="content-main">
5+
{% include "admin/custom_app_list.html" with app_list=app_list show_changelinks=True %}
6+
</div>
7+
{% endblock %}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{# templates/admin/custom_page.html #}
2+
3+
{% extends 'admin/change_list.html' %}
4+
5+
{% block pagination %}{% endblock %}
6+
{% block filters %}{% endblock filters %}
7+
{% block object-tools %}{% endblock object-tools %}
8+
{% block search %}{% endblock %}
9+
10+
{% block breadcrumbs %}
11+
<div class="breadcrumbs">
12+
<a href="{% url 'admin:index' %}">Home</a>
13+
{% if page_name %} &rsaquo; {{ page_name }}{% endif %}
14+
</div>
15+
{% endblock %}
16+
17+
{% block result_list %}
18+
{{text}}
19+
{% endblock result_list %}

0 commit comments

Comments
 (0)