예제 #1
0
    def get_group_data_permissions(self, group_id, tool_id):
        """
        TEMPRORARY DOESN'T WORK

        TO DO Method don't return anything
        Ver.1 - Get data by group
        Q:Why to put tool_id if data models_managers is fully defined by group_id?

        :param group_id:
        :type group_id:
        :param tool_id:
        :type tool_id:
        :return:
        :rtype:
        """
        # Validate inputs
        tool_id = _get_id_or_err(tool_id, 'tool_id')
        group_id = _get_id_or_err(group_id, 'group_id')

        # Get objects
        tool = a_m.get_tool_by_id(self.ssn, tool_id)
        if tool is None:
            raise ex.NotExistsError('Tool', 'id', tool_id)

        group = a_m.get_user_group_by_id(self.ssn, group_id)
        if group is None:
            raise ex.NotExistsError('UserGroup', 'id', group_id)

        return a_m.get_data_permission(group_id)
예제 #2
0
    def add_user_to_group(self, user_id, group_id):
        """
        TEMPRORARY DOESN'T WORK

        Add user to group

        :param user_id:
        :type user_id:
        :param group_id:
        :type group_id:
        :return:
        :rtype:
        """
        # Validate inputs
        user_id = _get_id_or_err(user_id, 'user_id')
        group_id = _get_id_or_err(group_id, 'group_id')

        # Get objects
        user = a_m.get_user_by_id(self.ssn, user_id)
        if user is None:
            raise ex.NotExistsError('User', 'id', user_id)
        group = a_m.get_user_group_by_id(self.ssn, group_id)
        if group is None:
            raise ex.NotExistsError('UserGroup', 'id', group_id)

        if user in group.users:
            raise ex.AlreadyExistsError('UserGroup', 'users', user.id)

        return group.users.append(user)
예제 #3
0
def jj_extract(wb, meta_cols, data_cols, dates_cols):
    ws = wb.sheet_by_index(0)
    if ws.nrows <= 1:
        raise ex.EmptyInputsError('jj_extract')
    header_row = ws.row(0)
    last_col = get_last_col(ws, header_row)
    # Init headers cols: names
    for key, val in meta_cols.items():
        if key >= last_col:
            raise ex.NotExistsError('DataProcessing', 'column', key)
        if val == '':
            meta_cols[key] = header_row[key].value
        new_val = meta_cols[key]
    for key, val in data_cols.items():
        if key >= last_col:
            raise ex.NotExistsError('DataProcessing', 'column', key)
        if val == '':
            data_cols[key] = header_row[key].value
        new_val = data_cols[key]
    for key, val in dates_cols.items():
        if key >= last_col:
            raise ex.NotExistsError('DataProcessing', 'column', key)
    # Create output: Append data
    output = []
    data = get_cell_range(0, 0, ws.ncols, ws.nrows, ws)
    for row_index in range(1, ws.nrows):
        new_row = {'meta': {}, 'data': {}, 'dates': 0}
        for key, val in meta_cols.items():
            new_row['meta'][val] = data[row_index][key].value
        for key, val in data_cols.items():
            new_row['data'][val] = data[row_index][key].value
        new_row['dates'] = date_func(dates_cols, data[row_index])
        output.append(new_row)
    return output
예제 #4
0
    def add_role(self, name, tool_id):
        """
        Add role is correct
        :param name:
        :type name:
        :param tool_id:
        :type tool_id:
        :return:
        :rtype:
        """
        # Validate inputs
        name = _get_str_or_err(name, 'name')
        #tool_id = _get_id_or_err(tool_id, 'tool_id')

        # Get Objects
        tool = a_m.get_tool_by_id(self.ssn, tool_id)
        if tool is None:
            raise ex.NotExistsError('Tool', 'id', tool_id)

        existing = [role for role in tool.roles if role.name == name]
        if existing is not None and len(existing) > 0:
            raise ex.AlreadyExistsError('Role', 'name', name)

        new_role = self._add_role(self.ssn, name)
        # a_m.add_role_to_tool(self.ssn, new_role, tool)
        tool.roles.append(new_role)
        return new_role
