From 0b574227fb65611e0b867d57848cb4015076c5ae Mon Sep 17 00:00:00 2001 From: "R. Miles McCain" Date: Wed, 15 Apr 2020 10:00:20 -0400 Subject: [PATCH] Improve admin panel & display --- shynet/analytics/admin.py | 23 ++++++++++++++++--- shynet/analytics/models.py | 20 +++++++++++++++- shynet/core/admin.py | 9 +++++++- shynet/core/models.py | 7 ++++++ shynet/dashboard/templates/base.html | 5 ++++ .../templates/dashboard/pages/service.html | 4 ++-- 6 files changed, 61 insertions(+), 7 deletions(-) diff --git a/shynet/analytics/admin.py b/shynet/analytics/admin.py index 309f2f0..dfb4f70 100644 --- a/shynet/analytics/admin.py +++ b/shynet/analytics/admin.py @@ -2,8 +2,25 @@ from django.contrib import admin from .models import Hit, Session -admin.site.register(Session) +class HitInline(admin.TabularInline): + model = Hit + fk_name = "session" + extra = 0 -admin.site.register(Hit) +class SessionAdmin(admin.ModelAdmin): + list_display = ("uuid", "service", "start_time", "identifier", "ip", "asn", "country") + list_display_links = ("uuid",) + search_fields = ("ip", "user_agent", "device", "device_type", "identifier", "asn", "time_zone") + list_filter = ("device_type",) + inlines = [HitInline] -# Register your models here. +admin.site.register(Session, SessionAdmin) + + +class HitAdmin(admin.ModelAdmin): + list_display = ("session", "initial", "start_time", "heartbeats", "tracker", "load_time") + list_display_links = ("session",) + search_fields = ("initial", "tracker", "location", "referrer") + list_filter = ("initial", "tracker") + +admin.site.register(Hit, HitAdmin) diff --git a/shynet/analytics/models.py b/shynet/analytics/models.py index 30fada4..e0ddbeb 100644 --- a/shynet/analytics/models.py +++ b/shynet/analytics/models.py @@ -5,6 +5,7 @@ from django.db import models from django.utils import timezone from core.models import Service +from django.shortcuts import reverse def _default_uuid(): @@ -62,6 +63,15 @@ class Session(models.Model): def duration(self): return self.last_seen - self.start_time + def __str__(self): + return f"{self.identifier if self.identifier != '' else 'Anonymous'} @ {self.service.name} [{str(self.uuid)[:6]}]" + + def get_absolute_url(self): + return reverse( + "dashboard:service_session", + kwargs={"pk": self.service.pk, "session_pk": self.uuid}, + ) + class Hit(models.Model): session = models.ForeignKey(Session, on_delete=models.CASCADE, db_index=True) @@ -71,7 +81,9 @@ class Hit(models.Model): start_time = models.DateTimeField(auto_now_add=True, db_index=True) last_seen = models.DateTimeField(auto_now_add=True) heartbeats = models.IntegerField(default=0) - tracker = models.TextField() # Tracking pixel or JS + tracker = models.TextField( + choices=[("JS", "JavaScript"), ("PIXEL", "Pixel (noscript)")] + ) # Tracking pixel or JS # Advanced page information location = models.TextField(blank=True, db_index=True) @@ -89,3 +101,9 @@ class Hit(models.Model): @property def duration(self): return self.last_seen - self.start_time + + def get_absolute_url(self): + return reverse( + "dashboard:service_session", + kwargs={"pk": self.session.service.pk, "session_pk": self.session.pk}, + ) diff --git a/shynet/core/admin.py b/shynet/core/admin.py index 168fe7b..16be324 100644 --- a/shynet/core/admin.py +++ b/shynet/core/admin.py @@ -4,4 +4,11 @@ from django.contrib.auth.admin import UserAdmin from .models import Service, User admin.site.register(User, UserAdmin) -admin.site.register(Service) + +class ServiceAdmin(admin.ModelAdmin): + list_display = ("name", "link", "owner", "status") + list_display_links = ("name",) + list_filter = ("status",) + search_fields = ("name", "link", "owner") + +admin.site.register(Service, ServiceAdmin) diff --git a/shynet/core/models.py b/shynet/core/models.py index 7671a09..a4860c5 100644 --- a/shynet/core/models.py +++ b/shynet/core/models.py @@ -7,6 +7,7 @@ from django.db import models from django.db.models.functions import TruncDate from django.db.utils import NotSupportedError from django.utils import timezone +from django.shortcuts import reverse def _default_uuid(): @@ -173,3 +174,9 @@ class Service(models.Model): ), "online": True, } + + def get_absolute_url(self): + return reverse( + "dashboard:service", + kwargs={"pk": self.pk}, + ) diff --git a/shynet/dashboard/templates/base.html b/shynet/dashboard/templates/base.html index 83113ac..fc03200 100644 --- a/shynet/dashboard/templates/base.html +++ b/shynet/dashboard/templates/base.html @@ -68,6 +68,11 @@ {% if user.is_authenticated %} + {% if user.is_superuser %} + {% url 'admin:index' as url %} + {% include 'dashboard/includes/sidebar_portal.html' with label="Admin" url=url %} + {% endif %} + {% url 'account_email' as url %} {% include 'dashboard/includes/sidebar_portal.html' with label="Emails" url=url %} diff --git a/shynet/dashboard/templates/dashboard/pages/service.html b/shynet/dashboard/templates/dashboard/pages/service.html index 6b664c6..7a8688a 100644 --- a/shynet/dashboard/templates/dashboard/pages/service.html +++ b/shynet/dashboard/templates/dashboard/pages/service.html @@ -98,7 +98,7 @@ {% for referrer in stats.referrers %} - {{referrer.referrer|default:"Direct"|urldisplay}} + {{referrer.referrer|default:"Direct"|urlize}} {{referrer.count|intcomma}} {% endfor %} @@ -178,7 +178,7 @@ -
+
{% include 'dashboard/includes/session_list.html' %}
View more sessions