upgrade_20180709.py 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. # -*- coding: utf-8 -*-
  2. # !/usr/bin/env python
  3. """
  4. Goals:
  5. 升级资金池授权相关
  6. """
  7. import os
  8. import sys
  9. from script.upgrade.executed.upgrade_reactor_withdraw import _WechatPaymentApp
  10. PROJECT_ROOT = os.path.join(os.path.abspath(os.path.split(os.path.realpath(__file__))[0] + "/.."), '..')
  11. sys.path.insert(0, PROJECT_ROOT)
  12. from django.conf import settings
  13. import logging
  14. from script.base import init_env
  15. logging.basicConfig(
  16. filename = 'upgrade_20180709.log',
  17. level = logging.INFO,
  18. format = '[%(asctime)s] {%(pathname)s:%(lineno)d} %(levelname)s - %(message)s',
  19. datefmt = '%H:%M:%S'
  20. )
  21. console = logging.StreamHandler()
  22. console.setLevel(logging.DEBUG)
  23. formatter = logging.Formatter('%(name)-12s: %(levelname)-8s %(message)s')
  24. console.setFormatter(formatter)
  25. logging.getLogger('').addHandler(console)
  26. logger = logging.getLogger(__name__)
  27. init_env(interactive = True)
  28. from apps.web.agent.models import Agent
  29. from apps.web.core.models import WechatAuthApp, WechatUserManagerApp, WechatManagerApp
  30. def update_agent():
  31. for agent in Agent.objects().all():
  32. # 如果配置了资金池, 那么auth, pay, manager全部是一个
  33. if agent.appid:
  34. payApp = _WechatPaymentApp()
  35. payApp.appid = agent.appid
  36. payApp.mchid = agent.mchid
  37. payApp.apikey = agent.apikey
  38. payApp.sslcert_path = agent.ssl_cert
  39. payApp.sslkey_path = agent.ssl_key
  40. payApp.rootca_path = agent.rootca_path
  41. if not agent.userAppId or agent.userAppId != agent.appid:
  42. print 'error userAppId. agentId = %s' % str(agent.id)
  43. continue
  44. payApp.secret = agent.userSecretId
  45. print 'new pay app. id = %s; app = %s' % (str(agent.id), payApp.to_dict())
  46. agent.wechatPayApp = payApp
  47. agent.save()
  48. agent.wechatLoginAuthApp = WechatAuthApp(appid = payApp.appid, secret = payApp.secret)
  49. agent.save()
  50. agent.wechatUserManagerialApp = WechatUserManagerApp(appid = payApp.appid, secret = payApp.secret,
  51. templateIdMap = WechatUserManagerApp.MESSAGE_TEMPLATE)
  52. agent.save()
  53. for templateName in WechatUserManagerApp.MESSAGE_TEMPLATE.keys():
  54. if 'user' in agent.templateIdMap and templateName in agent.templateIdMap['user'] and \
  55. agent.templateIdMap['user'][templateName] is not None:
  56. agent.wechatUserManagerialApp.templateIdMap[templateName] = {
  57. 'templateId': agent.templateIdMap['user'][templateName],
  58. 'context': settings.WECHAT_MESSAGE_TEMPLATE_MAP['user'][templateName]['context']}
  59. elif 'user' in agent.primary_agent.templateIdMap and templateName in \
  60. agent.primary_agent.templateIdMap['user'] and agent.primary_agent.templateIdMap['user'][
  61. templateName] is not None:
  62. agent.wechatUserManagerialApp.templateIdMap[templateName] = {
  63. 'templateId': agent.primary_agent.templateIdMap['user'][templateName],
  64. 'context': settings.WECHAT_MESSAGE_TEMPLATE_MAP['user'][templateName]['context']
  65. }
  66. print 'new user manager app. id = %s; app = %s' % (
  67. str(agent.id), agent.wechatUserManagerialApp.to_dict())
  68. agent.wechatDealerManagerialApp = WechatManagerApp(appid = payApp.appid, secret = payApp.secret,
  69. templateIdMap = WechatManagerApp.MESSAGE_TEMPLATE)
  70. agent.save()
  71. for templateName in settings.WECHAT_MESSAGE_TEMPLATE_MAP['dealer'].keys():
  72. if 'dealer' in agent.templateIdMap and templateName in agent.templateIdMap['dealer'] and \
  73. agent.templateIdMap['dealer'][templateName] is not None:
  74. agent.wechatDealerManagerialApp.templateIdMap[templateName] = {
  75. 'templateId': agent.templateIdMap['dealer'][templateName],
  76. 'context': settings.WECHAT_MESSAGE_TEMPLATE_MAP['dealer'][templateName]['context']
  77. }
  78. elif 'dealer' in agent.primary_agent.templateIdMap and templateName in \
  79. agent.primary_agent.templateIdMap['dealer'] and agent.primary_agent.templateIdMap['dealer'][
  80. templateName] is not None:
  81. agent.wechatDealerManagerialApp.templateIdMap[templateName] = {
  82. 'templateId': agent.primary_agent.templateIdMap['dealer'][templateName],
  83. 'context': settings.WECHAT_MESSAGE_TEMPLATE_MAP['dealer'][templateName]['context']
  84. }
  85. agent.save()
  86. print 'new dealer manager app. id = %s; app = %s' % (
  87. str(agent.id), agent.wechatDealerManagerialApp.to_dict())
  88. elif agent.dealerAppId:
  89. dealerManagerApp = WechatManagerApp()
  90. dealerManagerApp.appid = agent.dealerAppId
  91. dealerManagerApp.secret = agent.dealerSecretId
  92. dealerManagerApp.templateIdMap = WechatManagerApp.MESSAGE_TEMPLATE
  93. agent.wechatDealerManagerialApp = dealerManagerApp
  94. agent.save()
  95. userManagerApp = WechatUserManagerApp()
  96. userManagerApp.appid = agent.dealerAppId
  97. userManagerApp.secret = agent.dealerSecretId
  98. userManagerApp.templateIdMap = WechatUserManagerApp.MESSAGE_TEMPLATE
  99. agent.wechatUserManagerialApp = userManagerApp
  100. agent.save()
  101. for templateName in settings.WECHAT_MESSAGE_TEMPLATE_MAP['dealer'].keys():
  102. if 'dealer' in agent.templateIdMap and templateName in agent.templateIdMap['dealer'] and \
  103. agent.templateIdMap['dealer'][templateName] is not None:
  104. agent.wechatDealerManagerialApp.templateIdMap[templateName] = {
  105. 'templateId': agent.templateIdMap['dealer'][templateName],
  106. 'context': settings.WECHAT_MESSAGE_TEMPLATE_MAP['dealer'][templateName]['context']
  107. }
  108. elif 'dealer' in agent.primary_agent.templateIdMap and templateName in \
  109. agent.primary_agent.templateIdMap['dealer'] and agent.primary_agent.templateIdMap['dealer'][
  110. templateName] is not None:
  111. agent.wechatDealerManagerialApp.templateIdMap[templateName] = {
  112. 'templateId': agent.primary_agent.templateIdMap['dealer'][templateName],
  113. 'context': settings.WECHAT_MESSAGE_TEMPLATE_MAP['dealer'][templateName]['context']
  114. }
  115. print 'new dealer manager app. id = %s; app = %s' % (
  116. str(agent.id), agent.wechatDealerManagerialApp.to_dict())
  117. for templateName in WechatUserManagerApp.MESSAGE_TEMPLATE.keys():
  118. if 'user' in agent.templateIdMap and templateName in agent.templateIdMap['user'] and \
  119. agent.templateIdMap['user'][templateName] is not None:
  120. agent.wechatUserManagerialApp.templateIdMap[templateName] = {
  121. 'templateId': agent.templateIdMap['user'][templateName],
  122. 'context': settings.WECHAT_MESSAGE_TEMPLATE_MAP['user'][templateName]['context']
  123. }
  124. elif 'user' in agent.primary_agent.templateIdMap and templateName in \
  125. agent.primary_agent.templateIdMap['user'] and agent.primary_agent.templateIdMap['user'][
  126. templateName] is not None:
  127. agent.wechatUserManagerialApp.templateIdMap[templateName] = {
  128. 'templateId': agent.primary_agent.templateIdMap['user'][templateName],
  129. 'context': settings.WECHAT_MESSAGE_TEMPLATE_MAP['user'][templateName]['context']
  130. }
  131. print 'new user manager app. id = %s; app = %s' % (
  132. str(agent.id), agent.wechatUserManagerialApp.to_dict())
  133. agent.save()
  134. if __name__ == '__main__':
  135. print 'started'
  136. update_agent()
  137. print 'finished!'