22from netbox .forms import NetBoxModelForm , NetBoxModelFilterSetForm
33from utilities .forms .fields import DynamicModelMultipleChoiceField , TagFilterField
44from utilities .forms .rendering import FieldSet
5+ from utilities .forms .widgets import DatePicker
56
67from cesnet_service_path_plugin .models import Segment , ContractInfo
78from cesnet_service_path_plugin .models .custom_choices import (
@@ -43,17 +44,6 @@ class ContractInfoForm(NetBoxModelForm):
4344 required = False , min_value = 0 , help_text = "Number of recurring charge periods (0 for no recurring charges)"
4445 )
4546
46- start_date = forms .DateField (
47- required = False ,
48- help_text = "When this contract version starts (optional)" ,
49- widget = forms .DateInput (attrs = {"type" : "date" })
50- )
51-
52- end_date = forms .DateField (
53- required = False ,
54- help_text = "When this contract version ends (optional)" ,
55- widget = forms .DateInput (attrs = {"type" : "date" })
56- )
5747
5848 notes = forms .CharField (
5949 required = False , widget = forms .Textarea (attrs = {"rows" : 3 }), help_text = "Notes specific to this version"
@@ -63,14 +53,18 @@ def __init__(self, *args, **kwargs):
6353 super ().__init__ (* args , ** kwargs )
6454
6555 # Check if this is an amendment (has previous_version)
66- instance = kwargs .get (' instance' )
67- initial = kwargs .get (' initial' ) or {}
56+ instance = kwargs .get (" instance" )
57+ initial = kwargs .get (" initial" ) or {}
6858
6959 # Check for previous_version in initial data (from clone URL)
70- has_previous_version_in_initial = 'previous_version' in initial and initial ['previous_version' ]
71- has_previous_version_on_instance = instance and hasattr (instance , 'previous_version' ) and instance .previous_version
60+ has_previous_version_in_initial = "previous_version" in initial and initial ["previous_version" ]
61+ has_previous_version_on_instance = (
62+ instance and hasattr (instance , "previous_version" ) and instance .previous_version
63+ )
7264
73- is_amendment = (instance and instance .pk is None ) and (has_previous_version_in_initial or has_previous_version_on_instance )
65+ is_amendment = (instance and instance .pk is None ) and (
66+ has_previous_version_in_initial or has_previous_version_on_instance
67+ )
7468
7569 # Store this flag for use in clean()
7670 self ._is_amendment = is_amendment
@@ -81,7 +75,7 @@ def __init__(self, *args, **kwargs):
8175 elif has_previous_version_in_initial :
8276 # Get previous version from initial data
8377 try :
84- prev_id = initial [' previous_version' ]
78+ prev_id = initial [" previous_version" ]
8579 if isinstance (prev_id , int ) or (isinstance (prev_id , str ) and prev_id .isdigit ()):
8680 self ._previous_version = ContractInfo .objects .get (pk = int (prev_id ))
8781 else :
@@ -94,15 +88,17 @@ def __init__(self, *args, **kwargs):
9488 if is_amendment :
9589 # For amendments, make currency field disabled in the UI
9690 # Note: disabled fields don't submit values, but we handle this in clean_charge_currency()
97- self .fields ['charge_currency' ].disabled = True
98- self .fields ['charge_currency' ].help_text = "Currency cannot be changed in amendments (inherited from original contract)"
91+ self .fields ["charge_currency" ].disabled = True
92+ self .fields ["charge_currency" ].help_text = (
93+ "Currency cannot be changed in amendments (inherited from original contract)"
94+ )
9995
10096 def clean_charge_currency (self ):
10197 """
10298 For amendments, enforce that currency matches the previous version.
10399 This prevents the issue where disabled fields don't submit values.
104100 """
105- charge_currency = self .cleaned_data .get (' charge_currency' )
101+ charge_currency = self .cleaned_data .get (" charge_currency" )
106102
107103 # For amendments, always use the previous version's currency
108104 if self ._is_amendment and self ._previous_version :
@@ -143,6 +139,10 @@ class Meta:
143139 "end_date" ,
144140 "notes" ,
145141 ]
142+ widgets = {
143+ "start_date" : DatePicker (),
144+ "end_date" : DatePicker (),
145+ }
146146
147147 fieldsets = (
148148 FieldSet (
@@ -155,11 +155,14 @@ class Meta:
155155 ),
156156 FieldSet (
157157 "charge_currency" ,
158+ "non_recurring_charge" ,
159+ name = "Charges" ,
160+ ),
161+ FieldSet (
158162 "recurring_charge" ,
159163 "recurring_charge_period" ,
160164 "number_of_recurring_charges" ,
161- "non_recurring_charge" ,
162- name = "Charges" ,
165+ name = "Recurring Charges" ,
163166 ),
164167 FieldSet (
165168 "start_date" ,
@@ -179,42 +182,21 @@ class ContractInfoFilterForm(NetBoxModelFilterSetForm):
179182 model = ContractInfo
180183
181184 # Text search (inherited from base, just needs to be defined)
182- contract_number = forms .CharField (
183- required = False ,
184- label = "Contract Number"
185- )
185+ contract_number = forms .CharField (required = False , label = "Contract Number" )
186186
187- contract_type = forms .MultipleChoiceField (
188- required = False ,
189- choices = ContractTypeChoices ,
190- label = "Contract Type"
191- )
187+ contract_type = forms .MultipleChoiceField (required = False , choices = ContractTypeChoices , label = "Contract Type" )
192188
193189 # Financial filters
194- charge_currency = forms .MultipleChoiceField (
195- required = False ,
196- choices = CurrencyChoices ,
197- label = "Currency"
198- )
190+ charge_currency = forms .MultipleChoiceField (required = False , choices = CurrencyChoices , label = "Currency" )
199191
200192 recurring_charge_period = forms .MultipleChoiceField (
201- required = False ,
202- choices = RecurringChargePeriodChoices ,
203- label = "Recurring Charge Period"
193+ required = False , choices = RecurringChargePeriodChoices , label = "Recurring Charge Period"
204194 )
205195
206196 # Date filters
207- start_date = forms .DateField (
208- required = False ,
209- label = "Start Date" ,
210- widget = forms .DateInput (attrs = {"type" : "date" })
211- )
197+ start_date = forms .DateField (required = False , label = "Start Date" , widget = forms .DateInput (attrs = {"type" : "date" }))
212198
213- end_date = forms .DateField (
214- required = False ,
215- label = "End Date" ,
216- widget = forms .DateInput (attrs = {"type" : "date" })
217- )
199+ end_date = forms .DateField (required = False , label = "End Date" , widget = forms .DateInput (attrs = {"type" : "date" }))
218200
219201 # Version chain filters
220202 is_active = forms .NullBooleanField (
@@ -227,7 +209,7 @@ class ContractInfoFilterForm(NetBoxModelFilterSetForm):
227209 ("true" , "Active" ),
228210 ("false" , "Superseded" ),
229211 ]
230- )
212+ ),
231213 )
232214
233215 has_previous_version = forms .NullBooleanField (
@@ -240,15 +222,12 @@ class ContractInfoFilterForm(NetBoxModelFilterSetForm):
240222 ("true" , "Has Previous Version" ),
241223 ("false" , "First Version (v1)" ),
242224 ]
243- )
225+ ),
244226 )
245227
246228 # Segments filter
247229 segments = DynamicModelMultipleChoiceField (
248- queryset = Segment .objects .all (),
249- required = False ,
250- label = "Segments" ,
251- help_text = "Filter by associated segments"
230+ queryset = Segment .objects .all (), required = False , label = "Segments" , help_text = "Filter by associated segments"
252231 )
253232
254233 # Tags filter
@@ -277,4 +256,4 @@ class ContractInfoFilterForm(NetBoxModelFilterSetForm):
277256 "segments" ,
278257 name = "Relationships" ,
279258 ),
280- )
259+ )
0 commit comments