Пример #1
0
    def test_duplicate_dimension(self):
        ws = Workspace()
        ws.import_model(self.model_path("templated_dimension.json"))

        model = {"dimensions": [{"name": "date"}]}
        with self.assertRaises(ModelError):
            ws.import_model(model)
Пример #2
0
    def test_get_namespace_cube(self):
        ws = Workspace()
        ws.import_model(self.model_path("model.json"), namespace="local")

        with self.assertRaises(NoSuchCubeError):
            cube = ws.cube("contracts")

        cube = ws.cube("local.contracts")
        self.assertEqual("local.contracts", cube.name)
Пример #3
0
 def test_init(self):
     valid_configs = [
         read_slicer_config(None),  # Allow empty configuration object
         'slicer.ini',  # Allow file to be loaded
         None,  # Allow no file
         'abc/de/F',  # Allow wrong path
     ]
     invalid_configs = [Workspace, 2]
     for config in valid_configs:
         Workspace(config=config)
     for config in invalid_configs:
         with self.assertRaises(ConfigurationError,
                                msg='Expect to raise for config = "%s"' %
                                config):
             Workspace(config=config)
Пример #4
0
def create_local_workspace(config, cubes_root):
    """
    Returns or creates a thread-local instance of Workspace
    """
    if not hasattr(data, 'workspace'):
        data.workspace = Workspace(config=config, cubes_root=cubes_root)

    return data.workspace
Пример #5
0
    def initialize_slicer(self):
        if self.workspace is None:
            try:
                config = settings.SLICER_CONFIG_FILE
                cubes_root = settings.SLICER_MODELS_DIR
            except AttributeError:
                raise ImproperlyConfigured('settings.SLICER_CONFIG_FILE and settings.SLICER_MODELS_DIR are not set.')

            self.workspace = Workspace(config=config, cubes_root=cubes_root)
Пример #6
0
    def test_external_template(self):
        ws = Workspace()
        ws.import_model(self.model_path("templated_dimension.json"))
        ws.import_model(self.model_path("templated_dimension_ext.json"))

        dim = ws.dimension("another_date")
        self.assertEqual("another_date", dim.name)
        self.assertEqual(3, len(dim.levels))
Пример #7
0
    def test_duplicate_dimension(self):
        ws = Workspace()
        ws.import_model(self.model_path("templated_dimension.json"))

        model = {"dimensions": [{"name": "date"}]}
        with self.assertRaises(ModelError):
            ws.import_model(model)
Пример #8
0
    def test_local_dimension(self):
        # Test whether we can use local dimension with the same name as the
        # public one
        ws = Workspace()
        ws.import_model(self.model_path("model_public_dimensions.json"))
        ws.import_model(self.model_path("model_private_dimensions.json"))

        dim = ws.dimension("date")
        self.assertEqual(3, len(dim.levels))
        self.assertEqual(["year", "month", "day"], dim.level_names)

        cube = ws.cube("events")
        dim = cube.dimension("date")
        self.assertEqual(["year", "month", "day"], dim.level_names)

        cube = ws.cube("lonely_yearly_events")
        dim = cube.dimension("date")
        self.assertEqual(["lonely_year"], dim.level_names)
Пример #9
0
    def test_external_template(self):
        ws = Workspace()
        ws.import_model(self.model_path("templated_dimension.json"))
        ws.import_model(self.model_path("templated_dimension_ext.json"))

        dim = ws.dimension("another_date")
        self.assertEqual("another_date", dim.name)
        self.assertEqual(3, len(dim.levels))
Пример #10
0
    def test_get_namespace_cube(self):
        ws = Workspace()
        ws.import_model(self.model_path("model.json"), namespace="local")

        with self.assertRaises(NoSuchCubeError):
            cube = ws.cube("contracts")

        cube = ws.cube("local.contracts")
        self.assertEqual("local.contracts", cube.name)
Пример #11
0
    def test_template(self):
        ws = Workspace()
        ws.import_model(self.model_path("templated_dimension.json"))

        dim = ws.dimension("date")
        self.assertEqual("date", dim.name)
        self.assertEqual(3, len(dim.levels))

        dim = ws.dimension("start_date")
        self.assertEqual("start_date", dim.name)
        self.assertEqual(3, len(dim.levels))

        dim = ws.dimension("end_date")
        self.assertEqual("end_date", dim.name)
Пример #12
0
    def test_local_dimension(self):
        # Test whether we can use local dimension with the same name as the
        # public one
        ws = Workspace()
        ws.import_model(self.model_path("model_public_dimensions.json"))
        ws.import_model(self.model_path("model_private_dimensions.json"))

        dim = ws.dimension("date")
        self.assertEqual(3, len(dim.levels))
        self.assertEqual(["year", "month", "day"], dim.level_names)

        cube = ws.cube("events")
        dim = cube.dimension("date")
        self.assertEqual(["year", "month", "day"], dim.level_names)

        cube = ws.cube("lonely_yearly_events")
        dim = cube.dimension("date")
        self.assertEqual(["lonely_year"], dim.level_names)
Пример #13
0
    def test_template(self):
        ws = Workspace()
        ws.import_model(self.model_path("templated_dimension.json"))

        dim = ws.dimension("date")
        self.assertEqual("date", dim.name)
        self.assertEqual(3, len(dim.levels))

        dim = ws.dimension("start_date")
        self.assertEqual("start_date", dim.name)
        self.assertEqual(3, len(dim.levels))

        dim = ws.dimension("end_date")
        self.assertEqual("end_date", dim.name)
