datasolve.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. from userprofile.models import Users
  2. import re, base64, json
  3. from rest_framework.exceptions import APIException
  4. def data_validate(data):
  5. script_obj = re.findall(r'script', str(data), re.IGNORECASE)
  6. select_obj = re.findall(r'select', str(data), re.IGNORECASE)
  7. if script_obj:
  8. raise APIException({'detail': 'Bad Data can‘not be store'})
  9. elif select_obj:
  10. raise APIException({'detail': 'Bad Data can‘not be store'})
  11. else:
  12. return data
  13. def qty_0_data_validate(data):
  14. script_obj = re.findall(r'script', str(data), re.IGNORECASE)
  15. select_obj = re.findall(r'select', str(data), re.IGNORECASE)
  16. if script_obj:
  17. raise APIException({'detail': 'Bad Data can‘not be store'})
  18. elif select_obj:
  19. raise APIException({'detail': 'Bad Data can‘not be store'})
  20. else:
  21. if data > 0:
  22. return data
  23. else:
  24. raise APIException({'detail': 'Qty Must > 0'})
  25. def qty_data_validate(data):
  26. script_obj = re.findall(r'script', str(data), re.IGNORECASE)
  27. select_obj = re.findall(r'select', str(data), re.IGNORECASE)
  28. if script_obj:
  29. raise APIException({'detail': 'Bad Data can‘not be store'})
  30. elif select_obj:
  31. raise APIException({'detail': 'Bad Data can‘not be store'})
  32. else:
  33. if data >= 0:
  34. return data
  35. else:
  36. raise APIException({'detail': 'Qty Must >= 0'})
  37. def openid_validate(data):
  38. if Users.objects.filter(openid=data).exists():
  39. return data
  40. else:
  41. raise APIException({'detail': 'User does not exists'})
  42. def appid_validate(data):
  43. if Users.objects.filter(appid=data).exists():
  44. return data
  45. else:
  46. raise APIException({'detail': 'User does not exists'})
  47. def asn_data_validate(data):
  48. script_obj = re.findall(r'script', str(data), re.IGNORECASE)
  49. select_obj = re.findall(r'select', str(data), re.IGNORECASE)
  50. if script_obj:
  51. raise APIException({'detail': 'Bad Data can‘not be store'})
  52. elif select_obj:
  53. raise APIException({'detail': 'Bad Data can‘not be store'})
  54. else:
  55. asn_last_code = re.findall(r'\d+', str(data), re.IGNORECASE)
  56. if str(asn_last_code[0]) == '00000001':
  57. data = 'ASN' + '00000001'
  58. else:
  59. data = 'ASN' + str(int(asn_last_code[0]) + 1).zfill(8)
  60. return data
  61. def dn_data_validate(data):
  62. script_obj = re.findall(r'script', str(data), re.IGNORECASE)
  63. select_obj = re.findall(r'select', str(data), re.IGNORECASE)
  64. if script_obj:
  65. raise APIException({'detail': 'Bad Data can‘not be store'})
  66. elif select_obj:
  67. raise APIException({'detail': 'Bad Data can‘not be store'})
  68. else:
  69. dn_last_code = re.findall(r'\d+', str(data), re.IGNORECASE)
  70. if str(dn_last_code[0]) == '00000001':
  71. data = 'DN' + '00000001'
  72. else:
  73. data = 'DN' + str(int(dn_last_code[0]) + 1).zfill(8)
  74. return data
  75. def sumOfList(list, size):
  76. if (size == 0):
  77. return 0
  78. else:
  79. return list[size - 1] + sumOfList(list, size - 1)
  80. def is_number(data):
  81. try:
  82. float(data)
  83. return True
  84. except ValueError:
  85. pass
  86. try:
  87. import unicodedata
  88. unicodedata.numeric(data)
  89. return True
  90. except (TypeError, ValueError):
  91. pass
  92. return False
  93. def secret_bar_code(data):
  94. return base64.b64encode(str(data).encode()).decode()
  95. def verify_bar_code(data):
  96. return json.loads(base64.b64decode(str(data).encode()).decode().replace('\'', '\"'))
  97. def transportation_calculate(weight, volume, weight_fee, volume_fee, min_fee):
  98. weight_cost = weight * weight_fee
  99. volume_cost = volume * volume_fee
  100. max_ = (weight_cost if weight_cost > volume_cost else volume_cost) if (weight_cost if weight_cost > volume_cost
  101. else volume_cost) > min_fee else min_fee
  102. data = round(max_, 2)
  103. return data