import base64
+from pgweb.util.widgets import TemplateRenderWidget
+from pgweb.util.db import exec_to_dict
+
from .models import CommunityAuthSite, CommunityAuthOrg
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
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"""
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)
--- /dev/null
+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