Source

thot / bin / ldap_import.py

Full commit
#!/usr/bin/env python
#coding: utf-8

"""
Import ldap accounts in the application database
"""

import sys
import os


def main(argv=None):

    if not argv:
        argv = sys.argv

    if len(argv) != 3:
        sys.stderr.write('This script requires 2 arguments')

    name = argv[0]
    sys.path[0] = argv[1]
    os.environ['DJANGO_SETTINGS_MODULE'] = argv[2]

    return ldap_import()

def ldap_import():

    import datetime

    from thot.log import logger
    from thot.registration.models import ActivityLogger
    from thot.utils import ldaplib

    ldap_users = ldaplib.all_users()
    logger.debug(ldap_users)

    now = datetime.datetime.now()

    for ldap_user in ldap_users:
        try:
            user = ActivityLogger.objects.get(username=ldap_user['username'][0])
            logger.debug('User %s already created' % ldap_user['username'][0])
        except ActivityLogger.DoesNotExist:
            user = ActivityLogger(username=ldap_user['username'][0],
                email=ldap_user['email'][0],
                last_name=ldap_user['last_name'][0],
                first_name=ldap_user['first_name'][0],
                is_staff=False, is_superuser=False, is_active=True,
                last_login=now, date_joined=now
            )
            user.set_unusable_password()
            user.save()
            logger.info('Creation of user with login %s'
                % ldap_user['username'][0])


if __name__ == '__main__':
    sys.exit(main())