Coverage for tenancy/forms/model_forms.py: 100%

51 statements  

« prev     ^ index     » next       coverage.py v7.15.2, created at 2026-10-10 18:35 +0000

1from django import forms 

2from django.utils.translation import gettext_lazy as _ 

3 

4from netbox.forms import NestedGroupModelForm, NetBoxModelForm, OrganizationalModelForm, PrimaryModelForm 

5from tenancy.choices import ContactPriorityChoices 

6from utilities.forms import add_blank_choice 

7from utilities.forms.fields import ( 

8 DynamicModelChoiceField, 

9 DynamicModelMultipleChoiceField, 

10 SlugField, 

11 TypedChoiceField, 

12) 

13from utilities.forms.rendering import FieldSet, ObjectAttribute 

14 

15from ..models import * 

16 

17__all__ = ( 

18 'ContactAssignmentForm', 

19 'ContactForm', 

20 'ContactGroupForm', 

21 'ContactRoleForm', 

22 'TenantForm', 

23 'TenantGroupForm', 

24) 

25 

26 

27# 

28# Tenants 

29# 

30 

31class TenantGroupForm(NestedGroupModelForm): 

32 parent = DynamicModelChoiceField( 

33 label=_('Parent'), 

34 queryset=TenantGroup.objects.all(), 

35 required=False 

36 ) 

37 

38 fieldsets = ( 

39 FieldSet('parent', 'name', 'slug', 'description', 'tags', name=_('Tenant Group')), 

40 ) 

41 

42 class Meta: 

43 model = TenantGroup 

44 fields = [ 

45 'parent', 'name', 'slug', 'description', 'owner', 'comments', 'tags', 

46 ] 

47 

48 

49class TenantForm(PrimaryModelForm): 

50 slug = SlugField() 

51 group = DynamicModelChoiceField( 

52 label=_('Group'), 

53 queryset=TenantGroup.objects.all(), 

54 required=False 

55 ) 

56 

57 fieldsets = ( 

58 FieldSet('name', 'slug', 'group', 'description', 'tags', name=_('Tenant')), 

59 ) 

60 

61 class Meta: 

62 model = Tenant 

63 fields = ( 

64 'name', 'slug', 'group', 'description', 'owner', 'comments', 'tags', 

65 ) 

66 

67 

68# 

69# Contacts 

70# 

71 

72class ContactGroupForm(NestedGroupModelForm): 

73 parent = DynamicModelChoiceField( 

74 label=_('Parent'), 

75 queryset=ContactGroup.objects.all(), 

76 required=False 

77 ) 

78 

79 fieldsets = ( 

80 FieldSet('parent', 'name', 'slug', 'description', 'tags', name=_('Contact Group')), 

81 ) 

82 

83 class Meta: 

84 model = ContactGroup 

85 fields = ('parent', 'name', 'slug', 'description', 'owner', 'comments', 'tags') 

86 

87 

88class ContactRoleForm(OrganizationalModelForm): 

89 fieldsets = ( 

90 FieldSet('name', 'slug', 'description', 'tags', name=_('Contact Role')), 

91 ) 

92 

93 class Meta: 

94 model = ContactRole 

95 fields = ('name', 'slug', 'description', 'owner', 'comments', 'tags') 

96 

97 

98class ContactForm(PrimaryModelForm): 

99 groups = DynamicModelMultipleChoiceField( 

100 label=_('Groups'), 

101 queryset=ContactGroup.objects.all(), 

102 required=False 

103 ) 

104 link = forms.URLField( 

105 label=_('Link'), 

106 assume_scheme='https', 

107 required=False, 

108 ) 

109 

110 fieldsets = ( 

111 FieldSet( 

112 'groups', 'name', 'title', 'phone', 'email', 'address', 'link', 'description', 'tags', 

113 name=_('Contact') 

114 ), 

115 ) 

116 

117 class Meta: 

118 model = Contact 

119 fields = ( 

120 'groups', 'name', 'title', 'phone', 'email', 'address', 'link', 'description', 'owner', 'comments', 'tags', 

121 ) 

122 widgets = { 

123 'address': forms.Textarea(attrs={'rows': 3}), 

124 } 

125 

126 

127class ContactAssignmentForm(NetBoxModelForm): 

128 priority = TypedChoiceField( 

129 label=_('Priority'), 

130 choices=add_blank_choice(ContactPriorityChoices), 

131 required=False, 

132 ) 

133 group = DynamicModelChoiceField( 

134 label=_('Group'), 

135 queryset=ContactGroup.objects.all(), 

136 required=False, 

137 initial_params={ 

138 'contact': '$contact' 

139 } 

140 ) 

141 contact = DynamicModelChoiceField( 

142 label=_('Contact'), 

143 queryset=Contact.objects.all(), 

144 query_params={ 

145 'group_id': '$group' 

146 } 

147 ) 

148 role = DynamicModelChoiceField( 

149 label=_('Role'), 

150 queryset=ContactRole.objects.all() 

151 ) 

152 

153 fieldsets = ( 

154 FieldSet(ObjectAttribute('object'), 'group', 'contact', 'role', 'priority', 'tags'), 

155 ) 

156 

157 class Meta: 

158 model = ContactAssignment 

159 fields = ( 

160 'object_type', 'object_id', 'group', 'contact', 'role', 'priority', 'tags' 

161 ) 

162 widgets = { 

163 'object_type': forms.HiddenInput(), 

164 'object_id': forms.HiddenInput(), 

165 }