Skip to content

Commit

Permalink
Merge branch 'userimport-patch'
Browse files Browse the repository at this point in the history
  • Loading branch information
Christian Glatthard committed Aug 9, 2015
2 parents 349f9c6 + 65f7aee commit 2571d66
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 0 deletions.
25 changes: 25 additions & 0 deletions ipynbsrv/admin/admin.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
from django_admin_conf_vars.models import ConfigurationVariable
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.models import *
from ipynbsrv.core.management.commands import import_users


class CoreAdminSite(admin.AdminSite):
Expand Down Expand Up @@ -534,6 +536,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.
Expand All @@ -555,6 +561,25 @@ 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:
# 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)))
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.
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 2571d66

Please sign in to comment.