def staff_log(self, operator_id, station_id, type, staff_name, modify_content=None): """ 为员工模块的数据操作记录日志 :param self: 任务本身 :param operator_id: 操作人id :param station_id: 中转站id :param type: 操作类型,1:添加,2.删除,3.编辑 :param staff_name: 操作对象名称 :param modify_content: 编辑内容 :return: """ session = DBSession() if type == 1: detail = "添加员工({0})".format(staff_name) elif type == 2: detail = "删除员工({0})".format(staff_name) elif type == 3: detail = "{0}".format(modify_content) else: return "参数有误" log = models.OperationLog( log_type=1, # 1:员工 2:供货商 operation_object=staff_name, detail=detail, creator_id=operator_id, # 创建人 ID station_id=station_id # 中转站 ID ) session.add(log) session.commit() session.close() return "success"
def gen_msg_token(phone): s = DBSession() code = "".join(random.sample("123456789",4)) flag = False url = "http://106.ihuyi.cn/webservice/sms.php?method=Submit&account={account}&password={password}&mobile={phone}&content={content}".format(account=account,password=password,phone=phone,content=url_escape(content.format(code=code))) h = HTTPClient() try: res = h.fetch(url,connect_timeout = 5.0) except: flag,msg = sendTemplateSMS(phone,{code},32417) if flag: update_code(phone,code) return True else: return msg h.close() root = ElementTree.fromstring(res.body.decode()) if not root[0].text == '2': # print("[VerifyMsg]Send error:",root[0].text,root[1].text) #如果发送失败,则改用云通讯发送 flag,msg = sendTemplateSMS(phone,{code},32417) if flag: update_code(phone,code) return True else: return msg else: update_code(phone,code) return True
def run_statistics(self, user_id, shop_id, chain_shop_id, for_dates): """ 为指定日期运行采购统计 :param for_dates iterable %Y-%m-%d s """ session = DBSession() statistic_session = statistic_DBSession() for date in for_dates: try: date = datetime.datetime.strptime(date, "%Y-%m-%d").date() except ValueError: raise ValueError("invalid date format: {}".format(date)) date_delta = (datetime.date.today() - date).days pub_statistic.purchase_order_goods_firm(session, statistic_session, user_id, shop_id, chain_shop_id, date_delta) session.close() statistic_session.close()
def update_code(phone,code): s = DBSession() try: print("[VerifyMsg]VerifyCode",code,"have sent to phone:",phone) q = s.query(_VerifyCode).filter(_VerifyCode.phone == phone).one() except: q = None if q is not None: q.code = code q.create_time = datetime.datetime.now() q.count = 1 # q.phone = phone else: v = _VerifyCode() v.phone = phone v.code = code v.count = 1 # when count = 1,code is usefull s.add(v) s.commit() s.close() return True
def generating_acronym(): session = DBSession() firm_list = session.query(models.Firm)\ .filter(models.Firm.status == 0)\ .all() goods_list = session.query(models.Goods)\ .filter(models.Goods.status == 0)\ .all() for firm in firm_list: firm.name = firm.name for goods in goods_list: goods.name = goods.name session.commit() session.close() return "success"
def check_msg_token(phone, code): s = DBSession() try: q = s.query(_VerifyCode).filter_by(phone=phone).one() except: return False #print("[验证短信]验证码验证:",q.code,code,q.count,q.wx_id) # t = (datetime.datetime.now() - q.create_time) # if t.days == 0 and t.seconds >= 18000: if q.count != 1: return False if q.code == code: q.count = 0 # make the code useless s.commit() s.close() return True return False
def session(self): if hasattr(self, "_session"): return self._session self._session = DBSession() return self._session
class GlobalBaseHandler(BaseHandler): # senguocc数据库会话 @property def session(self): if hasattr(self, "_session"): return self._session self._session = DBSession() return self._session @property def statistic_session(self): if hasattr(self, "_statistic_session"): return self._statistic_session self._statistic_session = statistic_DBSession() return self._statistic_session # 关闭数据库会话 def on_finish(self): if hasattr(self, "_session"): self._session.close() if hasattr(self, "_statistic_session"): self._statistic_session.close() # 判断是否为微信浏览器 def is_wexin_browser(self): if "User-Agent" in self.request.headers: ua = self.request.headers["User-Agent"] else: ua = "" return "MicroMessenger" in ua # 判断是否为PC浏览器 def is_pc_browser(self): if "User-Agent" in self.request.headers: ua = self.request.headers["User-Agent"] else: ua = "" return not ("Mobile" in ua) #判断是否是采购助手APP def is_carrefour_app(self): if "User-Agent" in self.request.headers: ua = self.request.headers["User-Agent"] else: ua = "" return "senguo:cfapp" in ua # 错误页面的处理 def write_error(self, status_code, error_msg='', error_deal='', **kwargs): request_uri = self.request.uri method = self.request.method.upper() if status_code == 400: if method == "POST": self.send_fail("参数错误: %s" % error_msg, 400) else: self.render('notice/400.html', error_msg=error_msg, error_deal=error_deal, return_url="/") elif status_code == 404: if method == "POST": self.send_fail("地址错误", 404) else: self.render('notice/404.html', error_msg=error_msg, error_deal=error_deal, return_url="/") elif status_code == 500: # from handlers.server_alarm import ServerAlarm # ServerAlarm.send_server_error(self.session,request_uri,**kwargs) if method == "POST": self.send_fail("系统错误", 500) else: self.render('notice/500.html', error_msg=error_msg, error_deal=error_deal, return_url="/") elif status_code == 403: if method == "POST": if not self.current_user: return_url = self.reverse_url("Login") self.send_fail("您的登录已过期,请重新登录", 4031, return_url) else: self.send_fail("没有权限", 4032) else: self.render('notice/404.html', error_msg=error_msg, error_deal=error_deal, return_url="/") else: super(GlobalBaseHandler, self).write_error(status_code, **kwargs)
def purchasing_dynamics(self, record_type, purchase_goods_dict): """ 给采购小程序记录采购动态 """ record_type = record_type # 记录类型 goods_id = purchase_goods_dict["goods_id"] # 商品 ID purchaser_id = purchase_goods_dict["purchaser_id"] # 采购员 ID,不安排采购时为空 creator_id = purchase_goods_dict["creator_id"] # 操作用户 ID goods_name = purchase_goods_dict["goods_name"] # 商品名称 last_goods_name = purchase_goods_dict["last_goods_name"] # 上次的商品名称 last_goods_name = last_goods_name if last_goods_name else goods_name actual_amount = check_int(purchase_goods_dict["actual_amount"] * 100) # 实际采购件数 last_actual_amount = purchase_goods_dict["last_actual_amount"] # 上次的采购件数 last_actual_amount = last_actual_amount if last_actual_amount else actual_amount actual_weight = check_int(purchase_goods_dict["actual_weight"] * 100) # 实际采购重量 last_actual_weight = purchase_goods_dict["last_actual_weight"] # 上次的采购重量 last_actual_weight = last_actual_weight if last_actual_weight else actual_weight actual_unit = purchase_goods_dict["actual_unit"] # 实际采购单位 0: 件 1: 斤 price = check_int(purchase_goods_dict["price"] * 100) # 采购单价 last_price = purchase_goods_dict["last_price"] # 上次的采购单价 last_price = last_price if last_price else price subtotal = check_int(purchase_goods_dict["subtotal"] * 100) # 小计 last_subtotal = purchase_goods_dict["last_subtotal"] # 上次的采购小计 last_subtotal = last_subtotal if last_subtotal else subtotal firm_id = purchase_goods_dict["firm_id"] # 供货商id last_firm_id = purchase_goods_dict["last_firm_id"] # 上次的供货商id last_firm_id = last_firm_id if last_firm_id else firm_id payment = purchase_goods_dict["payment"] # 支付方式 0: 现金 1: 银行卡 2: 微信 3: 支付宝 4: 赊账 5: 其他 is_purchase = purchase_goods_dict["is_purchase"] # 0: 正常 1: 不采了 tag = purchase_goods_dict["tag"] # 商品标签 0: 正常 1:采购员手动添加 remarks = purchase_goods_dict["remarks"] # 采购备注 purchase_order_goods_id = purchase_goods_dict["id"] # 采购单商品 ID purchase_order_id = purchase_goods_dict["purchase_order_id"] # 采购单 ID wish_order_goods_id = purchase_goods_dict["wish_order_goods_id"] # 意向单商品 ID, 手动添加的采购商品,没有对应的意向单id session = DBSession() new_dynamic = models.PurchasingDynamics( record_type=record_type, goods_id=goods_id, purchaser_id=purchaser_id, creator_id=creator_id, goods_name=goods_name, last_goods_name=last_goods_name, actual_amount=actual_amount, last_actual_amount=last_actual_amount, actual_weight=actual_weight, last_actual_weight=last_actual_weight, actual_unit=actual_unit, price=price, last_price=last_price, subtotal=subtotal, last_subtotal=last_subtotal, firm_id=firm_id, last_firm_id=last_firm_id, payment=payment, is_purchase=is_purchase, tag=tag, remarks=remarks, purchase_order_goods_id=purchase_order_goods_id, purchase_order_id=purchase_order_id, wish_order_goods_id=wish_order_goods_id ) session.add(new_dynamic) session.commit() session.close() return "success"
class GlobalBaseHandler(BaseHandler): @property def session(self): if hasattr(self, "_session"): return self._session self._session = DBSession() return self._session @property def statistic_session(self): if hasattr(self, "_statistic_session"): return self._statistic_session self._statistic_session = statistic_DBSession() return self._statistic_session # 关闭数据库会话 def on_finish(self): if hasattr(self, "_session"): self._session.close() if hasattr(self, "_statistic_session"): self._statistic_session.close() # 判断是否为微信浏览器 def is_wexin_browser(self): if "User-Agent" in self.request.headers: ua = self.request.headers["User-Agent"] else: ua = "" return "MicroMessenger" in ua # 判断是否为PC浏览器 def is_pc_browser(self): if "User-Agent" in self.request.headers: ua = self.request.headers["User-Agent"] else: ua = "" return not ("Mobile" in ua) # 判断是否是采购助手APP def is_caigou_app(self): if "User-Agent" in self.request.headers: ua = self.request.headers["User-Agent"] else: ua = "" return "senguo:cgapp" in ua # 判断采购助手客户端操作系统 def client_os(self): ua = self.request.headers.get("User-Agent", "") if "senguo:ioscgapp" in ua: return "ios" elif "senguo:androidcgapp" in ua: return "android" else: return "" # 错误页面的处理 def write_error(self, status_code, error_msg='', error_deal='', **kwargs): if status_code == 400: self.send_fail("参数错误: %s" % error_msg, 400) elif status_code == 401: self.send_fail(error_msg or "未授权调用", 401) elif status_code == 404: self.send_fail(error_msg or "地址错误", 404) elif status_code == 500: from handlers.servererror import ServerAlarm ServerAlarm.send_server_error(self.request.uri, **kwargs) self.send_fail(error_msg or "系统错误", 500) elif status_code == 403: self.send_fail(error_msg or "没有权限", 403) else: super().write_error(status_code, **kwargs) # 导出csv文件 def export_to_csv(self, data_content, file_name): import csv import io csv_file = io.StringIO() csv_writer = csv.writer(csv_file) csv_writer.writerows(data_content) filename = 'attachment; filename="%s"' % (file_name + ".csv") self.set_header('Content-Type', 'application/octet-stream;charset=UTF-8') self.set_header('Content-Disposition', filename.encode("utf-8")) return self.write(csv_file.getvalue().encode("utf-8-sig")) def export_xlsx(self, workbook, filename): from openpyxl.writer.excel import save_virtual_workbook virtual_workbook = save_virtual_workbook(workbook) filename = 'attachment; filename="%s"' % (filename + ".xlsx") self.set_header('Content-Type', 'application/vnd.ms-excel;charset=UTF-8') self.set_header('Content-Disposition', filename.encode("utf-8")) return self.write(virtual_workbook)
from openpyxl import load_workbook sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "../"))) from dal.db_configs import DBSession import dal.models as models # TODO 先把这部分填了! WISH_ORDER_ID = 114 # 意向单 ID,意向单需要先手动创建 if __name__ == '__main__': if not WISH_ORDER_ID: raise Exception('先把代码里的常量填了!') session = DBSession() filename = '2018.12.4西青道店意向单.xlsx' wb = load_workbook(filename) sheet = wb.active # 从文件名里匹配门店名 matcher = re.search('\d([^\d]+)店', filename) if matcher: shop_name = matcher.group(1) else: raise Exception('{} 文件里没找到有效店名') # 把意向单拿出来 wish_order = models.WishOrder.get_by_id(session, WISH_ORDER_ID) if not wish_order:
import os,sys import datetime sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__),"../"))) import multiprocessing from multiprocessing import Process from sqlalchemy import or_ import tornado.web from tornado.options import options, define import dal.models as models from dal.db_configs import DBSession, statistic_DBSession, redis from handlers.base.pub_func import TimeFunc define("action", default="", help="") define("p2", default="", help="") session = DBSession() def set_latest_version(param): info = { "Android": { "verCode": "1801220947", "verName": "1.0.9", "verDate": "2018-01-23", "url": "http://d.senguo.cc/android/SenguoSecretary-V1.0.9-1801220947-release.apk", "log": "增加老板直接开票功能" } } redis.set("cgapp_update_info", json.dumps(info))
"大鹏": 62, "王哥": 61, "郭树": 72, "二胖": 80, "库房": 85, "八哥小号": 32, "树℡": 72, "朱会勇": 80, "仓库": 85, } if __name__ == '__main__': if not STATION_ID: raise Exception('先把代码里的常量填了!') session = DBSession() filename = '2018-12-30各店意向单(1).xlsx' wb = load_workbook(filename) sheet = wb.active # 先搞个意向单 wish_order = session.query(models.WishOrder) \ .filter(models.WishOrder.wish_date == WISH_DATE, models.WishOrder.station_id == STATION_ID) \ .first() if not wish_order: wish_order = models.WishOrder( wish_date=WISH_DATE, status=2, # 直接是提交状态,要改再说 station_id=STATION_ID,