Ejemplo n.º 1
0
    def testToJSon(self):
        # The json of the initial data we load to the table.
        init_data_json = ("{cols:"
                          "[{id:'a',label:'A',type:'number'},"
                          "{id:'b',label:'b',type:'string'},"
                          "{id:'c',label:'c',type:'boolean'}],"
                          "rows:["
                          "{c:[{v:1},,{v:null}]},"
                          "{c:[,{v:'z'},{v:true}]}"
                          "]}")
        table = DataTable([("a", "number", "A"), "b", ("c", "boolean")],
                          [[1], [None, "z", True]])
        self.assertEqual(2, table.NumberOfRows())
        self.assertEqual(init_data_json, table.ToJSon())
        table.AppendData([[-1, "w", False]])
        self.assertEqual(3, table.NumberOfRows())
        self.assertEqual(
            init_data_json[:-2] + ",{c:[{v:-1},{v:'w'},{v:false}]}]}",
            table.ToJSon())

        cols_json = ("{cols:"
                     "[{id:'t',label:'T',type:'timeofday'},"
                     "{id:'d',label:'d',type:'date'},"
                     "{id:'dt',label:'dt',type:'datetime'}],")
        table = DataTable({
            ("d", "date"): [("t", "timeofday", "T"), ("dt", "datetime")]
        })
        table.LoadData({date(1, 2, 3): [time(1, 2, 3)]})
        self.assertEqual(1, table.NumberOfRows())
        self.assertEqual(
            cols_json +
            "rows:[{c:[{v:[1,2,3]},{v:new Date(1,1,3)},{v:null}]}]}",
            table.ToJSon(columns_order=["t", "d", "dt"]))
        table.LoadData({
            date(2, 3, 4): [(time(2, 3, 4), "time 2 3 4"),
                            datetime(1, 2, 3, 4, 5, 6)],
            date(3, 4, 5): []
        })
        self.assertEqual(2, table.NumberOfRows())
        self.assertEqual((cols_json + "rows:["
                          "{c:[{v:[2,3,4],f:'time 2 3 4'},{v:new Date(2,2,4)},"
                          "{v:new Date(1,1,3,4,5,6)}]},"
                          "{c:[,{v:new Date(3,3,5)},{v:null}]}]}"),
                         table.ToJSon(columns_order=["t", "d", "dt"]))

        json = ("{cols:[{id:\"a'\",label:\"a'\",type:'string'},"
                "{id:'b',label:\"bb'\",type:'number'}],"
                "rows:[{c:[{v:'a1'},{v:1}]},{c:[{v:'a2'},{v:2}]},"
                "{c:[{v:'a3'},{v:3}]}]}")
        table = DataTable({"a'": ("b", "number", "bb'", {})}, {
            "a1": 1,
            "a2": 2,
            "a3": 3
        })
        self.assertEqual(3, table.NumberOfRows())
        self.assertEqual(json, table.ToJSon())
    def testToJSonResponse(self):
        description = ["col1", "col2", "col3"]
        data = [("1", "2", "3"), ("a", "b", "c"), ("One", "Two", "Three")]
        req_id = 4
        table = DataTable(description, data)

        start_str_default = r"google.visualization.Query.setResponse"
        start_str_handler = r"MyHandlerFunction"

        json_str = table.ToJSon().strip()

        json_response = table.ToJSonResponse(req_id=req_id)

        self.assertEqual(json_response.find(start_str_default + "("), 0)

        json_response_obj = json.loads(json_response[len(start_str_default) +
                                                     1:-2])
        self.assertEqual(json_response_obj["table"], json.loads(json_str))
        self.assertEqual(json_response_obj["version"], "0.6")
        self.assertEqual(json_response_obj["reqId"], str(req_id))
        self.assertEqual(json_response_obj["status"], "ok")

        json_response = table.ToJSonResponse(
            req_id=req_id, response_handler=start_str_handler)

        self.assertEqual(json_response.find(start_str_handler + "("), 0)
        json_response_obj = json.loads(json_response[len(start_str_handler) +
                                                     1:-2])
        self.assertEqual(json_response_obj["table"], json.loads(json_str))
