This commit is contained in:
xie7654
2025-06-29 21:45:27 +08:00
commit f6e68e37c8
1539 changed files with 129319 additions and 0 deletions

30
backend/utils/utils.py Normal file
View File

@@ -0,0 +1,30 @@
import re
from datetime import datetime
from decimal import Decimal, ROUND_HALF_UP
from rest_framework.exceptions import ValidationError
from django.utils import timezone
def validate_mobile(value):
if value and not re.findall(r"1\d{10}", value):
raise ValidationError('手机格式不正确')
def validate_amount(value):
if value is None:
raise ValidationError('金额不能为空')
if value and value < 0:
raise ValidationError('金额不能为负')
def to_cent(value):
if value is None:
value = 0
return Decimal(value).quantize(Decimal('.01'), rounding=ROUND_HALF_UP)
# 定义一个小工具:从时间戳转换为 aware datetime如果时间戳有效
def ts_to_aware(ts):
if ts:
naive_dt = datetime.fromtimestamp(ts)
return timezone.make_aware(naive_dt)
return None