예제 #5
0
    def add_group(self, name, tool_id):
        """
        Add user group to Tool

        :param name:
        :type name:
        :param tool_id:
        :type tool_id:
        :return:
        :rtype:
        """
        # Validate inputs
        name = _get_str_or_err(name, 'name')
        tool_id = _get_id_or_err(tool_id, 'tool_id')

        # Get Objects
        tool = a_m.get_tool_by_id(self.ssn, tool_id)
        if tool is None:
            raise ex.NotExistsError('Tool', 'id', tool_id)

        existing = [ug for ug in tool.user_groups if ug.name == name]
        #  TODO Check
        if existing is not None and isinstance(existing, list):
            raise ex.AlreadyExistsError('UserGroup', 'name', name)

        return self._add_user_group(self.ssn, name, tool)
예제 #6
0
    def add_permission(self, tool_id, node_type, name, parent_id=None):
        tool_id = _get_id_or_err(tool_id, 'tool_id')
        node_type = _get_str_or_err(node_type, 'node_type')  # Defined string
        name = _get_str_or_err(name, 'name')

        tool = a_m.get_tool_by_id(self.ssn, tool_id)
        if tool is None:
            raise ex.NotExistsError('Tool', 'id', tool_id)

        parent = None
        if parent_id is not None:
            parent_id = _get_id_or_err(parent_id, 'parent_id')
            parent = a_m.get_perm_node_in_tool(self.ssn, parent_id, tool)
            if parent is None:
                raise ex.NotExistsError('PermNode', 'id', parent_id)

        return a_m.add_perm_node(self.ssn, tool, node_type, name, parent)
예제 #7
0
    def add_permission_value(self, tool_id, perm_node_id, value, user_id):
        tool_id = _get_id_or_err(tool_id, 'tool_id')
        perm_node_id = _get_id_or_err(perm_node_id, 'perm_node_id')
        user_id = _get_id_or_err(user_id, 'user_id')

        tool = a_m.get_tool_by_id(self.ssn, tool_id)
        if tool is None:
            raise ex.NotExistsError('Tool', 'id', tool_id)

        user = a_m.get_user_by_id(self.ssn, user_id)
        if user is None:
            raise ex.NotExistsError('User', 'id', user_id)

        perm_node = a_m.get_perm_node_in_tool(self.ssn, perm_node_id, tool)
        if perm_node is None:
            raise ex.NotExistsError('PermNode', 'id', perm_node_id)

        return a_m.add_perm_value(self.ssn, tool, perm_node, value, user)
예제 #8
0
    def update_role_features(self, role_id, features_id):
        """
        Update role by given list of features.
        Delete non common features, and add new

        :param role_id:
        :type role_id:
        :param features_id:
        :type features_id:
        :return:
        :rtype:
        """
        # Validate inputs
        role_id = _get_id_or_err(role_id, 'id')
        if features_id is None or not isinstance(features_id, list):
            raise ex.WrongArgEx('features_id', features_id)

        # Get Objects
        role = a_m.get_role_by_id(self.ssn, role_id)
        if role is None:
            raise ex.NotExistsError('Role', 'id', role_id)
        new_features = []
        for fid in features_id:
            f = a_m.get_feature_by_id(self.ssn, fid)
            if f is not None:
                new_features.append(f)
            else:
                raise ex.NotExistsError('Feature', 'id', fid)

        old_f = [x.id for x in role.features]
        new_f = features_id

        to_keep = set(old_f).intersection(new_f)
        # Delete
        if len(old_f) > len(to_keep):
            delete_ids = set(old_f) - set(to_keep)
            self._del_features_from_role(self.ssn, role, delete_ids)
        # Add
        if len(new_f) > len(to_keep):
            add = set(new_f) - set(to_keep)
            to_add_features = [x for x in new_features if x.id in add]
            self._add_features_to_role(self.ssn, role, to_add_features)

        return role.features