Пример #14
0
 def default_workspace(self, model_name=None):
     model_name = model_name or "model.json"
     config = read_slicer_config(self.data_path("slicer.ini"))
     ws = Workspace(config=config)
     ws.import_model(self.model_path("model.json"))
     return ws
Пример #15
0
    def test_cube_with_dimensions_in_two_namespaces(self):
        ws = Workspace()
        ws.import_model(self.model_path("model.json"), namespace="store1")
        ws.import_model(self.model_path("other.json"), namespace="store2")

        # This should not pass, since the dimension is in another namespace
        with self.assertRaises(NoSuchDimensionError):
            ws.cube("store2.other")

        ws = Workspace()
        ws.import_model(self.model_path("model.json"), namespace="default")
        ws.import_model(self.model_path("other.json"), namespace="store2")

        # This should pass, since the dimension is in the default namespace
        ws.cube("store2.other")
Пример #16
0
    def test_cube_with_dimensions_in_two_namespaces(self):
        ws = Workspace()
        ws.import_model(self.model_path("model.json"), namespace="store1")
        ws.import_model(self.model_path("other.json"), namespace="store2")

        # This should not pass, since the dimension is in another namespace
        with self.assertRaises(NoSuchDimensionError):
            ws.cube("store2.other")

        ws = Workspace()
        ws.import_model(self.model_path("model.json"), namespace="default")
        ws.import_model(self.model_path("other.json"), namespace="store2")

        # This should pass, since the dimension is in the default namespace
        ws.cube("store2.other")
Пример #17
0
class CubesView(APIView):
    permission_classes = (permissions.IsAuthenticated,)
    workspace = None
    SET_CUT_SEPARATOR_CHAR = '~'

    def __init__(self, *args, **kwargs):
        super(CubesView, self).__init__(*args, **kwargs)
        self._fix_cut_separators()

    def _fix_cut_separators(self):
        browser.PATH_ELEMENT = r"(?:\\.|[^:%s|-])*" % self.SET_CUT_SEPARATOR_CHAR
        browser.RE_ELEMENT = re.compile(r"^%s$" % browser.PATH_ELEMENT)
        browser.RE_POINT = re.compile(r"^%s$" % browser.PATH_ELEMENT)
        browser.SET_CUT_SEPARATOR_CHAR = self.SET_CUT_SEPARATOR_CHAR
        browser.SET_CUT_SEPARATOR = re.compile(r'(?<!\\)%s' % self.SET_CUT_SEPARATOR_CHAR)
        browser.RE_SET = re.compile(r"^(%s)(%s(%s))*$" % (
            browser.PATH_ELEMENT, self.SET_CUT_SEPARATOR_CHAR, browser.PATH_ELEMENT
        ))

    def initialize_slicer(self):
        if self.workspace is None:
            try:
                config = settings.SLICER_CONFIG_FILE
                cubes_root = settings.SLICER_MODELS_DIR
            except AttributeError:
                raise ImproperlyConfigured('settings.SLICER_CONFIG_FILE and settings.SLICER_MODELS_DIR are not set.')

            self.workspace = Workspace(config=config, cubes_root=cubes_root)

    def get_cube(self, request, cube_name):
        self.initialize_slicer()
        try:
            cube = self.workspace.cube(cube_name, request.user)
        except NoSuchCubeError:
            raise Http404

        return cube

    def get_browser(self, cube):
        return self.workspace.browser(cube)

    def get_cell(self, request, cube, argname="cut", restrict=False):
        """Returns a `Cell` object from argument with name `argname`"""
        converters = {
            "time": CalendarMemberConverter(self.workspace.calendar)
        }

        cuts = []
        for cut_string in request.QUERY_PARAMS.getlist(argname):
            cuts += cuts_from_string(
                cube, cut_string, role_member_converters=converters
            )

        if cuts:
            cell = Cell(cube, cuts)
        else:
            cell = None

        if restrict:
            if self.workspace.authorizer:
                cell = self.workspace.authorizer.restricted_cell(
                    request.user, cube=cube, cell=cell
                )
        return cell

    def get_info(self):
        self.initialize_slicer()
        if self.workspace.info:
            info = OrderedDict(self.workspace.info)
        else:
            info = OrderedDict()

        info["cubes_version"] = __version__
        info["timezone"] = self.workspace.calendar.timezone_name
        info["first_weekday"] = self.workspace.calendar.first_weekday
        info["api_version"] = API_VERSION
        return info

    def assert_enabled_action(self, request, browser, action):
        features = browser.features()
        if action not in features['actions']:
            message = u"The action '{}' is not enabled".format(action)
            logging.error(message)
            raise ParseError(detail=message)

    def _handle_pagination_and_order(self, request):
        try:
            page = request.QUERY_PARAMS.get('page', None)
        except ValueError:
            page = None
        request.page = page

        try:
            page_size = request.QUERY_PARAMS.get('pagesize', None)
        except ValueError:
            page_size = None
        request.page_size = page_size

        # Collect orderings:
        # order is specified as order=<field>[:<direction>]
        order = []
        for orders in request.QUERY_PARAMS.getlist('order'):
            for item in orders.split(","):
                split = item.split(":")
                if len(split) == 1:
                    order.append((item, None))
                else:
                    order.append((split[0], split[1]))
        request.order = order

    def initialize_request(self, request, *args, **kwargs):
        request = super(CubesView, self).initialize_request(request, *args, **kwargs)
        self._handle_pagination_and_order(request)
        return request
Пример #18
0
 def default_workspace(self, model_name=None):
     model_name = model_name or "model.json"
     ws = Workspace(config=self.data_path("slicer.ini"))
     ws.import_model(self.model_path("model.json"))
     return ws