def test_from_dict_predefined_graph3(self): graph_item_dict = { 'type': 'predefined-graph', 'value': 'does-not-exist-graph' } graph_items = GraphItem.from_dict(graph_item_dict) self.assertEquals(len(graph_items), 0)
def test_from_dict4(self): layout_dict = {'color': '00ff00'} graph_item_dict = { 'type': 'line', 'location': 'fews-location-id', 'parameter': 'fews-parameter-id', 'module': 'fews-module-id', 'timestep': 'fews-time-step-id', 'qualifierset': 'fews-qualifier-set-id', 'value': 'blabla', 'layout': layout_dict, } graph_items = GraphItem.from_dict(graph_item_dict) self.assertEquals(len(graph_items), 1) self.assertEquals( graph_items[0].graph_type, GraphItem.GRAPH_TYPE_LINE) self.assertEquals( graph_items[0].location.ident, 'fews-location-id') self.assertEquals( graph_items[0].parameter.ident, 'fews-parameter-id') self.assertEquals( graph_items[0].module.ident, 'fews-module-id') self.assertEquals( graph_items[0].time_step.ident, 'fews-time-step-id') self.assertEquals( graph_items[0].qualifierset.ident, 'fews-qualifier-set-id') self.assertEquals( graph_items[0].value, 'blabla') self.assertEquals( graph_items[0].layout_as_dict(), layout_dict) self.assertEquals(graph_items[0].as_dict(), graph_item_dict)
def test_from_dict(self): graph_item_dict = { 'layout': {}, 'type': 'line', } graph_items = GraphItem.from_dict(graph_item_dict) self.assertEquals(len(graph_items), 1) self.assertEquals( graph_items[0].graph_type, GraphItem.GRAPH_TYPE_LINE) self.assertEquals(graph_items[0].as_dict(), graph_item_dict)
def test_from_dict2(self): graph_item_dict = { 'layout': {}, 'type': 'line', 'location': 'fews-location-id', 'parameter': 'fews-parameter-id', 'module': 'fews-module-id', } graph_items = GraphItem.from_dict(graph_item_dict) self.assertEquals(len(graph_items), 1) self.assertEquals( graph_items[0].graph_type, GraphItem.GRAPH_TYPE_LINE) self.assertEquals(graph_items[0].as_dict(), graph_item_dict)
def test_from_dict_predefined_graph(self): pg = PredefinedGraph(name='test', slug='test-graph') pg.save() # Make 2 dummy items pg.graphitem_set.create( graph_type=GraphItem.GRAPH_TYPE_LINE, value='test-value1', index=100) pg.graphitem_set.create( graph_type=GraphItem.GRAPH_TYPE_LINE, value='test-value2', index=110) graph_item_dict = { 'type': 'predefined-graph', 'value': 'test-graph' } graph_items = GraphItem.from_dict(graph_item_dict) self.assertEquals(len(graph_items), 2) self.assertEquals( graph_items[0].graph_type, GraphItem.GRAPH_TYPE_LINE) self.assertEquals(graph_items[0].value, 'test-value1') self.assertEquals(graph_items[1].value, 'test-value2')
def _graph_items_from_request(self): """ Return list of graph items from request. The graph items are created in memory or retrieved from memory. """ result = [] graph_settings = { 'aggregation-period': 'month', 'aggregation': 'sum', 'reset-period': 'month', 'width': 1200, 'height': 500, 'legend-location': -1, 'flags': False, 'now-line': False, 'format': 'png', 'unit_as_y_label': False, # Take unit as y-label } get = self.request.GET # Using the shortcut graph=<graph-slug> predefined_graph_slug = get.get('graph', None) if predefined_graph_slug is not None: # Add all graph items of graph to result location_get = get.get('location', None) if location_get is not None: # If multiple instances, just take one. try: location_get = GeoLocationCache.objects.filter( ident=location_get)[0] except IndexError: # Beware: read-only. Throw away this 'useless' # exception message. logger.error( ('Tried to fetch a non-existing GeoLocationCache ' 'object %s') % location_get) location_get = GeoLocationCache(ident=location_get) # This item is probably not going to show, because # the fews_norm_source is not defined. try: predefined_graph = PredefinedGraph.objects.get( slug=predefined_graph_slug) graph_settings.update(predefined_graph.graph_settings()) result.extend(predefined_graph.unfolded_graph_items( location=location_get)) except PredefinedGraph.DoesNotExist: logger.exception("Tried to fetch a non-existing predefined " "graph %s" % predefined_graph_slug) # All standard items: make memory objects of them. graph_items_json = self.request.GET.getlist('item') for graph_item_json in graph_items_json: # Create memory object for each graph_item and append to result. graph_item_dict = json.loads(graph_item_json) graph_items = GraphItem.from_dict(graph_item_dict) result.extend(graph_items) # Graph settings can be overruled graph_parameters = [ 'title', 'x-label', 'y-label', 'y-range-min', 'y-range-max', 'aggregation', 'aggregation-period', 'reset-period', 'width', 'height', 'legend-location', 'flags', 'now-line', 'format', 'unit_as_y_label', ] for graph_parameter in graph_parameters: if graph_parameter in get: graph_settings[graph_parameter] = get[graph_parameter] # legend-location can be a numeric value (3), or it can be # text ("lower left"). try: graph_settings['legend-location'] = int( graph_settings['legend-location']) except ValueError: pass return result, graph_settings