Browse Source

Preparations for production

sleepytaco 3 years ago
parent
commit
343a302dc4

+ 6 - 5
UnTube/asgi.py

@@ -8,13 +8,14 @@ https://docs.djangoproject.com/en/3.2/howto/deployment/asgi/
 """
 
 import os
-
+from dotenv import load_dotenv
 from django.core.asgi import get_asgi_application
 
-# If WEBSITE_HOSTNAME is defined as an environment variable, then we're running
-# on Azure App Service and should use the production settings in production.py.
-# settings_module = "UnTube.production" if 'WEBSITE_HOSTNAME' in os.environ else 'UnTube.settings'
-settings_module = "UnTube.production" if 'PYTHONANYWHERE_SITE' in os.environ else 'UnTube.settings'
+settings_module = "UnTube.production" if 'UNTUBE' in os.environ else 'UnTube.settings'
 os.environ.setdefault('DJANGO_SETTINGS_MODULE', settings_module)
 
+# to use env variables on pythonanywhere
+project_folder = os.path.expanduser('~/bakaabu.pythonanywhere.com')
+load_dotenv(os.path.join(project_folder, '.env'))
+
 application = get_asgi_application()

+ 6 - 20
UnTube/production.py

@@ -1,19 +1,17 @@
 from .settings import *
 import os
 
+SECRET_KEY = os.environ['SECRET_KEY']
+
+# configure the domain name using the environment variable found on pythonanywhere
+ALLOWED_HOSTS = ['bakaabu.pythonanywhere.com', '127.0.0.1', 'untube.it'] if 'UNTUBE' in os.environ else ['bakaabu.pythonanywhere.com', 'untube.it']
+SITE_ID = 8
+
 DEBUG = False
 CSRF_COOKIE_SECURE = True
 SESSION_COOKIE_SECURE = True
 SECURE_SSL_REDIRECT = True
 
-# Configure the domain name using the environment variable
-# that Azure automatically creates for us.
-# ALLOWED_HOSTS = [os.environ['WEBSITE_HOSTNAME'], '127.0.0.1'] if 'WEBSITE_HOSTNAME' in os.environ else []
-
-# configure the domain name using the environment variable found on pythonanywhere
-ALLOWED_HOSTS = ['bakaabu.pythonanywhere.com', '127.0.0.1'] if 'PYTHONANYWHERE_SITE' in os.environ else ['bakaabu.pythonanywhere.com']
-SITE_ID = 8
-
 # WhiteNoise configuration
 MIDDLEWARE = [
     'django.middleware.security.SecurityMiddleware',
@@ -33,18 +31,6 @@ STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
 # DBHOST is only the server name, not the full URL (azure)
 hostname = os.environ['DBHOST']
 
-# Configure Postgres database; the full username is username@servername,
-# which we construct using the DBHOST value.
-# DATABASES = {
-#    'default': {
-#        'ENGINE': 'django.db.backends.postgresql',
-#        'NAME': os.environ['DBNAME'],
-#        'HOST': hostname + ".postgres.database.azure.com",
-#        'USER': os.environ['DBUSER'] + "@" + hostname,
-#        'PASSWORD': os.environ['DBPASS']
-#    }
-# }
-
 # Configure MySQL database on pythonanywhere
 # See https://django-mysql.readthedocs.io/en/latest/checks.html for options
 DATABASES = {

+ 1 - 1
UnTube/secrets.py

@@ -1,6 +1,6 @@
+# Make sure you change these before production ;)
 SECRETS = {"SECRET_KEY": 'django-insecure-ycs22y+20sq67y(6dm6ynqw=dlhg!)%vuqpd@$p6rf3!#1h$u=',
            "YOUTUBE_V3_API_KEY": 'AIzaSyCBOucAIJ5PdLeqzTfkTQ_6twsjNaMecS8',
                     "GOOGLE_OAUTH_CLIENT_ID": "901333803283-1lscbdmukcjj3qp0t3relmla63h6l9k6.apps.googleusercontent.com",
            "GOOGLE_OAUTH_CLIENT_SECRET": "ekdBniL-_mAnNPwCmugfIL2q",
-           "GOOGLE_OAUTH_SCOPES": ['https://www.googleapis.com/auth/youtube'],
            "UNSPLASH_API_ACCESS_KEY": "fGf0hsfg24Hj1mUrwvtETl1Dl2-TvaMP3w1moGEb1hs"}

+ 1 - 1
UnTube/settings.py

@@ -29,7 +29,7 @@ YOUTUBE_V3_API_KEY = SECRETS['YOUTUBE_V3_API_KEY']
 # SECURITY WARNING: don't run with debug turned on in production!
 DEBUG = True
 
-ALLOWED_HOSTS = ['127.0.0.1', 'bakaabu.pythonanywhere.com']
+ALLOWED_HOSTS = ['127.0.0.1']
 
 # Application definition
 INSTALLED_APPS = [

+ 6 - 2
UnTube/wsgi.py

@@ -8,13 +8,17 @@ https://docs.djangoproject.com/en/3.2/howto/deployment/wsgi/
 """
 
 import os
