Files
asset-management/assetapp/forms.py
T
cnbugs 9b79c14267 feat: 新增软件资产(软资产)管理模块
- 新增 SoftwareCategory/SoftwareAsset/SoftwareChangeLog 模型与迁移0010
- 软资产CRUD:编号/名称/分类/供应商/版本/许可类型/授权数量/许可密钥/账号/费用/起止日期/负责人/部门/状态
- 许可类型:永久授权/订阅/开源免费/按需付费;状态:在用/闲置/已过期/已注销
- 到期提醒:已过期/即将到期(30天)自动标识
- Excel导入导出:模板下载、ID存在时更新、分类自动创建、空白行跳过、缺编号/名称报错
- 软件分类独立管理,分类下有资产时禁止删除
- 独立软件变更记录页;仪表盘增加软件统计卡片、软件分类统计、许可类型分布
- 导航栏改造:资产管理/分类管理/变更记录/导入导出下拉菜单
- 全流程15项功能验证通过(CRUD/导入导出/权限/仪表盘)
2026-11-11 17:22:23 +08:00

98 lines
3.5 KiB
Python

from django import forms
from django.contrib.auth.password_validation import password_validators_help_text_html, validate_password
from .models import Asset, Category, SoftwareAsset, SoftwareCategory
class AssetForm(forms.ModelForm):
class Meta:
model = Asset
fields = [
'asset_number', 'name', 'category', 'brand', 'model', 'asset_value', 'serial_number',
'location', 'cabinet', 'cabinet_position', 'bmc_address', 'ip_address',
'has_gpu', 'gpu_type', 'gpu_count',
'purchase_date', 'warranty_expire', 'supplier',
'responsible_person', 'department', 'user', 'business_type', 'status', 'remark',
]
widgets = {
'purchase_date': forms.DateInput(attrs={'type': 'date'}),
'warranty_expire': forms.DateInput(attrs={'type': 'date'}),
'remark': forms.Textarea(attrs={'rows': 3}),
'status': forms.Select(attrs={'class': 'form-select'}),
'category': forms.Select(attrs={'class': 'form-select'}),
}
class AssetImportForm(forms.Form):
excel_file = forms.FileField(
label='Excel文件',
help_text='支持 .xlsx 格式',
widget=forms.FileInput(attrs={'accept': '.xlsx,.xls'})
)
class CategoryForm(forms.ModelForm):
class Meta:
model = Category
fields = ['name', 'description']
class SoftwareCategoryForm(forms.ModelForm):
class Meta:
model = SoftwareCategory
fields = ['name', 'description']
class SoftwareAssetForm(forms.ModelForm):
class Meta:
model = SoftwareAsset
fields = [
'asset_number', 'name', 'category',
'vendor', 'version', 'license_type', 'license_key', 'seats',
'account', 'cost', 'start_date', 'expire_date',
'responsible_person', 'department', 'status', 'remark',
]
widgets = {
'start_date': forms.DateInput(attrs={'type': 'date'}),
'expire_date': forms.DateInput(attrs={'type': 'date'}),
'remark': forms.Textarea(attrs={'rows': 3}),
'status': forms.Select(attrs={'class': 'form-select'}),
'category': forms.Select(attrs={'class': 'form-select'}),
'license_type': forms.Select(attrs={'class': 'form-select'}),
}
class AdminPasswordResetForm(forms.Form):
"""管理员重置指定用户密码的表单"""
new_password1 = forms.CharField(
label='新密码',
widget=forms.PasswordInput(attrs={'class': 'form-control', 'autocomplete': 'new-password'}),
help_text=password_validators_help_text_html(),
)
new_password2 = forms.CharField(
label='确认新密码',
widget=forms.PasswordInput(attrs={'class': 'form-control', 'autocomplete': 'new-password'}),
)
def __init__(self, *args, target_user=None, **kwargs):
self.target_user = target_user
super().__init__(*args, **kwargs)
def clean(self):
cleaned_data = super().clean()
password1 = cleaned_data.get('new_password1')
password2 = cleaned_data.get('new_password2')
if password1 and password2 and password1 != password2:
raise forms.ValidationError('两次输入的密码不一致')
if password1 and self.target_user is not None:
validate_password(password1, self.target_user)
return cleaned_data
def save(self):
self.target_user.set_password(self.cleaned_data['new_password1'])
self.target_user.save(update_fields=['password'])
return self.target_user