Skip to content

Commit 62ee761

Browse files
committed
Rename several methods to be private
1 parent 1f83181 commit 62ee761

File tree

1 file changed

+32
-50
lines changed

1 file changed

+32
-50
lines changed

src/django_segments/models/base.py

+32-50
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,11 @@ def _set_boundary(
8484
)
8585

8686
if lower is not None:
87-
validate_value_type(instance=instance, value=lower)
87+
_validate_value_type(instance=instance, value=lower)
8888
if upper is not None:
89-
validate_value_type(instance=instance, value=upper)
89+
_validate_value_type(instance=instance, value=upper)
9090

91-
RangeClass = get_range_type(instance) # pylint: disable=C0103
91+
RangeClass = _get_range_type(instance) # pylint: disable=C0103
9292

9393
if lower is not None:
9494
print(f"boundary_helper_factory _set_boundary: [{lower=} {model_range_field.upper=})")
@@ -110,15 +110,15 @@ def _set_boundary(
110110
setattr(instance, range_field_name, range_value)
111111
instance.save()
112112

113-
def validate_value_type(
113+
def _validate_value_type(
114114
instance: Union[AbstractSpan, AbstractSegment],
115115
value: Union[int, Decimal, date, datetime],
116116
) -> None:
117117
"""Validate the type of the provided value against the range_field_type."""
118118
if value is None:
119119
raise ValueError("Value cannot be None")
120120

121-
SpanConfig = get_span_config(instance) # pylint: disable=C0103
121+
SpanConfig = _get_span_config(instance) # pylint: disable=C0103
122122

123123
if SpanConfig.range_field_type not in POSTGRES_RANGE_FIELDS:
124124
raise IncorrectRangeTypeError(f"Unsupported field type: {SpanConfig.range_field_type}")
@@ -128,61 +128,43 @@ def validate_value_type(
128128
if not isinstance(value, field_type):
129129
raise ValueError(f"Value must be of type {field_type}, not {type(value)}")
130130

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]):
150132
"""Get the range class for the instance based on the range_field_type."""
151133
try:
152-
span_config = get_span_config(instance) # pylint: disable=C0103
134+
span_config = _get_span_config(instance) # pylint: disable=C0103
153135
RangeClass = POSTGRES_RANGE_FIELDS[span_config.range_field_type]["range_type"] # pylint: disable=C0103
154136

155137
except AttributeError as e:
156138
raise IncorrectRangeTypeError(f"Range type cannot be obtained for {instance.__class__.__name__}") from e
157139

158140
return RangeClass
159141

160-
def get_span_config(instance: Union[AbstractSpan, AbstractSegment]):
142+
def _get_span_config(instance: Union[AbstractSpan, AbstractSegment]):
161143
"""Return the SpanConfig class for the instance."""
162144
if not hasattr(instance, "SpanConfig"):
163145
instance.SpanConfig = SegmentConfigurationHelper.get_span_model(instance).SpanConfig
164146
return instance.SpanConfig
165147

166-
def set_boundaries(
148+
def _set_boundaries(
167149
instance: Union[AbstractSpan, AbstractSegment],
168150
lower: Union[int, Decimal, date, datetime],
169151
upper: Union[int, Decimal, date, datetime],
170152
):
171153
"""Set the lower and upper boundaries of the specified range field."""
172154
_set_boundary(instance, range_field_name, lower=lower, upper=upper)
173155

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]):
175157
"""Set the lower boundary of the specified range field."""
176158
_set_boundary(instance, range_field_name, lower=value)
177159

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]):
179161
"""Set the upper boundary of the specified range field."""
180162
_set_boundary(instance, range_field_name, upper=value)
181163

182164
return (
183-
set_boundaries,
184-
set_lower_boundary,
185-
set_upper_boundary,
165+
_set_boundaries,
166+
_set_lower_boundary,
167+
_set_upper_boundary,
186168
)
187169

188170

@@ -310,21 +292,21 @@ def __new__(cls, name, bases, attrs, **kwargs):
310292
model = super().__new__(cls, name, bases, attrs, **kwargs) # pylint: disable=E1121
311293

312294
# 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):
314296
raise IncorrectSubclassError("BaseSpanMetaclass applied to incorrect Span MRO")
315297