예제 #9
0
    def get_permissions(self, tool_id, user_id):
        tool_id = _get_id_or_err(tool_id, 'tool_id')
        user_id = _get_id_or_err(user_id, 'user_id')

        tool = wha.get_tool_by_id(self.ssn, tool_id)
        user = wha.get_user_by_id(self.ssn, user_id)

        if tool is None:
            raise ex.NotExistsError('Tool', 'id', tool_id)
        if user is None:
            raise ex.NotExistsError('User', 'id', user_id)

        # default_perms = wha.get_perms_to_tool(ssn, tool)
        # wha.get_user_perms_to_tool(sess, tool, user),

        return {
            'permissions': wha.get_perms_to_tool(self.ssn, tool, user),
            'features': wha.get_user_features_to_tool(self.ssn, tool, user)
        }
예제 #10
0
    def init_user_wb(self, user_id, tool_id):

        # Validate inputs

        tool_id = _get_id_or_err(tool_id, 'tool_id')
        user_id = _get_id_or_err(user_id, 'user_id')

        # Get Objects
        tool = a_m.get_tool_by_id(self.ssn, tool_id)
        if tool is None:
            raise ex.NotExistsError('Tool', 'id', tool_id)
        user = a_m.get_user_by_id(self.ssn, user_id)
        if user is None:
            raise ex.NotExistsError('User', 'id', user_id)

        # Version 2
        # Get raw data from table with masks
        raw_perm_values = a_m.get_raw_perm_values_for_user(
            self.ssn, tool, None)

        # Copy/Insert this raw data for specified user
        # TODO - check if there is no data for this user!!!
        for per_v in raw_perm_values:
            perm_node = per_v.perm_node
            a_m.add_perm_value(self.ssn, tool, perm_node, per_v.value, user)

        # TODO Fix bug with initialize db
        # # Get recently created permissions for user
        iaccess = _IAccess(ssn=self.ssn)
        u_perms = iaccess.get_permissions(tool_id, user_id)

        # self.istorage.backup.save(user_id, tool_id, u_perms, 'models_managers')

        # # Make backup file
        # dir_path = os.path.dirname(os.path.realpath(__file__))
        # file_name = "backup_tool_" + str(tool_id) + "_user_" \
        #             + str(user_id) + ".json"
        # file_path = os.path.join(dir_path, file_name)
        # with open(file_path, "w+") as backup_file:
        #     json.dump(u_perms, backup_file)

        pass  # TODO Confirm This Realization
예제 #11
0
    def get_user_roles(self, user_id):
        """
        Return Users Role's

        :param user_id:
        :type user_id:
        :return:
        :rtype:
        """
        user_id = _get_id_or_err(user_id, 'user_id')
        user = a_m.get_user_by_id(self.ssn, user_id)
        if user is None:
            raise ex.NotExistsError('User', 'id', user_id)

        return user.roles
예제 #12
0
    def update_user_data_permissions(self, tool_id, user_id, permissions):
        """
        TEMPRORARY DOESN'T WORK
        :param tool_id:
        :type tool_id:
        :param user_id:
        :type user_id:
        :param permissions:
        :type permissions:
        :return:
        :rtype:
        """
        #TODO

        # Validate inputs
        tool_id = _get_id_or_err(tool_id, 'user_id')
        user_id = _get_id_or_err(user_id, 'user_id')

        # Get objects
        tool = a_m.get_tool_by_id(self.ssn, tool_id)
        if tool is None:
            raise ex.NotExistsError('Tool', 'id', tool_id)
        user = a_m.get_user_by_id(self.ssn, user_id)
        if user is None:
            raise ex.NotExistsError('User', 'id', user_id)

        # TODO Validating paths for nodes

        # Delete previous permissions for user
        a_m.del_perm_values_for_user(self.ssn, tool, user)

        # Get objects
        storage = {}
        permissions = sorted(permissions, key=lambda p: len(p['path']))
        for perm in permissions:
            self._add_permission(tool, storage, perm, user)
