Bulk edit channels efficiently. Validates all updates first, then applies in a single transaction.
(self, request)
| 1049 | ) |
| 1050 | @action(detail=False, methods=["patch"], url_path="edit/bulk") |
| 1051 | def edit_bulk(self, request): |
| 1052 | """ |
| 1053 | Bulk edit channels efficiently. |
| 1054 | Validates all updates first, then applies in a single transaction. |
| 1055 | """ |
| 1056 | data = request.data |
| 1057 | if not isinstance(data, list): |
| 1058 | return Response( |
| 1059 | {"error": "Expected a list of channel updates"}, |
| 1060 | status=status.HTTP_400_BAD_REQUEST, |
| 1061 | ) |
| 1062 | |
| 1063 | # Extract IDs and validate presence |
| 1064 | channel_updates = {} |
| 1065 | missing_ids = [] |
| 1066 | |
| 1067 | for i, channel_data in enumerate(data): |
| 1068 | channel_id = channel_data.get("id") |
| 1069 | if not channel_id: |
| 1070 | missing_ids.append(f"Item {i}: Channel ID is required") |
| 1071 | else: |
| 1072 | channel_updates[channel_id] = channel_data |
| 1073 | |
| 1074 | if missing_ids: |
| 1075 | return Response( |
| 1076 | {"errors": missing_ids}, |
| 1077 | status=status.HTTP_400_BAD_REQUEST, |
| 1078 | ) |
| 1079 | |
| 1080 | # Fetch all channels at once (one query) |
| 1081 | channels_dict = { |
| 1082 | c.id: c for c in Channel.objects.filter(id__in=channel_updates.keys()) |
| 1083 | } |
| 1084 | |
| 1085 | # Validate and prepare updates |
| 1086 | validated_updates = [] |
| 1087 | errors = [] |
| 1088 | |
| 1089 | for channel_id, channel_data in channel_updates.items(): |
| 1090 | channel = channels_dict.get(channel_id) |
| 1091 | |
| 1092 | if not channel: |
| 1093 | errors.append({ |
| 1094 | "channel_id": channel_id, |
| 1095 | "error": "Channel not found" |
| 1096 | }) |
| 1097 | continue |
| 1098 | |
| 1099 | # Handle channel_group_id conversion |
| 1100 | if 'channel_group_id' in channel_data: |
| 1101 | group_id = channel_data['channel_group_id'] |
| 1102 | if group_id is not None: |
| 1103 | try: |
| 1104 | channel_data['channel_group_id'] = int(group_id) |
| 1105 | except (ValueError, TypeError): |
| 1106 | channel_data['channel_group_id'] = None |
| 1107 | |
| 1108 | # Validate with serializer |
nothing calls this directly
no test coverage detected