316-
cls.setup_span_model(model, name)
298+
cls._setup_span_model(model, name)
317299
model._meta._expire_cache()
318300
return model
319301

320302
@staticmethod
321-
def is_valid_subclass(bases):
303+
def _is_valid_subclass(bases):
322304
"""Check if the metaclass is applied to the correct subclass."""
323305
base_list = [base.__name__ for base in bases]
324306
return any([(len(base_list) == 1 and base_list[0] == "Model"), "AbstractSpan" in base_list])
325307

326308
@classmethod
327-
def setup_span_model(cls, model, name):
309+
def _setup_span_model(cls, model, name):
328310
"""Set up the span model."""
329311
if "AbstractSpan" in [base.__name__ for base in model.__bases__]:
330312
ConcreteModelValidationHelper.check_model_is_concrete(model)
@@ -338,11 +320,11 @@ def setup_span_model(cls, model, name):
338320
"current_range", config_dict["range_field_type"](_("Current Range"), blank=True, null=True)
339321
)
340322
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)
343325

344326
@staticmethod
345-
def add_indexes(model, model_short_hash):
327+
def _add_indexes(model, model_short_hash):
346328
"""Add indexes for the initial_range and current_range fields."""
347329
indexes_list = list(model._meta.indexes) # pylint: disable=W0212
348330
indexes_list.extend(
@@ -354,7 +336,7 @@ def add_indexes(model, model_short_hash):
354336
model._meta.indexes = indexes_list # pylint: disable=W0212
355337

356338
@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):
358340
"""Add a deleted_at field to the model if soft_delete is enabled."""
359341
if config_dict["soft_delete"]:
360342
model.add_to_class(
@@ -398,21 +380,21 @@ def __new__(cls, name, bases, attrs, **kwargs):
398380
model = super().__new__(cls, name, bases, attrs, **kwargs) # pylint: disable=E1121
399381

400382
# 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):
402384
raise IncorrectSubclassError("BaseSegmentMetaclass applied to incorrect Segment MRO")
403385

404-
cls.setup_segment_model(model, name)
386+
cls._setup_segment_model(model, name)
405387
model._meta._expire_cache()
406388
return model
407389

408390
@staticmethod
409-
def is_valid_subclass(bases):
391+
def _is_valid_subclass(bases):
410392
"""Check if the model is a valid subclass of AbstractSegment."""
411393
base_list = [base.__name__ for base in bases]
412394
return any([(len(base_list) == 1 and base_list[0] == "Model"), "AbstractSegment" in base_list])
413395

414396
@classmethod
415-
def setup_segment_model(cls, model, name):
397+
def _setup_segment_model(cls, model, name):
416398
"""Set up the segment model."""
417399
if "AbstractSegment" in [base.__name__ for base in model.__bases__]:
418400
ConcreteModelValidationHelper.check_model_is_concrete(model)
@@ -445,19 +427,19 @@ def setup_segment_model(cls, model, name):
445427
)
446428

447429
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)
451433

452434
@classmethod
453-
def add_indexes(cls, model, model_short_hash):
435+
def _add_indexes(cls, model, model_short_hash):
454436
"""Add the segment_range index to the model."""
455437
indexes_list = list(model._meta.indexes) # pylint: disable=W0212
456438
indexes_list.append(models.Index(fields=["segment_range"], name=f"segment_range_idx_{model_short_hash}"))
457439
model._meta.indexes = indexes_list # pylint: disable=W0212
458440

459441
@classmethod
460-
def add_constraints(cls, model, model_short_hash):
442+
def _add_constraints(cls, model, model_short_hash):
461443
"""Ensure that the segment_range does not overlap with other segments associated with the same span."""
462444
constraints_list = list(model._meta.constraints) # pylint: disable=W0212
463445
constraints_list.append(
@@ -470,7 +452,7 @@ def add_constraints(cls, model, model_short_hash):
470452
model._meta.constraints = constraints_list # pylint: disable=W0212
471453

472454
@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):
474456
"""Add the deleted_at field to the model if soft_delete is enabled."""
475457
if config_dict["soft_delete"]:
476458
model.add_to_class(

0 commit comments

Comments
 (0)