PythonからElixir Reportのレポートサーバーにアクセスするサンプルコード
修訂 | faddf891ae1c8d4d161a9abcaf48cbc4becd0981 (tree) |
---|---|
時間 | 2014-12-03 15:41:24 |
作者 | hylom <hylom@user...> |
Commiter | hylom |
implement /pdf/traffic
@@ -56,6 +56,42 @@ class TrafficDataHandler(web.RequestHandler): | ||
56 | 56 | self.finish(xml) |
57 | 57 | |
58 | 58 | |
59 | +class PdfHandler(web.RequestHandler): | |
60 | + "PDF出力用ハンドラ" | |
61 | + def get(self): | |
62 | + try: | |
63 | + year = int(self.get_argument("y")) | |
64 | + month = int(self.get_argument("m")) | |
65 | + except ValueError: | |
66 | + self.send_err(400) | |
67 | + return | |
68 | + | |
69 | + # パラメータで指定された年/月から | |
70 | + # 開始/終了年月日を生成する | |
71 | + start_date = date(year, month, 1) | |
72 | + if month == 12: | |
73 | + end_date = date(year + 1, 1, 1) + timedelta(-1) | |
74 | + else: | |
75 | + end_date = date(year, month + 1, 1) + timedelta(-1) | |
76 | + | |
77 | + # レポートサーバーに投げるパラメータを作成 | |
78 | + param = { | |
79 | + "start_date": start_date, | |
80 | + "end_date": end_date | |
81 | + } | |
82 | + | |
83 | + # リクエスト送信 | |
84 | + r = ReportServer(config["report_server"]["user"], | |
85 | + config["report_server"]["password"], | |
86 | + config["report_server"]["host"], | |
87 | + config["report_server"]["port"]) | |
88 | + r.login() | |
89 | + pdf = r.pdf(config["report_template"]["traffic"], param) | |
90 | + # 取得したXMLをクライアントに送信 | |
91 | + self.set_header("Content-type", "application/pdf") | |
92 | + self.set_status(200) | |
93 | + self.finish(pdf) | |
94 | + | |
59 | 95 | class DataTestHandler(web.RequestHandler): |
60 | 96 | "テスト用ハンドラ" |
61 | 97 | def get(self, request_path): |
@@ -91,7 +127,8 @@ class DataTestHandler(web.RequestHandler): | ||
91 | 127 | |
92 | 128 | application = web.Application([ |
93 | 129 | ("/", MainHandler), |
94 | -# ("/data/traffic", TrafficDataHandler), | |
130 | + ("/data/traffic", TrafficDataHandler), | |
131 | + ("/pdf/traffic", PdfHandler), | |
95 | 132 | ("/data/(.*)", DataTestHandler), |
96 | 133 | ("/css/(.*)", web.StaticFileHandler, {"path": CSS_PATH}), |
97 | 134 | ("/js/(.*)", web.StaticFileHandler, {"path": JS_PATH}), |