Show community account last login and count info on admin page
authorMagnus Hagander <magnus@hagander.net>
Tue, 7 Jul 2020 10:24:08 +0000 (12:24 +0200)
committerMagnus Hagander <magnus@hagander.net>
Tue, 7 Jul 2020 10:25:39 +0000 (12:25 +0200)
The collection facility was added in
[200~9e70a4e0c32f8db0178f05dac4c1fca7b317e7c5, but no way was added to
view it. To make it a  bit more useful, add a static set of info on the
user edit page in /admin/ that shows the last login and number of logins
per site.

pgweb/account/admin.py
pgweb/util/templates/forms/widgets/community_auth_usage_widget.html [new file with mode: 0644]
pgweb/util/widgets.py [new file with mode: 0644]

index 47f747cfe3cffce951078e6322d7d65b3b83d2aa..809961b366df17efde005e04e11caa44dfed4c72 100644 (file)
@@ -6,6 +6,9 @@ from django import forms
 
 import base64
 
+from pgweb.util.widgets import TemplateRenderWidget
+from pgweb.util.db import exec_to_dict
+
 from .models import CommunityAuthSite, CommunityAuthOrg
 
 
@@ -31,7 +34,8 @@ class CommunityAuthSiteAdmin(admin.ModelAdmin):
 
 
 class PGUserChangeForm(UserChangeForm):
-    """just like UserChangeForm, butremoves "username" requirement"""
+    logininfo = forms.CharField(label="Community login history")
+
     def __init__(self, *args, **kwargs):
         super(PGUserChangeForm, self).__init__(*args, **kwargs)
         # because the auth.User model is set to "blank=False" and the Django
@@ -41,6 +45,14 @@ class PGUserChangeForm(UserChangeForm):
         if self.fields.get('username'):
             del self.fields['username']
 
+        self.fields['logininfo'].widget = TemplateRenderWidget(
+            template='forms/widgets/community_auth_usage_widget.html',
+            context={
+                'logins': exec_to_dict("SELECT s.name AS service, lastlogin, logincount FROM account_communityauthsite s INNER JOIN account_communityauthlastlogin l ON s.id=l.site_id WHERE user_id=%(userid)s ORDER BY lastlogin DESC", {
+                    'userid': self.instance.pk,
+                }),
+            })
+
 
 class PGUserAdmin(UserAdmin):
     """overrides default Django user admin"""
@@ -52,6 +64,14 @@ class PGUserAdmin(UserAdmin):
             return self.readonly_fields + ('username',)
         return self.readonly_fields
 
+    @property
+    def fieldsets(self):
+        fs = list(super().fieldsets)
+        fs.append(
+            ('Community authentication', {'fields': ('logininfo', )}),
+        )
+        return fs
+
 
 admin.site.register(CommunityAuthSite, CommunityAuthSiteAdmin)
 admin.site.register(CommunityAuthOrg)
diff --git a/pgweb/util/templates/forms/widgets/community_auth_usage_widget.html b/pgweb/util/templates/forms/widgets/community_auth_usage_widget.html
new file mode 100644 (file)
index 0000000..dc798e3
--- /dev/null
@@ -0,0 +1,14 @@
+<table>
+  <tr>
+    <th>Service</th>
+    <th>Last login</th>
+    <th>Login count</th>
+  </tr>
+{%for l in logins%}
+  <tr>
+    <td>{{l.service}}</td>
+    <td>{{l.lastlogin}}</td>
+    <td>{{l.logincount}}</td>
+  </tr>
+{%endfor%}
+</table>
diff --git a/pgweb/util/widgets.py b/pgweb/util/widgets.py
new file mode 100644 (file)
index 0000000..070ba53
--- /dev/null
@@ -0,0 +1,12 @@
+from django.forms.widgets import Widget
+
+
+class TemplateRenderWidget(Widget):
+    def __init__(self, *args, **kwargs):
+        self.template_name = kwargs.pop('template')
+        self.templatecontext = kwargs.pop('context')
+
+        super().__init__(*args, **kwargs)
+
+    def get_context(self, name, value, attrs):
+        return self.templatecontext