-
+from dotenv import load_dotenv
 from django.core.wsgi import get_wsgi_application
 
 # If WEBSITE_HOSTNAME is defined as an environment variable, then we're running
 # on Azure App Service and should use the production settings in production.py.
 # settings_module = "UnTube.production" if 'WEBSITE_HOSTNAME' in os.environ else 'UnTube.settings'
-settings_module = "UnTube.production" if 'PYTHONANYWHERE_SITE' in os.environ else 'UnTube.settings'
+settings_module = "UnTube.production" if 'UNTUBE' in os.environ else 'UnTube.settings'
 os.environ.setdefault('DJANGO_SETTINGS_MODULE', settings_module)
 
+# to use env variables on pythonanywhere
+project_folder = os.path.expanduser('~/bakaabu.pythonanywhere.com')
+load_dotenv(os.path.join(project_folder, '.env'))
+
 application = get_wsgi_application()

+ 1 - 1
apps/charts/views.py

@@ -45,7 +45,7 @@ def overall_playlists_distribution(request):
     labels = []
     data = []
 
-    user_playlists = request.user.playlists.filter(is_in_db=True)
+    user_playlists = request.user.playlists.filter(is_in_db=True).exclude(playlist_id="LL")
     total_num_playlists = user_playlists.count()
 
 

+ 10 - 0
apps/main/models.py

@@ -1403,6 +1403,9 @@ class Playlist(models.Model):
         return [num_channels, channels_list]
 
     def generate_playlist_thumbnail_url(self):
+        """
+        Generates a playlist thumnail url based on the playlist name
+        """
         pl_name = self.name
         response = requests.get(
             f'https://api.unsplash.com/search/photos/?client_id={SECRETS["UNSPLASH_API_ACCESS_KEY"]}&page=1&query={pl_name}')
@@ -1412,6 +1415,13 @@ class Playlist(models.Model):
 
         return image
 
+    def get_playlist_thumbnail_url(self):
+        playlist_items = self.playlist_items.filter(Q(video__was_deleted_on_yt=False) & Q(video__is_unavailable_on_yt=False))
+        if playlist_items.exists():
+            return playlist_items.first().video.thumbnail_url
+        else:
+            return "https://i.ytimg.com/vi/9219YrnwDXE/maxresdefault.jpg"
+
     def get_unavailable_videos_count(self):
         return self.video_count - self.get_watchable_videos_count()
 

+ 2 - 2
apps/main/templates/all_playlists.html

@@ -57,13 +57,13 @@
                     {% elif library_type == "all" %}
                         There's no playlists in your UnTube right now.
                     {% elif library_type == "imported" %}
-                        To import public playlists into your UnTube collection you can head over to <a href="{% url 'manage_playlists' %}" class="btn btn-sm btn-primary ms-2">Manage</a>
+                        To import public playlists into your UnTube collection you can head over to <a href="{% url 'manage_view_page' 'import' %}" class="btn btn-sm btn-primary ms-2">Import</a>
                     {% elif library_type == "plan-to-watch" %}
                         You can mark a playlist as plan to watch by heading over to the playlist and marking it from the dropdown.
                     {% elif library_type == "favorites" %}
                         You can mark a playlist as favorite by heading over to the playlist page and pressing the star icon.
                     {% elif library_type == "yt-mix" %}
-                        No YouTube mixes imported. Head over to Manage to import the ones you like.
+                        No YouTube mixes imported. Head over to Import to import the ones you like.
                     {% elif library_type == "user-owned" %}
                         {% if user.profile.imported_yt_playlists %}
                             Looks like you have no playlists on YouTube.

+ 3 - 3
apps/main/templates/home.html

@@ -2,11 +2,11 @@
 {% extends 'base.html' %}
 {% load humanize %}
 {% block content %}
-    {% if user.playlists.all.count == 0 %}
+    {% if user.playlists.all.count|add:"-1" == 0 %}
         <div class="alert alert-success" role="alert">
             <h4 class="alert-heading">It's empty in here</h4>
             <p>
-                There's no playlists in your UnTube right now. You can change that by heading over to <a href="{% url 'manage_playlists' %}" class="btn btn-sm btn-primary">Manage</a> to import some public playlists into your UnTube.
+                There's no playlists in your UnTube right now. You can change that by heading over to <a href="{% url 'manage_view_page' 'import' %}" class="btn btn-sm btn-primary">Import</a> to import some public playlists into your UnTube.
                 {% if not user.profile.imported_yt_playlists %}
                     Or you could always head over to your <a href="{% url 'settings' %}" class="btn btn-sm btn-primary">Profile</a> to import all of your public/private YouTube playlists.
                 {% else %}
@@ -49,7 +49,7 @@
             <div class="col-6 mb-4">
                 <div class="card bg-transparent text-dark">
                     <div class="card-body">