Ejemplo n.º 3
0
    def testToJSonResponse(self):
        description = ["col1", "col2", "col3"]
        data = [("1", "2", "3"), ("a", "b", "c"), ("One", "Two", "Three")]
        req_id = 4
        table = DataTable(description, data)

        start_str_default = r"google.visualization.Query.setResponse"
        start_str_handler = r"MyHandlerFunction"
        default_params = (
            r"\s*'version'\s*:\s*'0.6'\s*,\s*'reqId'\s*:\s*'%s'\s*,"
            r"\s*'status'\s*:\s*'OK'\s*" % req_id)
        regex1 = re.compile("%s\(\s*\{%s,\s*'table'\s*:\s*{(.*)}\s*\}\s*\);" %
                            (start_str_default, default_params))
        regex2 = re.compile("%s\(\s*\{%s,\s*'table'\s*:\s*{(.*)}\s*\}\s*\);" %
                            (start_str_handler, default_params))

        json_str = table.ToJSon().strip()

        json_response = table.ToJSonResponse(req_id=req_id)
        match = regex1.findall(json_response)
        self.assertEquals(len(match), 1)
        # We want to match against the json_str without the curly brackets.
        self.assertEquals(match[0], json_str[1:-1])

        json_response = table.ToJSonResponse(
            req_id=req_id, response_handler=start_str_handler)
        match = regex2.findall(json_response)
        self.assertEquals(len(match), 1)
        # We want to match against the json_str without the curly brackets.
        self.assertEquals(match[0], json_str[1:-1])
    def testOrderBy(self):
        data = [("b", 3), ("a", 3), ("a", 2), ("b", 1)]
        description = ["col1", ("col2", "number", "Second Column")]
        table = DataTable(description, data)

        table_num_sorted = DataTable(description,
                                     sorted(data, key=lambda x: (x[1], x[0])))

        table_str_sorted = DataTable(description,
                                     sorted(data, key=lambda x: x[0]))

        table_diff_sorted = DataTable(
            description,
            sorted(sorted(data, key=lambda x: x[1]),
                   key=lambda x: x[0],
                   reverse=True))

        self.assertEqual(table_num_sorted.ToJSon(),
                         table.ToJSon(order_by=("col2", "col1")))
        self.assertEqual(table_num_sorted.ToJSCode("mytab"),
                         table.ToJSCode("mytab", order_by=("col2", "col1")))

        self.assertEqual(table_str_sorted.ToJSon(),
                         table.ToJSon(order_by="col1"))
        self.assertEqual(table_str_sorted.ToJSCode("mytab"),
                         table.ToJSCode("mytab", order_by="col1"))

        self.assertEqual(table_diff_sorted.ToJSon(),
                         table.ToJSon(order_by=[("col1", "desc"), "col2"]))
        self.assertEqual(
            table_diff_sorted.ToJSCode("mytab"),
            table.ToJSCode("mytab", order_by=[("col1", "desc"), "col2"]))
Ejemplo n.º 5
0
    def get(self, request, *args, **kwargs):

        if not request.is_ajax():
            raise SuspiciousOperation(
                'This endpoint should only be called from JavaScript.')

        search_terms = self.get_search_terms(request.session)

        qs = get_queryset_filtered_by_search_terms(self.get_pixels_queryset(),
                                                   search_terms=search_terms)

        dt = DataTable(self.get_headers())
        dt.LoadData(qs.values(*self.get_columns()))

        return HttpResponse(dt.ToJSon(), content_type='application/json')
    def testDifferentStrings(self):
        # Checking escaping of strings in JSON output
        the_strings = [
            "new\nline", r"one\slash", r"two\\slash", "unicode eng",
            "unicode \\u05e2\\u05d1\\u05e8\\u05d9\\u05ea",
            "unicode \\u05e2\\u05d1\\u05e8\\u05d9\\u05ea".encode("utf-8"),
            "\u05e2\u05d1\\\u05e8\u05d9\u05ea"
        ]
        table = DataTable([("a", "string")], [[x] for x in the_strings])

        json_obj = json.loads(table.ToJSon())
        for i, row in enumerate(json_obj["rows"]):
            utf8_str = the_strings[i]
            if isinstance(utf8_str, str):
                utf8_str = utf8_str.encode("utf-8")

            out_str = row["c"][0]["v"]
            self.assertEqual(out_str.encode("utf-8"), utf8_str)
