78 lines
2.3 KiB
Python
78 lines
2.3 KiB
Python
from decimal import Decimal
|
|
|
|
from django import forms
|
|
from django.conf import settings
|
|
|
|
from apps.billing.models import RechargeOrder
|
|
from apps.licensing.models import ClientDevice, SoftwareOrder, SoftwarePlan
|
|
|
|
|
|
class ApiKeyCreateForm(forms.Form):
|
|
name = forms.CharField(
|
|
label="名称",
|
|
max_length=80,
|
|
required=False,
|
|
widget=forms.TextInput(
|
|
attrs={
|
|
"autocomplete": "off",
|
|
"class": "form-control",
|
|
"placeholder": "例如:桌面端",
|
|
}
|
|
),
|
|
)
|
|
|
|
|
|
class RechargeCreateForm(forms.Form):
|
|
amount = forms.DecimalField(
|
|
label="充值金额",
|
|
max_digits=12,
|
|
decimal_places=2,
|
|
min_value=Decimal("0.01"),
|
|
widget=forms.NumberInput(
|
|
attrs={
|
|
"class": "form-control",
|
|
"inputmode": "decimal",
|
|
"min": "0.01",
|
|
"step": "0.01",
|
|
"placeholder": "例如:100.00",
|
|
}
|
|
),
|
|
)
|
|
pay_method = forms.ChoiceField(
|
|
label="支付方式",
|
|
choices=(
|
|
(RechargeOrder.PayMethod.WEIXIN, "微信"),
|
|
),
|
|
initial=RechargeOrder.PayMethod.WEIXIN,
|
|
widget=forms.RadioSelect(attrs={"class": "form-check-input"}),
|
|
)
|
|
|
|
def clean_amount(self):
|
|
amount = self.cleaned_data["amount"]
|
|
max_amount = Decimal(str(settings.RECHARGE_MAX_AMOUNT_CNY))
|
|
if amount > max_amount:
|
|
raise forms.ValidationError(f"单笔充值金额不能超过 {max_amount:.2f} CNY")
|
|
return amount
|
|
|
|
|
|
class SoftwareOrderCreateForm(forms.Form):
|
|
plan = forms.ModelChoiceField(
|
|
label="套餐",
|
|
queryset=SoftwarePlan.objects.none(),
|
|
empty_label=None,
|
|
widget=forms.Select(attrs={"class": "form-select"}),
|
|
)
|
|
pay_method = forms.ChoiceField(
|
|
label="支付方式",
|
|
choices=((SoftwareOrder.PayMethod.WEIXIN, "微信"),),
|
|
initial=SoftwareOrder.PayMethod.WEIXIN,
|
|
widget=forms.RadioSelect(attrs={"class": "form-check-input"}),
|
|
)
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
super().__init__(*args, **kwargs)
|
|
self.fields["plan"].queryset = SoftwarePlan.objects.filter(
|
|
product_code=ClientDevice.ProductCode.CMSHOPEE,
|
|
status=SoftwarePlan.Status.ACTIVE,
|
|
).order_by("name", "id")
|