OSDN Git Service

Add validate
[karesansui/karesansui.git] / karesansui / gadget / hostby1watchtemplate.py
1 # -*- coding: utf-8 -*-
2 #
3 # This file is part of Karesansui.
4 #
5 # Copyright (C) 2010 HDE, Inc.
6 #
7 # This program is free software; you can redistribute it and/or
8 # modify it under the terms of the GNU General Public License
9 # as published by the Free Software Foundation; either version 2
10 # of the License, or (at your option) any later version.
11 #
12
13 import web
14
15 from karesansui.lib.rest import Rest, auth
16 from karesansui.lib.utils import read_file, json_dumps
17 from karesansui.lib.const import TEMPLATE_DIR, \
18     MAIL_TEMPLATE_COLLECTD_WARNING, \
19     MAIL_TEMPLATE_COLLECTD_FAILURE, \
20     MAIL_TEMPLATE_COLLECTD_OKAY, \
21     WATCH_PLUGINS, DEFAULT_LANGS
22
23 from karesansui.lib.checker import Checker
24
25 def validates_watch(obj, target, lang):
26     checker = Checker()
27     check = True
28     _ = obj._
29     checker.errors = []
30
31     if target not in WATCH_PLUGINS.values():
32         check = False
33         # TRANSLATORS:
34         #  %sは監視対象ではありません。
35         checker.add_error(_('"%s" is not watch target.') %_(target))
36
37     if lang not in DEFAULT_LANGS:
38         check = False
39         # TRANSLATORS:
40         #  %sの言語には対応していません。
41         checker.add_error(_('"%s" is not supported langs.') %_(lang))
42
43     obj.view.alert = checker.errors
44     return check
45
46 class HostBy1WatchTemplate(Rest):
47     @auth
48     def _GET(self, *param, **params):
49         host_id = self.chk_hostby1(param)
50         if host_id is None: return web.notfound()
51
52         target = param[1]
53         if target is None: return web.notfound()
54
55         lang = param[2]
56         if lang is None: return web.notfound()
57
58         if not validates_watch(self, target, lang):
59             self.logger.debug("Get watch mail template failed. Did not validate.")
60             return web.badrequest(self.view.alert)
61
62         template_dir = "%s/%s" % (TEMPLATE_DIR,lang[0:2],)
63
64         mail_template_warning = read_file("%s/%s" % (template_dir,MAIL_TEMPLATE_COLLECTD_WARNING[target]))
65         mail_template_failure = read_file("%s/%s" % (template_dir,MAIL_TEMPLATE_COLLECTD_FAILURE[target]))
66         mail_template_okay = read_file("%s/%s" % (template_dir,MAIL_TEMPLATE_COLLECTD_OKAY[target]))
67
68         data = {
69             "mail_template_warning" : mail_template_warning,
70             "mail_template_failure" : mail_template_failure,
71             "mail_template_okay" : mail_template_okay,
72             }
73
74         self.view.data = json_dumps(data)
75         return True
76
77 urls = (
78     '/host/(\d+)/watch/([a-zA-Z0-9]+)/([a-z]{2}|[a-z]{2}_[A-Z]{2})?(\.json)$', HostBy1WatchTemplate,
79     )