2
0

common_service.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. import time
  2. import hashlib
  3. from service.base_service import BaseService
  4. from service.common.log_service import auto_log
  5. class CommonService(BaseService):
  6. def __init__(self):
  7. pass
  8. @classmethod
  9. @auto_log
  10. def signature_check(cls, timestamp: str, signature: str, md5_key: str)->tuple:
  11. """
  12. signature check
  13. :param timestamp:
  14. :param signature:
  15. :param md5_key:
  16. :return:
  17. """
  18. ori_str = timestamp + md5_key
  19. tar_str = hashlib.md5(ori_str.encode(encoding='utf-8')).hexdigest()
  20. if tar_str == signature:
  21. # The validity of the signature: 120s
  22. time_now_int = int(time.time())
  23. if abs(time_now_int - int(timestamp)) <= 120:
  24. # if abs(time_now_int - int(timestamp)) <= 12000000000000000:
  25. return True, ''
  26. else:
  27. msg = 'The signature you provide in request header is expire, please ensure in 120s'
  28. else:
  29. msg = 'The signature you provide in request header is invalid'
  30. return False, msg
  31. @classmethod
  32. @auto_log
  33. def gen_signature(cls, app_name: str)->tuple:
  34. """
  35. gen signature info
  36. :param app_name:
  37. :return:
  38. """
  39. from apps.account.models import AppToken
  40. app_obj = AppToken.objects.filter(app_name=app_name).first()
  41. md5_key = app_obj.token
  42. timestamp = str(int(time.time()))
  43. ori_str = timestamp + md5_key
  44. tar_str = hashlib.md5(ori_str.encode(encoding='utf-8')).hexdigest()
  45. return True, dict(signature=tar_str, timestamp=timestamp)
  46. @classmethod
  47. @auto_log
  48. def gen_signature_by_token(cls, token: str)->tuple:
  49. md5_key = token
  50. timestamp = str(int(time.time()))
  51. ori_str = timestamp + md5_key
  52. tar_str = hashlib.md5(ori_str.encode(encoding='utf-8')).hexdigest()
  53. return True, dict(signature=tar_str, timestamp=timestamp)
  54. @classmethod
  55. @auto_log
  56. def gen_hook_signature(cls, token: str)->tuple:
  57. """
  58. gen hook token signature
  59. :param token:
  60. :return:
  61. """
  62. timestamp = str(int(time.time()))
  63. ori_str = timestamp + token
  64. tar_str = hashlib.md5(ori_str.encode(encoding='utf-8')).hexdigest()
  65. return True, dict(signature=tar_str, timestamp=timestamp)
  66. @classmethod
  67. @auto_log
  68. def get_model_field(cls, app_name: str, model_name: str)->tuple:
  69. """
  70. get model's field list
  71. :param app_name:
  72. :param model_name:
  73. :return:
  74. """
  75. from django.apps import apps
  76. model_obj = apps.get_model(app_name, model_name)
  77. fields = model_obj._meta.fields
  78. field_dict = {}
  79. for field0 in fields:
  80. field_dict[field0.name] = field0.verbose_name
  81. return True, dict(field_dict=field_dict)
  82. @classmethod
  83. @auto_log
  84. def get_dict_blank_or_false_value_key_list(cls, dict_obj: object)->tuple:
  85. """
  86. get blank item value's key list in dict
  87. :param dict_obj:
  88. :return:
  89. """
  90. result_list = []
  91. for key, value in dict_obj.items():
  92. if not value:
  93. result_list.append(key)
  94. return True, dict(result_list=result_list)
  95. @classmethod
  96. @auto_log
  97. def check_dict_has_all_same_value(cls, dict_obj: object)->tuple:
  98. """
  99. check whether all key are equal in a dict
  100. :param dict_obj:
  101. :return:
  102. """
  103. value_list = []
  104. for key, value in dict_obj.items():
  105. value_list.append(value)
  106. value_0 = value_list[0]
  107. for value in value_list:
  108. if value_0 != value:
  109. return False, 'not all dict value is same'
  110. return True, ''
  111. @classmethod
  112. @auto_log
  113. def list_intersection(cls, list1, list2):
  114. """
  115. intersection between two list
  116. :param list1:
  117. :param list2:
  118. :return:
  119. """
  120. new_list = [val for val in list1 if val in list2]
  121. return True, new_list
  122. @classmethod
  123. @auto_log
  124. def list_union(cls, list1, list2):
  125. """
  126. union between two list
  127. :param list1:
  128. :param list2:
  129. :return:
  130. """
  131. new_list = list(set(list1).union(set(list2)))
  132. return True, new_list
  133. @classmethod
  134. @auto_log
  135. def list_difference(cls, list1, list2):
  136. """
  137. difference between two list
  138. :param list1:
  139. :param list2:
  140. :return:
  141. """
  142. new_list = list(set(list1).difference(set(list2)))
  143. return True, new_list
  144. @classmethod
  145. @auto_log
  146. def list_subtraction(cls, list1, list2):
  147. """
  148. subtraction between two list
  149. :param list1:
  150. :param list2:
  151. :return:
  152. """
  153. new_list = list(set(list1) - set(list2))
  154. return True, new_list
  155. common_service_ins = CommonService()
  156. if __name__ == '__main__':
  157. print(common_service_ins.check_dict_has_all_same_value({'a': {'a': 1, 'b': 2}, 'b': {'a': 1, 'b': 2}}))