2
0

views.py 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602
  1. import datetime
  2. import pytz
  3. from django.db.models import Q
  4. from django.http import HttpResponse
  5. from django.shortcuts import render, redirect
  6. from apps.main.models import Playlist
  7. from django.contrib.auth.decorators import login_required # redirects user to settings.LOGIN_URL
  8. from allauth.socialaccount.models import SocialToken
  9. from django.views.decorators.http import require_POST
  10. from django.contrib import messages
  11. from django.template import Context, loader
  12. # Create your views here.
  13. @login_required
  14. def home(request):
  15. user_profile = request.user.profile
  16. user_playlists = user_profile.playlists.filter(is_in_db=True).order_by("-num_of_accesses")
  17. watching = user_profile.playlists.filter(Q(marked_as="watching") & Q(is_in_db=True)).order_by("-num_of_accesses")
  18. recently_accessed_playlists = user_profile.playlists.filter(is_in_db=True).order_by("-updated_at")[:6]
  19. recently_added_playlists = user_profile.playlists.filter(is_in_db=True).order_by("-created_at")[:6]
  20. #### FOR NEWLY JOINED USERS ######
  21. channel_found = True
  22. if user_profile.just_joined:
  23. if user_profile.import_in_progress:
  24. return render(request, "import_in_progress.html")
  25. else:
  26. if user_profile.access_token.strip() == "" or user_profile.refresh_token.strip() == "":
  27. user_social_token = SocialToken.objects.get(account__user=request.user)
  28. user_profile.access_token = user_social_token.token
  29. user_profile.refresh_token = user_social_token.token_secret
  30. user_profile.expires_at = user_social_token.expires_at
  31. request.user.save()
  32. user_profile.just_joined = False
  33. user_profile.save()
  34. return render(request, "home.html", {"import_successful": True})
  35. # if Playlist.objects.getUserYTChannelID(request.user) == -1: # user channel not found
  36. # channel_found = False
  37. # else:
  38. # Playlist.objects.initPlaylist(request.user, None) # get all playlists from user's YT channel
  39. # return render(request, "home.html", {"import_successful": True})
  40. ##################################
  41. if request.method == "POST":
  42. print(request.POST)
  43. if Playlist.objects.initPlaylist(request.user, request.POST['playlist-id'].strip()) == -1:
  44. print("No such playlist found.")
  45. playlist = []
  46. videos = []
  47. else:
  48. playlist = user_profile.playlists.get(playlist_id__exact=request.POST['playlist-id'].strip())
  49. videos = playlist.videos.all()
  50. else: # GET request
  51. videos = []
  52. playlist = []
  53. print("TESTING")
  54. return render(request, 'home.html', {"channel_found": channel_found,
  55. "playlist": playlist,
  56. "videos": videos,
  57. "user_playlists": user_playlists,
  58. "watching": watching,
  59. "recently_accessed_playlists": recently_accessed_playlists,
  60. "recently_added_playlists": recently_added_playlists})
  61. @login_required
  62. def view_video(request, playlist_id, video_id):
  63. video = request.user.profile.playlists.get(playlist_id=playlist_id).videos.get(video_id=video_id)
  64. print(video.name)
  65. return HttpResponse(loader.get_template("intercooler/video_details.html").render({"video": video}))
  66. @login_required
  67. def video_notes(request, playlist_id, video_id):
  68. video = request.user.profile.playlists.get(playlist_id=playlist_id).videos.get(video_id=video_id)
  69. if request.method == "POST":
  70. if 'video-notes-text-area' in request.POST:
  71. video.user_notes = request.POST['video-notes-text-area']
  72. video.save()
  73. return HttpResponse(loader.get_template("intercooler/messages.html").render(
  74. {"message_type": "success", "message_content": "Saved!"}))
  75. else:
  76. print("GET VIDEO NOTES")
  77. return HttpResponse(loader.get_template("intercooler/video_notes.html").render({"video": video,
  78. "playlist_id": playlist_id}))
  79. @login_required
  80. def view_playlist(request, playlist_id):
  81. user_profile = request.user.profile
  82. # specific playlist requested
  83. if user_profile.playlists.filter(Q(playlist_id=playlist_id) & Q(is_in_db=True)).count() != 0:
  84. playlist = user_profile.playlists.get(playlist_id__exact=playlist_id)
  85. playlist.num_of_accesses += 1
  86. playlist.save()
  87. else:
  88. messages.error(request, "No such playlist found!")
  89. return redirect('home')
  90. videos = playlist.videos.order_by("video_position")
  91. return render(request, 'view_playlist.html', {"playlist": playlist,
  92. "videos": videos})
  93. @login_required
  94. def all_playlists(request, playlist_type):
  95. """
  96. Possible playlist types for marked_as attribute: (saved in database like this)
  97. "none", "watching", "plan-to-watch"
  98. """
  99. playlist_type = playlist_type.lower()
  100. if playlist_type == "" or playlist_type == "all":
  101. playlists = request.user.profile.playlists.all().filter(is_in_db=True)
  102. playlist_type_display = "All Playlists"
  103. elif playlist_type == "user-owned": # YT playlists owned by user
  104. playlists = request.user.profile.playlists.all().filter(Q(is_user_owned=True) & Q(is_in_db=True))
  105. playlist_type_display = "Your YouTube Playlists"
  106. elif playlist_type == "imported": # YT playlists (public) owned by others
  107. playlists = request.user.profile.playlists.all().filter(Q(is_user_owned=False) & Q(is_in_db=True))
  108. playlist_type_display = "Imported playlists"
  109. elif playlist_type == "favorites": # YT playlists (public) owned by others
  110. playlists = request.user.profile.playlists.all().filter(Q(is_favorite=True) & Q(is_in_db=True))
  111. playlist_type_display = "Favorites"
  112. elif playlist_type.lower() in ["watching", "plan-to-watch"]:
  113. playlists = request.user.profile.playlists.filter(Q(marked_as=playlist_type.lower()) & Q(is_in_db=True))
  114. playlist_type_display = playlist_type.lower().replace("-", " ")
  115. elif playlist_type.lower() == "home": # displays cards of all playlist types
  116. return render(request, 'playlists_home.html')
  117. else:
  118. return redirect('home')
  119. return render(request, 'all_playlists.html', {"playlists": playlists,
  120. "playlist_type": playlist_type,
  121. "playlist_type_display": playlist_type_display})
  122. @login_required
  123. def order_playlist_by(request, playlist_id, order_by):
  124. playlist = request.user.profile.playlists.get(Q(playlist_id=playlist_id) & Q(is_in_db=True))
  125. display_text = "Nothing in this playlist! Add something!" # what to display when requested order/filter has no videws
  126. if order_by == "all":
  127. videos = playlist.videos.order_by("video_position")
  128. elif order_by == "favorites":
  129. videos = playlist.videos.filter(is_favorite=True).order_by("video_position")
  130. display_text = "No favorites yet!"
  131. elif order_by == "popularity":
  132. videos = playlist.videos.order_by("-like_count")
  133. elif order_by == "date-published":
  134. videos = playlist.videos.order_by("-published_at")
  135. elif order_by == "views":
  136. videos = playlist.videos.order_by("-view_count")
  137. elif order_by == "has-cc":
  138. videos = playlist.videos.filter(has_cc=True).order_by("video_position")
  139. display_text = "No videos in this playlist have CC :("
  140. elif order_by == "duration":
  141. videos = playlist.videos.order_by("-duration_in_seconds")
  142. elif order_by == 'new-updates':
  143. videos = []
  144. display_text = "No new updates! Note that deleted videos will not show up here."
  145. if playlist.has_new_updates:
  146. recently_updated_videos = playlist.videos.filter(video_details_modified=True)
  147. for video in recently_updated_videos:
  148. if video.video_details_modified_at + datetime.timedelta(hours=12) < datetime.datetime.now(
  149. pytz.utc): # expired
  150. video.video_details_modified = False
  151. video.save()
  152. if recently_updated_videos.count() == 0:
  153. playlist.has_new_updates = False
  154. playlist.save()
  155. else:
  156. videos = recently_updated_videos.order_by("video_position")
  157. else:
  158. return redirect('home')
  159. return HttpResponse(loader.get_template("intercooler/videos.html").render({"playlist": playlist,
  160. "videos": videos,
  161. "display_text": display_text}))
  162. @login_required
  163. def order_playlists_by(request, playlist_type, order_by):
  164. if playlist_type == "" or playlist_type.lower() == "all":
  165. playlists = request.user.profile.playlists.all()
  166. playlist_type_display = "All Playlists"
  167. elif playlist_type.lower() == "favorites":
  168. playlists = request.user.profile.playlists.filter(Q(is_favorite=True) & Q(is_in_db=True))
  169. playlist_type_display = "Favorites"
  170. elif playlist_type.lower() in ["watching", "plan-to-watch"]:
  171. playlists = request.user.profile.playlists.filter(Q(marked_as=playlist_type.lower()) & Q(is_in_db=True))
  172. playlist_type_display = "Watching"
  173. else:
  174. return redirect('home')
  175. if order_by == 'recently-accessed':
  176. playlists = playlists.order_by("-updated_at")
  177. elif order_by == 'playlist-duration-in-seconds':
  178. playlists = playlists.order_by("-playlist_duration_in_seconds")
  179. elif order_by == 'video-count':
  180. playlists = playlists.order_by("-video_count")
  181. return HttpResponse(loader.get_template("intercooler/playlists.html")
  182. .render({"playlists": playlists,
  183. "playlist_type_display": playlist_type_display,
  184. "playlist_type": playlist_type}))
  185. @login_required
  186. def mark_playlist_as(request, playlist_id, mark_as):
  187. playlist = request.user.profile.playlists.get(playlist_id=playlist_id)
  188. marked_as_response = ""
  189. if mark_as in ["watching", "on-hold", "plan-to-watch"]:
  190. playlist.marked_as = mark_as
  191. playlist.save()
  192. marked_as_response = f'<span class="badge bg-success text-white" >{mark_as.replace("-", " ")}</span>'
  193. elif mark_as == "none":
  194. playlist.marked_as = mark_as
  195. playlist.save()
  196. elif mark_as == "favorite":
  197. if playlist.is_favorite:
  198. playlist.is_favorite = False
  199. playlist.save()
  200. return HttpResponse('<i class="far fa-star"></i>')
  201. else:
  202. playlist.is_favorite = True
  203. playlist.save()
  204. return HttpResponse('<i class="fas fa-star"></i>')
  205. else:
  206. return render('home')
  207. return HttpResponse(marked_as_response)
  208. @login_required
  209. def playlists_home(request):
  210. return render(request, 'playlists_home.html')
  211. @login_required
  212. @require_POST
  213. def delete_videos(request, playlist_id, command):
  214. video_ids = request.POST.getlist("video-id", default=[])
  215. if command == "confirm":
  216. print(video_ids)
  217. num_vids = len(video_ids)
  218. extra_text = " "
  219. if num_vids == 0:
  220. return HttpResponse("<h5>Select some videos first!</h5>")
  221. elif num_vids == request.user.profile.playlists.get(playlist_id=playlist_id).videos.all().count():
  222. delete_text = "ALL VIDEOS"
  223. extra_text = " This will not delete the playlist itself, will only make the playlist empty. "
  224. else:
  225. delete_text = f"{num_vids} videos"
  226. return HttpResponse(
  227. f"<h5>Are you sure you want to delete {delete_text} from your YouTube playlist?{extra_text}This cannot be undone.</h5>")
  228. elif command == "confirmed":
  229. return HttpResponse(
  230. f'<div class="spinner-border text-light" role="status" hx-post="/from/{playlist_id}/delete-videos/start" hx-trigger="load" hx-swap="outerHTML"></div>')
  231. elif command == "start":
  232. for i in range(1000):
  233. pass
  234. return HttpResponse('DONE!')
  235. print(len(video_ids), request.POST)
  236. return HttpResponse("Worked!")
  237. @login_required
  238. @require_POST
  239. def search_playlists(request, playlist_type):
  240. print(request.POST) # prints <QueryDict: {'search': ['aa']}>
  241. search_query = request.POST["search"]
  242. if playlist_type == "all":
  243. try:
  244. playlists = request.user.profile.playlists.all().filter(Q(name__startswith=search_query) & Q(is_in_db=True))
  245. except:
  246. playlists = request.user.profile.playlists.all()
  247. playlist_type_display = "All Playlists"
  248. elif playlist_type == "user-owned": # YT playlists owned by user
  249. try:
  250. playlists = request.user.profile.playlists.filter(Q(name__startswith=search_query) & Q(is_user_owned=True) & Q(is_in_db=True))
  251. except:
  252. playlists = request.user.profile.playlists.filter(Q(is_user_owned=True) & Q(is_in_db=True))
  253. playlist_type_display = "Your YouTube Playlists"
  254. elif playlist_type == "imported": # YT playlists (public) owned by others
  255. try:
  256. playlists = request.user.profile.playlists.filter(Q(name__startswith=search_query) & Q(is_user_owned=False) & Q(is_in_db=True))
  257. except:
  258. playlists = request.user.profile.playlists.filter(Q(is_user_owned=True) & Q(is_in_db=True))
  259. playlist_type_display = "Imported Playlists"
  260. elif playlist_type == "favorites": # YT playlists (public) owned by others
  261. try:
  262. playlists = request.user.profile.playlists.filter(Q(name__startswith=search_query) & Q(is_favorite=True) & Q(is_in_db=True))
  263. except:
  264. playlists = request.user.profile.playlists.filter(Q(is_favorite=True) & Q(is_in_db=True))
  265. playlist_type_display = "Your Favorites"
  266. elif playlist_type in ["watching", "plan-to-watch"]:
  267. try:
  268. playlists = request.user.profile.playlists.filter(
  269. Q(name__startswith=search_query) & Q(marked_as=playlist_type) & Q(is_in_db=True))
  270. except:
  271. playlists = request.user.profile.playlists.all().filter(Q(marked_as=playlist_type) & Q(is_in_db=True))
  272. playlist_type_display = playlist_type.replace("-", " ")
  273. return HttpResponse(loader.get_template("intercooler/playlists.html")
  274. .render({"playlists": playlists,
  275. "playlist_type_display": playlist_type_display,
  276. "playlist_type": playlist_type,
  277. "search_query": search_query}))
  278. #### MANAGE VIDEOS #####
  279. def mark_video_favortie(request, playlist_id, video_id):
  280. video = request.user.profile.playlists.get(playlist_id=playlist_id).videos.get(video_id=video_id)
  281. if video.is_favorite:
  282. video.is_favorite = False
  283. video.save()
  284. return HttpResponse('<i class="far fa-heart"></i>')
  285. else:
  286. video.is_favorite = True
  287. video.save()
  288. return HttpResponse('<i class="fas fa-heart"></i>')
  289. ###########
  290. @login_required
  291. @require_POST
  292. def search_UnTube(request):
  293. print(request.POST)
  294. search_query = request.POST["search"]
  295. all_playlists = request.user.profile.playlists.filter(is_in_db=True)
  296. videos = []
  297. starts_with = False
  298. contains = False
  299. if request.POST['search-settings'] == 'starts-with':
  300. playlists = request.user.profile.playlists.filter(Q(name__startswith=search_query) & Q(is_in_db=True)) if search_query != "" else []
  301. if search_query != "":
  302. for playlist in all_playlists:
  303. pl_videos = playlist.videos.filter(name__startswith=search_query)
  304. if pl_videos.count() != 0:
  305. for v in pl_videos.all():
  306. videos.append(v)
  307. starts_with = True
  308. else:
  309. playlists = request.user.profile.playlists.filter(Q(name__contains=search_query) & Q(is_in_db=True)) if search_query != "" else []
  310. if search_query != "":
  311. for playlist in all_playlists:
  312. pl_videos = playlist.videos.filter(Q(name__contains=search_query) & Q(is_in_db=True))
  313. if pl_videos.count() != 0:
  314. for v in pl_videos.all():
  315. videos.append(v)
  316. contains = True
  317. return HttpResponse(loader.get_template("intercooler/search_untube.html")
  318. .render({"playlists": playlists,
  319. "videos": videos,
  320. "videos_count": len(videos),
  321. "search_query": search_query,
  322. "starts_with": starts_with,
  323. "contains": contains}))
  324. @login_required
  325. def manage_playlists(request):
  326. return render(request, "manage_playlists.html")
  327. @login_required
  328. def manage_view_page(request, page):
  329. if page == "import":
  330. return HttpResponse(loader.get_template("intercooler/manage_playlists_import.html")
  331. .render(
  332. {"manage_playlists_import_textarea": request.user.profile.manage_playlists_import_textarea}))
  333. elif page == "create":
  334. return HttpResponse(loader.get_template("intercooler/manage_playlists_create.html")
  335. .render(
  336. {}))
  337. else:
  338. return redirect('home')
  339. @login_required
  340. @require_POST
  341. def manage_save(request, what):
  342. if what == "manage_playlists_import_textarea":
  343. request.user.profile.manage_playlists_import_textarea = request.POST["import-playlist-textarea"]
  344. request.user.save()
  345. return HttpResponse("")
  346. @login_required
  347. @require_POST
  348. def manage_import_playlists(request):
  349. playlist_links = request.POST["import-playlist-textarea"].replace(",", "").split("\n")
  350. num_playlists_already_in_db = 0
  351. num_playlists_initialized_in_db = 0
  352. num_playlists_not_found = 0
  353. new_playlists = []
  354. old_playlists = []
  355. not_found_playlists = []
  356. done = []
  357. for playlist_link in playlist_links:
  358. if playlist_link.strip() != "" and playlist_link.strip() not in done:
  359. pl_id = Playlist.objects.getPlaylistId(playlist_link.strip())
  360. if pl_id is None:
  361. num_playlists_not_found += 1
  362. continue
  363. status = Playlist.objects.initPlaylist(request.user, pl_id)
  364. if status == -1 or status == -2:
  365. print("\nNo such playlist found:", pl_id)
  366. num_playlists_not_found += 1
  367. not_found_playlists.append(playlist_link)
  368. elif status == -3:
  369. num_playlists_already_in_db += 1
  370. playlist = request.user.profile.playlists.get(playlist_id__exact=pl_id)
  371. old_playlists.append(playlist)
  372. else:
  373. print(status)
  374. playlist = request.user.profile.playlists.get(playlist_id__exact=pl_id)
  375. new_playlists.append(playlist)
  376. num_playlists_initialized_in_db += 1
  377. done.append(playlist_link.strip())
  378. request.user.profile.manage_playlists_import_textarea = ""
  379. request.user.save()
  380. return HttpResponse(loader.get_template("intercooler/manage_playlists_import_results.html")
  381. .render(
  382. {"new_playlists": new_playlists,
  383. "old_playlists": old_playlists,
  384. "not_found_playlists": not_found_playlists,
  385. "num_playlists_already_in_db": num_playlists_already_in_db,
  386. "num_playlists_initialized_in_db": num_playlists_initialized_in_db,
  387. "num_playlists_not_found": num_playlists_not_found
  388. }))
  389. @login_required
  390. @require_POST
  391. def manage_create_playlist(request):
  392. print(request.POST)
  393. return HttpResponse("")
  394. @login_required
  395. def update_playlist(request, playlist_id, type):
  396. playlist = request.user.profile.playlists.get(playlist_id=playlist_id)
  397. if type == "checkforupdates":
  398. print("Checking if playlist changed...")
  399. result = Playlist.objects.checkIfPlaylistChangedOnYT(request.user, playlist_id)
  400. if result[0] == 1: # full scan was done (full scan is done for a playlist if a week has passed)
  401. deleted_videos, unavailable_videos, added_videos = result[1:]
  402. print("CHANGES", deleted_videos, unavailable_videos, added_videos)
  403. playlist_changed_text = ["The following modifications happened to this playlist on YouTube:"]
  404. if deleted_videos != 0 or unavailable_videos != 0 or added_videos != 0:
  405. if added_videos > 0:
  406. playlist_changed_text.append(f"{added_videos} new video(s) were added")
  407. if deleted_videos > 0:
  408. playlist_changed_text.append(f"{deleted_videos} video(s) were deleted")
  409. if unavailable_videos > 0:
  410. playlist_changed_text.append(f"{unavailable_videos} video(s) went private/unavailable")
  411. playlist.playlist_changed_text = "\n".join(playlist_changed_text)
  412. playlist.has_playlist_changed = True
  413. playlist.save()
  414. elif result[0] == -1: # playlist changed
  415. print("!!!Playlist changed")
  416. current_playlist_vid_count = playlist.video_count
  417. new_playlist_vid_count = result[1]
  418. print(current_playlist_vid_count)
  419. print(new_playlist_vid_count)
  420. if current_playlist_vid_count > new_playlist_vid_count:
  421. playlist.playlist_changed_text = f"Looks like {current_playlist_vid_count - new_playlist_vid_count} video(s) were deleted from this playlist on YouTube!"
  422. else:
  423. playlist.playlist_changed_text = f"Looks like {new_playlist_vid_count - current_playlist_vid_count} video(s) were added to this playlist on YouTube!"
  424. playlist.has_playlist_changed = True
  425. playlist.save()
  426. print(playlist.playlist_changed_text)
  427. else: # no updates found
  428. return HttpResponse("""
  429. <div class="alert alert-success alert-dismissible fade show visually-hidden" role="alert">
  430. No new updates!
  431. </div>
  432. """)
  433. return HttpResponse(f"""
  434. <div hx-get="/playlist/{playlist_id}/update/auto" hx-trigger="load" hx-target="#view_playlist">
  435. <div class="alert alert-success alert-dismissible fade show" role="alert">
  436. {playlist.playlist_changed_text}
  437. <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
  438. </div>
  439. <div class="d-flex justify-content-center mt-4 mb-3" id="loading-sign">
  440. <img src="/static/svg-loaders/circles.svg" width="40" height="40">
  441. <h5 class="mt-2 ms-2">Updating playlist '{playlist.name}', please wait!</h5>
  442. </div>
  443. </div>
  444. """)
  445. if type == "manual":
  446. print("MANUAL")
  447. return HttpResponse(
  448. f"""<div hx-get="/playlist/{playlist_id}/update/auto" hx-trigger="load" hx-swap="outerHTML">
  449. <div class="d-flex justify-content-center mt-4 mb-3" id="loading-sign">
  450. <img src="/static/svg-loaders/circles.svg" width="40" height="40">
  451. <h5 class="mt-2 ms-2">Refreshing playlist '{playlist.name}', please wait!</h5>
  452. </div>
  453. </div>""")
  454. print("Attempting to update playlist")
  455. status, deleted_video_ids, unavailable_videos, added_videos = Playlist.objects.updatePlaylist(request.user, playlist_id)
  456. playlist = request.user.profile.playlists.get(playlist_id=playlist_id)
  457. if status == -1:
  458. playlist_name = playlist.name
  459. playlist.delete()
  460. return HttpResponse(
  461. f"""
  462. <div class="d-flex justify-content-center mt-4 mb-3" id="loading-sign">
  463. <h5 class="mt-2 ms-2">Looks like the playlist '{playlist_name}' was deleted on YouTube. It has been removed from UnTube as well.</h5>
  464. </div>
  465. """)
  466. print("Updated playlist")
  467. playlist_changed_text = []
  468. if len(added_videos) != 0:
  469. playlist_changed_text.append(f"{len(added_videos)} added")
  470. for video in added_videos[:3]:
  471. playlist_changed_text.append(f"--> {video.name}")
  472. if len(added_videos) > 3:
  473. playlist_changed_text.append(f"+ {len(added_videos) - 3} more")
  474. if len(unavailable_videos) != 0:
  475. if len(playlist_changed_text) == 0:
  476. playlist_changed_text.append(f"{len(unavailable_videos)} went unavailable")
  477. else:
  478. playlist_changed_text.append(f"\n{len(unavailable_videos)} went unavailable")
  479. for video in unavailable_videos:
  480. playlist_changed_text.append(f"--> {video.name}")
  481. if len(deleted_video_ids) != 0:
  482. if len(playlist_changed_text) == 0:
  483. playlist_changed_text.append(f"{len(deleted_video_ids)} deleted")
  484. else:
  485. playlist_changed_text.append(f"\n{len(deleted_video_ids)} deleted")
  486. for video_id in deleted_video_ids:
  487. video = playlist.videos.get(video_id=video_id)
  488. playlist_changed_text.append(f"--> {video.name}")
  489. video.delete()
  490. if len(playlist_changed_text) == 0:
  491. playlist_changed_text = ["Successfully refreshed playlist! No new changes found!"]
  492. return HttpResponse(loader.get_template("intercooler/updated_playlist.html")
  493. .render(
  494. {"playlist_changed_text": "\n".join(playlist_changed_text),
  495. "playlist": playlist,
  496. "videos": playlist.videos.order_by("video_position")}))