2
0

common_service.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. def gen_signature_by_token(cls, token: str)->tuple:
  48. md5_key = token
  49. timestamp = str(int(time.time()))
  50. ori_str = timestamp + md5_key
  51. signature = hashlib.md5(ori_str.encode(encoding='utf-8')).hexdigest()
  52. return signature, timestamp
  53. @classmethod
  54. @auto_log
  55. def gen_hook_signature(cls, token: str)->tuple:
  56. """
  57. gen hook token signature
  58. :param token:
  59. :return:
  60. """
  61. timestamp = str(int(time.time()))
  62. ori_str = timestamp + token
  63. tar_str = hashlib.md5(ori_str.encode(encoding='utf-8')).hexdigest()
  64. return True, dict(signature=tar_str, timestamp=timestamp)
  65. @classmethod
  66. @auto_log
  67. def get_model_field(cls, app_name: str, model_name: str)->tuple:
  68. """
  69. get model's field list
  70. :param app_name:
  71. :param model_name:
  72. :return:
  73. """
  74. from django.apps import apps
  75. model_obj = apps.get_model(app_name, model_name)
  76. fields = model_obj._meta.fields
  77. field_dict = {}
  78. for field0 in fields:
  79. field_dict[field0.name] = field0.verbose_name
  80. return True, dict(field_dict=field_dict)
  81. @classmethod
  82. @auto_log
  83. def get_dict_blank_or_false_value_key_list(cls, dict_obj: object)->tuple:
  84. """
  85. get blank item value's key list in dict
  86. :param dict_obj:
  87. :return:
  88. """
  89. result_list = []
  90. for key, value in dict_obj.items():
  91. if not value:
  92. result_list.append(key)
  93. return True, dict(result_list=result_list)
  94. @classmethod
  95. @auto_log
  96. def check_dict_has_all_same_value(cls, dict_obj: object)->tuple:
  97. """
  98. check whether all key are equal in a dict
  99. :param dict_obj:
  100. :return:
  101. """
  102. value_list = []
  103. for key, value in dict_obj.items():
  104. value_list.append(value)
  105. value_0 = value_list[0]
  106. for value in value_list:
  107. if value_0 != value:
  108. return False, 'not all dict value is same'
  109. return True, ''
  110. @classmethod
  111. @auto_log
  112. def list_intersection(cls, list1, list2):
  113. """
  114. intersection between two list
  115. :param list1:
  116. :param list2:
  117. :return:
  118. """
  119. new_list = [val for val in list1 if val in list2]
  120. return True, new_list
  121. @classmethod
  122. @auto_log
  123. def list_union(cls, list1, list2):
  124. """
  125. union between two list
  126. :param list1:
  127. :param list2:
  128. :return:
  129. """
  130. new_list = list(set(list1).union(set(list2)))
  131. return True, new_list
  132. @classmethod
  133. @auto_log
  134. def list_difference(cls, list1, list2):
  135. """
  136. difference between two list
  137. :param list1:
  138. :param list2:
  139. :return:
  140. """
  141. new_list = list(set(list1).difference(set(list2)))
  142. return True, new_list
  143. @classmethod
  144. @auto_log
  145. def list_subtraction(cls, list1, list2):
  146. """
  147. subtraction between two list
  148. :param list1:
  149. :param list2:
  150. :return:
  151. """
  152. new_list = list(set(list1) - set(list2))
  153. return True, new_list
  154. common_service_ins = CommonService()
  155. if __name__ == '__main__':
  156. print(common_service_ins.check_dict_has_all_same_value({'a': {'a': 1, 'b': 2}, 'b': {'a': 1, 'b': 2}}))