<s> import web <EOL> from karesansui . lib . rest import Rest , auth <EOL> from karesansui . lib . utils import read_file , json_dumps <EOL> from karesansui . lib . const import TEMPLATE_DIR , MAIL_TEMPLATE_COLLECTD_WARNING , MAIL_TEMPLATE_COLLECTD_FAILURE , MAIL_TEMPLATE_COLLECTD_OKAY , WATCH_PLUGINS , DEFAULT_LANGS <EOL> from karesansui . lib . checker import Checker <EOL> def validates_watch ( obj , target , lang ) : <EOL> checker = Checker ( ) <EOL> check = True <EOL> _ = obj . _ <EOL> checker . errors = [ ] <EOL> if target not in WATCH_PLUGINS . values ( ) : <EOL> check = False <EOL> checker . add_error ( _ ( '<STR_LIT>' ) % _ ( target ) ) <EOL> if lang not in DEFAULT_LANGS : <EOL> check = False <EOL> checker . add_error ( _ ( '<STR_LIT>' ) % _ ( lang ) ) <EOL> obj . view . alert = checker . errors <EOL> return check <EOL> class HostBy1WatchTemplate ( Rest ) : <EOL> @ auth <EOL> def _GET ( self , * param , ** params ) : <EOL> host_id = self . chk_hostby1 ( param ) <EOL> if host_id is None : return web . notfound ( ) <EOL> target = param [ <NUM_LIT:1> ] <EOL> if target is None : return web . notfound ( ) <EOL> lang = param [ <NUM_LIT:2> ] <EOL> if lang is None : return web . notfound ( ) <EOL> if not validates_watch ( self , target , lang ) : <EOL> self . logger . debug ( "<STR_LIT>" ) <EOL> return web . badrequest ( self . view . alert ) <EOL> template_dir = "<STR_LIT>" % ( TEMPLATE_DIR , lang [ <NUM_LIT:0> : <NUM_LIT:2> ] , ) <EOL> mail_template_warning = read_file ( "<STR_LIT>" % ( template_dir , MAIL_TEMPLATE_COLLECTD_WARNING [ target ] ) ) <EOL> mail_template_failure = read_file ( "<STR_LIT>" % ( template_dir , MAIL_TEMPLATE_COLLECTD_FAILURE [ target ] ) ) <EOL> mail_template_okay = read_file ( "<STR_LIT>" % ( template_dir , MAIL_TEMPLATE_COLLECTD_OKAY [ target ] ) ) <EOL> data = { <EOL> "<STR_LIT>" : mail_template_warning , <EOL> "<STR_LIT>" : mail_template_failure , <EOL> "<STR_LIT>" : mail_template_okay , <EOL> } <EOL> self . view . data = json_dumps ( data ) <EOL> return True <EOL> urls = ( <EOL> '<STR_LIT>' , HostBy1WatchTemplate , <EOL> ) </s>
