This action will force synchronization from 现任明教教主-乾颐堂/DevNet2019, which will overwrite any changes that you have made since you forked the repository, and can not be recovered!!!
Synchronous operation will process in the background and will refresh the page when finishing processing. Please be patient.
#!/usr/bin/env python3# -*- coding=utf-8 -*-# 本脚由亁颐堂现任明教教主编写,用于乾颐盾Python课程!# 教主QQ:605658506# 亁颐堂官网www.qytang.com# 教主技术进化论拓展你的技术新边疆# https://ke.qq.com/course/271956?tuin=24199d8afrom django import formsfrom devnet.models import SNMPtype, DeviceSNMP, Devicetype, Devicedbfrom django.core.validators import RegexValidator# 添加设备类型class AddDeviceType(forms.Form):# 如果希望出现必选左边的红色星标 ,必须配置下面的内容,并且在HTML中还要配置CSSrequired_css_class = 'required'# 设备类型名称device_type_name = forms.CharField(label='设备类型名称',widget=forms.TextInput(attrs={"class": "form-control"}))# CPU利用率cpu_usage = forms.CharField(label='CPU利用率',widget=forms.TextInput(attrs={"class": "form-control"}))# 内存使用mem_usage = forms.CharField(label='内存使用',widget=forms.TextInput(attrs={"class": "form-control"}))# 内存闲置mem_free = forms.CharField(label='内存闲置',widget=forms.TextInput(attrs={"class": "form-control"}))# 接口名称if_name = forms.CharField(label='接口名称',widget=forms.TextInput(attrs={"class": "form-control"}))# 接口速率if_speed = forms.CharField(label='接口速率',widget=forms.TextInput(attrs={"class": "form-control"}))# 接口状态if_state = forms.CharField(label='接口状态',widget=forms.TextInput(attrs={"class": "form-control"}))# 接口入向字节数if_in_bytes = forms.CharField(label='接口入向字节数',widget=forms.TextInput(attrs={"class": "form-control"}))# 接口出向字节数if_out_bytes = forms.CharField(label='接口出向字节数',widget=forms.TextInput(attrs={"class": "form-control"}))def clean_device_type_name(self):device_type_name = self.cleaned_data['device_type_name']try:Devicetype.objects.get(type_name=device_type_name)raise forms.ValidationError("设备类型已存在!")except Devicetype.DoesNotExist:return device_type_name# 添加设备类型class EditDeviceType(forms.Form):# 如果希望出现必选左边的红色星标 ,必须配置下面的内容,并且在HTML中还要配置CSSrequired_css_class = 'required'device_id = forms.IntegerField(label='设备类型ID',required=True,widget=forms.TextInput(attrs={"class": "form-control", 'readonly': True}))# 设备类型名称device_type_name = forms.CharField(label='设备类型名称',widget=forms.TextInput(attrs={"class": "form-control"}))# CPU利用率cpu_usage = forms.CharField(label='CPU利用率',widget=forms.TextInput(attrs={"class": "form-control"}))# 内存使用mem_usage = forms.CharField(label='内存使用',widget=forms.TextInput(attrs={"class": "form-control"}))# 内存闲置mem_free = forms.CharField(label='内存闲置',widget=forms.TextInput(attrs={"class": "form-control"}))# 接口名称if_name = forms.CharField(label='接口名称',widget=forms.TextInput(attrs={"class": "form-control"}))# 接口速率if_speed = forms.CharField(label='接口速率',widget=forms.TextInput(attrs={"class": "form-control"}))# 接口状态if_state = forms.CharField(label='接口状态',widget=forms.TextInput(attrs={"class": "form-control"}))# 接口入向字节数if_in_bytes = forms.CharField(label='接口入向字节数',widget=forms.TextInput(attrs={"class": "form-control"}))# 接口出向字节数if_out_bytes = forms.CharField(label='接口出向字节数',widget=forms.TextInput(attrs={"class": "form-control"}))def clean_device_type_name(self):device_type_name = self.cleaned_data['device_type_name']device_id = self.cleaned_data['device_id']try:if device_id != Devicetype.objects.get(type_name=device_type_name).id:raise forms.ValidationError("设备类型已存在!")else:return device_type_nameexcept Devicetype.DoesNotExist:return device_type_name# 添加设备表单class AddDevice(forms.Form):# 如果希望出现必选左边的红色星标 ,必须配置下面的内容,并且在HTML中还要配置CSSrequired_css_class = 'required'name = forms.CharField(max_length=100,min_length=2,label='设备名称',required=True,widget=forms.TextInput(attrs={"class": "form-control"}))# 类型为GenericIPAddressField,可以对输入的IP地址进行校验ip = forms.GenericIPAddressField(required=True,label='IP地址',widget=forms.TextInput(attrs={"class": "form-control"}))description = forms.CharField(label="设备描述",required=False,# 输入为Textarea,支持多行输入,"rows": 3 控制展示的行数widget=forms.Textarea(attrs={"class": "form-control", "rows": 3}))# 选择的设备类型type_choices = []devicetype = Devicetype.objects.all()for x in devicetype:type_choices.append([x.id, x.type_name])type = forms.CharField(label='设备类型',required=True,widget=forms.Select(choices=type_choices,attrs={"class": "form-control"}))TRUE_FALSE_CHOICES = ((True, 'Yes'), (False, 'No'))snmp_enable = forms.ChoiceField(label='是否激活SNMP',required=True,choices=TRUE_FALSE_CHOICES,initial=False,widget=forms.Select(attrs={"class": "required checkbox form-control"}))community_regex = RegexValidator(regex=r'[0-9a-zA-Z~!@#$%^&*()_+=,./]+',message="SNMP community 只能包含数字,小写,大写字母 ~!@#$%^&*()_+=,./")snmp_ro_community = forms.CharField(max_length=100,min_length=2,label='SNMP只读',required=True,validators=[community_regex],widget=forms.TextInput(attrs={"class": "form-control"}))snmp_rw_community = forms.CharField(max_length=100,min_length=2,label='SNMP读写',required=False,validators=[community_regex],widget=forms.TextInput(attrs={"class": "form-control"}))username_regex = RegexValidator(regex=r'[0-9a-zA-Z~!@#$%^&*()_+=,./]+',message="用户名只能包含数字,小写,大写字母 ~!@#$%^&*()_+=,./")ssh_username = forms.CharField(max_length=100,min_length=2,label='SSH用户名',required=True,validators=[username_regex],widget=forms.TextInput(attrs={"class": "form-control"}))password_regex = RegexValidator(regex=r'[0-9a-zA-Z~!@#$%^&*()_+=,./]+',message="密码只能包含数字,小写,大写字母 ~!@#$%^&*()_+=,./")ssh_password = forms.CharField(max_length=100,min_length=2,label='SSH密码',required=True,validators=[password_regex],widget=forms.PasswordInput(attrs={"class": "form-control"}))enable_password_regex = RegexValidator(regex=r'[0-9a-zA-Z~!@#$%^&*()_+=,./]+',message="特权密码只能包含数字,小写,大写字母 ~!@#$%^&*()_+=,./")enable_password = forms.CharField(max_length=100,min_length=2,label='特权密码',required=False,validators=[enable_password_regex],widget=forms.PasswordInput(attrs={"class": "form-control"}))# 校验设备名称不能重复, 在此系统中,设备并没有指定唯一ID,设备名就是唯一ID,不能重复def clean_name(self):name = self.cleaned_data['name'] # 提取客户输入的设备名# 在数据库中查找是否存在这个设备名existing = Devicedb.objects.filter(name=name).exists()# 如果存在就显示校验错误信息if existing:raise forms.ValidationError("设备名不能重复")# 如果校验成功就返回设备名return name# 校验IP地址不能重复def clean_ip(self):ip = self.cleaned_data['ip'] # 提取客户输入的设备IP# 在数据库中查找是否存在这个设备IPexisting = Devicedb.objects.filter(ip=ip).exists()# 如果存在就显示校验错误信息if existing:raise forms.ValidationError("设备IP不能重复")# 如果校验成功就返回设备IPreturn ip# 确认激活SNMP,才能设置只读Communitydef clean_snmp_ro_community(self):snmp_enable = self.cleaned_data['snmp_enable']snmp_ro_community = self.cleaned_data['snmp_ro_community']if snmp_enable == 'True' and snmp_ro_community:return snmp_ro_communityelse:raise forms.ValidationError("设置只读Community之前请激活SNMP")# 确认激活SNMP,才能设置读写Communitydef clean_snmp_rw_community(self):snmp_enable = self.cleaned_data['snmp_enable']snmp_rw_community = self.cleaned_data['snmp_rw_community']if snmp_rw_community:if snmp_enable == 'True' and snmp_rw_community:return snmp_rw_communityelse:raise forms.ValidationError("设置读写Community之前请激活SNMP")else:return snmp_rw_community# 编辑设备表单class EditDevice(forms.Form):# 如果希望出现必选左边的红色星标 ,必须配置下面的内容,并且在HTML中还要配置CSSrequired_css_class = 'required'id = forms.IntegerField(label='设备ID',required=True,widget=forms.TextInput(attrs={"class": "form-control", 'readonly': True}))name = forms.CharField(max_length=100,min_length=2,label='设备名称',required=True,widget=forms.TextInput(attrs={"class": "form-control"}))ip = forms.GenericIPAddressField(required=True,label='IP地址',widget=forms.TextInput(attrs={"class": "form-control"}))description = forms.CharField(label="设备描述",required=False,widget=forms.Textarea(attrs={"class": "form-control", "rows": 3}))type_choices = []devicetype = Devicetype.objects.all()for x in devicetype:type_choices.append([x.id, x.type_name])type = forms.CharField(label='设备类型',required=True,widget=forms.Select(choices=type_choices,attrs={"class": "form-control"}))TRUE_FALSE_CHOICES = ((True, 'Yes'), (False, 'No'))snmp_enable = forms.ChoiceField(label='是否激活SNMP',required=True,choices=TRUE_FALSE_CHOICES,initial=False,widget=forms.Select(attrs={"class": "required checkbox form-control"}))community_regex = RegexValidator(regex=r'[0-9a-zA-Z~!@#$%^&*()_+=,./]+',message="SNMP community 只能包含数字,小写,大写字母 ~!@#$%^&*()_+=,./")snmp_ro_community = forms.CharField(max_length=100,min_length=2,label='SNMP只读',required=True,validators=[community_regex],widget=forms.TextInput(attrs={"class": "form-control"}))snmp_rw_community = forms.CharField(max_length=100,min_length=2,label='SNMP读写',required=False,validators=[community_regex],widget=forms.TextInput(attrs={"class": "form-control"}))username_regex = RegexValidator(regex=r'[0-9a-zA-Z~!@#$%^&*()_+=,./]+',message="用户名只能包含数字,小写,大写字母 ~!@#$%^&*()_+=,./")ssh_username = forms.CharField(max_length=100,min_length=2,label='SSH用户名',required=True,validators=[username_regex],widget=forms.TextInput(attrs={"class": "form-control"}))password_regex = RegexValidator(regex=r'[0-9a-zA-Z~!@#$%^&*()_+=,./]+',message="密码只能包含数字,小写,大写字母 ~!@#$%^&*()_+=,./")ssh_password = forms.CharField(max_length=100,min_length=2,label='SSH密码',required=True,validators=[password_regex],widget=forms.TextInput(attrs={"class": "form-control"}))enable_password_regex = RegexValidator(regex=r'[0-9a-zA-Z~!@#$%^&*()_+=,./]+',message="特权密码只能包含数字,小写,大写字母 ~!@#$%^&*()_+=,./")enable_password = forms.CharField(max_length=100,min_length=2,label='特权密码',required=False,validators=[enable_password_regex],widget=forms.TextInput(attrs={"class": "form-control"}))def clean_name(self):name = self.cleaned_data['name'] # 提取客户输入的设备名称for i in Devicedb.objects.filter(name=name):if i.id != self.cleaned_data['id']:raise forms.ValidationError("设备名称已经存在")else:return namedef clean_ip(self):ip = self.cleaned_data['ip'] # 提取客户输入的设备IP# 在数据库中查找是否存在其他设备使用这个IP地址for i in Devicedb.objects.filter(ip=ip):if i.id != self.cleaned_data['id']:raise forms.ValidationError("设备IP地址已经存在")else:return ipdef clean_snmp_ro_community(self):snmp_enable = self.cleaned_data['snmp_enable']snmp_ro_community = self.cleaned_data['snmp_ro_community']if snmp_enable == 'True' and snmp_ro_community:return snmp_ro_communityelse:raise forms.ValidationError("设置只读Community之前请激活SNMP")def clean_snmp_rw_community(self):snmp_enable = self.cleaned_data['snmp_enable']snmp_rw_community = self.cleaned_data['snmp_rw_community']if snmp_rw_community:if snmp_enable == 'True' and snmp_rw_community:return snmp_rw_communityelse:raise forms.ValidationError("设置读写Community之前请激活SNMP")else:return snmp_rw_community# 系统设置, 监控周期表单class SysconfigmonitorintervalForm(forms.Form):# 如果希望出现必选左边的红色星标 ,必须配置下面的内容,并且在HTML中还要配置CSSrequired_css_class = 'required'interval_regex = RegexValidator(regex=r'^\d{1,2}$',message="监控周期只能支持最多2位整数")# CPU监控周期cpu_interval = forms.CharField(validators=[interval_regex],min_length=1,max_length=2,label='CPU监控周期(单位小时,默认1小时)',required=True,widget=forms.NumberInput(attrs={"class": "form-control"}))# CPU最大值计算周期cpu_max_interval = forms.CharField(validators=[interval_regex],min_length=1,max_length=2,label='CPU最大值计算周期(单位小时,默认1小时)',required=True,widget=forms.NumberInput(attrs={"class": "form-control"}))# 内存监控周期mem_interval = forms.CharField(validators=[interval_regex],min_length=1,max_length=2,label='内存监控周期(单位小时,默认1小时)',required=True,widget=forms.NumberInput(attrs={"class": "form-control"}))# 内存最大值计算周期mem_max_interval = forms.CharField(validators=[interval_regex],min_length=1,max_length=2,label='内存最大值计算周期(单位小时,默认1小时)',required=True,widget=forms.NumberInput(attrs={"class": "form-control"}))# 速率监控周期speed_interval = forms.CharField(validators=[interval_regex],min_length=1,max_length=2,label='接口速率监控周期(单位小时,默认1小时)',required=True,widget=forms.NumberInput(attrs={"class": "form-control"}))# 利用率监控周期utilization_interval = forms.CharField(validators=[interval_regex],min_length=1,max_length=2,label='接口利用率监控周期(单位小时,默认1小时)',required=True,widget=forms.NumberInput(attrs={"class": "form-control"}))# 系统设置, 监控周期表单class SysconfigdatabaselifetimeForm(forms.Form):# 如果希望出现必选左边的红色星标 ,必须配置下面的内容,并且在HTML中还要配置CSSrequired_css_class = 'required'interval_regex = RegexValidator(regex=r'^\d{1,3}$',message="数据老化时间只能支持最多3位整数")# 可达性数据老化时间reachable_lifetime = forms.CharField(validators=[interval_regex],min_length=1,max_length=3,label='可达性数据老化时间(单位小时,默认24小时)',required=True,widget=forms.NumberInput(attrs={"class": "form-control"}))# CPU数据老化时间cpu_lifetime = forms.CharField(validators=[interval_regex],min_length=1,max_length=3,label='CPU数据老化时间(单位小时,默认24小时)',required=True,widget=forms.NumberInput(attrs={"class": "form-control"}))# 内存数据老化时间mem_lifetime = forms.CharField(validators=[interval_regex],min_length=1,max_length=3,label='内存数据老化时间(单位小时,默认24小时)',required=True,widget=forms.NumberInput(attrs={"class": "form-control"}))# 接口数据老化时间interface_lifetime = forms.CharField(validators=[interval_regex],min_length=1,max_length=3,label='接口数据老化时间(单位小时,默认24小时)',required=True,widget=forms.NumberInput(attrs={"class": "form-control"}))# Netflow数据老化时间netflow_lifetime = forms.CharField(validators=[interval_regex],min_length=1,max_length=3,label='Netflow数据老化时间(单位小时,默认24小时)',required=True,widget=forms.NumberInput(attrs={"class": "form-control"}))# 系统设置, 告警阈值,周期与SMTP相关表单class Sysconfigthreshold(forms.Form):# 如果希望出现必选左边的红色星标 ,必须配置下面的内容,并且在HTML中还要配置CSSrequired_css_class = 'required'threshold_regex = RegexValidator(regex=r'^1?\d{1,2}$',message="阈值取值范围为1-100的整数")interval_regex = RegexValidator(regex=r'^\d{1,2}$',message="监控周期只能支持最多2位整数")# CPU告警阈值cpu_threshold = forms.CharField(validators=[threshold_regex],min_length=1,max_length=3,label='CPU告警阈值(单位%)设置为0表示取消',required=True,widget=forms.NumberInput(attrs={"class": "form-control"}))# CPU告警周期cpu_alarm_interval = forms.CharField(validators=[interval_regex],min_length=1,max_length=5,label='CPU告警周期(单位分钟)',required=True,widget=forms.NumberInput(attrs={"class": "form-control"}))# 内存告警阈值mem_threshold = forms.CharField(validators=[threshold_regex],min_length=1,max_length=3,label='内存告警阈值(单位%)设置为0表示取消',required=True,widget=forms.NumberInput(attrs={"class": "form-control"}))# 内存告警周期mem_alarm_interval = forms.CharField(validators=[interval_regex],min_length=1,max_length=5,label='内存告警周期(单位分钟)',required=True,widget=forms.NumberInput(attrs={"class": "form-control"}))# 接口利用率告警阈值utilization_threshold = forms.CharField(validators=[threshold_regex],min_length=1,max_length=3,label='接口利用率告警阈值(单位%)设置为0表示取消',required=True,widget=forms.NumberInput(attrs={"class": "form-control"}))# 接口利用率告警周期utilization_alarm_interval = forms.CharField(validators=[interval_regex],min_length=1,max_length=5,label='接口利用率告警周期(单位分钟)',required=True,widget=forms.NumberInput(attrs={"class": "form-control"}))# SMTP邮件服务器mailserver = forms.CharField(min_length=1,max_length=50,label='邮件服务器',required=False,widget=forms.TextInput(attrs={"class": "form-control"}))# SMTP认证用用户名mailusername = forms.CharField(min_length=1,max_length=50,label='用户名',required=False,widget=forms.TextInput(attrs={"class": "form-control"}))# SMTP认证用密码mailpassword = forms.CharField(min_length=1,max_length=50,label='密码',required=False,widget=forms.TextInput(attrs={"class": "form-control"}))# 发件人mailfrom = forms.CharField(min_length=1,max_length=50,label='发件人FROM',required=False,widget=forms.TextInput(attrs={"class": "form-control"}))# 收件人mailto = forms.CharField(min_length=1,max_length=50,label='收件人TO',required=False,widget=forms.TextInput(attrs={"class": "form-control"}))def clean_cpu_alarm_interval(self):cpu_threshold = int(self.cleaned_data['cpu_threshold'])cpu_alarm_interval = int(self.cleaned_data['cpu_alarm_interval'])if (cpu_threshold and cpu_alarm_interval) >= 1:passelif cpu_threshold == 0 and cpu_alarm_interval == 0:passelse:raise forms.ValidationError("CPU阈值与告警周期,要么都设置,要么都保持默认的0!不能只设置其中一个!")def clean_mem_alarm_interval(self):mem_threshold = int(self.cleaned_data['mem_threshold'])mem_alarm_interval = int(self.cleaned_data['mem_alarm_interval'])if (mem_threshold and mem_alarm_interval) >= 1:passelif mem_threshold == 0 and mem_alarm_interval == 0:passelse:raise forms.ValidationError("内存阈值与告警周期,要么都设置,要么都保持默认的0!不能只设置其中一个!")def clean_utilization_alarm_interval(self):utilization_threshold = int(self.cleaned_data['utilization_threshold'])utilization_alarm_interval = int(self.cleaned_data['utilization_alarm_interval'])if (utilization_threshold and utilization_alarm_interval) >= 1:passelif utilization_threshold == 0 and utilization_alarm_interval == 0:passelse:raise forms.ValidationError("利用率阈值与告警周期,要么都设置,要么都保持默认的0!不能只设置其中一个!")def clean_mailto(self):mailserver = self.cleaned_data['mailserver']mailusername = self.cleaned_data['mailusername']mailpassword = self.cleaned_data['mailpassword']mailfrom = self.cleaned_data['mailfrom']mailto = self.cleaned_data['mailto']if mailserver and mailusername and mailpassword and mailfrom and mailto:passelif not mailserver and not mailusername and not mailpassword and not mailfrom and not mailto:passelse:raise forms.ValidationError("邮件信息要么全部设置!要么全部保持空!不能只设置其中的一部分!")class UserForm(forms.Form):username = forms.CharField(label='用户名',max_length=100,required=True,widget=forms.TextInput(attrs={"class": "form-control", "placeholder": "用户名"}))password = forms.CharField(label='密码',max_length=100,required=True,widget=forms.PasswordInput(attrs={"class": "form-control", "placeholder": "密码"}))
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。