예제 #13
0
    def get_feature(self, **kwargs):
        """
        Args:
            **kwargs: id|name & tool_id
        """
        if 'id' in kwargs:
            feature_id = _get_id_or_err(kwargs['id'], 'id')
            return a_m.get_feature_by_id(self.ssn, feature_id)

        elif 'name' in kwargs and 'tool_id' in kwargs:
            name = _get_str_or_err(kwargs['name'], 'name')
            tool_id = _get_id_or_err(kwargs['tool_id'], 'tool_id')
            tool = a_m.get_tool_by_id(self.ssn, tool_id)
            if tool is None:
                raise ex.NotExistsError('Tool', 'id', tool_id)
            return a_m.get_feature_by_name_in_tool(self.ssn, name, tool)
예제 #14
0
    def get_role_features(self, role_id):
        """
        Return Features related to Role

        :param role_id:
        :type role_id:
        :return:
        :rtype:
        """
        # Validate inputs
        role_id = _get_id_or_err(role_id, 'id')

        # Get Objects
        role = a_m.get_role_by_id(self.ssn, role_id)
        if role is None:
            raise ex.NotExistsError('Role', 'id', role_id)

        return role.features
예제 #15
0
    def get_features(self, tool_id):
        """
        Return features related to given tool

        :param tool_id:
        :type tool_id:
        :return:
        :rtype:
        """
        # Validate inputs
        #tool_id = _get_id_or_err(tool_id, 'tool_id')

        # Get Objects
        tool = a_m.get_tool_by_id(self.ssn, tool_id)
        if tool is None:
            raise ex.NotExistsError('Tool', 'id', tool_id)

        return a_m.get_features_by_tool(self.ssn, tool)
예제 #16
0
    def get_users(self, tool_id=None):
        """
        Return user object by given tool

        :param tool_id:
        :type tool_id:
        :return:
        :rtype:
        """
        # Validate inputs & Get object
        if tool_id is not None:
            tool_id = _get_id_or_err(tool_id, 'tool_id')
            tool = a_m.get_tool_by_id(self.ssn, tool_id)
            if tool is None:
                raise ex.NotExistsError('Tool', 'id', tool_id)

            return a_m.get_users_by_tool(self.ssn, tool)

        return a_m.get_all_users(self.ssn)
예제 #17
0
    def get_group_users(self, group_id):
        """

        Return Get User of Specific Group

        :param group_id:
        :type group_id:
        :return:
        :rtype:
        """
        # Validate inputs
        group_id = _get_id_or_err(group_id, 'group_id')

        # Get objects
        group = a_m.get_user_group_by_id(self.ssn, group_id)
        if group is None:
            raise ex.NotExistsError('UserGroup', 'id', group_id)

        return group.users
예제 #18
0
    def add_user(self, email, password, roles_id=None):
        # Validate inputs
        #email = _get_str_or_err(email, 'email')
        #password = _get_str_or_err(password, 'password')  # TODO Hashing Pass

        # Get Objects
        roles = []
        if roles_id is not None and isinstance(roles_id, list):
            for role_id in roles_id:
                role_id = _get_id_or_err(role_id, 'id')
                role = a_m.get_role_by_id(self.ssn, role_id)
                if role is None:
                    raise ex.NotExistsError('Role', 'id', role_id)
                else:
                    roles.append(role)

        existing = a_m.get_user_by_email(self.ssn, email)
        if existing is not None:
            raise ex.AlreadyExistsError('User', 'email', email)

        return self._add_user(self.ssn, email, password, roles)
예제 #19
0
    def set_permissions_template(self, tool_id, template):
        # Validate inputs
        tool_id = _get_id_or_err(tool_id, 'tool_id')
        if 'permissions' not in template or \
                not isinstance(template['permissions'], list):
            raise ex.WrongArgEx('permissions', template.get('permissions'))

        if 'features' not in template or \
                not isinstance(template['features'], list):
            raise ex.WrongArgEx('features', template.get('features'))

        feats = template['features']
        perms = template['permissions']

        # Get objects
        tool = a_m.get_tool_by_id(self.ssn, tool_id)
        if tool is None:
            raise ex.NotExistsError('Tool', 'id', tool_id)

        # Create & validate features dict for adding
        features_to_add = []
        for _f in feats:
            features_to_add.append({'name': _f.get('name')})

        # Add permissions
        perms = sorted(perms, key=lambda perm: len(perm['path']))
        storage = {}
        for perm in perms:
            self._add_permission(tool, storage, perm)

        # Add default features for tool
        if len(features_to_add) > 0:
            for f in features_to_add:
                a_m.add_feature(self.ssn, f['name'], tool)

        return None
