<commit_before>#!/usr/bin/env python

import urllib
import json
import csv
from datetime import date, timedelta

class Admin:
    '''A class of tools for administering AGO Orgs or Portals'''
    def __init__(self, username, portal=None, password=None):
        from . import User
        self.user = User(username, portal, password)

    def __users__(self, start=0):
        '''Retrieve a single page of users.'''
        parameters = urllib.urlencode({'token' : self.user.token,
                                       'f' : 'json',
                                       'start' : start,
                                       'num' : 100})
        portalId = self.user.__portalId__()
        response = urllib.urlopen(self.user.portalUrl + '/sharing/rest/portals/' + portalId + '/users?' + parameters).read()
        users = json.loads(response)
        return users

    def getUsers(self):
        ''' Returns a list of all users in the organization (requires admin access).'''
        allUsers = []
        users = self.__users__()
        for user in users['users']:
            allUsers.append(user)
        while users['nextStart'] > 0:
            users = self.__users__(users['nextStart'])
            for user in users['users']:
                allUsers.append(user)
        return allUsers
    
    def getNewUsers(self, daysToCheck):
            '''
            REQUIRES ADMIN ACCESS
            Add new organization users to multiple groups and return a list of the status.
            '''
            # daysToCheck is the time interval to check for new users.
            # e.g. 1 will check past day, 7 will check past week, etc.
            
            users = self.getUsers()
            
            # Create a list of all new users (joined in the last 'daysToCheck' days).
            newUsers = []
            for user in users:
                if date.fromtimestamp(float(user['created'])/1000) > date.today()-timedelta(days=daysToCheck):
                    newUsers.append(user)
                    
            return newUsers
    
    def addUsersToGroups(self, users, groups):
            '''
            REQUIRES ADMIN ACCESS
            Add organization users to multiple groups and return a list of the status
            '''
            # Provide one or more usernames in a list.
            # e.g. ['user_1', 'user_2']
            # Provide one or more group IDs as strings in a list.
            # e.g. ['d93aabd856f8459a8905a5bd434d4d4a', 'f84c841a3dfc4591b1ff83281ea5025f']
            
            toolSummary = []
            
            # Assign users to the specified group(s).
            parameters = urllib.urlencode({'token': self.user.token, 'f': 'json'})
            for groupID in groups:
                userSummary = []
                for user in users:
                    print 'Attempting to add ' + user + ' to groupID ' + groupID
                    # Add Users - REQUIRES POST method (undocumented operation as of 2013-07-10).
                    response = urllib.urlopen(self.user.portalUrl + '/sharing/rest/community/groups/' + groupID + '/addUsers?', 'users=' + user + "&" + parameters).read()
                    responseKeys = json.loads(response).keys() # Returns a list of the keys from the response JSON.
                    # Will likely report a false-negative.
                    userSummary.append({user: responseKeys[0]})
                toolSummary.append({groupID: userSummary})
    
            return toolSummary

    def reassignAllUser1ItemsToUser2(self, userFrom, userTo):
        '''
        REQUIRES ADMIN ACCESS
        Transfers ownership of all items in userFrom/User1's account to userTo/User2's account, keeping same folder names.
        - Does not check for existing folders in userTo's account.
        - Does not delete content from userFrom's account.
        '''

        # request user content for userFrom
        # response contains list of items in root folder and list of all folders
        parameters = urllib.urlencode({'token': self.user.token, 'f': 'json'})
        request = self.user.portalUrl + '/sharing/rest/content/users/' + userFrom + '?' + parameters
        userContent = json.loads(urllib.urlopen(request).read())

        # create same folders in userTo's account like those in userFrom's account
        for folder in userContent['folders']:
            parameters2 = urllib.urlencode({'title' : folder['title'], 'token': self.user.token, 'f': 'json'})
            request2 = self.user.portalUrl + '/sharing/rest/content/users/' + userTo + '/createFolder?'
            response2 = urllib.urlopen(request2, parameters2).read()   # requires POST

        # keep track of items and folders<commit_msg>Changed addUsersToGroups to make one request per group<commit_after>#!/usr/bin/env python

