Browse Source

clean up get YT channel ID logic

Mohammed Khan 1 năm trước cách đây
mục cha
commit
f46fe5f663
3 tập tin đã thay đổi với 18 bổ sung13 xóa
  1. 7 3
      backend/main/models.py
  2. 4 3
      backend/main/views.py
  3. 7 7
      backend/users/views.py

+ 7 - 3
backend/main/models.py

@@ -44,7 +44,11 @@ class PlaylistManager(models.Manager):
     # Used to check if the user has a vaild YouTube channel
     # Will return -1 if user does not have a YouTube channel
     def getUserYTChannelID(self, user):
-        credentials = user.profile.get_credentials()
+        user_profile = user.profile
+        if user_profile.yt_channel_id != "":
+            return 0
+
+        credentials = user_profile.get_credentials()
 
         with build('youtube', 'v3', credentials=credentials) as youtube:
             pl_request = youtube.channels().list(
@@ -60,8 +64,8 @@ class PlaylistManager(models.Manager):
                 print("Looks like do not have a channel on youtube. Create one to import all of your playlists. Retry?")
                 return -1
             else:
-                user.profile.yt_channel_id = pl_response['items'][0]['id']
-                user.save()
+                user_profile.yt_channel_id = pl_response['items'][0]['id']
+                user_profile.save(update_fields=['yt_channel_id'])
 
         return 0
 

+ 4 - 3
backend/main/views.py

@@ -19,7 +19,7 @@ def home(request):
     user_profile = request.user
 
     #### FOR NEWLY JOINED USERS ######
-    channel_found = True
+    # channel_found = True
     if user_profile.profile.show_import_page:
         """
         Logic:
@@ -29,6 +29,8 @@ def home(request):
         
         show_import_page is only set false in the import_in_progress.html page, i.e when user cancels YT import
         """
+        Playlist.objects.getUserYTChannelID(request.user)
+
         # after user imports all their YT playlists no need to show_import_page again
         if user_profile.profile.imported_yt_playlists:
             user_profile.profile.show_import_page = False
@@ -37,7 +39,6 @@ def home(request):
                 playlist_id="LL").count()
             return render(request, "home.html", {"import_successful": True, "imported_playlists_count": imported_playlists_count})
 
-        Playlist.objects.getUserYTChannelID(request.user)
         return render(request, "import_in_progress.html")
     ##################################
 
@@ -48,7 +49,7 @@ def home(request):
     videos = request.user.videos.filter(Q(is_unavailable_on_yt=False) & Q(was_deleted_on_yt=False))
     channels = videos.values('channel_name').annotate(channel_videos_count=Count('video_id'))
 
-    return render(request, 'home.html', {"channel_found": channel_found,
+    return render(request, 'home.html', {# "channel_found": channel_found,
                                          "playlist_tags": playlist_tags,
                                          "watching": watching,
                                          "recently_accessed_playlists": recently_accessed_playlists,

+ 7 - 7
backend/users/views.py

@@ -4,7 +4,7 @@ from django.shortcuts import render, redirect
 from django.contrib.auth import logout
 from django.views.decorators.http import require_POST
 from django.contrib.auth.decorators import login_required
-from allauth.socialaccount.models import SocialToken, SocialApp
+from allauth.socialaccount.models import SocialApp
 from django.http import HttpResponse
 from django.contrib.auth.models import User
 from django.contrib import messages
@@ -168,6 +168,7 @@ def log_out(request):
     return redirect('/')
 
 
+@login_required
 def cancel_import(request):
     user_profile = request.user.profile
     user_profile.imported_yt_playlists = False
@@ -177,6 +178,7 @@ def cancel_import(request):
     return redirect('home')
 
 
+@login_required
 def import_user_yt_playlists(request):
     request.user.profile.show_import_page = True
     request.user.profile.save(update_fields=['show_import_page'])
@@ -189,8 +191,6 @@ def start_import(request):
     """
     Initializes only the user's playlist data in the database. Returns the progress bar, which will
     keep calling continue_import
-    :param request:
-    :return:
     """
     user_profile = request.user.profile
 
@@ -212,8 +212,8 @@ def start_import(request):
 
         print("User has no playlists on YT")
 
-        if request.user.profile.yt_channel_id == "":
-            Playlist.objects.getUserYTChannelID(request.user)
+        # if request.user.profile.yt_channel_id == "":
+        #     Playlist.objects.getUserYTChannelID(request.user)
 
         Playlist.objects.initializePlaylist(request.user, "LL")
 
@@ -224,8 +224,8 @@ def start_import(request):
              "progress": 100,
              "channel_found": True}))
     else:
-        if request.user.profile.yt_channel_id == "":
-            Playlist.objects.getUserYTChannelID(request.user)
+        # if request.user.profile.yt_channel_id == "":
+        #     Playlist.objects.getUserYTChannelID(request.user)
 
         Playlist.objects.initializePlaylist(request.user, "LL")