예제 #20
0
def jj_brand_extract(warehouse, wb, options_list):
    # t1 = datetime.datetime.now()

    meta_cols = options_list['meta_cols']
    data_cols = options_list['data_cols']
    dates_info = options_list['dates_cols']
    date_func = options_list['date_func']
    date_col = dates_info['date_col']
    series_name = dates_info['scale']
    ws = wb.sheet_by_index(0)
    if ws.nrows <= 1:
        raise ex.EmptyInputsError('jj_extract')
    data = get_cell_range(0, 0, ws.ncols, ws.nrows, ws)
    header_row_index = 0
    last_col = get_last_col(data, header_row_index)
    # Init headers cols: names
    for item in meta_cols:
        column_number = item['Col_number']
        if column_number >= last_col:
            raise ex.NotExistsError('DataProcessing', 'column', column_number)
        if item['Dimension_name'] == '':
            item['Dimension_name'] = data[0][column_number].value
    for key, val in data_cols.items():
        if key[0] >= last_col:
            raise ex.NotExistsError('DataProcessing', 'column', key)
        if val == '':
            data_cols[key] = data[0][key[0]].value
    if date_col >= last_col:
        raise ex.NotExistsError('DataProcessing', 'column', key[0])
    # Create output: Append data
    if 'mapping_rule' in options_list:
        mapping_rule = options_list['mapping_rule']
    else:
        mapping_rule = None
    date_values = []
    for row_index in range(1, len(data)):
        date_values.append(data[row_index][date_col].value)
    time_line = get_time_line(date_values)
    times_series = warehouse.add_time_scale(series_name, time_line)
    for row_index in range(1, len(data)):
        # print(row_index)
        meta = []
        for item in meta_cols:
            copy_item = item.copy()
            column_index = copy_item['Col_number']
            copy_item['Name'] = data[row_index][column_index].value
            meta.append(copy_item)
        if mapping_rule is not None:
            new_meta, is_mapped = mapping(meta, mapping_rule)
            if is_mapped:
                meta = new_meta
        num_of_dates = 1
        date_value = data[row_index][date_col].value
        start_label = date_func(date_value, num_of_dates)
        path = [x['Name'] for x in meta]
        item_meta = [(x['Dimension_name'], x['Layer']) for x in meta]
        entity = warehouse.add_entity(path, item_meta)
        for key, val in data_cols.items():
            value = data[row_index][key[0]].value
            value = convert_value(value, key[1])
            variable = warehouse.force_ent_variable(entity, val, key[1])
            time_series = warehouse.force_var_time_series(
                variable, times_series)
            history_value = time_series.get_value(start_label)
            if not history_value:
                new_value = [value]
            else:
                new_value = [history_value + value]
            time_series.set_values(start_label, new_value)