-                        <h6 class="d-flex justify-content-center align-items-center mb-3">You have a total of <span class="text-warning ms-1 me-1">{{ user.playlists.count }}</span> Playlists in your UnTube collection</h6>
+                        <h6 class="d-flex justify-content-center align-items-center mb-3">You have a total of <span class="text-warning ms-1 me-1">{{ user.playlists.count|add:"-1" }}</span> Playlists in your UnTube collection</h6>
                         <div class="d-flex align-items-center mb-3">
 
                             <canvas id="overall-playlists-distribution" data-url="{% url 'overall_playlists_distribution' %}">

+ 2 - 2
apps/main/templates/intercooler/playlists.html

@@ -4,7 +4,7 @@
         <div class="col">
 
             <div class="card overflow-auto" style="background-color: {{ bg_color|default:"#357779" }};">
-                <img  class="bd-placeholder-img card-img-top" src="{% if playlist.playlist_items.all.count > 0 %}{{ playlist.playlist_items.all.0.video.thumbnail_url }}{% else %}https://i.ytimg.com/vi/9219YrnwDXE/maxresdefault.jpg{% endif %}" style="max-width:100%; height: 200px;   object-fit: cover;" alt="{{ playlist.name }} thumbnail">
+                <img  class="bd-placeholder-img card-img-top" src="{{ playlist.get_playlist_thumbnail_url }}" style="max-width:100%; height: 200px;   object-fit: cover;" alt="{{ playlist.name }} thumbnail">
 
                 <div class="card-body">
                     <h5 class="card-title"><a href="{% url 'playlist' playlist.playlist_id %}" class="stretched-link" style="text-decoration: none; color: white">{{ playlist.name }}</a></h5>
@@ -23,7 +23,7 @@
         <div class="col">
             <div class="card overflow-auto" style="background-color: {{ bg_color|default:"#357779" }};">
                 <a href="{% url 'playlist' playlist.playlist_id %}" style="text-decoration: none; color: black">
-                    <img  class="bd-placeholder-img card-img-top" src="{% if playlist.playlist_items.all.count > 0 %}{{ playlist.playlist_items.all.0.video.thumbnail_url }}{% else %}https://i.ytimg.com/vi/9219YrnwDXE/maxresdefault.jpg{% endif %}" style="max-width:100%; height: 200px;   object-fit: cover;" alt="{{ playlist.name }} thumbnail">
+                    <img  class="bd-placeholder-img card-img-top" src="{{ playlist.get_playlist_thumbnail_url }}" style="max-width:100%; height: 200px;   object-fit: cover;" alt="{{ playlist.name }} thumbnail">
                 </a>
                 <div class="card-body">
                     <h5 class="card-title">

+ 1 - 1
apps/main/templates/library.html

@@ -68,7 +68,7 @@
         <div class="card h-100" style="background-color: #bd39a7;">
             <div class="card-body">
                 <h5 class="card-title">Your YouTube Mixes <span class="badge bg-warning">BETA</span> </h5>
-                <p class="card-text">YouTube creates nice mixes that relate to songs you're currently jamming to. You can import those YT Mixes by going over to Manage. Any YouTube mixes you import will all be here.</p>
+                <p class="card-text">YouTube creates nice mixes that relate to songs you're currently jamming to. You can import those YT Mixes by going over to Import. Any YouTube mixes you import will all be here.</p>
             </div>
         </div>
         </a>

+ 1 - 1
apps/main/templates/view_playlist.html

@@ -62,7 +62,7 @@
                 <div class="card text-white" style="max-width: 100%; background-color: #1A4464;">
                     <div class="row g-0">
                         <div class="col-md-4 p-3">
-                            <img  class="img-fluid rounded-3" src="{{ playlist_items.0.video.thumbnail_url }}" style="max-width:100%; height: auto;   object-fit: cover;">
+                            <img  class="img-fluid rounded-3" src="{{ playlist.get_playlist_thumbnail_url }}" style="max-width:100%; height: auto;   object-fit: cover;">
                         </div>
                         <div class="col-md-8">
                             <div class="card-body">

+ 0 - 6
apps/users/models.py

@@ -9,11 +9,6 @@ from django.dispatch import receiver
 
 
 # Create your models here.
-class ProfileManager(models.Manager):
-    def updateUserProfile(self, details):
-        pass
-
-
 class Untube(models.Model):
     page_likes = models.IntegerField(default=0)
 
@@ -41,7 +36,6 @@ class Profile(models.Model):
     ###########################
 
     # manage user
-    objects = ProfileManager()
     show_import_page = models.BooleanField(default=True)  # shows the user tips for a week
     yt_channel_id = models.TextField(default='')
     import_in_progress = models.BooleanField(

BIN
requirements.txt


+ 1 - 1
templates/base.html

@@ -165,7 +165,7 @@
                         </li>
 
                         <li class="nav-item">
-                            <a class="nav-link" href="{% url 'manage_playlists' %}">Manage</a>
+                            <a class="nav-link" href="{% url 'manage_view_page' 'import' %}">Import</a>
                         </li>