Пример #1
0
    def create_view(self, cls, add_filters, name, label, dimension,
                    breakdown=None, view_filters={}):
        '''\
        Create a view. The view will be computed when you call
        :meth:`finalize`.

        ``cls``
            A model class (inheriting from :class:`openspending.model.Base`)
        ``add_filters``
            A :term:`mongodb query spec` used as a query to select the
            instances of *cls* that will be used for the view.
        ``name``
            A name for the view. This name must be unique for all views
            for *cls* in an Open Spending site.
        ``label``
            A label that can be displayed to the user.
        ``dimensions``
            The dimensions that will be used to compute the view
        ``breakdown``
            ...
        ``view_filters``
            ...

        Returns: A :class:`openspending.lib.views.View` object.
        '''
        log.debug("pre-aggregating view %s on %r where %r",
                  name, cls, view_filters)
        view = View(self.dataset, name, label, dimension,
                    breakdown, cuts=view_filters)
        view.apply_to(cls, add_filters)
        view.compute()
        Dataset.c.update({'name': self.dataset.name},
                         {'$set': {'cubes': self.dataset.get('cubes', {})}})
        self.dataset = Dataset.by_id(self.dataset.name)
        return view
Пример #2
0
    def test_by_name(self):
        assert len(self.dataset.data['views']) == 4

        default = View.by_name(self.dataset, self.dataset, 'default')
        assert default is not None, default
        assert default.entity == 'dataset', default.entity
        assert default.name == 'default', default.name
        assert View.by_name(self.dataset, self.dataset, 'region')

        cf = {'taxonomy': 'cofog'}
        cfa = View.by_name(self.dataset, cf, 'default', 'cofog1')
        assert cfa, cfa
        assert cfa.dimension == 'cofog1', cfa.dimension

        assert_raises(ValueError, View.by_name, self.dataset, cfa, 'not-there')
Пример #3
0
    def test_by_name(self):
        assert len(self.dataset.data['views'])==4

        default = View.by_name(self.dataset, self.dataset, 'default')
        assert default is not None, default
        assert default.entity=='dataset', default.entity
        assert default.name=='default', default.name
        assert View.by_name(self.dataset, self.dataset, 'region')

        cf = {'taxonomy': 'cofog'}
        cfa = View.by_name(self.dataset, cf, 'default', 'cofog1')
        assert cfa, cfa
        assert cfa.dimension=='cofog1', cfa.dimension

        h.assert_raises(ValueError, View.by_name, self.dataset, 
                cfa, 'not-there')
Пример #4
0
    def test_by_name(self):
        view = self.get_view("test1", cuts={"foo": "Hello"})
        view.apply_to(model.entry, {"region": u"ENGLAND_South West"})

        soc = model.entry.find_one({"region": u"ENGLAND_South West"})

        loaded = View.by_name(soc, "test1")
        h.assert_equal(loaded.label, view.label)
        h.assert_equal(loaded.cuts, view.cuts)

        h.assert_raises(ValueError, View.by_name, soc, "not-there")
Пример #5
0
 def timeline(self, id):
     c.dataset = model.Dataset.by_id(id)
     view = View.by_name(c.dataset, "default")
     viewstate = ViewState(c.dataset, view, None)
     data = []
     meta = []
     for entry, year_data in viewstate.aggregates:
         meta.append({"label": entry.get("label"),
                      "description": entry.get("description", ""),
                      "name": entry.get("name"),
                      "index": len(meta),
                      "taxonomy": entry.get("taxonomy")})
         sorted_year_data = sorted(year_data.items(), key=lambda kv: kv[0])
         data.append([{"x": k, "y": v,
                       "meta": len(meta) - 1} for
                      k, v in sorted_year_data])
     c.data = json.dumps(data)
     c.meta = json.dumps(meta)
     return render('dataset/timeline.html')
Пример #6
0
 def timeline(self, name):
     self._get_dataset(name)
     c.dataset = Dataset.by_name(name)
     view = View.by_name(c.dataset, "default")
     viewstate = ViewState(c.dataset, view, None)
     data = []
     meta = []
     for entry, year_data in viewstate.aggregates:
         meta.append({
             "label": entry.get("label"),
             "description": entry.get("description", ""),
             "name": entry.get("name"),
             "index": len(meta),
             "taxonomy": entry.get("taxonomy")
         })
         sorted_year_data = sorted(year_data.items(), key=lambda kv: kv[0])
         data.append([{
             "x": k,
             "y": v,
             "meta": len(meta) - 1
         } for k, v in sorted_year_data])
     c.data = json.dumps(data)
     c.meta = json.dumps(meta)
     return render('dataset/timeline.html')
Пример #7
0
 def get_view(self, name='default', **kwargs):
     return View(self.dataset, kwargs)