예제 #21
0
def jj_oral_care_media_spend(warehouse, wb, options_list):
    ws = wb.sheet_by_index(0)
    data = get_cell_range(0, 0, ws.ncols, ws.nrows, ws)
    meta_cols = options_list['meta_cols']
    date_func = options_list['date_func']
    header_row_index = options_list['info']['header_row']
    name_col = options_list['name_col']
    if 'map_names' in options_list:
        has_map_names = True
        map_names = options_list['map_names']
    else:
        has_map_names = False
    series_name = options_list['dates_info']['scale']
    start_data_row = options_list['info']['data_row']
    start_date_col = options_list['dates_info']['start_column']
    last_col = options_list['dates_info']['end_column']
    if last_col == '':
        last_col = get_last_col(data, header_row_index)
    # Init headers cols: names
    for item in meta_cols:
        column_number = item['Col_number']
        if column_number >= last_col:
            raise ex.NotExistsError('DataProcessing', 'column', column_number)
        if item['Dimension_name'] == '':
            item['Dimension_name'] = data[header_row_index][column_number]\
                .value
    if 'mapping_rule' in options_list:
        mapping_rule = options_list['mapping_rule']
    else:
        mapping_rule = None
    # Data processing
    date_values = []
    date_rows = options_list['dates_info']['dates_rows']
    for row_index in date_rows:
        date_values.append(data[row_index][start_date_col].value)
    num_of_dates = last_col - start_date_col
    first_label, time_line = date_func(date_values, num_of_dates)
    times_series = warehouse.add_time_scale(series_name, time_line)
    for row_index in range(start_data_row, len(data)):
        meta = []
        for item in meta_cols:
            copy_item = item.copy()
            column_index = copy_item['Col_number']
            copy_item['Name'] = data[row_index][column_index].value.strip()
            meta.append(copy_item)
        if mapping_rule is not None:
            new_meta, is_mapped = mapping(meta, mapping_rule)
            if is_mapped:
                meta = new_meta
        # Using db interface
        var_name = data[row_index][name_col].value
        if has_map_names:
            if var_name in map_names:
                var_name = map_names[var_name]
        entity = warehouse.add_entity(meta)
        variable = warehouse.force_variable(entity, var_name, 'float')
        time_series = warehouse.force_time_series(variable, times_series)
        values = []
        for col_index in range(start_date_col, last_col):
            value = data[row_index][col_index].value
            if value == '':
                value = 0.0
            values.append(value)
        time_series.set_values(first_label, values)
예제 #22
0
def jj_oral_care_sku(warehouse, wb, options_list):
    t1 = datetime.datetime.now()

    ws = wb.sheet_by_index(0)
    data = get_cell_range(0, 0, ws.ncols, ws.nrows, ws)
    meta_cols = options_list['meta_cols']
    date_func = options_list['date_func']
    header_row_index = options_list['info']['header_row']
    name_col = options_list['name_col']
    map_names = options_list['map_names']
    series_name = options_list['dates_info']['scale']
    start_data_row = options_list['info']['data_row']
    start_date_col = options_list['dates_info']['start_column']
    last_col = options_list['dates_info']['end_column']
    if last_col == '':
        last_col = get_last_col(data, header_row_index)
    # Rename headers cols: names
    for item in meta_cols:
        column_number = item['Col_number']
        if column_number >= last_col:
            raise ex.NotExistsError('DataProcessing', 'column', column_number)
        if item['Dimension_name'] == '':
            item['Dimension_name'] = data[header_row_index][column_number]\
                .value
    if 'mapping_rule' in options_list:
        mapping_rule = options_list['mapping_rule']
    else:
        mapping_rule = None
    num_of_dates = last_col - start_date_col
    first_label, time_line = date_func(
        data[header_row_index][start_date_col].value, wb.datemode,
        num_of_dates)
    times_series = warehouse.add_time_scale(series_name, time_line)
    for row_index in range(start_data_row, len(data)):
        meta = []
        for item in meta_cols:
            copy_item = item.copy()
            column_index = copy_item['Col_number']
            copy_item['Name'] = data[row_index][column_index].value.strip()
            meta.append(copy_item)
        if mapping_rule is not None:
            new_meta, is_mapped = mapping(meta, mapping_rule)
            if is_mapped:
                meta = new_meta
        # Using db interface
        var_name = data[row_index][name_col].value
        if var_name in map_names:
            var_name = map_names[var_name]
        entity = warehouse.add_entity(meta)
        variable = warehouse.force_variable(entity, var_name, 'float')
        time_series = warehouse.force_time_series(variable, times_series)
        values = []
        for col_index in range(start_date_col, last_col):
            value = data[row_index][col_index].value
            if value == '':
                value = 0.0
            values.append(value)
        time_series.set_values(first_label, values)
    t2 = datetime.datetime.now()
    delta = (t2 - t1)
    minutes_delta_time = delta.seconds / 60.0
    print('Algorithm sku takes minutes:' + str(minutes_delta_time))
    print('Algorithm sku takes seconds:' + str(delta.seconds))