123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157 |
- #coding=utf-8
- from utils.exceptions import CustomError
- import re
- import tablib
- import datetime
- from openpyxl.utils.exceptions import InvalidFileException
- class Formater():
- @staticmethod
- def formatStr(value):
- res = u''
- if value != None:
- try:
- res = str(value)
- except:
- pass
- return res
- @staticmethod
- def formatCount(value):
- return int(round(float(value or 0) * 100,0))
- @staticmethod
- def formatPrice(value):
- return int(round(float(value or 0) * 100,0))
- @staticmethod
- def formatCountShow(value):
- return '%.2f' % (float(value or 0)/100.0)
- @staticmethod
- def formatPriceShow(value):
- return '%.2f' % (float(value or 0)/100.0)
- @staticmethod
- def formatAmount(value):
- return int(round(float(value or 0) * 10000,0))
- @staticmethod
- def formatAmountShow(value):
- return '%.2f' % (float(value or 0) / 10000.0 + 0.0000001)
- class CustomFormaterByUser():
- @staticmethod
- def formatEmptyStr(value,user):
- return Formater.formatEmptyStr(value)
- @staticmethod
- def formatEmptyFloat(value, user):
- #return round(value or 0, 2)
- return Formater.formatEmptyFloat(value)
- @staticmethod
- def formatTime(value,user):
- return Formater.formatTime(value)
- @staticmethod
- def formatDate(value, user):
- return Formater.formatDate(value)
- @staticmethod
- def formatCountShow(value,user):
- return Formater.formatCountShow(value)
- @staticmethod
- def formatPartAmountShow(value, user):
- return Formater.formatAmountShow(value)
- class ExcelImporter():
- def getExcelData(self,file):
- if not file:
- raise CustomError(u'请上传数据文件')
- try:
- data = tablib.import_set(file.read(), format='xlsx').dict
- if not len(data):
- raise CustomError(u'上传的文件内没有发现数据')
- return data
- except InvalidFileException:
- raise CustomError(u'请上传<strong>xlsx</strong>格式的数据文件,老版本的xls格式不被支持!')
- def validRow(self,row):
- data = {}
- for (k,v) in self.fields.items():
- is_required = v[0]
- format_proc = v[1]
- try:
- value = row[k]
- except:
- raise CustomError(u'缺少[%s]列,请检查模板文件或重新下载' % k)
- if is_required and value == None:
- raise CustomError(u'%s:为必填项' % k)
- if format_proc and value != None:
- try:
- value = format_proc(value)
- except CustomError as e:
- raise CustomError(u'%s:错误的值[%s]' % (k,e.get_error_msg()))
- data[k] = value
- return data
- @staticmethod
- def formatUnicode(value):
- try:
- return str(value).strip(u' ')
- except:
- return ''
- @staticmethod
- def formatInt(value):
- try:
- return int(float(str(value).strip(u' ')))
- except:
- raise CustomError(u'不能转换为整数')
- @staticmethod
- def formatFloat(value):
- try:
- return round(float(str(value).strip(u' ')) or 0,2)
- except:
- raise CustomError(u'不能转换为小数')
- @staticmethod
- def formatFloatGtZ(value):
- v = ExcelImporter.formatFloat(value)
- if v <= 0:
- raise CustomError(u'必须大于0')
- return v
- @staticmethod
- def formatFloatGeZ(value):
- v = ExcelImporter.formatFloat(value)
- if v < 0:
- raise CustomError(u'必须大于等于0')
- return v
- @staticmethod
- def formatTel(value):
- try:
- value = str(value).strip(u' ')
- except:
- value = ''
- if value == '':
- return value
- 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)
- is_tel = re.match(r'^([0-9]{3,4}-)?[0-9]{7,8}$', value)
- if not (is_mobile or is_tel):
- raise CustomError(u'不合法的手机号或者固话号')
- return value
- @staticmethod
- def formatDate(value):
- try:
- return datetime.datetime.strptime(value, '%Y-%m-%d').date()
- except:
- raise CustomError(u'日期格式错误,格式为:2018-01-01')
|