Skip to content

Commit

Permalink
import_user added as manage.py command
Browse files Browse the repository at this point in the history
  • Loading branch information
Christian Glatthard committed Aug 9, 2015
1 parent 7d472d8 commit 65f7aee
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 20 deletions.
26 changes: 6 additions & 20 deletions ipynbsrv/admin/admin.py
Original file line number Diff line number Diff line change
@@ -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):
Expand Down Expand Up @@ -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'))
Expand Down
1 change: 1 addition & 0 deletions ipynbsrv/core/management/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
__import__('pkg_resources').declare_namespace(__name__)
1 change: 1 addition & 0 deletions ipynbsrv/core/management/commands/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
__import__('pkg_resources').declare_namespace(__name__)
49 changes: 49 additions & 0 deletions ipynbsrv/core/management/commands/import_users.py
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit 65f7aee

Please sign in to comment.