瀏覽代碼

fixed untube search

sleepytaco 3 年之前
父節點
當前提交
cbe3667345

+ 4 - 4
apps/search/templates/intercooler/search_untube_results.html

@@ -1,8 +1,8 @@
 {% load humanize %}
 {% load humanize %}
 
 
-{% if playlists %}
+{% if view_mode == "playlists" %}
     <div class="d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center pt-3 pb-2 mb-3 border-bottom">
     <div class="d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center pt-3 pb-2 mb-3 border-bottom">
-        <h1 class="h2">{% if search_query == "" %}All{% endif %} Playlists <span class="badge bg-primary rounded-pill">{{ playlists.count|default:"0" }}</span></h1>
+        <h1 class="h2">{% if search_query == "" %}All{% endif %} Playlists {% if search_query != "" %}results for '{{ search_query|escape }}' {% endif %} <span class="badge bg-primary rounded-pill">{{ playlists.count|default:"0" }}</span></h1>
     </div>
     </div>
 
 
     <div class="row row-cols-1 row-cols-md-4 g-4">
     <div class="row row-cols-1 row-cols-md-4 g-4">
@@ -15,13 +15,13 @@
 {% else %}
 {% else %}
 
 
     <div class="d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center pt-3 pb-2 mb-3 border-bottom">
     <div class="d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center pt-3 pb-2 mb-3 border-bottom">
-        <h1 class="h2">Videos <span class="badge bg-primary rounded-pill">{{ videos.count }}</span>{% if videos.count > 250 %}(Only top 250 results shown){% endif %}</h1>
+        <h1 class="h2">{% if search_query == "" %}All{% endif %} Videos <span class="badge bg-primary rounded-pill">{{ videos.count }}</span> {% if videos.count > 250 %}(Only top 100 results shown){% endif %}</h1>
     </div>
     </div>
 
 
     <div>
     <div>
         <div class="row row-cols-1 row-cols-md-4 g-4">
         <div class="row row-cols-1 row-cols-md-4 g-4">
             {% if videos %}
             {% if videos %}
-                {% include 'intercooler/video_cards.html' with videos=videos %}
+                {% include 'intercooler/video_cards.html' with videos=videos|slice:"0:100" %}
             {% else %}
             {% else %}
                 <h5 class="text-dark align-content-center">Nothing found :(</h5>
                 <h5 class="text-dark align-content-center">Nothing found :(</h5>
             {% endif %}
             {% endif %}

+ 1 - 8
apps/search/templates/search_untube_page.html

@@ -84,14 +84,7 @@
     </div>
     </div>
 
 
     <div id="untube-searchbar-results">
     <div id="untube-searchbar-results">
-        {% if playlists %}
-        <div class="d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center pt-3 pb-2 mb-3 border-bottom">
-            <h1 class="h2">All Playlists <span class="badge bg-primary rounded-pill">{{ playlists.count }}</span></h1>
-        </div>
-            <div class="row row-cols-1 row-cols-md-4 g-4">
-            {% include 'intercooler/playlists.html' %}
-            </div>
-        {% endif %}
+
     </div>
     </div>
 
 
     <button class="scrollToTopBtn sticky-top">
     <button class="scrollToTopBtn sticky-top">

+ 24 - 27
apps/search/views.py

@@ -1,3 +1,4 @@
+import bleach
 from django.contrib.auth.decorators import login_required
 from django.contrib.auth.decorators import login_required
 from django.db.models import Q
 from django.db.models import Q
 from django.http import HttpResponse
 from django.http import HttpResponse
@@ -21,46 +22,42 @@ def search(request):
 def search_UnTube(request):
 def search_UnTube(request):
     print(request.POST)
     print(request.POST)
 
 
-    search_query = request.POST["search"]
-
-    all_playlists = request.user.playlists.filter(is_in_db=True)
-    if 'playlist-tags' in request.POST:
-        tags = request.POST.getlist('playlist-tags')
-        for tag in tags:
-            all_playlists = all_playlists.filter(tags__name=tag)
-
-    channels = []
-    if 'channel-names' in request.POST:
-        channels = request.POST.getlist('channel-names')
+    search_query = bleach.clean(request.POST["search"])
+    print(search_query)
 
 
     if request.POST['search-settings'] == 'playlists':
     if request.POST['search-settings'] == 'playlists':
-        playlists = all_playlists.filter(Q(name__istartswith=search_query) | Q(
-            user_label__istartswith=search_query)) if search_query != "" else all_playlists.none()
+        all_playlists = request.user.playlists.filter(is_in_db=True)
+        if 'playlist-tags' in request.POST:
+            tags = request.POST.getlist('playlist-tags')
+            for tag in tags:
+                all_playlists = all_playlists.filter(tags__name=tag)
+
+        playlists = all_playlists.filter(Q(name__icontains=search_query) | Q(
+            user_label__icontains=search_query))
 
 
-        if search_query == "":
+        if search_query.strip() == "":
             playlists = all_playlists
             playlists = all_playlists
 
 
         return HttpResponse(loader.get_template("intercooler/search_untube_results.html")
         return HttpResponse(loader.get_template("intercooler/search_untube_results.html")
                             .render({"playlists": playlists,
                             .render({"playlists": playlists,
+                                     "view_mode": "playlists",
                                      "search_query": search_query}))
                                      "search_query": search_query}))
     else:
     else:
-        playlists = all_playlists.filter(Q(name__icontains=search_query) | Q(
-            user_label__istartswith=search_query)) if search_query != "" else all_playlists.none()
-
-        if search_query == "":
-            playlists = all_playlists
+        all_videos = request.user.videos.filter(is_unavailable_on_yt=False)
+        if 'channel-names' in request.POST:
+            channels = request.POST.getlist('channel-names')
+            all_videos = all_videos.filter(channel_name__in=channels)
 
 
-        videos = Video.objects.none()
-        for playlist in playlists:
-            pl_videos = playlist.videos.filter(is_unavailable_on_yt=False)
-            videos = videos | pl_videos
+        videos = all_videos.filter(
+            Q(name__icontains=search_query) | Q(user_label__icontains=search_query))
 
 
-        videos = videos.filter(
-            Q(name__icontains=search_query) | Q(user_label__istartswith=search_query)).distinct()
+        if search_query.strip() == "":
+            videos = all_videos
 
 
         return HttpResponse(loader.get_template("intercooler/search_untube_results.html")
         return HttpResponse(loader.get_template("intercooler/search_untube_results.html")
-                            .render({"videos": videos[:250],
-                                     "show_all_videos": len(channels) > 0}))
+                            .render({"videos": videos,
+                                     "view_mode": "videos",
+                                     "search_query": search_query}))
 
 
 
 
 @login_required
 @login_required