Files
xie7654 f6e68e37c8 init
2025-06-29 21:45:27 +08:00

30 lines
858 B
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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