Ejemplo n.º 7
0
    def testCustomProperties(self):
        # The json of the initial data we load to the table.
        json = (
            "{cols:"
            "[{id:'a',label:'A',type:'number',p:{'col_cp':'col_v'}},"
            "{id:'b',label:'b',type:'string'},"
            "{id:'c',label:'c',type:'boolean'}],"
            "rows:["
            "{c:[{v:1},,{v:null,p:{'null_cp':'null_v'}}],p:{'row_cp':'row_v'}},"
            "{c:[,{v:'z',p:{'cell_cp':'cell_v'}},{v:true}]},"
            "{c:[{v:3},,{v:null}],p:{'row_cp2':'row_v2'}}],"
            "p:{'global_cp':'global_v'}"
            "}")
        jscode = ("var mytab = new google.visualization.DataTable();\n"
                  "mytab.setTableProperties({'global_cp':'global_v'});\n"
                  "mytab.addColumn('number', 'A', 'a');\n"
                  "mytab.setColumnProperties(0, {'col_cp':'col_v'});\n"
                  "mytab.addColumn('string', 'b', 'b');\n"
                  "mytab.addColumn('boolean', 'c', 'c');\n"
                  "mytab.addRows(3);\n"
                  "mytab.setCell(0, 0, 1);\n"
                  "mytab.setCell(0, 2, null, null, {'null_cp':'null_v'});\n"
                  "mytab.setRowProperties(0, {'row_cp':'row_v'});\n"
                  "mytab.setCell(1, 1, 'z', null, {'cell_cp':'cell_v'});\n"
                  "mytab.setCell(1, 2, true);\n"
                  "mytab.setCell(2, 0, 3);\n"
                  "mytab.setRowProperties(2, {'row_cp2':'row_v2'});\n")

        table = DataTable([("a", "number", "A", {
            "col_cp": "col_v"
        }), "b", ("c", "boolean")],
                          custom_properties={"global_cp": "global_v"})
        table.AppendData([[1, None, (None, None, {
            "null_cp": "null_v"
        })]],
                         custom_properties={"row_cp": "row_v"})
        table.AppendData([[None, ("z", None, {
            "cell_cp": "cell_v"
        }), True], [3]])
        table.SetRowsCustomProperties(2, {"row_cp2": "row_v2"})
        self.assertEqual(json, table.ToJSon())
        self.assertEqual(jscode, table.ToJSCode("mytab"))
    def testCustomProperties(self):
        # The json of the initial data we load to the table.
        json_obj = {
            "cols": [{
                "id": "a",
                "label": "A",
                "type": "number",
                "p": {
                    "col_cp": "col_v"
                }
            }, {
                "id": "b",
                "label": "b",
                "type": "string"
            }, {
                "id": "c",
                "label": "c",
                "type": "boolean"
            }],
            "rows": [{
                "c": [{
                    "v": 1
                }, None, {
                    "v": None,
                    "p": {
                        "null_cp": "null_v"
                    }
                }],
                "p": {
                    "row_cp": "row_v"
                }
            }, {
                "c":
                [None, {
                    "v": "z",
                    "p": {
                        "cell_cp": "cell_v"
                    }
                }, {
                    "v": True
                }]
            }, {
                "c": [{
                    "v": 3
                }, None, None],
                "p": {
                    "row_cp2": "row_v2"
                }
            }],
            "p": {
                "global_cp": "global_v"
            }
        }
        jscode = (
            "var mytab = new google.visualization.DataTable();\n"
            "mytab.setTableProperties({\"global_cp\":\"global_v\"});\n"
            "mytab.addColumn(\"number\", \"A\", \"a\");\n"
            "mytab.setColumnProperties(0, {\"col_cp\":\"col_v\"});\n"
            "mytab.addColumn(\"string\", \"b\", \"b\");\n"
            "mytab.addColumn(\"boolean\", \"c\", \"c\");\n"
            "mytab.addRows(3);\n"
            "mytab.setCell(0, 0, 1);\n"
            "mytab.setCell(0, 2, null, null, {\"null_cp\":\"null_v\"});\n"
            "mytab.setRowProperties(0, {\"row_cp\":\"row_v\"});\n"
            "mytab.setCell(1, 1, \"z\", null, {\"cell_cp\":\"cell_v\"});\n"
            "mytab.setCell(1, 2, true);\n"
            "mytab.setCell(2, 0, 3);\n"
            "mytab.setRowProperties(2, {\"row_cp2\":\"row_v2\"});\n")

        table = DataTable([("a", "number", "A", {
            "col_cp": "col_v"
        }), "b", ("c", "boolean")],
                          custom_properties={"global_cp": "global_v"})
        table.AppendData([[1, None, (None, None, {
            "null_cp": "null_v"
        })]],
                         custom_properties={"row_cp": "row_v"})
        table.AppendData([[None, ("z", None, {
            "cell_cp": "cell_v"
        }), True], [3]])
        table.SetRowsCustomProperties(2, {"row_cp2": "row_v2"})
        self.assertEqual(json.dumps(json_obj, separators=(",", ":")),
                         table.ToJSon())
        self.assertEqual(jscode, table.ToJSCode("mytab"))
    def testToJSon(self):
        json_obj = {
            "cols": [{
                "id": "a",
                "label": "A",
                "type": "number"
            }, {
                "id": "b",
                "label": "b",
                "type": "string"
            }, {
                "id": "c",
                "label": "c",
                "type": "boolean"
            }],
            "rows": [{
                "c": [{
                    "v": 1
                }, None, None]
            }, {
                "c": [None, {
                    "v": "z"
                }, {
                    "v": True
                }]
            }, {
                "c": [None, {
                    "v": "\\u05d0"
                }, None]
            }, {
                "c": [None, {
                    "v": "\\u05d1"
                }, None]
            }]
        }

        table = DataTable([("a", "number", "A"), "b", ("c", "boolean")],
                          [[1], [None, "z", True], [None, "\\u05d0"],
                           [None, "\\u05d1".encode("utf-8")]])
        self.assertEqual(4, table.NumberOfRows())
        self.assertEqual(
            json.dumps(json_obj, separators=(",", ":"),
                       ensure_ascii=False).encode("utf-8"), table.ToJSon())
        table.AppendData([[-1, "w", False]])
        self.assertEqual(5, table.NumberOfRows())
        json_obj["rows"].append({"c": [{"v": -1}, {"v": "w"}, {"v": False}]})
        self.assertEqual(
            json.dumps(json_obj, separators=(",", ":"),
                       ensure_ascii=False).encode("utf-8"), table.ToJSon())

        json_obj = {
            "cols": [{
                "id": "t",
                "label": "T",
                "type": "timeofday"
            }, {
                "id": "d",
                "label": "d",
                "type": "date"
            }, {
                "id": "dt",
                "label": "dt",
                "type": "datetime"
            }],
            "rows": [{
                "c": [{
                    "v": [1, 2, 3]
                }, {
                    "v": "Date(1,1,3)"
                }, None]
            }]
        }
        table = DataTable({
            ("d", "date"): [("t", "timeofday", "T"), ("dt", "datetime")]
        })
        table.LoadData({date(1, 2, 3): [time(1, 2, 3)]})
        self.assertEqual(1, table.NumberOfRows())
        self.assertEqual(json.dumps(json_obj, separators=(",", ":")),
                         table.ToJSon(columns_order=["t", "d", "dt"]))

        json_obj["rows"] = [{
            "c": [{
                "v": [2, 3, 4],
                "f": "time 2 3 4"
            }, {
                "v": "Date(2,2,4)"
            }, {
                "v": "Date(1,1,3,4,5,6,555)"
            }]
        }, {
            "c": [None, {
                "v": "Date(3,3,5)"
            }, None]
        }]

        table.LoadData({
            date(2, 3, 4): [(time(2, 3, 4), "time 2 3 4"),
                            datetime(1, 2, 3, 4, 5, 6, 555000)],
            date(3, 4, 5): []
        })
        self.assertEqual(2, table.NumberOfRows())

        self.assertEqual(json.dumps(json_obj, separators=(",", ":")),
                         table.ToJSon(columns_order=["t", "d", "dt"]))

        json_obj = {
            "cols": [{
                "id": "a\"",
                "label": "a\"",
                "type": "string"
            }, {
                "id": "b",
                "label": "bb\"",
                "type": "number"
            }],
            "rows": [{
                "c": [{
                    "v": "a1"
                }, {
                    "v": 1
                }]
            }, {
                "c": [{
                    "v": "a2"
                }, {
                    "v": 2
                }]
            }, {
                "c": [{
                    "v": "a3"
                }, {
                    "v": 3
                }]
            }]
        }
        table = DataTable({"a\"": ("b", "number", "bb\"", {})}, {
            "a1": 1,
            "a2": 2,
            "a3": 3
        })
        self.assertEqual(3, table.NumberOfRows())
        self.assertEqual(json.dumps(json_obj, separators=(",", ":")),
                         table.ToJSon())
