@@ -116,6 +116,7 @@ def create_cohort(
116116 name : str ,
117117 description : str | None = None ,
118118 source_type : CohortSourceType = CohortSourceType .CSV ,
119+ external_id : str | None = None ,
119120) -> Cohort :
120121 project = environment .project
121122 # Mirrors the segment limit enforced by SegmentSerializer, which cohort
@@ -137,7 +138,10 @@ def create_cohort(
137138 )
138139 rule = SegmentRule .objects .create (segment = segment , type = SegmentRule .ALL_RULE )
139140 cohort : Cohort = Cohort .objects .create (
140- environment = environment , segment = segment , source_type = source_type
141+ environment = environment ,
142+ segment = segment ,
143+ source_type = source_type ,
144+ external_id = external_id ,
141145 )
142146 Condition .objects .create (
143147 rule = rule ,
@@ -161,10 +165,16 @@ def create_cohort_for_source(
161165 environment : "Environment" ,
162166 name : str ,
163167 source_type : CohortSourceType ,
168+ external_id : str | None = None ,
164169) -> Cohort :
165170 """Create a cohort on behalf of an external source, where no Flagsmith
166171 user is acting."""
167- cohort = create_cohort (environment = environment , name = name , source_type = source_type )
172+ cohort = create_cohort (
173+ environment = environment ,
174+ name = name ,
175+ source_type = source_type ,
176+ external_id = external_id ,
177+ )
168178 # Nothing records a user for these calls, so the audit log that Flagsmith
169179 # derives from historical records is skipped — and with it the environment
170180 # document rebuild that makes the new segment visible to SDKs. Write the
@@ -182,6 +192,37 @@ def create_cohort_for_source(
182192 return cohort
183193
184194
195+ def get_cohort_for_source (
196+ * ,
197+ environment : "Environment" ,
198+ source_type : CohortSourceType ,
199+ external_id : str ,
200+ ) -> Cohort | None :
201+ cohort : Cohort | None = Cohort .objects .filter (
202+ environment = environment ,
203+ source_type = source_type ,
204+ external_id = external_id ,
205+ deletion_requested_at__isnull = True ,
206+ ).first ()
207+ return cohort
208+
209+
210+ def cohort_deletion_in_progress (
211+ * ,
212+ environment : "Environment" ,
213+ source_type : CohortSourceType ,
214+ external_id : str ,
215+ ) -> bool :
216+ return bool (
217+ Cohort .objects .filter (
218+ environment = environment ,
219+ source_type = source_type ,
220+ external_id = external_id ,
221+ deletion_requested_at__isnull = False ,
222+ ).exists ()
223+ )
224+
225+
185226def add_cohort_members (cohort : Cohort , identifiers : "typing.Iterable[str]" ) -> None :
186227 from cohorts .tasks import apply_cohort_membership_deltas
187228
0 commit comments