Material.py 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. import json
  4. from alipay.aop.api.constant.ParamConstants import *
  5. from alipay.aop.api.domain.Article import Article
  6. from alipay.aop.api.domain.Text import Text
  7. class Material(object):
  8. def __init__(self):
  9. self._articles = None
  10. self._msg_type = None
  11. self._text = None
  12. @property
  13. def articles(self):
  14. return self._articles
  15. @articles.setter
  16. def articles(self, value):
  17. if isinstance(value, list):
  18. self._articles = list()
  19. for i in value:
  20. if isinstance(i, Article):
  21. self._articles.append(i)
  22. else:
  23. self._articles.append(Article.from_alipay_dict(i))
  24. @property
  25. def msg_type(self):
  26. return self._msg_type
  27. @msg_type.setter
  28. def msg_type(self, value):
  29. self._msg_type = value
  30. @property
  31. def text(self):
  32. return self._text
  33. @text.setter
  34. def text(self, value):
  35. if isinstance(value, Text):
  36. self._text = value
  37. else:
  38. self._text = Text.from_alipay_dict(value)
  39. def to_alipay_dict(self):
  40. params = dict()
  41. if self.articles:
  42. if isinstance(self.articles, list):
  43. for i in range(0, len(self.articles)):
  44. element = self.articles[i]
  45. if hasattr(element, 'to_alipay_dict'):
  46. self.articles[i] = element.to_alipay_dict()
  47. if hasattr(self.articles, 'to_alipay_dict'):
  48. params['articles'] = self.articles.to_alipay_dict()
  49. else:
  50. params['articles'] = self.articles
  51. if self.msg_type:
  52. if hasattr(self.msg_type, 'to_alipay_dict'):
  53. params['msg_type'] = self.msg_type.to_alipay_dict()
  54. else:
  55. params['msg_type'] = self.msg_type
  56. if self.text:
  57. if hasattr(self.text, 'to_alipay_dict'):
  58. params['text'] = self.text.to_alipay_dict()
  59. else:
  60. params['text'] = self.text
  61. return params
  62. @staticmethod
  63. def from_alipay_dict(d):
  64. if not d:
  65. return None
  66. o = Material()
  67. if 'articles' in d:
  68. o.articles = d['articles']
  69. if 'msg_type' in d:
  70. o.msg_type = d['msg_type']
  71. if 'text' in d:
  72. o.text = d['text']
  73. return o