def add(self, name, obj, expire_time, lead_times): try: _id = "_".join([obj.__class__.__name__, str(obj.id)]) if not isinstance(lead_times, list): lead_times = [lead_times] content = "神盾局OPS系统:前方英雄【%s 编号:%s】即将在【%s】过期,请及时充能!!" % ( name, str(obj.id), expire_time.strftime("%Y-%m-%d")) for lead_time in lead_times: date = expire_time - lead_time BaseSms.send(self, [ u.mobile for u in Group.get_users(DEFAULT_GROUP.SMS.ALERT) ], content, date=date, _id="_".join([_id, str(lead_time.days)])) BaseMail.send(self, [app.config["MAIL_ALERT"]], content, "", "mail.alert.tpl.html", date=date, _id="_".join([_id, str(lead_time.days)])) except Exception, e: traceback.print_exc() log.exception(e) raise ExceptionEx("添加邮件或短信提示失败!")
def add(cls, _dicts, auto_commit=True): results = [] if not isinstance(_dicts, list): _dicts = [_dicts] for _dict in _dicts: try: result = { "ip": _dict["ip"], "type_id": _dict["type_id"], "success": False, "error": "" } results.append(result) if Idc_host.query \ .filter(Idc_host.ip == _dict["ip"]) \ .filter(Idc_host.type_id == _dict["type_id"]) \ .count(): result["error"] = "%s 已经存在" % _dict["ip"] continue idc_host = Idc_host.__update(_dict) db.session.add(idc_host) result["success"] = True except Exception, e: traceback.print_exc() log.exception(e)
def add(self, name, obj, expire_time, lead_times): try: _id = "_".join([obj.__class__.__name__, str(obj.id)]) if not isinstance(lead_times, list): lead_times = [lead_times] content = "神盾局OPS系统:前方英雄【%s 编号:%s】即将在【%s】过期,请及时充能!!" % ( name, str(obj.id), expire_time.strftime("%Y-%m-%d") ) for lead_time in lead_times: date = expire_time - lead_time BaseSms.send(self, [u.mobile for u in Group.get_users(DEFAULT_GROUP.SMS.ALERT)], content, date=date, _id="_".join([_id, str(lead_time.days)])) BaseMail.send(self, [app.config["MAIL_ALERT"]], content, "", "mail.alert.tpl.html", date=date, _id="_".join([_id, str(lead_time.days)])) except Exception, e: traceback.print_exc() log.exception(e) raise ExceptionEx("添加邮件或短信提示失败!")
def to(self, l): try: uids = [l.uid, l.approval_uid] + l.copy_to_uid.split(",") if l.status == LEAVE.PASS: uids += [u.uid for u in Group.get_users(DEFAULT_GROUP.LEAVE.MAIL)] to = [User.get(uid)["mail"] for uid in uids] to.append("*****@*****.**") else: to = [User.get(uid)["mail"] for uid in uids] status = l.status if status != LEAVE.PASS: status = u"已退回" sub = u"%s的请假申请%s 审批时间:%s 编号:%d" % ( l.uid,status,l.approval_time.strftime("%Y-%m-%d"),l.id) self.send( to, sub, "/templates/leave/approval.html", "mail.leave.tpl.html", leave=l, leave_type=Ref.map(LEAVE.TYPE)[int(l.type_id)]) except Exception,e: log.exception(e) raise ExceptionEx("邮件发送失败,请重试!")
def add(cls, _dicts, auto_commit=True): results = [] if not isinstance(_dicts, list): _dicts = [_dicts] for _dict in _dicts: try: result = {"ip": _dict["ip"], "type_id": _dict["type_id"], "success": False, "error": ""} results.append(result) if Idc_host.query \ .filter(Idc_host.ip == _dict["ip"]) \ .filter(Idc_host.type_id == _dict["type_id"]) \ .count(): result["error"] = "%s 已经存在" % _dict["ip"] continue idc_host = Idc_host.__update(_dict) db.session.add(idc_host) result["success"] = True except Exception, e: traceback.print_exc() log.exception(e)
def dispatch_request(self, *args, **kwargs): try: if self.auth(): if request.form.__contains__("method"): method = getattr(self, request.form["method"].lower(), None) return method(*args, **kwargs) else: return super(BaseApi, self).dispatch_request(*args, **kwargs) else: return self.fail("未登录") except ExceptionEx, e: traceback.print_exc() log.exception(e) return self.fail(e.message)
def to(self, uid, newpass): try: user = User.get(uid) sub = u"密码重置提醒 %s" % (datetime.now().strftime("%Y-%m-%d %M:%S")) self.send( [user["mail"]], sub, "", "mail.passwd.tpl.html", newpass=newpass, ) except Exception, e: log.exception(e) raise ExceptionEx("邮件发送失败,请重试!")
def __deploy_database(rid, data): r = Release.query.filter(Release.id == rid).one() try: client = Client(app.config["SSH_HOST"], app.config["SSH_USER"], app.config["SSH_PASSWORD"]) if u"任务完成" not in client.exec_script("/root/csfscript/dump_data/dump_data.py", data): return r.is_deployed = True log_path = "%s/data_deploy_log/myapp.%s" % (app.root_path, datetime.now().strftime("%Y-%m-%d")) Release.add_log(r.id, log_path) except Exception, e: log.exception(e)
def _wrap_job(_id, args, job, minutes): try: job(*args) except Exception, e: traceback.print_exc() log.exception(e) if minutes < 120: minutes += 10 sched.add_job(job, "date", id=_id, run_date=datetime.now() + timedelta(minutes=minutes), misfire_grace_time=60 * 60 * 24 * 365 * 100, args=(_id, args, job, minutes), replace_existing=True)
class BaseApi(MethodView): def auth(self): session.permanent = True if session.__contains__(USER_SESSION): return True return False def dispatch_request(self, *args, **kwargs): try: if self.auth(): if request.form.__contains__("method"): method = getattr(self, request.form["method"].lower(), None) return method(*args, **kwargs) else: return super(BaseApi, self).dispatch_request(*args, **kwargs) else: return self.fail("未登录") except ExceptionEx, e: traceback.print_exc() log.exception(e) return self.fail(e.message) except KeyError, e: traceback.print_exc() log.exception(e) return self.fail("字段 %s 未提交" % e.message)
def delete(self, obj, lead_times): try: for lead_time in lead_times: try: _id = "_".join([obj.__class__.__name__, str(obj.id), str(lead_time.days)]) BaseSms.remove(self, _id) BaseMail.remove(self, _id) except JobLookupError, e: traceback.print_exc() log.exception(e) continue except Exception, e: traceback.print_exc() log.exception(e) raise ExceptionEx("删除邮件或短信提示失败!")
def to(self, l): try: uids = [l.approval_uid] + l.copy_to_uid.split(",") to = [User.get(uid)["mail"] for uid in uids] sub = u"请假申请 申请人:%s 申请时间:%s 编号:%d" % ( l.uid, l.apply_time.strftime("%Y-%m-%d"), l.id) self.send(to, sub, "/templates/leave/approval.html", "mail.leave.tpl.html", leave=l, leave_type=Ref.map(LEAVE.TYPE)[int(l.type_id)]) except Exception, e: log.exception(e) raise ExceptionEx("邮件发送失败,请重试!")
def to(self, l): try: uids = [l.approval_uid] + l.copy_to_uid.split(",") to = [User.get(uid)["mail"] for uid in uids] sub = u"请假申请 申请人:%s 申请时间:%s 编号:%d" % (l.uid,l.apply_time.strftime("%Y-%m-%d"),l.id) self.send( to, sub, "/templates/leave/approval.html", "mail.leave.tpl.html", leave=l, leave_type=Ref.map(LEAVE.TYPE)[int(l.type_id)]) except Exception,e: log.exception(e) raise ExceptionEx("邮件发送失败,请重试!")
def __deploy_database(rid, data): r = Release.query.filter(Release.id == rid).one() try: client = Client(app.config["SSH_HOST"], app.config["SSH_USER"], app.config["SSH_PASSWORD"]) if u"任务完成" not in client.exec_script( "/root/csfscript/dump_data/dump_data.py", data): return r.is_deployed = True log_path = "%s/data_deploy_log/myapp.%s" % ( app.root_path, datetime.now().strftime("%Y-%m-%d")) Release.add_log(r.id, log_path) except Exception, e: log.exception(e)
def delete(self, obj, lead_times): try: for lead_time in lead_times: try: _id = "_".join([ obj.__class__.__name__, str(obj.id), str(lead_time.days) ]) BaseSms.remove(self, _id) BaseMail.remove(self, _id) except JobLookupError, e: traceback.print_exc() log.exception(e) continue except Exception, e: traceback.print_exc() log.exception(e) raise ExceptionEx("删除邮件或短信提示失败!")
def send(mobile, content): try: root = dom.parseString(requests.post( app.config["SMS_GATEWAY"], { "method": "Submit", "account": app.config["SMS_USER"], "password": app.config["SMS_PASSWORD"], "mobile": mobile, "content": content } ).text.encode("utf-8")) code, msg, smsid = [ node.childNodes[0].data for node in root.getElementsByTagName("SubmitResult")[0].childNodes if isinstance(node, Element)] if code != "2": raise Exception("短信发送失败 %s %s 手机号%s 内容%s" % (code, msg, mobile, content)) except Exception, e: log.exception(e) raise e
return super(BaseApi, self).dispatch_request(*args, **kwargs) else: return self.fail("未登录") except ExceptionEx, e: traceback.print_exc() log.exception(e) return self.fail(e.message) except KeyError, e: traceback.print_exc() log.exception(e) return self.fail("字段 %s 未提交" % e.message) except Exception, e: traceback.print_exc() log.exception(e) return self.fail("接口异常", e.__str__()) def fail(self, error="", msg=""): return jsonify({ "success": False, "error": error, "message": msg, "content": {} }) def get_search_fields(self, _clss, is_fuzzy=True): _filters = [] if not isinstance(_clss, list): _clss = [_clss]