Beispiel #1
0
    def handle(self, *args, **options):
        verbose = int(options['verbosity']) > 1
        if HAS_CKAN:  # Prevent user to be automatically created
            post_save.disconnect(sync_ckan_on_save, sender=User, dispatch_uid="youckan.ckan.sync_user")

        users = User.objects.filter(slug__in=args) if args else User.objects.all()
        if verbose:
            self.stdout.write('{0} user(s) to process'.format(users.count()))
        for user in users:
            if verbose:
                self.stdout.write('-> Processing user "{0}"'.format(user.full_name))
            if HAS_CKAN:  # Store CKAN id before slug change
                try:
                    response = client.action('user_show', {'id': user.slug})
                except:
                    self.stderr.write('Unable to fetch CKAN user for "{0}"'.format(user.full_name))
                    continue
                if not response.get('success'):
                    self.stderr.write('Unable to fetch CKAN user for "{0}"'.format(user.full_name))
                    continue
                ckan_user_id = response['result']['id']

            # Trigger slug update
            user.slug = None
            user.save()

            if HAS_CKAN:
                response = client.action('user_update', {
                    'id': ckan_user_id,
                    'name': user.slug,
                    'email': user.email,
                    'fullname': user.full_name,
                    'about': user.profile.about,
                    'sysadmin': user.is_superuser,
                })
Beispiel #2
0
 def update_ckan_user(self, user, ckan_id):
     response = client.action('user_update', {
         'id': ckan_id,
         'name': user.slug,
         'email': user.email,
     })
     return response['success']
Beispiel #3
0
def sync_ckan_user(email):
    logger.info('Synchronizing user %s with CKAN', email)
    try:
        user = User.objects.get(email=email)
    except User.DoesNotExists:
        logger.error('User %s does not exists', email)
        return

    if not user.is_active:
        logger.debug('Skipping inactive user %s', user.email)

    ckan_user_id = None
    try:
        response = client.action('user_show', {'id': user.slug})
        if response['success']:
            ckan_user_id = response['result']['id']
    except HTTPError as error:
        if error.response.status_code != 404:
            raise

    if ckan_user_id:
        response = client.action('user_update', {
            'id': ckan_user_id,
            'name': user.slug,
            'email': user.email,
            'fullname': user.full_name,
            'about': user.profile.about,
            'sysadmin': user.is_superuser,
        })
    else:
        response = client.action('user_create', {
            'name': user.slug,
            'email': user.email,
            'password': '******',
            'fullname': user.full_name,
            'about': user.profile.about,
            'sysadmin': user.is_superuser,
        })

    if response['success']:
        logger.info('Synchronized CKAN user %s', user.slug)
    else:
        logger.error('Failed to synchre CKAN user %s: %s', user.slug, response['error']['message'])
        raise Exception(response['error']['message'])
Beispiel #4
0
    def handle(self, *args, **options):
        # Prevent user to be automatically created
        post_save.disconnect(sync_ckan_on_save, sender=User, dispatch_uid="youckan.ckan.sync_user")

        update_ckan_user = options['update']
        notify = options['notify']

        response = client.action('user_list', timeout=60)
        for userdata in response['result']:
            try:
                user = self.create_user(userdata)
            except Exception as e:
                self.stdout.write('Error creating user {0}: {1}'.format(userdata['id'], e))
                user = None
            ckan_id = userdata['id']
            if not user:
                self.stdout.write('Skipping user {id}'.format(**userdata))
                continue
            if update_ckan_user:
                self.update_ckan_user(user, ckan_id)
            if notify:
                self.stdout.write('Sending reset password mail to {0}'.format(user.email))
                mail.reset_password(user)