# -*- coding: utf-8 -*- # !/usr/bin/env python """ Goals: 升级资金池授权相关 """ import os import sys from script.upgrade.executed.upgrade_reactor_withdraw import _WechatPaymentApp PROJECT_ROOT = os.path.join(os.path.abspath(os.path.split(os.path.realpath(__file__))[0] + "/.."), '..') sys.path.insert(0, PROJECT_ROOT) from django.conf import settings import logging from script.base import init_env logging.basicConfig( filename = 'upgrade_20180709.log', level = logging.INFO, format = '[%(asctime)s] {%(pathname)s:%(lineno)d} %(levelname)s - %(message)s', datefmt = '%H:%M:%S' ) console = logging.StreamHandler() console.setLevel(logging.DEBUG) formatter = logging.Formatter('%(name)-12s: %(levelname)-8s %(message)s') console.setFormatter(formatter) logging.getLogger('').addHandler(console) logger = logging.getLogger(__name__) init_env(interactive = True) from apps.web.agent.models import Agent from apps.web.core.models import WechatAuthApp, WechatUserManagerApp, WechatManagerApp def update_agent(): for agent in Agent.objects().all(): # 如果配置了资金池, 那么auth, pay, manager全部是一个 if agent.appid: payApp = _WechatPaymentApp() payApp.appid = agent.appid payApp.mchid = agent.mchid payApp.apikey = agent.apikey payApp.sslcert_path = agent.ssl_cert payApp.sslkey_path = agent.ssl_key payApp.rootca_path = agent.rootca_path if not agent.userAppId or agent.userAppId != agent.appid: print 'error userAppId. agentId = %s' % str(agent.id) continue payApp.secret = agent.userSecretId print 'new pay app. id = %s; app = %s' % (str(agent.id), payApp.to_dict()) agent.wechatPayApp = payApp agent.save() agent.wechatLoginAuthApp = WechatAuthApp(appid = payApp.appid, secret = payApp.secret) agent.save() agent.wechatUserManagerialApp = WechatUserManagerApp(appid = payApp.appid, secret = payApp.secret, templateIdMap = WechatUserManagerApp.MESSAGE_TEMPLATE) agent.save() for templateName in WechatUserManagerApp.MESSAGE_TEMPLATE.keys(): if 'user' in agent.templateIdMap and templateName in agent.templateIdMap['user'] and \ agent.templateIdMap['user'][templateName] is not None: agent.wechatUserManagerialApp.templateIdMap[templateName] = { 'templateId': agent.templateIdMap['user'][templateName], 'context': settings.WECHAT_MESSAGE_TEMPLATE_MAP['user'][templateName]['context']} elif 'user' in agent.primary_agent.templateIdMap and templateName in \ agent.primary_agent.templateIdMap['user'] and agent.primary_agent.templateIdMap['user'][ templateName] is not None: agent.wechatUserManagerialApp.templateIdMap[templateName] = { 'templateId': agent.primary_agent.templateIdMap['user'][templateName], 'context': settings.WECHAT_MESSAGE_TEMPLATE_MAP['user'][templateName]['context'] } print 'new user manager app. id = %s; app = %s' % ( str(agent.id), agent.wechatUserManagerialApp.to_dict()) agent.wechatDealerManagerialApp = WechatManagerApp(appid = payApp.appid, secret = payApp.secret, templateIdMap = WechatManagerApp.MESSAGE_TEMPLATE) agent.save() for templateName in settings.WECHAT_MESSAGE_TEMPLATE_MAP['dealer'].keys(): if 'dealer' in agent.templateIdMap and templateName in agent.templateIdMap['dealer'] and \ agent.templateIdMap['dealer'][templateName] is not None: agent.wechatDealerManagerialApp.templateIdMap[templateName] = { 'templateId': agent.templateIdMap['dealer'][templateName], 'context': settings.WECHAT_MESSAGE_TEMPLATE_MAP['dealer'][templateName]['context'] } elif 'dealer' in agent.primary_agent.templateIdMap and templateName in \ agent.primary_agent.templateIdMap['dealer'] and agent.primary_agent.templateIdMap['dealer'][ templateName] is not None: agent.wechatDealerManagerialApp.templateIdMap[templateName] = { 'templateId': agent.primary_agent.templateIdMap['dealer'][templateName], 'context': settings.WECHAT_MESSAGE_TEMPLATE_MAP['dealer'][templateName]['context'] } agent.save() print 'new dealer manager app. id = %s; app = %s' % ( str(agent.id), agent.wechatDealerManagerialApp.to_dict()) elif agent.dealerAppId: dealerManagerApp = WechatManagerApp() dealerManagerApp.appid = agent.dealerAppId dealerManagerApp.secret = agent.dealerSecretId dealerManagerApp.templateIdMap = WechatManagerApp.MESSAGE_TEMPLATE agent.wechatDealerManagerialApp = dealerManagerApp agent.save() userManagerApp = WechatUserManagerApp() userManagerApp.appid = agent.dealerAppId userManagerApp.secret = agent.dealerSecretId userManagerApp.templateIdMap = WechatUserManagerApp.MESSAGE_TEMPLATE agent.wechatUserManagerialApp = userManagerApp agent.save() for templateName in settings.WECHAT_MESSAGE_TEMPLATE_MAP['dealer'].keys(): if 'dealer' in agent.templateIdMap and templateName in agent.templateIdMap['dealer'] and \ agent.templateIdMap['dealer'][templateName] is not None: agent.wechatDealerManagerialApp.templateIdMap[templateName] = { 'templateId': agent.templateIdMap['dealer'][templateName], 'context': settings.WECHAT_MESSAGE_TEMPLATE_MAP['dealer'][templateName]['context'] } elif 'dealer' in agent.primary_agent.templateIdMap and templateName in \ agent.primary_agent.templateIdMap['dealer'] and agent.primary_agent.templateIdMap['dealer'][ templateName] is not None: agent.wechatDealerManagerialApp.templateIdMap[templateName] = { 'templateId': agent.primary_agent.templateIdMap['dealer'][templateName], 'context': settings.WECHAT_MESSAGE_TEMPLATE_MAP['dealer'][templateName]['context'] } print 'new dealer manager app. id = %s; app = %s' % ( str(agent.id), agent.wechatDealerManagerialApp.to_dict()) for templateName in WechatUserManagerApp.MESSAGE_TEMPLATE.keys(): if 'user' in agent.templateIdMap and templateName in agent.templateIdMap['user'] and \ agent.templateIdMap['user'][templateName] is not None: agent.wechatUserManagerialApp.templateIdMap[templateName] = { 'templateId': agent.templateIdMap['user'][templateName], 'context': settings.WECHAT_MESSAGE_TEMPLATE_MAP['user'][templateName]['context'] } elif 'user' in agent.primary_agent.templateIdMap and templateName in \ agent.primary_agent.templateIdMap['user'] and agent.primary_agent.templateIdMap['user'][ templateName] is not None: agent.wechatUserManagerialApp.templateIdMap[templateName] = { 'templateId': agent.primary_agent.templateIdMap['user'][templateName], 'context': settings.WECHAT_MESSAGE_TEMPLATE_MAP['user'][templateName]['context'] } print 'new user manager app. id = %s; app = %s' % ( str(agent.id), agent.wechatUserManagerialApp.to_dict()) agent.save() if __name__ == '__main__': print 'started' update_agent() print 'finished!'