| 146 | return response |
| 147 | |
| 148 | def update(self, request, *args, **kwargs): |
| 149 | instance = self.get_object() |
| 150 | old_vod_enabled = False |
| 151 | |
| 152 | # Check current VOD setting |
| 153 | if instance.custom_properties: |
| 154 | custom_props = instance.custom_properties or {} |
| 155 | old_vod_enabled = custom_props.get("enable_vod", False) |
| 156 | |
| 157 | # Handle file upload first, if any |
| 158 | file_path = None |
| 159 | if "file" in request.FILES: |
| 160 | file = request.FILES["file"] |
| 161 | try: |
| 162 | file_path = safe_upload_path(file.name, "/data/uploads/m3us") |
| 163 | except ValueError: |
| 164 | return Response({"detail": "Invalid filename."}, status=status.HTTP_400_BAD_REQUEST) |
| 165 | |
| 166 | os.makedirs("/data/uploads/m3us", exist_ok=True) |
| 167 | with open(file_path, "wb+") as destination: |
| 168 | for chunk in file.chunks(): |
| 169 | destination.write(chunk) |
| 170 | |
| 171 | # Add file_path to the request data so it's available during creation |
| 172 | request.data._mutable = True # Allow modification of the request data |
| 173 | request.data["file_path"] = ( |
| 174 | file_path # Include the file path if a file was uploaded |
| 175 | ) |
| 176 | |
| 177 | # Handle the user_agent field - convert "null" string to None |
| 178 | if "user_agent" in request.data and request.data["user_agent"] == "null": |
| 179 | request.data["user_agent"] = None |
| 180 | |
| 181 | # Handle server_url appropriately |
| 182 | if "server_url" in request.data and not request.data["server_url"]: |
| 183 | request.data.pop("server_url") |
| 184 | |
| 185 | request.data._mutable = False # Make the request data immutable again |
| 186 | |
| 187 | if instance.file_path and os.path.exists(instance.file_path): |
| 188 | os.remove(instance.file_path) |
| 189 | |
| 190 | # Now call super().update() to update the instance |
| 191 | response = super().update(request, *args, **kwargs) |
| 192 | |
| 193 | # Check if VOD setting changed and trigger refresh if needed |
| 194 | new_vod_enabled = request.data.get("enable_vod", old_vod_enabled) |
| 195 | |
| 196 | if ( |
| 197 | instance.account_type == M3UAccount.Types.XC |
| 198 | and not old_vod_enabled |
| 199 | and new_vod_enabled |
| 200 | ): |
| 201 | # Create Uncategorized categories immediately so they're available in the UI |
| 202 | from apps.vod.models import VODCategory, M3UVODCategoryRelation |
| 203 | |
| 204 | # Create movie Uncategorized category |
| 205 | movie_category, _ = VODCategory.objects.get_or_create( |