Ejemplo n.º 10
0
    def get(self):
        question = self.request.get("question")
        if question != '':
            form = {"q": question}
            form_data = urllib.urlencode(form)
            resp = urlfetch.fetch(host + "ask",
                                  payload=form_data,
                                  method="POST",
                                  follow_redirects=False)
            if resp.status_code == 200:
                logging.debug("OK")
                logging.debug(result.content)
            else:
                logging.debug("FAILED ON QUESTION")
                pass
            country = 'US'
            indicator = 'TM.TAX.MRCH.SM.AR.ZS'
            pass
        else:
            country = self.request.get("country")
            indicator = self.request.get("indicator")
            pass
        start = self.request.get("start")
        if start == '':
            start = '2000'
        end = self.request.get("end")
        if end == '':
            end = '2012'

        if indicator == '':
            indicator = 'TM.TAX.MRCH.SM.AR.ZS'
            pass
        data = {}

        country_code = country.lower()
        indicator = indicator.upper()
        url = "http://api.worldbank.org/countries/" + country_code + "/indicators/" + indicator + "?" + \
        "date=" + start + ":" + end + "&" + "format=" + "json"

        resp = urlfetch.fetch(url, method="GET", follow_redirects=True)
        if resp.status_code == 200:
            logging.debug(resp.status_code)
            try:
                data = json.loads(resp.content)
            except:
                logging.info(resp.content)
                pass
        else:
            logging.debug(resp.status_code)
            logging.debug(resp.content)
            pass
        rows = {}
        old_viz_data = []
        viz_data = []
        countries = {}
        try:
            for row in data[1]:
                key = row['country']['id']
                countries[key] = row['country']['value']
                try:
                    rows[row['date']][key] = row['value']
                except:
                    rows[row['date']] = {}
                    rows[row['date']][key] = row['value']
                pass

            for yr in rows.keys():
                viz_row = {"date": date(int(yr), 1, 1)}
                for k in rows[yr].keys():
                    try:
                        viz_row[k] = float(rows[yr][k])
                    except:
                        viz_row[k] = None
                        pass
                    pass
                viz_data.append(viz_row)
        except:
            pass

            chart_data = {}
            chart_data['cols'] = [{
                'id': 'date',
                'label': 'Date',
                'type': 'number'
            }, {
                'id': 'value',
                'label': 'Value',
                'type': 'number'
            }]
            chart_data['rows'] = rows
            pass

        indicator_value = data[1][0]['indicator']['value']

        viz_desc = {"date": ("date", "Date")}
        order = ["date"]
        for k in countries.keys():
            viz_desc[k] = ("number", countries[k])
            order.append(k)

        data_table = DataTable(viz_desc)
        data_table.LoadData(viz_data)
        template_values = {
            'question': SafeString(question),
            'start': start,
            'end': end,
            'country': country,
            'indicator': indicator,
            'data': data_table.ToJSon(columns_order=order, order_by="date"),
            'title': SafeString(indicator_value)
        }

        if self.request.path == '/':
            path = os.path.join(os.path.dirname(__file__),
                                'templates/index.html')
        else:
            path = os.path.join(os.path.dirname(__file__),
                                'templates' + self.request.path)
            pass

        self.response.out.write(template.render(path, template_values))
        return