Files

36 lines
1.6 KiB
Python
Raw Permalink Normal View History

from django import forms
from .models import Plan, PlanApplication
class PlanApplicationForm(forms.ModelForm):
class Meta:
model = PlanApplication
fields = ("plan", "note")
labels = {"note": "申请说明(可选)"}
widgets = {"note": forms.Textarea(attrs={"rows": 4, "placeholder": "请填写使用需求或员工信息"})}
def __init__(self, *args, user=None, **kwargs):
super().__init__(*args, **kwargs)
self.user = user
plans = Plan.objects.filter(active=True)
if user and user.identity_type == "employee":
plans = plans.filter(audience__in=[Plan.Audience.EMPLOYEE, Plan.Audience.ALL])
else:
plans = plans.filter(audience__in=[Plan.Audience.EXTERNAL, Plan.Audience.ALL], employee_benefit=False)
self.fields["plan"].queryset = plans.order_by("sort_order", "price_cny")
self.fields["plan"].label_from_instance = lambda plan: plan.application_label(user)
self.fields["plan"].help_text = "内部员工核实身份并审核后免费开通;标注“暂未开放付款”的套餐仅登记申请,不扣款、不立即开通付费服务。"
def save(self, commit=True):
application = super().save(commit=False)
application.user = self.user
application.payment_status = (
PlanApplication.PaymentStatus.NOT_REQUIRED
if application.plan.price_for_user(self.user) == 0
else PlanApplication.PaymentStatus.UNPAID
)
if commit:
return PlanApplication.submit(user=self.user, plan=application.plan, note=application.note)
return application