88from django .core .exceptions import ValidationError
99from django .db import models , transaction
1010from django_lifecycle import ( # type: ignore[import-untyped]
11- AFTER_CREATE ,
1211 LifecycleModelMixin ,
13- hook ,
1412)
1513from flag_engine .segments import constants
1614
@@ -44,7 +42,7 @@ def get_queryset(self): # type: ignore[no-untyped-def]
4442 Returns only the canonical segments, which will always be
4543 the highest version.
4644 """
47- return super ().get_queryset ().filter (id = models . F ( "version_of" ) )
45+ return super ().get_queryset ().filter (version_of__isnull = True )
4846
4947
5048class ConfiguredOrderManager (SoftDeleteExportableManager , models .Manager [ModelT ]):
@@ -135,22 +133,17 @@ def __str__(self): # type: ignore[no-untyped-def]
135133 def get_skip_create_audit_log (self ) -> bool :
136134 if self .is_system_segment :
137135 return True
138- is_revision = bool ( self .version_of_id and self . version_of_id != self . id )
136+ is_revision = self .version_of_id is not None
139137 return is_revision
140138
141- @hook (AFTER_CREATE , when = "version_of" , is_now = None )
142- def set_version_of_to_self_if_none (self ): # type: ignore[no-untyped-def]
143- """
144- This allows the segment model to reference all versions of
145- itself including itself.
146- """
147- self .version_of = self
148- self .save_without_historical_record ()
149-
150139 @transaction .atomic
151140 def clone (self , is_revision : bool = False , ** extra_attrs : typing .Any ) -> "Segment" :
152141 """
153- Create a revision of the segment
142+ Create a revision of the segment.
143+
144+ Args:
145+ is_revision (bool): If True, creates a revision of the segment; if False, promotes the clone as canonical.
146+ **extra_attrs: Additional attributes to set on the cloned segment.
154147 """
155148 cloned_segment = deepcopy (self )
156149 cloned_segment .pk = None
@@ -164,7 +157,7 @@ def clone(self, is_revision: bool = False, **extra_attrs: typing.Any) -> "Segmen
164157 cloned_segment .copy_rules_and_conditions_from (self )
165158
166159 # Handle versioning
167- version_of = self if is_revision else cloned_segment
160+ version_of = self if is_revision else None
168161 cloned_segment .version_of = extra_attrs .get ("version_of" , version_of )
169162 cloned_segment .version = self .version if is_revision else 1
170163 Segment .objects .filter (pk = cloned_segment .pk ).update (
0 commit comments