@@ -84,11 +84,11 @@ def _set_boundary(
84
84
)
85
85
86
86
if lower is not None :
87
- validate_value_type (instance = instance , value = lower )
87
+ _validate_value_type (instance = instance , value = lower )
88
88
if upper is not None :
89
- validate_value_type (instance = instance , value = upper )
89
+ _validate_value_type (instance = instance , value = upper )
90
90
91
- RangeClass = get_range_type (instance ) # pylint: disable=C0103
91
+ RangeClass = _get_range_type (instance ) # pylint: disable=C0103
92
92
93
93
if lower is not None :
94
94
print (f"boundary_helper_factory _set_boundary: [{ lower = } { model_range_field .upper = } )" )
@@ -110,15 +110,15 @@ def _set_boundary(
110
110
setattr (instance , range_field_name , range_value )
111
111
instance .save ()
112
112
113
- def validate_value_type (
113
+ def _validate_value_type (
114
114
instance : Union [AbstractSpan , AbstractSegment ],
115
115
value : Union [int , Decimal , date , datetime ],
116
116
) -> None :
117
117
"""Validate the type of the provided value against the range_field_type."""
118
118
if value is None :
119
119
raise ValueError ("Value cannot be None" )
120
120
121
- SpanConfig = get_span_config (instance ) # pylint: disable=C0103
121
+ SpanConfig = _get_span_config (instance ) # pylint: disable=C0103
122
122
123
123
if SpanConfig .range_field_type not in POSTGRES_RANGE_FIELDS :
124
124
raise IncorrectRangeTypeError (f"Unsupported field type: { SpanConfig .range_field_type } " )
@@ -128,61 +128,43 @@ def validate_value_type(
128
128
if not isinstance (value , field_type ):
129
129
raise ValueError (f"Value must be of type { field_type } , not { type (value )} " )
130
130
131
- # def validate_delta_value_type(
132
- # instance: Union[AbstractSpan, AbstractSegment],
133
- # delta_value: Union[int, Decimal, timezone.timedelta],
134
- # ) -> None:
135
- # """Validate the type of the provided delta value against the range_field_type."""
136
- # if delta_value is None:
137
- # raise ValueError("Delta value cannot be None")
138
-
139
- # SpanConfig = get_span_config(instance) # pylint: disable=C0103
140
-
141
- # if SpanConfig.range_field_type not in POSTGRES_RANGE_FIELDS:
142
- # raise IncorrectRangeTypeError(f"Unsupported field type: {SpanConfig.range_field_type}")
143
-
144
- # delta_type = POSTGRES_RANGE_FIELDS[SpanConfig.range_field_type].get("delta_type")
145
-
146
- # if not isinstance(delta_value, delta_type):
147
- # raise ValueError(f"Delta value must be of type {delta_type}, not {type(delta_value)}")
148
-
149
- def get_range_type (instance : Union [AbstractSpan , AbstractSegment ]):
131
+ def _get_range_type (instance : Union [AbstractSpan , AbstractSegment ]):
150
132
"""Get the range class for the instance based on the range_field_type."""
151
133
try :
152
- span_config = get_span_config (instance ) # pylint: disable=C0103
134
+ span_config = _get_span_config (instance ) # pylint: disable=C0103
153
135
RangeClass = POSTGRES_RANGE_FIELDS [span_config .range_field_type ]["range_type" ] # pylint: disable=C0103
154
136
155
137
except AttributeError as e :
156
138
raise IncorrectRangeTypeError (f"Range type cannot be obtained for { instance .__class__ .__name__ } " ) from e
157
139
158
140
return RangeClass
159
141
160
- def get_span_config (instance : Union [AbstractSpan , AbstractSegment ]):
142
+ def _get_span_config (instance : Union [AbstractSpan , AbstractSegment ]):
161
143
"""Return the SpanConfig class for the instance."""
162
144
if not hasattr (instance , "SpanConfig" ):
163
145
instance .SpanConfig = SegmentConfigurationHelper .get_span_model (instance ).SpanConfig
164
146
return instance .SpanConfig
165
147
166
- def set_boundaries (
148
+ def _set_boundaries (
167
149
instance : Union [AbstractSpan , AbstractSegment ],
168
150
lower : Union [int , Decimal , date , datetime ],
169
151
upper : Union [int , Decimal , date , datetime ],
170
152
):
171
153
"""Set the lower and upper boundaries of the specified range field."""
172
154
_set_boundary (instance , range_field_name , lower = lower , upper = upper )
173
155
174
- def set_lower_boundary (instance : Union [AbstractSpan , AbstractSegment ], value : Union [int , Decimal , date , datetime ]):
156
+ def _set_lower_boundary (instance : Union [AbstractSpan , AbstractSegment ], value : Union [int , Decimal , date , datetime ]):
175
157
"""Set the lower boundary of the specified range field."""
176
158
_set_boundary (instance , range_field_name , lower = value )
177
159
178
- def set_upper_boundary (instance : Union [AbstractSpan , AbstractSegment ], value : Union [int , Decimal , date , datetime ]):
160
+ def _set_upper_boundary (instance : Union [AbstractSpan , AbstractSegment ], value : Union [int , Decimal , date , datetime ]):
179
161
"""Set the upper boundary of the specified range field."""
180
162
_set_boundary (instance , range_field_name , upper = value )
181
163
182
164
return (
183
- set_boundaries ,
184
- set_lower_boundary ,
185
- set_upper_boundary ,
165
+ _set_boundaries ,
166
+ _set_lower_boundary ,
167
+ _set_upper_boundary ,
186
168
)
187
169
188
170
@@ -310,21 +292,21 @@ def __new__(cls, name, bases, attrs, **kwargs):
310
292
model = super ().__new__ (cls , name , bases , attrs , ** kwargs ) # pylint: disable=E1121
311
293
312
294
# Validate subclass of AbstractSpan & set initial_range and current_range for the model.
313
- if not cls .is_valid_subclass (bases ):
295
+ if not cls ._is_valid_subclass (bases ):
314
296
raise IncorrectSubclassError ("BaseSpanMetaclass applied to incorrect Span MRO" )
315
297
316
- cls .setup_span_model (model , name )
298
+ cls ._setup_span_model (model , name )
317
299
model ._meta ._expire_cache ()
318
300
return model
319
301
320
302
@staticmethod
321
- def is_valid_subclass (bases ):
303
+ def _is_valid_subclass (bases ):
322
304
"""Check if the metaclass is applied to the correct subclass."""
323
305
base_list = [base .__name__ for base in bases ]
324
306
return any ([(len (base_list ) == 1 and base_list [0 ] == "Model" ), "AbstractSpan" in base_list ])
325
307
326
308
@classmethod
327
- def setup_span_model (cls , model , name ):
309
+ def _setup_span_model (cls , model , name ):
328
310
"""Set up the span model."""
329
311
if "AbstractSpan" in [base .__name__ for base in model .__bases__ ]:
330
312
ConcreteModelValidationHelper .check_model_is_concrete (model )
@@ -338,11 +320,11 @@ def setup_span_model(cls, model, name):
338
320
"current_range" , config_dict ["range_field_type" ](_ ("Current Range" ), blank = True , null = True )
339
321
)
340
322
model_short_hash = generate_short_hash (name )
341
- cls .add_indexes (model , model_short_hash )
342
- cls .add_soft_delete_field (model , model_short_hash , config_dict )
323
+ cls ._add_indexes (model , model_short_hash )
324
+ cls ._add_soft_delete_field (model , model_short_hash , config_dict )
343
325
344
326
@staticmethod
345
- def add_indexes (model , model_short_hash ):
327
+ def _add_indexes (model , model_short_hash ):
346
328
"""Add indexes for the initial_range and current_range fields."""
347
329
indexes_list = list (model ._meta .indexes ) # pylint: disable=W0212
348
330
indexes_list .extend (
@@ -354,7 +336,7 @@ def add_indexes(model, model_short_hash):
354
336
model ._meta .indexes = indexes_list # pylint: disable=W0212
355
337
356
338
@staticmethod
357
- def add_soft_delete_field (model , model_short_hash , config_dict ):
339
+ def _add_soft_delete_field (model , model_short_hash , config_dict ):
358
340
"""Add a deleted_at field to the model if soft_delete is enabled."""
359
341
if config_dict ["soft_delete" ]:
360
342
model .add_to_class (
@@ -398,21 +380,21 @@ def __new__(cls, name, bases, attrs, **kwargs):
398
380
model = super ().__new__ (cls , name , bases , attrs , ** kwargs ) # pylint: disable=E1121
399
381
400
382
# Validate subclass of AbstractSegment & set segment_range and span for the concrete model.
401
- if not cls .is_valid_subclass (bases ):
383
+ if not cls ._is_valid_subclass (bases ):
402
384
raise IncorrectSubclassError ("BaseSegmentMetaclass applied to incorrect Segment MRO" )
403
385
404
- cls .setup_segment_model (model , name )
386
+ cls ._setup_segment_model (model , name )
405
387
model ._meta ._expire_cache ()
406
388
return model
407
389
408
390
@staticmethod
409
- def is_valid_subclass (bases ):
391
+ def _is_valid_subclass (bases ):
410
392
"""Check if the model is a valid subclass of AbstractSegment."""
411
393
base_list = [base .__name__ for base in bases ]
412
394
return any ([(len (base_list ) == 1 and base_list [0 ] == "Model" ), "AbstractSegment" in base_list ])
413
395
414
396
@classmethod
415
- def setup_segment_model (cls , model , name ):
397
+ def _setup_segment_model (cls , model , name ):
416
398
"""Set up the segment model."""
417
399
if "AbstractSegment" in [base .__name__ for base in model .__bases__ ]:
418
400
ConcreteModelValidationHelper .check_model_is_concrete (model )
@@ -445,19 +427,19 @@ def setup_segment_model(cls, model, name):
445
427
)
446
428
447
429
model_short_hash = generate_short_hash (name )
448
- cls .add_indexes (model , model_short_hash )
449
- cls .add_constraints (model , model_short_hash )
450
- cls .add_soft_delete_field (model , model_short_hash , config_dict )
430
+ cls ._add_indexes (model , model_short_hash )
431
+ cls ._add_constraints (model , model_short_hash )
432
+ cls ._add_soft_delete_field (model , model_short_hash , config_dict )
451
433
452
434
@classmethod
453
- def add_indexes (cls , model , model_short_hash ):
435
+ def _add_indexes (cls , model , model_short_hash ):
454
436
"""Add the segment_range index to the model."""
455
437
indexes_list = list (model ._meta .indexes ) # pylint: disable=W0212
456
438
indexes_list .append (models .Index (fields = ["segment_range" ], name = f"segment_range_idx_{ model_short_hash } " ))
457
439
model ._meta .indexes = indexes_list # pylint: disable=W0212
458
440
459
441
@classmethod
460
- def add_constraints (cls , model , model_short_hash ):
442
+ def _add_constraints (cls , model , model_short_hash ):
461
443
"""Ensure that the segment_range does not overlap with other segments associated with the same span."""
462
444
constraints_list = list (model ._meta .constraints ) # pylint: disable=W0212
463
445
constraints_list .append (
@@ -470,7 +452,7 @@ def add_constraints(cls, model, model_short_hash):
470
452
model ._meta .constraints = constraints_list # pylint: disable=W0212
471
453
472
454
@classmethod
473
- def add_soft_delete_field (cls , model , model_short_hash , config_dict ):
455
+ def _add_soft_delete_field (cls , model , model_short_hash , config_dict ):
474
456
"""Add the deleted_at field to the model if soft_delete is enabled."""
475
457
if config_dict ["soft_delete" ]:
476
458
model .add_to_class (
0 commit comments