Skip to content

Commit

Permalink
feat(admin): add django admin for dashboard templates (#26093)
Browse files Browse the repository at this point in the history
  • Loading branch information
thmsobrmlr authored Nov 9, 2024
1 parent 9630c6d commit b51bcb1
Show file tree
Hide file tree
Showing 16 changed files with 73 additions and 2 deletions.
3 changes: 3 additions & 0 deletions posthog/admin/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
UserAdmin,
TeamAdmin,
DashboardAdmin,
DashboardTemplateAdmin,
InsightAdmin,
ExperimentAdmin,
FeatureFlagAdmin,
Expand All @@ -26,6 +27,7 @@
User,
Team,
Dashboard,
DashboardTemplate,
Insight,
Experiment,
FeatureFlag,
Expand All @@ -49,6 +51,7 @@
admin.site.register(User, UserAdmin)

admin.site.register(Dashboard, DashboardAdmin)
admin.site.register(DashboardTemplate, DashboardTemplateAdmin)
admin.site.register(Insight, InsightAdmin)

admin.site.register(Experiment, ExperimentAdmin)
Expand Down
1 change: 1 addition & 0 deletions posthog/admin/admins/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from .async_deletion_admin import AsyncDeletionAdmin
from .cohort_admin import CohortAdmin
from .dashboard_admin import DashboardAdmin
from .dashboard_template_admin import DashboardTemplateAdmin
from .data_warehouse_table_admin import DataWarehouseTableAdmin
from .experiment_admin import ExperimentAdmin
from .feature_flag_admin import FeatureFlagAdmin
Expand Down
1 change: 1 addition & 0 deletions posthog/admin/admins/cohort_admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class CohortAdmin(admin.ModelAdmin):
autocomplete_fields = ("team", "created_by")
ordering = ("-created_at",)

@admin.display(description="Team")
def team_link(self, cohort: Cohort):
return format_html(
'<a href="/admin/posthog/team/{}/change/">{}</a>',
Expand Down
2 changes: 2 additions & 0 deletions posthog/admin/admins/dashboard_admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,15 @@ class DashboardAdmin(admin.ModelAdmin):
ordering = ("-created_at", "creation_mode")
inlines = (DashboardTileInline,)

@admin.display(description="Team")
def team_link(self, dashboard: Dashboard):
return format_html(
'<a href="/admin/posthog/team/{}/change/">{}</a>',
dashboard.team.pk,
dashboard.team.name,
)

@admin.display(description="Organization")
def organization_link(self, dashboard: Dashboard):
return format_html(
'<a href="/admin/posthog/organization/{}/change/">{}</a>',
Expand Down
31 changes: 31 additions & 0 deletions posthog/admin/admins/dashboard_template_admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from django.contrib import admin
from django.utils.html import format_html

from posthog.models.dashboard_templates import DashboardTemplate


class DashboardTemplateAdmin(admin.ModelAdmin):
list_display = (
"template_name",
"deleted",
"created_at",
"created_by",
"scope",
"team_link",
)
list_display_links = ("template_name",)
list_select_related = ("team", "team__organization")
search_fields = ("id", "template_name", "team__name", "team__organization__name")
readonly_fields = ("team",)
autocomplete_fields = ("team", "created_by")
ordering = ("-created_at",)

@admin.display(description="Team")
def team_link(self, template: DashboardTemplate):
if template.team is None:
return None
return format_html(
'<a href="/admin/posthog/team/{}/change/">{}</a>',
template.team.pk,
template.team.name,
)
2 changes: 2 additions & 0 deletions posthog/admin/admins/data_warehouse_table_admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,15 @@ class DataWarehouseTableAdmin(admin.ModelAdmin):
autocomplete_fields = ("team", "created_by")
ordering = ("-created_at",)

@admin.display(description="Team")
def team_link(self, dashboard: Dashboard):
return format_html(
'<a href="/admin/posthog/team/{}/change/">{}</a>',
dashboard.team.pk,
dashboard.team.name,
)

@admin.display(description="Organization")
def organization_link(self, dashboard: Dashboard):
return format_html(
'<a href="/admin/posthog/organization/{}/change/">{}</a>',
Expand Down
1 change: 1 addition & 0 deletions posthog/admin/admins/experiment_admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class ExperimentAdmin(admin.ModelAdmin):
autocomplete_fields = ("team", "created_by")
ordering = ("-created_at",)

@admin.display(description="Team")
def team_link(self, experiment: Experiment):
return format_html(
'<a href="/admin/posthog/team/{}/change/">{}</a>',
Expand Down
1 change: 1 addition & 0 deletions posthog/admin/admins/feature_flag_admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class FeatureFlagAdmin(admin.ModelAdmin):
autocomplete_fields = ("team", "created_by")
ordering = ("-created_at",)

@admin.display(description="Team")
def team_link(self, flag: FeatureFlag):
return format_html(
'<a href="/admin/posthog/team/{}/change/">{}</a>',
Expand Down
1 change: 1 addition & 0 deletions posthog/admin/admins/hog_function_admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ class HogFunctionAdmin(admin.ModelAdmin):
"template_id",
)

@admin.display(description="Team")
def team_link(self, instance: HogFunction):
return format_html(
'<a href="/admin/posthog/team/{}/change/">{}</a>',
Expand Down
2 changes: 2 additions & 0 deletions posthog/admin/admins/insight_admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,15 @@ class InsightAdmin(admin.ModelAdmin):
def effective_name(self, insight: Insight):
return insight.name or format_html("<i>{}</>", insight.derived_name)

@admin.display(description="Team")
def team_link(self, insight: Insight):
return format_html(
'<a href="/admin/posthog/team/{}/change/">{}</a>',
insight.team.pk,
insight.team.name,
)

@admin.display(description="Organization")
def organization_link(self, insight: Insight):
return format_html(
'<a href="/admin/posthog/organization/{}/change/">{}</a>',
Expand Down
1 change: 1 addition & 0 deletions posthog/admin/admins/survey_admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ def get_form(self, request, obj=None, change=False, **kwargs):
form.base_fields[field].required = False
return form

@admin.display(description="Team")
def team_link(self, experiment: Experiment):
return format_html(
'<a href="/admin/posthog/team/{}/change/">{}</a>',
Expand Down
3 changes: 3 additions & 0 deletions posthog/admin/admins/user_admin.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin as DjangoUserAdmin
from django.contrib.auth.forms import UserChangeForm as DjangoUserChangeForm
from django.utils.html import format_html
Expand Down Expand Up @@ -70,6 +71,7 @@ class UserAdmin(DjangoUserAdmin):
readonly_fields = ["id", "current_team", "current_organization"]
ordering = ("email",)

@admin.display(description="Current Team")
def current_team_link(self, user: User):
if not user.team:
return "–"
Expand All @@ -80,6 +82,7 @@ def current_team_link(self, user: User):
user.team.name,
)

@admin.display(description="Current Organization")
def current_organization_link(self, user: User):
if not user.organization:
return "–"
Expand Down
17 changes: 17 additions & 0 deletions posthog/migrations/0513_alter_dashboardtemplate_github_url.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Generated by Django 4.2.15 on 2024-11-08 17:17

from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
("posthog", "0512_errortrackingissue_errortrackingissuefingerprintv2_and_more"),
]

operations = [
migrations.AlterField(
model_name="dashboardtemplate",
name="github_url",
field=models.CharField(blank=True, max_length=8201, null=True),
),
]
2 changes: 1 addition & 1 deletion posthog/migrations/max_migration.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0512_errortrackingissue_errortrackingissuefingerprintv2_and_more
0513_alter_dashboardtemplate_github_url
2 changes: 2 additions & 0 deletions posthog/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
from .cohort import Cohort, CohortPeople
from .comment import Comment
from .dashboard import Dashboard
from .dashboard_templates import DashboardTemplate
from .dashboard_tile import DashboardTile, Text
from .early_access_feature import EarlyAccessFeature
from .element import Element
Expand Down Expand Up @@ -94,6 +95,7 @@
"CohortPeople",
"Dashboard",
"DashboardTile",
"DashboardTemplate",
"DeletionType",
"EarlyAccessFeature",
"Element",
Expand Down
5 changes: 4 additions & 1 deletion posthog/models/dashboard_templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class Scope(models.TextChoices):
# URL length for browsers can be as much as 64Kb
# see https://stackoverflow.com/questions/417142/what-is-the-maximum-length-of-a-url-in-different-browsers
# but GitHub apparently is more likely 8kb https://stackoverflow.com/a/64565317
github_url = models.CharField(max_length=8201, null=True)
github_url = models.CharField(max_length=8201, null=True, blank=True)
# where this template is available, e.g. "general" and/or "onboarding"
availability_contexts = ArrayField(models.CharField(max_length=255), blank=True, null=True)

Expand All @@ -51,6 +51,9 @@ class Meta:
),
]

def __str__(self):
return self.template_name

@staticmethod
def original_template() -> "DashboardTemplate":
"""
Expand Down

0 comments on commit b51bcb1

Please sign in to comment.