|
8 | 8 | from django.contrib.auth.decorators import login_required |
9 | 9 | from django.core.cache import cache |
10 | 10 | from django.db.models import Max, Min |
| 11 | +from django.db.utils import OperationalError |
11 | 12 | from django.http import HttpResponse |
12 | 13 | from django.shortcuts import get_object_or_404, redirect, render |
| 14 | +from django.urls import reverse |
13 | 15 | from django.utils import timezone |
14 | 16 | from django.utils.http import url_has_allowed_host_and_scheme |
15 | 17 | from django.views.decorators.http import require_GET, require_POST |
|
21 | 23 | metadata_utils, |
22 | 24 | view_constants, |
23 | 25 | ) |
| 26 | +from app.db_retry import is_retryable_error, run_retryable_db_operation |
24 | 27 | from app.log_safety import exception_summary, safe_url |
25 | 28 | from app.models import ( |
26 | 29 | CollectionEntry, |
|
35 | 38 | from app.services import ( |
36 | 39 | anime_migration, |
37 | 40 | bulk_episode_tracking, |
| 41 | + library_migration, |
38 | 42 | metadata_resolution, |
39 | 43 | ) |
40 | 44 | from app.services import game_lengths as game_length_services |
@@ -174,6 +178,126 @@ def search_remap_candidates(request, source, media_type, media_id): |
174 | 178 | ) |
175 | 179 |
|
176 | 180 |
|
| 181 | +@login_required |
| 182 | +@require_GET |
| 183 | +def search_library_move_candidates(request, item_id): |
| 184 | + """Search the configured destination provider for a library move.""" |
| 185 | + item = get_object_or_404(Item, id=item_id) |
| 186 | + move_context = library_migration.get_move_context(request.user, item) |
| 187 | + query = (request.GET.get("q") or "").strip() |
| 188 | + results = [] |
| 189 | + if move_context and query: |
| 190 | + try: |
| 191 | + response = services.search( |
| 192 | + move_context["target_media_type"], |
| 193 | + query, |
| 194 | + page=1, |
| 195 | + source=move_context["target_source"], |
| 196 | + user=request.user, |
| 197 | + language=metadata_resolution.metadata_language_default( |
| 198 | + request.user, |
| 199 | + ), |
| 200 | + ) |
| 201 | + results = (response or {}).get("results") or [] |
| 202 | + except services.ProviderAPIError: |
| 203 | + logger.warning( |
| 204 | + "Destination library search failed for item %s", |
| 205 | + item_id, |
| 206 | + exc_info=True, |
| 207 | + ) |
| 208 | + |
| 209 | + return render( |
| 210 | + request, |
| 211 | + "app/components/library_move_results.html", |
| 212 | + { |
| 213 | + "results": results, |
| 214 | + "item_id": item.id, |
| 215 | + "query": query, |
| 216 | + "return_url": helpers.normalize_navigation_url( |
| 217 | + request.GET.get("return_url"), |
| 218 | + ), |
| 219 | + **(move_context or {}), |
| 220 | + }, |
| 221 | + ) |
| 222 | + |
| 223 | + |
| 224 | +@login_required |
| 225 | +@require_POST |
| 226 | +def move_library_item(request, item_id): |
| 227 | + """Move one owned TV/Anime show into an explicit destination identity.""" |
| 228 | + item = get_object_or_404(Item, id=item_id) |
| 229 | + return_url = helpers.normalize_navigation_url(request.POST.get("return_url")) |
| 230 | + target_media_type = request.POST.get("target_media_type") |
| 231 | + target_source = request.POST.get("target_source") |
| 232 | + target_media_id = request.POST.get("target_media_id") |
| 233 | + is_htmx = request.headers.get("HX-Request") == "true" |
| 234 | + try: |
| 235 | + target_item = run_retryable_db_operation( |
| 236 | + lambda: library_migration.migrate_library_item( |
| 237 | + request.user, |
| 238 | + item, |
| 239 | + target_media_type, |
| 240 | + target_source, |
| 241 | + target_media_id, |
| 242 | + ), |
| 243 | + operation_name="TV/Anime library migration", |
| 244 | + ).value |
| 245 | + except OperationalError as error: |
| 246 | + if not is_retryable_error(error): |
| 247 | + raise |
| 248 | + if is_htmx: |
| 249 | + return render( |
| 250 | + request, |
| 251 | + "app/components/library_move_results.html", |
| 252 | + { |
| 253 | + "error": ( |
| 254 | + "The database is busy with another operation. " |
| 255 | + "Nothing was changed; please try Move again." |
| 256 | + ), |
| 257 | + }, |
| 258 | + ) |
| 259 | + raise |
| 260 | + except library_migration.LibraryMigrationError as error: |
| 261 | + if is_htmx: |
| 262 | + return render( |
| 263 | + request, |
| 264 | + "app/components/library_move_results.html", |
| 265 | + {"error": str(error)}, |
| 266 | + ) |
| 267 | + messages.error(request, str(error)) |
| 268 | + if return_url and url_has_allowed_host_and_scheme( |
| 269 | + return_url, |
| 270 | + allowed_hosts=None, |
| 271 | + ): |
| 272 | + return redirect(return_url) |
| 273 | + return redirect( |
| 274 | + "media_details", |
| 275 | + source=item.source, |
| 276 | + media_type=library_migration.library_bucket(item), |
| 277 | + media_id=item.media_id, |
| 278 | + title=item.get_display_title(request.user) or "item", |
| 279 | + ) |
| 280 | + |
| 281 | + messages.success( |
| 282 | + request, |
| 283 | + f"Moved tracking to {target_item.get_display_title(request.user) or 'the selected title'}.", |
| 284 | + ) |
| 285 | + destination_url = reverse( |
| 286 | + "media_details", |
| 287 | + kwargs={ |
| 288 | + "source": target_item.source, |
| 289 | + "media_type": library_migration.library_bucket(target_item), |
| 290 | + "media_id": target_item.media_id, |
| 291 | + "title": target_item.get_display_title(request.user) or "item", |
| 292 | + }, |
| 293 | + ) |
| 294 | + if is_htmx: |
| 295 | + response = HttpResponse(status=204) |
| 296 | + response["HX-Redirect"] = destination_url |
| 297 | + return response |
| 298 | + return redirect(destination_url) |
| 299 | + |
| 300 | + |
177 | 301 | @login_required |
178 | 302 | @require_POST |
179 | 303 | def remap_metadata_provider(request, source, media_type, media_id): |
|
0 commit comments