mirror of
https://github.com/postgres/pgweb.git
synced 2025-08-10 00:42:06 +00:00
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.
This commit is contained in:
@ -6,6 +6,9 @@ from django import forms
|
|||||||
|
|
||||||
import base64
|
import base64
|
||||||
|
|
||||||
|
from pgweb.util.widgets import TemplateRenderWidget
|
||||||
|
from pgweb.util.db import exec_to_dict
|
||||||
|
|
||||||
from .models import CommunityAuthSite, CommunityAuthOrg
|
from .models import CommunityAuthSite, CommunityAuthOrg
|
||||||
|
|
||||||
|
|
||||||
@ -31,7 +34,8 @@ class CommunityAuthSiteAdmin(admin.ModelAdmin):
|
|||||||
|
|
||||||
|
|
||||||
class PGUserChangeForm(UserChangeForm):
|
class PGUserChangeForm(UserChangeForm):
|
||||||
"""just like UserChangeForm, butremoves "username" requirement"""
|
logininfo = forms.CharField(label="Community login history")
|
||||||
|
|
||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, *args, **kwargs):
|
||||||
super(PGUserChangeForm, self).__init__(*args, **kwargs)
|
super(PGUserChangeForm, self).__init__(*args, **kwargs)
|
||||||
# because the auth.User model is set to "blank=False" and the Django
|
# 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'):
|
if self.fields.get('username'):
|
||||||
del self.fields['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):
|
class PGUserAdmin(UserAdmin):
|
||||||
"""overrides default Django user admin"""
|
"""overrides default Django user admin"""
|
||||||
@ -52,6 +64,14 @@ class PGUserAdmin(UserAdmin):
|
|||||||
return self.readonly_fields + ('username',)
|
return self.readonly_fields + ('username',)
|
||||||
return self.readonly_fields
|
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(CommunityAuthSite, CommunityAuthSiteAdmin)
|
||||||
admin.site.register(CommunityAuthOrg)
|
admin.site.register(CommunityAuthOrg)
|
||||||
|
@ -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>
|
12
pgweb/util/widgets.py
Normal file
12
pgweb/util/widgets.py
Normal file
@ -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
|
Reference in New Issue
Block a user