123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836 |
- import datetime
- import pytz
- from django.db.models import Q
- from django.http import HttpResponse, HttpResponseRedirect
- from django.shortcuts import render, redirect, get_object_or_404
- from apps.main.models import Playlist, Tag
- from django.contrib.auth.decorators import login_required # redirects user to settings.LOGIN_URL
- from allauth.socialaccount.models import SocialToken
- from django.views.decorators.http import require_POST
- from django.contrib import messages
- from django.template import Context, loader
- # Create your views here.
- @login_required
- def home(request):
- user_profile = request.user.profile
- user_playlists = user_profile.playlists.filter(Q(is_in_db=True) & Q(num_of_accesses__gt=0)).order_by(
- "-num_of_accesses")
- watching = user_profile.playlists.filter(Q(marked_as="watching") & Q(is_in_db=True)).order_by("-num_of_accesses")
- recently_accessed_playlists = user_profile.playlists.filter(is_in_db=True).order_by("-updated_at")[:6]
- recently_added_playlists = user_profile.playlists.filter(is_in_db=True).order_by("-created_at")[:6]
- #### FOR NEWLY JOINED USERS ######
- channel_found = True
- if user_profile.show_import_page:
- """
- Logic:
- show_import_page is True by default. When a user logs in for the first time (infact anytime), google
- redirects them to 'home' url. Since, show_import_page is True by default, the user is then redirected
- from 'home' to 'import_in_progress' url
-
- show_import_page is only set false in the import_in_progress.html page, i.e when user cancels YT import
- """
- # user_profile.show_import_page = False
- if user_profile.access_token.strip() == "" or user_profile.refresh_token.strip() == "":
- user_social_token = SocialToken.objects.get(account__user=request.user)
- user_profile.access_token = user_social_token.token
- user_profile.refresh_token = user_social_token.token_secret
- user_profile.expires_at = user_social_token.expires_at
- request.user.save()
- if user_profile.imported_yt_playlists:
- user_profile.show_import_page = False # after user imports all their YT playlists no need to show_import_page again
- user_profile.save(update_fields=['show_import_page'])
- return render(request, "home.html", {"import_successful": True})
- return render(request, "import_in_progress.html")
- # if Playlist.objects.getUserYTChannelID(request.user) == -1: # user channel not found
- # channel_found = False
- # else:
- # Playlist.objects.initPlaylist(request.user, None) # get all playlists from user's YT channel
- # return render(request, "home.html", {"import_successful": True})
- ##################################
- if request.method == "POST":
- print(request.POST)
- if Playlist.objects.initPlaylist(request.user, request.POST['playlist-id'].strip()) == -1:
- print("No such playlist found.")
- playlist = []
- videos = []
- else:
- playlist = user_profile.playlists.get(playlist_id__exact=request.POST['playlist-id'].strip())
- videos = playlist.videos.all()
- else: # GET request
- videos = []
- playlist = []
- print("TESTING")
- return render(request, 'home.html', {"channel_found": channel_found,
- "playlist": playlist,
- "videos": videos,
- "user_playlists": user_playlists,
- "watching": watching,
- "recently_accessed_playlists": recently_accessed_playlists,
- "recently_added_playlists": recently_added_playlists})
- @login_required
- def view_video(request, playlist_id, video_id):
- video = request.user.profile.playlists.get(playlist_id=playlist_id).videos.get(video_id=video_id)
- print(video.name)
- return HttpResponse(loader.get_template("intercooler/video_details.html").render({"video": video}))
- @login_required
- def video_notes(request, playlist_id, video_id):
- video = request.user.profile.playlists.get(playlist_id=playlist_id).videos.get(video_id=video_id)
- if request.method == "POST":
- if 'video-notes-text-area' in request.POST:
- video.user_notes = request.POST['video-notes-text-area']
- video.save()
- return HttpResponse(loader.get_template("intercooler/messages.html").render(
- {"message_type": "success", "message_content": "Saved!"}))
- else:
- print("GET VIDEO NOTES")
- return HttpResponse(loader.get_template("intercooler/video_notes.html").render({"video": video,
- "playlist_id": playlist_id}))
- @login_required
- def view_playlist(request, playlist_id):
- user_profile = request.user.profile
- user_owned_playlists = user_profile.playlists.filter(Q(is_user_owned=True) & Q(is_in_db=True))
- # specific playlist requested
- if user_profile.playlists.filter(Q(playlist_id=playlist_id) & Q(is_in_db=True)).count() != 0:
- playlist = user_profile.playlists.get(playlist_id__exact=playlist_id)
- playlist.num_of_accesses += 1
- playlist.save()
- else:
- messages.error(request, "No such playlist found!")
- return redirect('home')
- if playlist.has_new_updates:
- recently_updated_videos = playlist.videos.filter(video_details_modified=True)
- for video in recently_updated_videos:
- if video.video_details_modified_at + datetime.timedelta(hours=12) < datetime.datetime.now(
- pytz.utc): # expired
- video.video_details_modified = False
- video.save()
- if recently_updated_videos.count() == 0:
- playlist.has_new_updates = False
- playlist.save()
- videos = playlist.videos.order_by("video_position")
- user_created_tags = Tag.objects.filter(created_by=request.user)
- playlist_tags = playlist.tags.all()
- unused_tags = user_created_tags.difference(playlist_tags)
- return render(request, 'view_playlist.html', {"playlist": playlist,
- "playlist_tags": playlist_tags,
- "unused_tags": unused_tags,
- "videos": videos,
- "user_owned_playlists": user_owned_playlists})
- @login_required
- def tagged_playlists(request, tag):
- tag = get_object_or_404(Tag, created_by=request.user, name=tag)
- playlists = tag.playlists.all()
- return render(request, 'all_playlists_with_tag.html', {"playlists": playlists, "tag": tag})
- @login_required
- def all_playlists(request, playlist_type):
- """
- Possible playlist types for marked_as attribute: (saved in database like this)
- "none", "watching", "plan-to-watch"
- """
- playlist_type = playlist_type.lower()
- if playlist_type == "" or playlist_type == "all":
- playlists = request.user.profile.playlists.all().filter(is_in_db=True)
- playlist_type_display = "All Playlists"
- elif playlist_type == "user-owned": # YT playlists owned by user
- playlists = request.user.profile.playlists.all().filter(Q(is_user_owned=True) & Q(is_in_db=True))
- playlist_type_display = "Your YouTube Playlists"
- elif playlist_type == "imported": # YT playlists (public) owned by others
- playlists = request.user.profile.playlists.all().filter(Q(is_user_owned=False) & Q(is_in_db=True))
- playlist_type_display = "Imported playlists"
- elif playlist_type == "favorites": # YT playlists (public) owned by others
- playlists = request.user.profile.playlists.all().filter(Q(is_favorite=True) & Q(is_in_db=True))
- playlist_type_display = "Favorites"
- elif playlist_type.lower() in ["watching", "plan-to-watch"]:
- playlists = request.user.profile.playlists.filter(Q(marked_as=playlist_type.lower()) & Q(is_in_db=True))
- playlist_type_display = playlist_type.lower().replace("-", " ")
- elif playlist_type.lower() == "home": # displays cards of all playlist types
- return render(request, 'playlists_home.html')
- else:
- return redirect('home')
- return render(request, 'all_playlists.html', {"playlists": playlists,
- "playlist_type": playlist_type,
- "playlist_type_display": playlist_type_display})
- @login_required
- def order_playlist_by(request, playlist_id, order_by):
- playlist = request.user.profile.playlists.get(Q(playlist_id=playlist_id) & Q(is_in_db=True))
- display_text = "Nothing in this playlist! Add something!" # what to display when requested order/filter has no videws
- videos_details = ""
- if order_by == "all":
- videos = playlist.videos.order_by("video_position")
- elif order_by == "favorites":
- videos = playlist.videos.filter(is_favorite=True).order_by("video_position")
- videos_details = "Sorted by Favorites"
- display_text = "No favorites yet!"
- elif order_by == "popularity":
- videos_details = "Sorted by Popularity"
- videos = playlist.videos.order_by("-like_count")
- elif order_by == "date-published":
- videos_details = "Sorted by Date Published"
- videos = playlist.videos.order_by("-published_at")
- elif order_by == "views":
- videos_details = "Sorted by View Count"
- videos = playlist.videos.order_by("-view_count")
- elif order_by == "has-cc":
- videos_details = "Filtered by Has CC"
- videos = playlist.videos.filter(has_cc=True).order_by("video_position")
- display_text = "No videos in this playlist have CC :("
- elif order_by == "duration":
- videos_details = "Sorted by Video Duration"
- videos = playlist.videos.order_by("-duration_in_seconds")
- elif order_by == 'new-updates':
- videos = []
- videos_details = "Sorted by New Updates"
- display_text = "No new updates! Note that deleted videos will not show up here."
- if playlist.has_new_updates:
- recently_updated_videos = playlist.videos.filter(video_details_modified=True)
- for video in recently_updated_videos:
- if video.video_details_modified_at + datetime.timedelta(hours=12) < datetime.datetime.now(
- pytz.utc): # expired
- video.video_details_modified = False
- video.save()
- if recently_updated_videos.count() == 0:
- playlist.has_new_updates = False
- playlist.save()
- else:
- videos = recently_updated_videos.order_by("video_position")
- elif order_by == 'unavailable-videos':
- videos = playlist.videos.filter(Q(is_unavailable_on_yt=True) & Q(was_deleted_on_yt=True))
- videos_details = "Sorted by Unavailable Videos"
- display_text = "None of the videos in this playlist have gone unavailable... yet."
- else:
- return redirect('home')
- return HttpResponse(loader.get_template("intercooler/videos.html").render({"playlist": playlist,
- "videos": videos,
- "videos_details": videos_details,
- "display_text": display_text}))
- @login_required
- def order_playlists_by(request, playlist_type, order_by):
- if playlist_type == "" or playlist_type.lower() == "all":
- playlists = request.user.profile.playlists.all()
- playlist_type_display = "All Playlists"
- elif playlist_type.lower() == "favorites":
- playlists = request.user.profile.playlists.filter(Q(is_favorite=True) & Q(is_in_db=True))
- playlist_type_display = "Favorites"
- elif playlist_type.lower() in ["watching", "plan-to-watch"]:
- playlists = request.user.profile.playlists.filter(Q(marked_as=playlist_type.lower()) & Q(is_in_db=True))
- playlist_type_display = "Watching"
- else:
- return redirect('home')
- if order_by == 'recently-accessed':
- playlists = playlists.order_by("-updated_at")
- elif order_by == 'playlist-duration-in-seconds':
- playlists = playlists.order_by("-playlist_duration_in_seconds")
- elif order_by == 'video-count':
- playlists = playlists.order_by("-video_count")
- return HttpResponse(loader.get_template("intercooler/playlists.html")
- .render({"playlists": playlists,
- "playlist_type_display": playlist_type_display,
- "playlist_type": playlist_type}))
- @login_required
- def mark_playlist_as(request, playlist_id, mark_as):
- playlist = request.user.profile.playlists.get(playlist_id=playlist_id)
- marked_as_response = ""
- if mark_as in ["watching", "on-hold", "plan-to-watch"]:
- playlist.marked_as = mark_as
- playlist.save()
- icon = ""
- if mark_as == "watching":
- icon = '<i class="fas fa-fire-alt me-2"></i>'
- elif mark_as == "plan-to-watch":
- icon = '<i class="fas fa-flag me-2"></i>'
- marked_as_response = f'<span class="badge bg-success text-white" >{icon}{mark_as}</span> <meta http-equiv="refresh" content="0" />'
- elif mark_as == "none":
- playlist.marked_as = mark_as
- playlist.save()
- elif mark_as == "favorite":
- if playlist.is_favorite:
- playlist.is_favorite = False
- playlist.save()
- return HttpResponse('<i class="far fa-star"></i>')
- else:
- playlist.is_favorite = True
- playlist.save()
- return HttpResponse('<i class="fas fa-star"></i>')
- else:
- return render('home')
- return HttpResponse(marked_as_response)
- @login_required
- def playlists_home(request):
- return render(request, 'playlists_home.html')
- @login_required
- @require_POST
- def delete_videos(request, playlist_id, command):
- video_ids = request.POST.getlist("video-id", default=[])
- if command == "confirm":
- print(video_ids)
- num_vids = len(video_ids)
- extra_text = " "
- if num_vids == 0:
- return HttpResponse("<h5>Select some videos first!</h5>")
- elif num_vids == request.user.profile.playlists.get(playlist_id=playlist_id).videos.all().count():
- delete_text = "ALL VIDEOS"
- extra_text = " This will not delete the playlist itself, will only make the playlist empty. "
- else:
- delete_text = f"{num_vids} videos"
- return HttpResponse(
- f"<h5>Are you sure you want to delete {delete_text} from your YouTube playlist?{extra_text}This cannot be undone.</h5>")
- elif command == "confirmed":
- print(video_ids)
- return HttpResponse(
- f'<div class="spinner-border text-light" role="status" hx-post="/from/{playlist_id}/delete-videos/start" hx-trigger="load" hx-swap="outerHTML"></div>')
- elif command == "start":
- Playlist.objects.deletePlaylistItems(request.user, playlist_id, video_ids)
- # playlist = request.user.profile.playlists.get(playlist_id=playlist_id)
- # playlist.has_playlist_changed = True
- # playlist.save(update_fields=['has_playlist_changed'])
- return HttpResponse(f"""
- <div hx-get="/playlist/{playlist_id}/update/checkforupdates" hx-trigger="load delay:1s" hx-target="#checkforupdates" class="sticky-top" style="top: 0.5rem;">
- Done! Playlist on UnTube will update in soon...
- </div>
- """)
- @login_required
- @require_POST
- def search_tagged_playlists(request, tag):
- tag = get_object_or_404(Tag, created_by=request.user, name=tag)
- playlists = tag.playlists.all()
- return HttpResponse("yay")
- @login_required
- @require_POST
- def search_playlists(request, playlist_type):
- # print(request.POST) # prints <QueryDict: {'search': ['aa']}>
- search_query = request.POST["search"]
- if playlist_type == "all":
- try:
- playlists = request.user.profile.playlists.all().filter(Q(name__startswith=search_query) & Q(is_in_db=True))
- except:
- playlists = request.user.profile.playlists.all()
- playlist_type_display = "All Playlists"
- elif playlist_type == "user-owned": # YT playlists owned by user
- try:
- playlists = request.user.profile.playlists.filter(
- Q(name__startswith=search_query) & Q(is_user_owned=True) & Q(is_in_db=True))
- except:
- playlists = request.user.profile.playlists.filter(Q(is_user_owned=True) & Q(is_in_db=True))
- playlist_type_display = "Your YouTube Playlists"
- elif playlist_type == "imported": # YT playlists (public) owned by others
- try:
- playlists = request.user.profile.playlists.filter(
- Q(name__startswith=search_query) & Q(is_user_owned=False) & Q(is_in_db=True))
- except:
- playlists = request.user.profile.playlists.filter(Q(is_user_owned=True) & Q(is_in_db=True))
- playlist_type_display = "Imported Playlists"
- elif playlist_type == "favorites": # YT playlists (public) owned by others
- try:
- playlists = request.user.profile.playlists.filter(
- Q(name__startswith=search_query) & Q(is_favorite=True) & Q(is_in_db=True))
- except:
- playlists = request.user.profile.playlists.filter(Q(is_favorite=True) & Q(is_in_db=True))
- playlist_type_display = "Your Favorites"
- elif playlist_type in ["watching", "plan-to-watch"]:
- try:
- playlists = request.user.profile.playlists.filter(
- Q(name__startswith=search_query) & Q(marked_as=playlist_type) & Q(is_in_db=True))
- except:
- playlists = request.user.profile.playlists.all().filter(Q(marked_as=playlist_type) & Q(is_in_db=True))
- playlist_type_display = playlist_type.replace("-", " ")
- return HttpResponse(loader.get_template("intercooler/playlists.html")
- .render({"playlists": playlists,
- "playlist_type_display": playlist_type_display,
- "playlist_type": playlist_type,
- "search_query": search_query}))
- #### MANAGE VIDEOS #####
- def mark_video_favortie(request, playlist_id, video_id):
- video = request.user.profile.playlists.get(playlist_id=playlist_id).videos.get(video_id=video_id)
- if video.is_favorite:
- video.is_favorite = False
- video.save()
- return HttpResponse('<i class="far fa-heart"></i>')
- else:
- video.is_favorite = True
- video.save()
- return HttpResponse('<i class="fas fa-heart"></i>')
- ###########
- @login_required
- def search(request):
- if request.method == "GET":
- return render(request, 'search_untube_page.html')
- else:
- return render('home')
- @login_required
- @require_POST
- def search_UnTube(request):
- print(request.POST)
- search_query = request.POST["search"]
- all_playlists = request.user.profile.playlists.filter(is_in_db=True)
- if 'playlist-tags' in request.POST:
- tags = request.POST.getlist('playlist-tags')
- all_playlists = all_playlists.filter(tags__name__in=tags)
- videos = []
- if request.POST['search-settings'] == 'starts-with':
- playlists = all_playlists.filter(name__istartswith=search_query) if search_query != "" else all_playlists.none()
- if search_query != "":
- for playlist in all_playlists:
- pl_videos = playlist.videos.filter(name__istartswith=search_query)
- if pl_videos.count() != 0:
- for v in pl_videos.all():
- videos.append(v)
- else:
- playlists = all_playlists.filter(name__icontains=search_query) if search_query != "" else all_playlists.none()
- if search_query != "":
- for playlist in all_playlists:
- pl_videos = playlist.videos.filter(name__icontains=search_query)
- if pl_videos.count() != 0:
- for v in pl_videos.all():
- videos.append(v)
- return HttpResponse(loader.get_template("intercooler/search_untube_results.html")
- .render({"playlists": playlists,
- "videos": videos,
- "videos_count": len(videos),
- "search_query": True if search_query != "" else False,
- "all_playlists": all_playlists}))
- @login_required
- def manage_playlists(request):
- return render(request, "manage_playlists.html")
- @login_required
- def manage_view_page(request, page):
- if page == "import":
- return render(request, "manage_playlists_import.html",
- {"manage_playlists_import_textarea": request.user.profile.manage_playlists_import_textarea})
- elif page == "create":
- return render(request, "manage_playlists_create.html")
- else:
- return HttpResponse('Working on this!')
- @login_required
- @require_POST
- def manage_save(request, what):
- if what == "manage_playlists_import_textarea":
- request.user.profile.manage_playlists_import_textarea = request.POST["import-playlist-textarea"]
- request.user.save()
- return HttpResponse("")
- @login_required
- @require_POST
- def manage_import_playlists(request):
- playlist_links = request.POST["import-playlist-textarea"].replace(",", "").split("\n")
- num_playlists_already_in_db = 0
- num_playlists_initialized_in_db = 0
- num_playlists_not_found = 0
- new_playlists = []
- old_playlists = []
- not_found_playlists = []
- done = []
- for playlist_link in playlist_links:
- if playlist_link.strip() != "" and playlist_link.strip() not in done:
- pl_id = Playlist.objects.getPlaylistId(playlist_link.strip())
- if pl_id is None:
- num_playlists_not_found += 1
- continue
- status = Playlist.objects.initPlaylist(request.user, pl_id)
- if status == -1 or status == -2:
- print("\nNo such playlist found:", pl_id)
- num_playlists_not_found += 1
- not_found_playlists.append(playlist_link)
- elif status == -3:
- num_playlists_already_in_db += 1
- playlist = request.user.profile.playlists.get(playlist_id__exact=pl_id)
- old_playlists.append(playlist)
- else:
- print(status)
- playlist = request.user.profile.playlists.get(playlist_id__exact=pl_id)
- new_playlists.append(playlist)
- num_playlists_initialized_in_db += 1
- done.append(playlist_link.strip())
- request.user.profile.manage_playlists_import_textarea = ""
- request.user.save()
- return HttpResponse(loader.get_template("intercooler/manage_playlists_import_results.html")
- .render(
- {"new_playlists": new_playlists,
- "old_playlists": old_playlists,
- "not_found_playlists": not_found_playlists,
- "num_playlists_already_in_db": num_playlists_already_in_db,
- "num_playlists_initialized_in_db": num_playlists_initialized_in_db,
- "num_playlists_not_found": num_playlists_not_found
- }))
- @login_required
- @require_POST
- def manage_create_playlist(request):
- print(request.POST)
- return HttpResponse("")
- @login_required
- def load_more_videos(request, playlist_id, page):
- playlist = request.user.profile.playlists.get(playlist_id=playlist_id)
- videos = playlist.videos.order_by("video_position")[50 * page:]
- return HttpResponse(loader.get_template("intercooler/videos.html")
- .render(
- {
- "playlist": playlist,
- "videos": videos,
- "page": page + 1}))
- @login_required
- @require_POST
- def update_playlist_settings(request, playlist_id):
- message_type = "success"
- message_content = "Saved!"
- if "user_label" in request.POST:
- playlist = request.user.profile.playlists.get(playlist_id=playlist_id)
- playlist.user_label = request.POST["user_label"]
- playlist.save(update_fields=['user_label'])
- return HttpResponse(loader.get_template("intercooler/messages.html")
- .render(
- {"message_type": message_type,
- "message_content": message_content}))
- details = {
- "title": request.POST['playlistTitle'],
- "description": request.POST['playlistDesc'],
- "privacyStatus": True if request.POST['playlistPrivacy'] == "Private" else False
- }
- status = Playlist.objects.updatePlaylistDetails(request.user, playlist_id, details)
- if status == -1:
- message_type = "error"
- message_content = "Could not save :("
- return HttpResponse(loader.get_template("intercooler/messages.html")
- .render(
- {"message_type": message_type,
- "message_content": message_content}))
- @login_required
- def update_playlist(request, playlist_id, type):
- playlist = request.user.profile.playlists.get(playlist_id=playlist_id)
- if type == "checkforupdates":
- print("Checking if playlist changed...")
- result = Playlist.objects.checkIfPlaylistChangedOnYT(request.user, playlist_id)
- if result[0] == 1: # full scan was done (full scan is done for a playlist if a week has passed)
- deleted_videos, unavailable_videos, added_videos = result[1:]
- print("CHANGES", deleted_videos, unavailable_videos, added_videos)
- # playlist_changed_text = ["The following modifications happened to this playlist on YouTube:"]
- if deleted_videos != 0 or unavailable_videos != 0 or added_videos != 0:
- pass
- # if added_videos > 0:
- # playlist_changed_text.append(f"{added_videos} new video(s) were added")
- # if deleted_videos > 0:
- # playlist_changed_text.append(f"{deleted_videos} video(s) were deleted")
- # if unavailable_videos > 0:
- # playlist_changed_text.append(f"{unavailable_videos} video(s) went private/unavailable")
- # playlist.playlist_changed_text = "\n".join(playlist_changed_text)
- # playlist.has_playlist_changed = True
- # playlist.save()
- else: # no updates found
- return HttpResponse("""
- <div id="checkforupdates" class="sticky-top" style="top: 0.5em;">
- <div class="alert alert-success alert-dismissible fade show visually-hidden" role="alert">
- No new updates!
- </div>
- </div>
- """)
- elif result[0] == -1: # playlist changed
- print("!!!Playlist changed")
- # current_playlist_vid_count = playlist.video_count
- # new_playlist_vid_count = result[1]
- # print(current_playlist_vid_count)
- # print(new_playlist_vid_count)
- # playlist.has_playlist_changed = True
- # playlist.save()
- # print(playlist.playlist_changed_text)
- else: # no updates found
- return HttpResponse("""
- <div id="checkforupdates" class="sticky-top" style="top: 0.5em;">
- <div class="alert alert-success alert-dismissible fade show visually-hidden sticky-top" role="alert" style="top: 0.5em;">
- No new updates!
- </div>
- </div>
- """)
- return HttpResponse(f"""
- <div hx-get="/playlist/{playlist_id}/update/auto" hx-trigger="load" hx-target="this" class="sticky-top" style="top: 0.5em;">
-
- <div class="alert alert-success alert-dismissible fade show" role="alert">
- <div class="d-flex justify-content-center" id="loading-sign">
- <img src="/static/svg-loaders/circles.svg" width="40" height="40">
- <h5 class="mt-2 ms-2">Changes detected on YouTube, updating playlist '{playlist.name}'...</h5>
- </div>
- </div>
- </div>
- """)
- if type == "manual":
- print("MANUAL")
- return HttpResponse(
- f"""<div hx-get="/playlist/{playlist_id}/update/auto" hx-trigger="load" hx-swap="outerHTML">
- <div class="d-flex justify-content-center mt-4 mb-3" id="loading-sign">
- <img src="/static/svg-loaders/circles.svg" width="40" height="40">
- <h5 class="mt-2 ms-2">Refreshing playlist '{playlist.name}', please wait!</h5>
- </div>
- </div>""")
- print("Attempting to update playlist")
- status, deleted_video_ids, unavailable_videos, added_videos = Playlist.objects.updatePlaylist(request.user,
- playlist_id)
- playlist = request.user.profile.playlists.get(playlist_id=playlist_id)
- if status == -1:
- playlist_name = playlist.name
- playlist.delete()
- return HttpResponse(
- f"""
- <div class="d-flex justify-content-center mt-4 mb-3" id="loading-sign">
- <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>
- </div>
- """)
- print("Updated playlist")
- playlist_changed_text = []
- if len(added_videos) != 0:
- playlist_changed_text.append(f"{len(added_videos)} added")
- for video in added_videos:
- playlist_changed_text.append(f"--> {video.name}")
- # if len(added_videos) > 3:
- # playlist_changed_text.append(f"+ {len(added_videos) - 3} more")
- if len(unavailable_videos) != 0:
- if len(playlist_changed_text) == 0:
- playlist_changed_text.append(f"{len(unavailable_videos)} went unavailable")
- else:
- playlist_changed_text.append(f"\n{len(unavailable_videos)} went unavailable")
- for video in unavailable_videos:
- playlist_changed_text.append(f"--> {video.name}")
- if len(deleted_video_ids) != 0:
- if len(playlist_changed_text) == 0:
- playlist_changed_text.append(f"{len(deleted_video_ids)} deleted")
- else:
- playlist_changed_text.append(f"\n{len(deleted_video_ids)} deleted")
- for video_id in deleted_video_ids:
- video = playlist.videos.get(video_id=video_id)
- playlist_changed_text.append(f"--> {video.name}")
- video.delete()
- if len(playlist_changed_text) == 0:
- playlist_changed_text = ["Successfully refreshed playlist! No new changes found!"]
- # return HttpResponse
- return HttpResponse(loader.get_template("intercooler/playlist_updates.html")
- .render(
- {"playlist_changed_text": "\n".join(playlist_changed_text),
- "playlist_id": playlist_id}))
- @login_required
- def view_playlist_settings(request, playlist_id):
- playlist = request.user.profile.playlists.get(playlist_id=playlist_id)
- return render(request, 'view_playlist_settings.html', {"playlist": playlist})
- def get_playlist_tags(request, playlist_id):
- playlist = request.user.profile.playlists.get(playlist_id=playlist_id)
- playlist_tags = playlist.tags.all()
- return HttpResponse(loader.get_template("intercooler/playlist_tags.html")
- .render(
- {"playlist_id": playlist_id,
- "playlist_tags": playlist_tags}))
- def get_unused_playlist_tags(request, playlist_id):
- playlist = request.user.profile.playlists.get(playlist_id=playlist_id)
- user_created_tags = Tag.objects.filter(created_by=request.user)
- playlist_tags = playlist.tags.all()
- unused_tags = user_created_tags.difference(playlist_tags)
- return HttpResponse(loader.get_template("intercooler/playlist_tags_unused.html")
- .render(
- {"unused_tags": unused_tags}))
- @login_required
- @require_POST
- def create_playlist_tag(request, playlist_id):
- tag_name = request.POST["createTagField"]
- if tag_name.lower() == 'Pick from existing unused tags'.lower():
- return HttpResponse("Can't use that! Try again >_<")
- playlist = request.user.profile.playlists.get(playlist_id=playlist_id)
- user_created_tags = Tag.objects.filter(created_by=request.user)
- if user_created_tags.filter(name__iexact=tag_name).count() == 0: # no tag found, so create it
- tag = Tag(name=tag_name, created_by=request.user)
- tag.save()
- # add it to playlist
- playlist.tags.add(tag)
- else:
- return HttpResponse("""
- Already created. Try Again >w<
- """)
- # playlist_tags = playlist.tags.all()
- # unused_tags = user_created_tags.difference(playlist_tags)
- return HttpResponse(f"""
- Created and Added!
- <span class="visually-hidden" hx-get="/playlist/{playlist_id}/get-tags" hx-trigger="load" hx-target="#playlist-tags"></span>
- """)
- @login_required
- @require_POST
- def add_playlist_tag(request, playlist_id):
- tag_name = request.POST["playlistTag"]
- if tag_name == 'Pick from existing unused tags':
- return HttpResponse("Pick something! >w<")
- playlist = request.user.profile.playlists.get(playlist_id=playlist_id)
- playlist_tags = playlist.tags.all()
- if playlist_tags.filter(name__iexact=tag_name).count() == 0: # tag not on this playlist, so add it
- tag = Tag.objects.filter(Q(created_by=request.user) & Q(name__iexact=tag_name)).first()
- # add it to playlist
- playlist.tags.add(tag)
- else:
- return HttpResponse("Already Added >w<")
- return HttpResponse(f"""
- Added!
- <span class="visually-hidden" hx-get="/playlist/{playlist_id}/get-tags" hx-trigger="load" hx-target="#playlist-tags"></span>
- """)
- @login_required
- @require_POST
- def remove_playlist_tag(request, playlist_id, tag_name):
- playlist = request.user.profile.playlists.get(playlist_id=playlist_id)
- playlist_tags = playlist.tags.all()
- if playlist_tags.filter(name__iexact=tag_name).count() != 0: # tag on this playlist, remove it it
- tag = Tag.objects.filter(Q(created_by=request.user) & Q(name__iexact=tag_name)).first()
- print("Removed tag", tag_name)
- # remove it from the playlist
- playlist.tags.remove(tag)
- else:
- return HttpResponse("Whoops >w<")
- return HttpResponse("")
|