<commit_before>from .endpoint import Endpoint, api
from .exceptions import MissingRequiredFieldError
from .. import RequestFactory, UserItem, WorkbookItem, PaginationItem
from ..pager import Pager
import logging
import copy

logger = logging.getLogger('tableau.endpoint.users')


class Users(Endpoint):
    @property
    def baseurl(self):
        return "{0}/sites/{1}/users".format(self.parent_srv.baseurl, self.parent_srv.site_id)

    # Gets all users
    @api(version="2.0")
    def get(self, req_options=None):
        logger.info('Querying all users on site')
        url = self.baseurl
        server_response = self.get_request(url, req_options)
        pagination_item = PaginationItem.from_response(server_response.content, self.parent_srv.namespace)
        all_user_items = UserItem.from_response(server_response.content, self.parent_srv.namespace)
        return all_user_items, pagination_item

    # Gets 1 user by id
    @api(version="2.0")
    def get_by_id(self, user_id):
        if not user_id:
            error = "User ID undefined."
            raise ValueError(error)
        logger.info('Querying single user (ID: {0})'.format(user_id))
        url = "{0}/{1}".format(self.baseurl, user_id)
        server_response = self.get_request(url)
        return UserItem.from_response(server_response.content, self.parent_srv.namespace).pop()

    # Update user
    @api(version="2.0")
    def update(self, user_item, password=None):
        if not user_item.id:
            error = "User item missing ID."
            raise MissingRequiredFieldError(error)

        url = "{0}/{1}".format(self.baseurl, user_item.id)
        update_req = RequestFactory.User.update_req(user_item, password)
        server_response = self.put_request(url, update_req)
        logger.info('Updated user item (ID: {0})'.format(user_item.id))
        updated_item = copy.copy(user_item)
        return updated_item._parse_common_tags(server_response.content, self.parent_srv.namespace)

    # Delete 1 user by id
    @api(version="2.0")
    def remove(self, user_id):
        if not user_id:
            error = "User ID undefined."
            raise ValueError(error)
        url = "{0}/{1}".format(self.baseurl, user_id)
        self.delete_request(url)
        logger.info('Removed single user (ID: {0})'.format(user_id))

    # Add new user to site
    @api(version="2.0")
    def add(self, user_item):
        url = self.baseurl
        add_req = RequestFactory.User.add_req(user_item)
        server_response = self.post_request(url, add_req)
        new_user = UserItem.from_response(server_response.content, self.parent_srv.namespace).pop()
        logger.info('Added new user (ID: {0})'.format(user_item.id))
        return new_user

    # Get workbooks for user
    @api(version="2.0")
    def populate_workbooks(self, user_item, req_options=None):
        if not user_item.id:
            error = "User item missing ID."
            raise MissingRequiredFieldError(error)
<commit_msg>Fix logger statement in User.add (#608)<commit_after>from .endpoint import Endpoint, api
from .exceptions import MissingRequiredFieldError
from .. import RequestFactory, UserItem, WorkbookItem, PaginationItem
from ..pager import Pager
import logging
import copy

logger = logging.getLogger('tableau.endpoint.users')


class Users(Endpoint):
    @property
    def baseurl(self):
        return "{0}/sites/{1}/users".format(self.parent_srv.baseurl, self.parent_srv.site_id)

    # Gets all users
    @api(version="2.0")
    def get(self, req_options=None):
        logger.info('Querying all users on site')
        url = self.baseurl
        server_response = self.get_request(url, req_options)
        pagination_item = PaginationItem.from_response(server_response.content, self.parent_srv.namespace)
        all_user_items = UserItem.from_response(server_response.content, self.parent_srv.namespace)
        return all_user_items, pagination_item

    # Gets 1 user by id
    @api(version="2.0")
    def get_by_id(self, user_id):
        if not user_id:
            error = "User ID undefined."
            raise ValueError(error)
        logger.info('Querying single user (ID: {0})'.format(user_id))
        url = "{0}/{1}".format(self.baseurl, user_id)
        server_response = self.get_request(url)
        return UserItem.from_response(server_response.content, self.parent_srv.namespace).pop()

    # Update user
    @api(version="2.0")
    def update(self, user_item, password=<PASSWORD>):
        if not user_item.id:
            error = "User item missing ID."
            raise MissingRequiredFieldError(error)

        url = "{0}/{1}".format(self.baseurl, user_item.id)
        update_req = RequestFactory.User.update_req(user_item, password)
        server_response = self.put_request(url, update_req)
        logger.info('Updated user item (ID: {0})'.format(user_item.id))
        updated_item = copy.copy(user_item)
        return updated_item._parse_common_tags(server_response.content, self.parent_srv.namespace)

    # Delete 1 user by id
    @api(version="2.0")
    def remove(self, user_id):
        if not user_id:
            error = "User ID undefined."
            raise ValueError(error)
        url = "{0}/{1}".format(self.baseurl, user_id)
        self.delete_request(url)
        logger.info('Removed single user (ID: {0})'.format(user_id))

    # Add new user to site
    @api(version="2.0")
    def add(self, user_item):
        url = self.baseurl
        add_req = RequestFactory.User.add_req(user_item)
        server_response = self.post_request(url, add_req)
        new_user = UserItem.from_response(server_response.content, self.parent_srv.namespace).pop()
        logger.info('Added new user (ID: {0})'.format(new_user.id))
        return new_user

    # Get workbooks for user
    @api(version="2.0")
    def populate_workbooks(self, user_item, req_options=None):
        if not user_item.id:
            error = "User item missing ID."
            raise MissingRequiredFieldError(error)

{'fraction_non_alphanumeric': 0.11301312946651156, 'fraction_numerical': 0.009140767824497258, 'mean_word_length': 3.0744752877454298, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4550838', 'n_tokens_mistral': 1884, 'n_tokens_neox': 1849, 'n_words': 401}