1from plain.admin.cards import ChartCard
2from plain.admin.views import (
3 AdminModelDetailView,
4 AdminModelListView,
5 AdminViewset,
6 register_viewset,
7)
8from plain.models import Count
9
10from .models import OAuthConnection
11
12
13class ProvidersChartCard(ChartCard):
14 title = "Providers"
15
16 def get_chart_data(self) -> dict:
17 results = (
18 OAuthConnection.objects.all()
19 .values("provider_key")
20 .annotate(count=Count("id"))
21 )
22 return {
23 "type": "doughnut",
24 "data": {
25 "labels": [result["provider_key"] for result in results],
26 "datasets": [
27 {
28 "label": "Providers",
29 "data": [result["count"] for result in results],
30 }
31 ],
32 },
33 }
34
35
36@register_viewset
37class OAuthConnectionViewset(AdminViewset):
38 class ListView(AdminModelListView):
39 nav_section = "OAuth"
40 model = OAuthConnection
41 title = "Connections"
42 fields = ["id", "user", "provider_key", "provider_user_id"]
43 cards = [ProvidersChartCard]
44
45 class DetailView(AdminModelDetailView):
46 model = OAuthConnection
47 title = "OAuth connection"