import urllib
import json
import csv
from datetime import date, timedelta

class Admin:
    '''A class of tools for administering AGO Orgs or Portals'''
    def __init__(self, username, portal=None, password=None):
        from . import User
        self.user = User(username, portal, password)

    def __users__(self, start=0):
        '''Retrieve a single page of users.'''
        parameters = urllib.urlencode({'token' : self.user.token,
                                       'f' : 'json',
                                       'start' : start,
                                       'num' : 100})
        portalId = self.user.__portalId__()
        response = urllib.urlopen(self.user.portalUrl + '/sharing/rest/portals/' + portalId + '/users?' + parameters).read()
        users = json.loads(response)
        return users

    def getUsers(self):
        ''' Returns a list of all users in the organization (requires admin access).'''
        allUsers = []
        users = self.__users__()
        for user in users['users']:
            allUsers.append(user)
        while users['nextStart'] > 0:
            users = self.__users__(users['nextStart'])
            for user in users['users']:
                allUsers.append(user)
        return allUsers
    
    def getNewUsers(self, daysToCheck):
            '''
            REQUIRES ADMIN ACCESS
            Add new organization users to multiple groups and return a list of the status.
            '''
            # daysToCheck is the time interval to check for new users.
            # e.g. 1 will check past day, 7 will check past week, etc.
            
            users = self.getUsers()
            
            # Create a list of all new users (joined in the last 'daysToCheck' days).
            newUsers = []
            for user in users:
                if date.fromtimestamp(float(user['created'])/1000) > date.today()-timedelta(days=daysToCheck):
                    newUsers.append(user)
                    
            return newUsers
    
    def addUsersToGroups(self, users, groups):
            '''
            REQUIRES ADMIN ACCESS
            Add organization users to multiple groups and return a list of the status
            '''
            # Provide one or more usernames in a list.
            # e.g. ['user_1', 'user_2']
            # Provide one or more group IDs in a list.
            # e.g. ['d93aabd856f8459a8905a5bd434d4d4a', 'f84c841a3dfc4591b1ff83281ea5025f']
            
            toolSummary = []
            
            # Assign users to the specified group(s).
            parameters = urllib.urlencode({'token': self.user.token, 'f': 'json'})
            for group in groups:
                # Add Users - REQUIRES POST method (undocumented operation as of 2013-11-12).
                response = urllib.urlopen(self.user.portalUrl + '/sharing/rest/community/groups/' + group + '/addUsers?', 'users=' + ','.join(users) + "&" + parameters).read()
                # Users not added will be reported back with each group.
                toolSummary.append({groupID: json.loads(response)})
    
            return toolSummary

    def reassignAllUser1ItemsToUser2(self, userFrom, userTo):
        '''
        REQUIRES ADMIN ACCESS
        Transfers ownership of all items in userFrom/User1's account to userTo/User2's account, keeping same folder names.
        - Does not check for existing folders in userTo's account.
        - Does not delete content from userFrom's account.
        '''

        # request user content for userFrom
        # response contains list of items in root folder and list of all folders
        parameters = urllib.urlencode({'token': self.user.token, 'f': 'json'})
        request = self.user.portalUrl + '/sharing/rest/content/users/' + userFrom + '?' + parameters
        userContent = json.loads(urllib.urlopen(request).read())

        # create same folders in userTo's account like those in userFrom's account
        for folder in userContent['folders']:
            parameters2 = urllib.urlencode({'title' : folder['title'], 'token': self.user.token, 'f': 'json'})
            request2 = self.user.portalUrl + '/sharing/rest/content/users/' + userTo + '/createFolder?'
            response2 = urllib.urlopen(request2, parameters2).read()   # requires POST

        # keep track of items and folders
{'fraction_non_alphanumeric': 0.10055743797136299, 'fraction_numerical': 0.0151929172587168, 'mean_word_length': 1.9411764705882353, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4747898', 'n_tokens_mistral': 2569, 'n_tokens_neox': 2427, 'n_words': 762}