From 7d472d82b1297d16114ac2fd581b84d1be897c52 Mon Sep 17 00:00:00 2001 From: Christian Glatthard Date: Sun, 9 Aug 2015 15:43:02 +0200 Subject: [PATCH 1/2] import working but not very nicely done yet --- ipynbsrv/admin/admin.py | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/ipynbsrv/admin/admin.py b/ipynbsrv/admin/admin.py index f99c56c..891d68a 100644 --- a/ipynbsrv/admin/admin.py +++ b/ipynbsrv/admin/admin.py @@ -1,11 +1,15 @@ from django_admin_conf_vars.models import ConfigurationVariable +from django.conf.urls import patterns, url from django.contrib import admin, messages from django.contrib.auth.admin import GroupAdmin from django.contrib.auth.models import User from django.core.urlresolvers import reverse from django.http import HttpResponseRedirect from ipynbsrv.admin.forms import CollaborationGroupAdminForm, ShareAdminForm +from ipynbsrv.core.auth.authentication_backends import BackendProxyAuthentication +from ipynbsrv.core.helpers import get_user_backend_connected from ipynbsrv.core.models import * +from ipynbsrv.contract.backends import GroupBackend, UserBackend class CoreAdminSite(admin.AdminSite): @@ -534,6 +538,10 @@ class UserAdmin(admin.ModelAdmin): list_display = ['username', 'is_active', 'is_staff'] list_filter = ['is_active', 'is_staff'] + class Media: + # javascript to add custom button to User lits + js = ('admin/js/user_import.js', ) + def get_fieldsets(self, request, obj=None): """ :inherit. @@ -555,6 +563,37 @@ def get_fieldsets(self, request, obj=None): }) ] + def get_urls(self): + urls = super(UserAdmin, self).get_urls() + my_urls = patterns('', (r'^import_users/$', self.import_users)) + return my_urls + urls + + def import_users(self, request): + # custom view which should return an HttpResponse + try: + backend = get_user_backend_connected() + users = backend.get_users() + helper = BackendProxyAuthentication() + new_users = [] + for user in users: + username = str(user.get(UserBackend.FIELD_PK)) + password = '' + obj = User.objects.filter(username=username) + if not obj: + # if user is not existing yet, create him + uid = BackendUser.generate_internal_uid() + group = helper.create_user_groups(username, uid) + user = helper.create_users(username, password, uid, group.backend_group) + group.add_user(user.backend_user) + new_users.append(username) + if len(new_users) == 0: + self.message_user(request, "All users already imported.", messages.INFO) + else: + self.message_user(request, "Successfully imported {} users. ({})".format(len(new_users), ', '.join(new_users))) + except Exception: + self.message_user(request, "Operation failed.", messages.ERROR) + return HttpResponseRedirect(reverse('admin:auth_user_changelist')) + def get_readonly_fields(self, request, obj=None): """ :inherit. From 65f7aee44b5028fadc115aeb5369a3db8455861f Mon Sep 17 00:00:00 2001 From: Christian Glatthard Date: Sun, 9 Aug 2015 16:14:40 +0200 Subject: [PATCH 2/2] import_user added as manage.py command --- ipynbsrv/admin/admin.py | 26 +++------- ipynbsrv/core/management/__init__.py | 1 + ipynbsrv/core/management/commands/__init__.py | 1 + .../core/management/commands/import_users.py | 49 +++++++++++++++++++ 4 files changed, 57 insertions(+), 20 deletions(-) create mode 100644 ipynbsrv/core/management/__init__.py create mode 100644 ipynbsrv/core/management/commands/__init__.py create mode 100644 ipynbsrv/core/management/commands/import_users.py diff --git a/ipynbsrv/admin/admin.py b/ipynbsrv/admin/admin.py index 891d68a..b53a461 100644 --- a/ipynbsrv/admin/admin.py +++ b/ipynbsrv/admin/admin.py @@ -1,15 +1,13 @@ from django_admin_conf_vars.models import ConfigurationVariable -from django.conf.urls import patterns, url +from django.conf.urls import patterns from django.contrib import admin, messages from django.contrib.auth.admin import GroupAdmin from django.contrib.auth.models import User from django.core.urlresolvers import reverse from django.http import HttpResponseRedirect from ipynbsrv.admin.forms import CollaborationGroupAdminForm, ShareAdminForm -from ipynbsrv.core.auth.authentication_backends import BackendProxyAuthentication -from ipynbsrv.core.helpers import get_user_backend_connected from ipynbsrv.core.models import * -from ipynbsrv.contract.backends import GroupBackend, UserBackend +from ipynbsrv.core.management.commands import import_users class CoreAdminSite(admin.AdminSite): @@ -571,25 +569,13 @@ def get_urls(self): def import_users(self, request): # custom view which should return an HttpResponse try: - backend = get_user_backend_connected() - users = backend.get_users() - helper = BackendProxyAuthentication() - new_users = [] - for user in users: - username = str(user.get(UserBackend.FIELD_PK)) - password = '' - obj = User.objects.filter(username=username) - if not obj: - # if user is not existing yet, create him - uid = BackendUser.generate_internal_uid() - group = helper.create_user_groups(username, uid) - user = helper.create_users(username, password, uid, group.backend_group) - group.add_user(user.backend_user) - new_users.append(username) + # Todo: imports + new_users = import_users.import_users() + if len(new_users) == 0: self.message_user(request, "All users already imported.", messages.INFO) else: - self.message_user(request, "Successfully imported {} users. ({})".format(len(new_users), ', '.join(new_users))) + self.message_user(request, "Successfully imported {} users: {}".format(len(new_users), ', '.join(new_users))) except Exception: self.message_user(request, "Operation failed.", messages.ERROR) return HttpResponseRedirect(reverse('admin:auth_user_changelist')) diff --git a/ipynbsrv/core/management/__init__.py b/ipynbsrv/core/management/__init__.py new file mode 100644 index 0000000..de40ea7 --- /dev/null +++ b/ipynbsrv/core/management/__init__.py @@ -0,0 +1 @@ +__import__('pkg_resources').declare_namespace(__name__) diff --git a/ipynbsrv/core/management/commands/__init__.py b/ipynbsrv/core/management/commands/__init__.py new file mode 100644 index 0000000..de40ea7 --- /dev/null +++ b/ipynbsrv/core/management/commands/__init__.py @@ -0,0 +1 @@ +__import__('pkg_resources').declare_namespace(__name__) diff --git a/ipynbsrv/core/management/commands/import_users.py b/ipynbsrv/core/management/commands/import_users.py new file mode 100644 index 0000000..c9486e0 --- /dev/null +++ b/ipynbsrv/core/management/commands/import_users.py @@ -0,0 +1,49 @@ +from django.core.management.base import BaseCommand, CommandError +from django.contrib.auth.models import User +from ipynbsrv.core.auth.authentication_backends import BackendProxyAuthentication +from ipynbsrv.core.helpers import get_user_backend_connected +from ipynbsrv.core.models import * +from ipynbsrv.contract.backends import UserBackend + + +def import_users(): + """ + Imports all the users found on the user backend into django. + """ + backend = get_user_backend_connected() + users = backend.get_users() + helper = BackendProxyAuthentication() + new_users = [] + for user in users: + username = str(user.get(UserBackend.FIELD_PK)) + password = '' + obj = User.objects.filter(username=username) + if not obj: + # if user is not existing yet, create him + uid = BackendUser.generate_internal_uid() + group = helper.create_user_groups(username, uid) + user = helper.create_users(username, password, uid, group.backend_group) + group.add_user(user.backend_user) + new_users.append(username) + return new_users + + +class Command(BaseCommand): + + """ + Custom manage.py command to import users from the user backend. + + https://docs.djangoproject.com/en/1.8/howto/custom-management-commands/ + """ + + help = 'Import all users from the UserBackend defined in the config vars.' + + # def add_arguments(self, parser): + # Todo: make command more powerful, to import single users by username + # parser.add_argument('username', nargs='+', type=str) + + def handle(self, *args, **options): + new_users = import_users() + self.stdout.write("Successfully imported {} users:".format(len(new_users))) + for user in new_users: + self.stdout.write(user)