88from django .core .exceptions import ValidationError
99from django .db import models , transaction
1010from django_lifecycle import ( # type: ignore[import-untyped]
11- AFTER_CREATE ,
11+ BEFORE_CREATE ,
1212 LifecycleModelMixin ,
13- hook ,
1413)
1514from flag_engine .segments import constants
1615
@@ -44,7 +43,7 @@ def get_queryset(self): # type: ignore[no-untyped-def]
4443 Returns only the canonical segments, which will always be
4544 the highest version.
4645 """
47- return super ().get_queryset ().filter (id = models . F ( "version_of" ) )
46+ return super ().get_queryset ().filter (version_of__isnull = True )
4847
4948
5049class ConfiguredOrderManager (SoftDeleteExportableManager , models .Manager [ModelT ]):
@@ -135,22 +134,22 @@ def __str__(self): # type: ignore[no-untyped-def]
135134 def get_skip_create_audit_log (self ) -> bool :
136135 if self .is_system_segment :
137136 return True
138- is_revision = bool ( self .version_of_id and self . version_of_id != self . id )
137+ is_revision = self .version_of_id is not None
139138 return is_revision
140139
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 ()
140+ @BEFORE_CREATE (when = "version_of" , is_now = None )
141+ def set_default_version_to_one_if_new_segment (self ): # type: ignore[no-untyped-def]
142+ if self .version is None :
143+ self .version = 1
149144
150145 @transaction .atomic
151146 def clone (self , is_revision : bool = False , ** extra_attrs : typing .Any ) -> "Segment" :
152147 """
153- Create a revision of the segment
148+ Create a revision of the segment.
149+
150+ Args:
151+ is_revision (bool): If True, creates a revision of the segment; if False, promotes the clone as canonical.
152+ **extra_attrs: Additional attributes to set on the cloned segment.
154153 """
155154 cloned_segment = deepcopy (self )
156155 cloned_segment .pk = None
@@ -164,7 +163,7 @@ def clone(self, is_revision: bool = False, **extra_attrs: typing.Any) -> "Segmen
164163 cloned_segment .copy_rules_and_conditions_from (self )
165164
166165 # Handle versioning
167- version_of = self if is_revision else cloned_segment
166+ version_of = self if is_revision else None
168167 cloned_segment .version_of = extra_attrs .get ("version_of" , version_of )
169168 cloned_segment .version = self .version if is_revision else 1
170169 Segment .objects .filter (pk = cloned_segment .pk ).update (
0 commit comments