1from dataclasses import dataclass
2
3from plain.admin.cards import ChartCard
4from plain.admin.views import (
5 AdminModelDetailView,
6 AdminModelListView,
7 AdminViewset,
8 register_viewset,
9)
10
11from .models import OAuthConnection
12
13
14@dataclass
15class ProviderCount:
16 provider_key: str
17 count: int
18
19
20class ProvidersChartCard(ChartCard):
21 title = "Providers"
22
23 def get_chart_data(self) -> dict:
24 results = OAuthConnection.query.sql(
25 t"""
26 SELECT {OAuthConnection.provider_key} AS provider_key,
27 count(*) AS "count!"
28 FROM {OAuthConnection}
29 GROUP BY 1
30 ORDER BY 1
31 """,
32 result_type=ProviderCount,
33 ).all()
34 return {
35 "type": "doughnut",
36 "data": {
37 "labels": [result.provider_key for result in results],
38 "datasets": [
39 {
40 "label": "Providers",
41 "data": [result.count for result in results],
42 }
43 ],
44 },
45 }
46
47
48@register_viewset
49class OAuthConnectionViewset(AdminViewset):
50 class ListView(AdminModelListView):
51 nav_section = "OAuth"
52 nav_icon = "link-45deg"
53 model = OAuthConnection
54 title = "Connections"
55 description = "User accounts linked to OAuth providers (Google, GitHub, etc)."
56 fields = ("id", "user", "provider_key", "provider_user_id")
57 cards = (ProvidersChartCard,)
58
59 class DetailView(AdminModelDetailView):
60 model = OAuthConnection
61 title = "OAuth connection"