Browse Source

move get_credentials to Profile model

Mohammed Khan 1 năm trước cách đây
mục cha
commit
6c561a1d3f
2 tập tin đã thay đổi với 51 bổ sung34 xóa
  1. 29 33
      backend/main/models.py
  2. 22 1
      backend/users/models.py

+ 29 - 33
backend/main/models.py

@@ -1,11 +1,7 @@
-import requests
 from django.contrib.auth.models import User
-from allauth.socialaccount.models import SocialApp
 from .util import *
 import pytz
 from django.db import models
-from google.oauth2.credentials import Credentials
-from google.auth.transport.requests import Request
 from googleapiclient.discovery import build
 import googleapiclient.errors
 from django.db.models import Q, Sum
@@ -16,24 +12,24 @@ def get_message_from_httperror(e):
 
 
 class PlaylistManager(models.Manager):
-    def getCredentials(self, user):
-        app = SocialApp.objects.get(provider='google')
-        credentials = Credentials(
-            token=user.profile.access_token,
-            refresh_token=user.profile.refresh_token,
-            token_uri="https://oauth2.googleapis.com/token",
-            client_id=app.client_id,
-            client_secret=app.secret,
-            scopes=['https://www.googleapis.com/auth/youtube']
-        )
-
-        if not credentials.valid:
-            credentials.refresh(Request())
-            user.profile.access_token = credentials.token
-            user.profile.refresh_token = credentials.refresh_token
-            user.save()
-
-        return credentials
+    # def getCredentials(self, user):
+    #     app = SocialApp.objects.get(provider='google')
+    #     credentials = Credentials(
+    #         token=user.profile.access_token,
+    #         refresh_token=user.profile.refresh_token,
+    #         token_uri="https://oauth2.googleapis.com/token",
+    #         client_id=app.client_id,
+    #         client_secret=app.secret,
+    #         scopes=['https://www.googleapis.com/auth/youtube']
+    #     )
+    #
+    #     if not credentials.valid:
+    #         credentials.refresh(Request())
+    #         user.profile.access_token = credentials.token
+    #         user.profile.refresh_token = credentials.refresh_token
+    #         user.save()
+    #
+    #     return credentials
 
     def getPlaylistId(self, playlist_link):
         if "?" not in playlist_link:
@@ -48,7 +44,7 @@ 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 = self.getCredentials(user)
+        credentials = user.profile.get_credentials()
 
         with build('youtube', 'v3', credentials=credentials) as youtube:
             pl_request = youtube.channels().list(
@@ -86,7 +82,7 @@ class PlaylistManager(models.Manager):
                   "error_message": "",
                   "playlist_ids": []}
 
-        credentials = self.getCredentials(user)
+        credentials = user.profile.get_credentials()
 
         playlist_ids = []
         with build('youtube', 'v3', credentials=credentials) as youtube:
@@ -191,7 +187,7 @@ class PlaylistManager(models.Manager):
         return result
 
     def getAllVideosForPlaylist(self, user, playlist_id):
-        credentials = self.getCredentials(user)
+        credentials = user.profile.get_credentials()
 
         playlist = user.playlists.get(playlist_id=playlist_id)
 
@@ -458,7 +454,7 @@ class PlaylistManager(models.Manager):
         If full_scan is False, only the playlist count difference on YT and UT is checked on every visit
         to the playlist page. This is done everytime.
         """
-        credentials = self.getCredentials(user)
+        credentials = user.profile.get_credentials()
 
         playlist = user.playlists.get(playlist_id=pl_id)
 
@@ -603,7 +599,7 @@ class PlaylistManager(models.Manager):
         return [0, "no change"]
 
     def updatePlaylist(self, user, playlist_id):
-        credentials = self.getCredentials(user)
+        credentials = user.profile.get_credentials()
 
         playlist = user.playlists.get(playlist_id__exact=playlist_id)
 
@@ -909,7 +905,7 @@ class PlaylistManager(models.Manager):
         """
         Takes in playlist itemids for the videos in a particular playlist
         """
-        credentials = self.getCredentials(user)
+        credentials = user.profile.get_credentials()
         playlist = user.playlists.get(playlist_id=playlist_id)
 
         # new_playlist_duration_in_seconds = playlist.playlist_duration_in_seconds
@@ -943,7 +939,7 @@ class PlaylistManager(models.Manager):
         """
         Takes in playlist itemids for the videos in a particular playlist
         """
-        credentials = self.getCredentials(user)
+        credentials = user.profile.get_credentials()
         playlist = user.playlists.get(playlist_id=playlist_id)
         playlist_items = user.playlists.get(playlist_id=playlist_id).playlist_items.select_related('video').filter(
             playlist_item_id__in=playlist_item_ids)
@@ -1027,7 +1023,7 @@ class PlaylistManager(models.Manager):
         """
         Takes in playlist details and creates a new private playlist in the user's account
         """
-        credentials = self.getCredentials(user)
+        credentials = user.profile.get_credentials()
         result = {
             "status": 0,
             "playlist_id": None
@@ -1061,7 +1057,7 @@ class PlaylistManager(models.Manager):
         """
         Takes in playlist itemids for the videos in a particular playlist
         """
-        credentials = self.getCredentials(user)
+        credentials = user.profile.get_credentials()
         playlist = user.playlists.get(playlist_id=playlist_id)
 
         with build('youtube', 'v3', credentials=credentials) as youtube:
@@ -1104,7 +1100,7 @@ class PlaylistManager(models.Manager):
         """
         Takes in playlist itemids for the videos in a particular playlist
         """
-        credentials = self.getCredentials(user)
+        credentials = user.profile.get_credentials()
         playlist_items = user.playlists.get(playlist_id=from_playlist_id).playlist_items.select_related('video').filter(
             playlist_item_id__in=playlist_item_ids)
 
@@ -1174,7 +1170,7 @@ class PlaylistManager(models.Manager):
         """
         Takes in playlist itemids for the videos in a particular playlist
         """
-        credentials = self.getCredentials(user)
+        credentials = user.profile.get_credentials()
 
         result = {
             "num_added": 0,

+ 22 - 1
backend/users/models.py

@@ -3,7 +3,9 @@ from django.contrib.auth.models import User
 from django.db.models import Count, Q
 from django.db.models.signals import post_save
 from django.dispatch import receiver
-
+from allauth.socialaccount.models import SocialApp
+from google.oauth2.credentials import Credentials
+from google.auth.transport.requests import Request
 
 # Create your models here.
 class Untube(models.Model):
@@ -57,6 +59,25 @@ class Profile(models.Model):
     def __str__(self):
         return f"{self.untube_user.username} ({self.untube_user.email})"
 
+    def get_credentials(self):
+        app = SocialApp.objects.get(provider='google')
+        credentials = Credentials(
+            token=self.access_token,
+            refresh_token=self.refresh_token,
+            token_uri="https://oauth2.googleapis.com/token",
+            client_id=app.client_id,
+            client_secret=app.secret,
+            scopes=['https://www.googleapis.com/auth/youtube']
+        )
+
+        if not credentials.valid:
+            credentials.refresh(Request())
+            self.access_token = credentials.token
+            self.refresh_token = credentials.refresh_token
+            self.save(update_fields=['access_token', 'refresh_token'])
+
+        return credentials
+
     def get_channels_list(self):
         channels_list = []
         videos = self.untube_user.videos.filter(Q(is_unavailable_on_yt=False) & Q(was_deleted_on_yt=False))