def index(): """默认的Get请求""" date_begin = request.args.get("date_begin") date_end = request.args.get("date_end") mode = request.args.get("mode") service = StatisticsService() GLOBAL_CONFIG.reload_config_file() title_line, data, total_line, type_list = service.get_data_from_db( date_begin, date_end, mode) logger.info('GET请求/api/icbc 处理完毕,返回值%s' % "success") return render_template("dashboard.html", title_line=title_line, data=data, total_line=total_line)
def make_statistics(): """POST方法,用于生成统计信息""" if not check_args(['date_begin', "date_end", 'mode']): return "args_missing", 400 date_begin, date_end, mode = g.data["date_begin"], g.data[ "date_end"], g.data["mode"] logger.info("用户[%s]请求统计业绩:" % g.user_name) result = False try: service = StatisticsService() GLOBAL_CONFIG.reload_config_file() result = service.create_files(date_begin, date_end, mode) except Exception as e: logger.error('发生错误!') logger.error(e) finally: return ("success", 201) if result else ("fail", 500)
def __init__(self): try: log_config_file_name = GLOBAL_CONFIG.get_field("Log", "log_config") # log_config_file_name = "logging_config.ini" fileConfig(log_config_file_name) except Exception as e: # TODO: 默认的logger pass finally: self._logger = logging.getLogger()
def send_email(): """POST方法,用于获取邮件""" if not check_args(['date_begin', "date_end", 'mode']): return "args_missing", 400 date_begin, date_end = g.data["date_begin"], g.data["date_end"] count_only = True if (g.data["count_only"] == "true") else False logger.info("用户[%s]请求发送业绩统计邮件:" % g.user_name) result = False try: service = EmailService() mode = GLOBAL_CONFIG.get_field("Setting", "mode") GLOBAL_CONFIG.reload_config_file() if mode == "test": GLOBAL_CONFIG.set_field("Setting", "mode", mode) result = service.send_range_email(date_begin, date_end, count_only) except Exception as e: logger.error('发生错误!') logger.error(e) finally: return ("success", 201) if result else ("fail", 500)
def send_range_email(self, date_begin, date_end, count_only=True, gather=False): try: self.read_config() title_line, data, total_line, type_list = StatisticsService().get_data_from_db(date_begin, date_end, gather) logger.info("统计数据Data构造成功") if date_begin == date_end: subject = "{} 网点报送汇总".format(date_begin) xls_file_name = os.path.join(GLOBAL_CONFIG.get_field("Excel", "xls_dir"), date_begin) + ".xls" html_file_name = os.path.join(GLOBAL_CONFIG.get_field("Html", "html_dir"), date_begin) + ".html" else: subject = "{} ~ {} 网点报送汇总".format(date_begin, date_end) xls_file_name = os.path.join(GLOBAL_CONFIG.get_field("Excel", "xls_dir"), "{} ~ {}".format(date_begin, date_end)) + ".xls" html_file_name = os.path.join(GLOBAL_CONFIG.get_field("Html", "html_dir"), "{} ~ {}".format(date_begin, date_end)) + ".html" html_content = StatisticsService().data_to_html(subject, title_line, data, total_line) StatisticsService().html_2_file(html_content, html_file_name) logger.info("html内容构造成功") attachment_name = StatisticsService().data_to_xls(xls_file_name, title_line, data, total_line, type_list) logger.info("xls内容构造成功") except Exception as e: logger.error("Error: 内容构造失败:") logger.error(e) return False else: if not count_only: try: msg = self.make_msg('望京支行机构金融业务部', '望京支行对公团队', '每日统计_' + date_begin, html_content, xls_file_name) server = smtplib.SMTP_SSL(self._smtp_server, 465) server.login(self._from_addr, self._password) server.sendmail(self._from_addr, self._to_addr, msg.as_string()) except smtplib.SMTPException as e: logger.error("Error: 无法发送邮件:") logger.error(e) return False else: # server.quit() logger.info("发送邮件成功") return True else: return True
def connect_url(): # section = GLOBAL_CONFIG.get_section("Database") mode = GLOBAL_CONFIG.get_field("Setting", "mode") # logger.info(section) user_name = GLOBAL_CONFIG.get_field("Database", "user_name") pass_word = GLOBAL_CONFIG.get_field("Database", "pass_word") server_address = GLOBAL_CONFIG.get_field("Database", "server_address") server_port = GLOBAL_CONFIG.get_field("Database", "server_port") database = GLOBAL_CONFIG.get_field("Database", "database") if mode == "test": database = GLOBAL_CONFIG.get_field("Database", "test_database") url = "%s:%s@%s:%s/%s" % (user_name, pass_word, server_address, server_port, database) return url
def create_files(date_begin, date_end, mode): try: title_line, data, total_line, type_list = StatisticsService( ).get_data_from_db(date_begin, date_end, mode) logger.info("统计数据Data构造成功") if mode == "daily": subject = "{} 网点报送汇总".format(date_begin) xls_file_name = os.path.join( GLOBAL_CONFIG.get_field("Excel", "xls_dir"), date_begin) + ".xls" html_file_name = os.path.join( GLOBAL_CONFIG.get_field("Html", "html_dir"), date_begin) + ".html" elif mode == "range": subject = "{} ~ {} 网点报送汇总".format(date_begin, date_end) xls_file_name = os.path.join( GLOBAL_CONFIG.get_field("Excel", "xls_dir"), "{}_{}_汇总".format(date_begin, date_end)) + ".xls" html_file_name = os.path.join( GLOBAL_CONFIG.get_field("Html", "html_dir"), "{}_{}_汇总".format(date_begin, date_end)) + ".html" else: subject = "{} ~ {} 网点报送明细".format(date_begin, date_end) xls_file_name = os.path.join( GLOBAL_CONFIG.get_field("Excel", "xls_dir"), "{}_{}_明细".format(date_begin, date_end)) + ".xls" html_file_name = os.path.join( GLOBAL_CONFIG.get_field("Html", "html_dir"), "{}_{}_明细".format(date_begin, date_end)) + ".html" # Html html_style_list = HtmlService().get_style_list(mode) html_content = HtmlService().data_to_html(subject, title_line, data, total_line, html_style_list) HtmlService().html_2_file(html_content, html_file_name) logger.info("html内容构造成功:" + html_file_name) # xls XlsService().data_to_xls(xls_file_name, title_line, data, total_line, type_list, mode) logger.info("xls内容构造成功:" + xls_file_name) return True except Exception as e: logger.error("Error: 内容构造失败:") logger.error(e) return False
def read_config(self): self._from_addr = GLOBAL_CONFIG.get_field("Email", "from_addr") self._password = GLOBAL_CONFIG.get_field("Email", "password") self._to_addr = GLOBAL_CONFIG.get_field_list("Email", "to_addr") self._smtp_server = GLOBAL_CONFIG.get_field("Email", "smtp_server")