Skip to content

Commit e850a9b

Browse files
committed
Show community account last login and count info on admin page
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.
1 parent e9c84c6 commit e850a9b

File tree

3 files changed

+47
-1
lines changed

3 files changed

+47
-1
lines changed

pgweb/account/admin.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66

77
import base64
88

9+
from pgweb.util.widgets import TemplateRenderWidget
10+
from pgweb.util.db import exec_to_dict
11+
912
from .models import CommunityAuthSite, CommunityAuthOrg
1013

1114

@@ -31,7 +34,8 @@ class CommunityAuthSiteAdmin(admin.ModelAdmin):
3134

3235

3336
class PGUserChangeForm(UserChangeForm):
34-
"""just like UserChangeForm, butremoves "username" requirement"""
37+
logininfo = forms.CharField(label="Community login history")
38+
3539
def __init__(self, *args, **kwargs):
3640
super(PGUserChangeForm, self).__init__(*args, **kwargs)
3741
# because the auth.User model is set to "blank=False" and the Django
@@ -41,6 +45,14 @@ def __init__(self, *args, **kwargs):
4145
if self.fields.get('username'):
4246
del self.fields['username']
4347

48+
self.fields['logininfo'].widget = TemplateRenderWidget(
49+
template='forms/widgets/community_auth_usage_widget.html',
50+
context={
51+
'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", {
52+
'userid': self.instance.pk,
53+
}),
54+
})
55+
4456

4557
class PGUserAdmin(UserAdmin):
4658
"""overrides default Django user admin"""
@@ -52,6 +64,14 @@ def get_readonly_fields(self, request, obj=None):
5264
return self.readonly_fields + ('username',)
5365
return self.readonly_fields
5466

67+
@property
68+
def fieldsets(self):
69+
fs = list(super().fieldsets)
70+
fs.append(
71+
('Community authentication', {'fields': ('logininfo', )}),
72+
)
73+
return fs
74+
5575

5676
admin.site.register(CommunityAuthSite, CommunityAuthSiteAdmin)
5777
admin.site.register(CommunityAuthOrg)
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<table>
2+
<tr>
3+
<th>Service</th>
4+
<th>Last login</th>
5+
<th>Login count</th>
6+
</tr>
7+
{%for l in logins%}
8+
<tr>
9+
<td>{{l.service}}</td>
10+
<td>{{l.lastlogin}}</td>
11+
<td>{{l.logincount}}</td>
12+
</tr>
13+
{%endfor%}
14+
</table>

pgweb/util/widgets.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from django.forms.widgets import Widget
2+
3+
4+
class TemplateRenderWidget(Widget):
5+
def __init__(self, *args, **kwargs):
6+
self.template_name = kwargs.pop('template')
7+
self.templatecontext = kwargs.pop('context')
8+
9+
super().__init__(*args, **kwargs)
10+
11+
def get_context(self, name, value, attrs):
12+
return self.templatecontext

0 commit comments

Comments
 (0)