base.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. #coding=utf-8
  2. from utils.exceptions import CustomError
  3. import re
  4. import tablib
  5. import datetime
  6. from openpyxl.utils.exceptions import InvalidFileException
  7. class Formater():
  8. @staticmethod
  9. def formatStr(value):
  10. res = u''
  11. if value != None:
  12. try:
  13. res = str(value)
  14. except:
  15. pass
  16. return res
  17. @staticmethod
  18. def formatCount(value):
  19. return int(round(float(value or 0) * 100,0))
  20. @staticmethod
  21. def formatPrice(value):
  22. return int(round(float(value or 0) * 100,0))
  23. @staticmethod
  24. def formatCountShow(value):
  25. return '%.2f' % (float(value or 0)/100.0)
  26. @staticmethod
  27. def formatPriceShow(value):
  28. return '%.2f' % (float(value or 0)/100.0)
  29. @staticmethod
  30. def formatAmount(value):
  31. return int(round(float(value or 0) * 10000,0))
  32. @staticmethod
  33. def formatAmountShow(value):
  34. return '%.2f' % (float(value or 0) / 10000.0 + 0.0000001)
  35. class CustomFormaterByUser():
  36. @staticmethod
  37. def formatEmptyStr(value,user):
  38. return Formater.formatEmptyStr(value)
  39. @staticmethod
  40. def formatEmptyFloat(value, user):
  41. #return round(value or 0, 2)
  42. return Formater.formatEmptyFloat(value)
  43. @staticmethod
  44. def formatTime(value,user):
  45. return Formater.formatTime(value)
  46. @staticmethod
  47. def formatDate(value, user):
  48. return Formater.formatDate(value)
  49. @staticmethod
  50. def formatCountShow(value,user):
  51. return Formater.formatCountShow(value)
  52. @staticmethod
  53. def formatPartAmountShow(value, user):
  54. return Formater.formatAmountShow(value)
  55. class ExcelImporter():
  56. def getExcelData(self,file):
  57. if not file:
  58. raise CustomError(u'请上传数据文件')
  59. try:
  60. data = tablib.import_set(file.read(), format='xlsx').dict
  61. if not len(data):
  62. raise CustomError(u'上传的文件内没有发现数据')
  63. return data
  64. except InvalidFileException:
  65. raise CustomError(u'请上传<strong>xlsx</strong>格式的数据文件,老版本的xls格式不被支持!')
  66. def validRow(self,row):
  67. data = {}
  68. for (k,v) in self.fields.items():
  69. is_required = v[0]
  70. format_proc = v[1]
  71. try:
  72. value = row[k]
  73. except:
  74. raise CustomError(u'缺少[%s]列,请检查模板文件或重新下载' % k)
  75. if is_required and value == None:
  76. raise CustomError(u'%s:为必填项' % k)
  77. if format_proc and value != None:
  78. try:
  79. value = format_proc(value)
  80. except CustomError as e:
  81. raise CustomError(u'%s:错误的值[%s]' % (k,e.get_error_msg()))
  82. data[k] = value
  83. return data
  84. @staticmethod
  85. def formatUnicode(value):
  86. try:
  87. return str(value).strip(u' ')
  88. except:
  89. return ''
  90. @staticmethod
  91. def formatInt(value):
  92. try:
  93. return int(float(str(value).strip(u' ')))
  94. except:
  95. raise CustomError(u'不能转换为整数')
  96. @staticmethod
  97. def formatFloat(value):
  98. try:
  99. return round(float(str(value).strip(u' ')) or 0,2)
  100. except:
  101. raise CustomError(u'不能转换为小数')
  102. @staticmethod
  103. def formatFloatGtZ(value):
  104. v = ExcelImporter.formatFloat(value)
  105. if v <= 0:
  106. raise CustomError(u'必须大于0')
  107. return v
  108. @staticmethod
  109. def formatFloatGeZ(value):
  110. v = ExcelImporter.formatFloat(value)
  111. if v < 0:
  112. raise CustomError(u'必须大于等于0')
  113. return v
  114. @staticmethod
  115. def formatTel(value):
  116. try:
  117. value = str(value).strip(u' ')
  118. except:
  119. value = ''
  120. if value == '':
  121. return value
  122. is_mobile = re.match(r'^0?(13[0-9]|14[0-9]|15[0-9]|17[0-9]|18[0-9]|19[0-9]|166)[0-9]{8}$', value)
  123. is_tel = re.match(r'^([0-9]{3,4}-)?[0-9]{7,8}$', value)
  124. if not (is_mobile or is_tel):
  125. raise CustomError(u'不合法的手机号或者固话号')
  126. return value
  127. @staticmethod
  128. def formatDate(value):
  129. try:
  130. return datetime.datetime.strptime(value, '%Y-%m-%d').date()
  131. except:
  132. raise CustomError(u'日期格式错误,格式为:2018-01-01')