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.query.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 nav_icon = "link-45deg"
41 model = OAuthConnection
42 title = "Connections"
43 fields = ["id", "user", "provider_key", "provider_user_id"]
44 cards = [ProvidersChartCard]
45
46 class DetailView(AdminModelDetailView):
47 model = OAuthConnection
48 title = "OAuth connection"