Ejemplo n.º 1
0
    def filter_add_user(cls, info, difference, trim):
        """
        A method that retrieves users' info using the difference.
        """
        def normalize(info):
            """
            A function that normalizes retrieved information from Slack.
            """
            user_dict = {}
            not_user_list = []

            for item in info:
                if 'deleted' in item and item['deleted'] is True:
                    not_user_list.append(item['id'])
                    continue
                if 'is_bot' in item and item['is_bot'] is True:
                    not_user_list.append(item['id'])
                    continue
                if 'email' not in item['profile'] or item['profile'].get('email') is None:
                    not_user_list.append(item['id'])
                    continue

                if not item['profile']['email'].endswith(settings.DOMAIN_LIST):
                    not_user_list.append(item['id'])
                    continue

                firstname, lastname = re.match(
                    '^([\w-]+)[.]{0,1}([\w-]+){0,1}@',
                    item['profile']['email']).groups()
                lastname = '' if lastname is None else lastname
                user_dict[item['id']] = {
                    'slack_id': item['id'],
                    'email': item['profile']['email'],
                    # use slack default image
                    'photo': item['profile'].get('image_original', item['profile'].get('image_192')),
                    'firstname': firstname.title(),
                    'lastname': lastname.title()
                }

            return user_dict, not_user_list

        info, not_user = normalize(info)

        difference = cls.get_real_users(difference[:], not_user)

        if len(difference) == 0:
            return "Users list not changed"

        if trim:
            return cls.trim_away(not_user)

        try:
            for user in difference:
                if user in info:
                    with transaction.atomic():
                        SlackUser.create(info[user])
            return "Users updated successfully"
        except Exception as e:
            return "Users couldn't be updated successfully - %s" % e.message
Ejemplo n.º 2
0
def create_user():
    """
    Creates a user.

    :Returns: SlackUser object
    """
    user_dummy_data = {
        'slack_id': 'UX03131',
        'firstname': 'Test',
        'lastname': 'User',
        'email': '*****@*****.**',
        'photo': 'inexistent_photo.jpg'
    }
    return SlackUser.create(user_dummy_data)
Ejemplo n.º 3
0
def create_user():
    """
    Creates a user.

    :Returns: SlackUser object
    """
    user_dummy_data = {
        "slack_id": "UX03131",
        "firstname": "Test",
        "lastname": "User",
        "email": "*****@*****.**",
        "photo": "inexistent_photo.jpg",
    }
    return SlackUser.create(user_dummy_data)
Ejemplo n.º 4
0
def create_user():
    """
    Creates a user.

    :Returns: SlackUser object
    """
    user_dummy_data = {
        'slack_id': 'UX03131',
        'firstname': 'Test',
        'lastname': 'User',
        'email': '*****@*****.**',
        'photo': 'inexistent_photo.jpg'
    }
    return SlackUser.create(user_dummy_data)
Ejemplo n.º 5
0
 def _add_new_users(cls, users_to_be_added, valid_users):
     for user_slack_id in users_to_be_added:
         current_user = valid_users[user_slack_id]
         SlackUser.create(current_user)
Ejemplo n.º 6
0
    def filter_add_user(cls, info, difference, trim):
        """
        A method that retrieves users' info using the difference.
        """
        def normalize(info):
            """
            A function that normalizes retrieved information from Slack.
            """
            user_dict = {}
            not_user_list = []

            for item in info:
                if 'deleted' in item and item['deleted'] is True:
                    not_user_list.append(item['id'])
                    continue
                if 'is_bot' in item and item['is_bot'] is True:
                    not_user_list.append(item['id'])
                    continue
                if 'email' not in item['profile'] or item['profile'].get(
                        'email') is None:
                    not_user_list.append(item['id'])
                    continue

                if not item['profile']['email'].endswith(settings.DOMAIN_LIST):
                    not_user_list.append(item['id'])
                    continue

                firstname = item.get('profile').get('first_name', '')
                lastname = item.get('profile').get('last_name', '')
                user_dict[item['id']] = {
                    'slack_id':
                    item['id'],
                    'email':
                    item['profile']['email'],
                    # use slack default image
                    'photo':
                    item['profile'].get('image_original',
                                        item['profile'].get('image_192')),
                    'firstname':
                    firstname.title(),
                    'lastname':
                    lastname.title()
                }

            return user_dict, not_user_list

        info, not_user = normalize(info)

        difference = cls.get_real_users(difference[:], not_user)

        if len(difference) == 0:
            return "Users list not changed"

        if trim:
            return cls.trim_away(not_user)

        try:
            for user in difference:
                if user in info:
                    with transaction.atomic():
                        SlackUser.create(info[user])
            return "Users updated successfully"
        except Exception as e:
            return "Users couldn't be updated successfully - %s" % e.message