card_add_agent_product_id.py 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. # coding=utf-8
  2. from base import init_env
  3. init_env(interactive = True)
  4. from apps.web.helpers import get_wechat_user_manager_mp_proxy
  5. from apps.web.user.models import Card
  6. from apps.web.agent.models import Agent
  7. agent_product_map = {}
  8. error_cards = []
  9. def write_record():
  10. with open("card_product_agent_id_add_record.txt", "wb") as f:
  11. for line in error_cards:
  12. f.write(line.encode("utf-8"))
  13. def get_product_agent_id(agentId):
  14. productId = agent_product_map.get(agentId, None)
  15. if not productId:
  16. agent = Agent.objects.get(id=agentId)
  17. productId = get_wechat_user_manager_mp_proxy(agent).occupantId
  18. agent_product_map[agentId] = productId
  19. return productId
  20. def main():
  21. cards = Card.objects.all()
  22. for card in cards:
  23. if card.agentId:
  24. card.productAgentId = get_product_agent_id(card.agentId)
  25. else:
  26. card.productAgentId = ""
  27. error_cards.append("%s\n" % str(card.id))
  28. try:
  29. card.save()
  30. except Exception as e:
  31. print e
  32. write_record()
  33. if __name__ == '__main__':
  34. main()