Method to update the priorities of multiple feature segments at once. :param new_feature_segment_id_priorities: a list of 2-tuples containing the id, new priority value of the feature segments :return: a 3-tuple consisting of: - a boolean detailing w
(
cls,
new_feature_segment_id_priorities: typing.List[typing.Tuple[int, int]],
)
| 344 | |
| 345 | @classmethod |
| 346 | def update_priorities( |
| 347 | cls, |
| 348 | new_feature_segment_id_priorities: typing.List[typing.Tuple[int, int]], |
| 349 | ) -> QuerySet["FeatureSegment"]: |
| 350 | """ |
| 351 | Method to update the priorities of multiple feature segments at once. |
| 352 | |
| 353 | :param new_feature_segment_id_priorities: a list of 2-tuples containing the id, new priority value of |
| 354 | the feature segments |
| 355 | :return: a 3-tuple consisting of: |
| 356 | - a boolean detailing whether any changes were made |
| 357 | - a list of 2-tuples containing the id, old priority value of the feature segments |
| 358 | - a queryset containing the updated feature segment model objects |
| 359 | """ |
| 360 | feature_segments = cls.objects.filter( |
| 361 | id__in=[pair[0] for pair in new_feature_segment_id_priorities] |
| 362 | ) |
| 363 | |
| 364 | existing_feature_segment_id_priority_pairs = cls.to_id_priority_tuple_pairs( |
| 365 | feature_segments |
| 366 | ) |
| 367 | |
| 368 | def sort_function(id_priority_pair): # type: ignore[no-untyped-def] |
| 369 | priority = id_priority_pair[1] |
| 370 | return priority |
| 371 | |
| 372 | if sorted( |
| 373 | existing_feature_segment_id_priority_pairs, key=sort_function |
| 374 | ) == sorted(new_feature_segment_id_priorities, key=sort_function): |
| 375 | # no changes needed - do nothing (but return existing feature segments) |
| 376 | return feature_segments # type: ignore[no-any-return] |
| 377 | |
| 378 | id_priority_dict = dict(new_feature_segment_id_priorities) |
| 379 | |
| 380 | for fs in feature_segments: |
| 381 | new_priority = id_priority_dict[fs.id] |
| 382 | fs.to(new_priority) |
| 383 | |
| 384 | request = getattr(HistoricalRecords.thread, "request", None) |
| 385 | if request: |
| 386 | create_segment_priorities_changed_audit_log.delay( |
| 387 | kwargs={ |
| 388 | "previous_id_priority_pairs": existing_feature_segment_id_priority_pairs, |
| 389 | "feature_segment_ids": [ |
| 390 | pair[0] for pair in new_feature_segment_id_priorities |
| 391 | ], |
| 392 | "user_id": getattr(request.user, "id", None), |
| 393 | "master_api_key_id": ( |
| 394 | request.master_api_key.id |
| 395 | if hasattr(request, "master_api_key") |
| 396 | else None |
| 397 | ), |
| 398 | "changed_at": timezone.now().isoformat(), |
| 399 | } |
| 400 | ) |
| 401 | |
| 402 | # since the `to` method updates the priority in place, we don't need to refresh |
| 403 | # the objects from the database. |