From f9931268a336c0b3fc00bd654985271dd11c0de6 Mon Sep 17 00:00:00 2001 From: Alain Date: Sun, 16 Jun 2024 20:05:48 +0200 Subject: [PATCH] Working search, missing studio and publisher view (TBD) --- .../templates/thefiltershop/base_games.html | 4 +- .../thefiltershop/vg_search_results.html | 131 ++++++++++++++++++ thefiltershop/filtershop_main/urls.py | 1 + .../filtershop_main/views/__init__.py | 3 +- .../views/videogames_search.py | 44 +++--- .../filtershop_main/views/view_a_studio.py | 14 ++ 6 files changed, 177 insertions(+), 20 deletions(-) create mode 100644 thefiltershop/filtershop_main/templates/thefiltershop/vg_search_results.html create mode 100644 thefiltershop/filtershop_main/views/view_a_studio.py diff --git a/thefiltershop/filtershop_main/templates/thefiltershop/base_games.html b/thefiltershop/filtershop_main/templates/thefiltershop/base_games.html index c6b0e69..36bdef4 100644 --- a/thefiltershop/filtershop_main/templates/thefiltershop/base_games.html +++ b/thefiltershop/filtershop_main/templates/thefiltershop/base_games.html @@ -9,8 +9,8 @@
Search -
- + +
diff --git a/thefiltershop/filtershop_main/templates/thefiltershop/vg_search_results.html b/thefiltershop/filtershop_main/templates/thefiltershop/vg_search_results.html new file mode 100644 index 0000000..52a010d --- /dev/null +++ b/thefiltershop/filtershop_main/templates/thefiltershop/vg_search_results.html @@ -0,0 +1,131 @@ +{% extends "thefiltershop/base_games.html" %} + +{% block title %}Search results{% endblock %} + +{% block title_content %}Search results{% endblock %} + +{% block content %} + +

Artisans' Games

+
+ + {% for game in artisan_games %} +
+ +
+ {{game.name}} + +

{{ game.description }}

+
+
+
+ {% for category in game.categories.all %} + {{ category.name }} + {% endfor %} +
+
+ {% endfor %} +
+
+ +

Indies' Games

+
+ + {% for game in indie_games %} +
+ +
+ {{game.name}} + +

{{ game.description }}

+
+
+
+ {% for category in game.categories.all %} + {{ category.name }} + {% endfor %} +
+
+ {% endfor %} +
+
+ +

Other Games

+
+ + {% for game in other_games %} +
+ +
+ {{game.name}} + +

{{ game.description }}

+
+
+
+ {% for category in game.categories.all %} + {{ category.name }} + {% endfor %} +
+
+ {% endfor %} +
+
+ +

Studios

+
+ + {% for game in studios %} +
+ +
+ {{game.name}} + +

{{ game.description }}

+
+
+
+ {% for category in game.categories.all %} + {{ category.name }} + {% endfor %} +
+
+ {% endfor %} +
+
+ +

Publishers

+
+ + {% for game in publishers %} + + {% endfor %} +
+
+ +

Filters

+
+ + {% for one_filter in filters %} + + {% endfor %} +
+
+ +{% endblock %} \ No newline at end of file diff --git a/thefiltershop/filtershop_main/urls.py b/thefiltershop/filtershop_main/urls.py index b1aa160..ffd3d58 100644 --- a/thefiltershop/filtershop_main/urls.py +++ b/thefiltershop/filtershop_main/urls.py @@ -37,6 +37,7 @@ path("physical_shop_filters", views.get_all_filters_for_an_entity_type_physical_shop, name="physical_shop_filters"), path("a_random_artisan_game", views.get_a_random_unfiltered_artisan_game, name="a_random_artisan_game"), path("a_random_indie_game", views.get_a_random_unfiltered_indie_game, name="a_random_indie_game"), + path("videogames_search/", views.get_search_results, name="videogames_search"), path("four_o_four", TemplateView.as_view(template_name="404.html"), name="404"), #path('admin/', admin_site.urls), ] \ No newline at end of file diff --git a/thefiltershop/filtershop_main/views/__init__.py b/thefiltershop/filtershop_main/views/__init__.py index 94b0eaa..3196c6d 100644 --- a/thefiltershop/filtershop_main/views/__init__.py +++ b/thefiltershop/filtershop_main/views/__init__.py @@ -25,4 +25,5 @@ from .list_shops import get_others_physical_shops from .list_shops import get_artisans_and_indies_physical_shops_that_made_it from .list_games import get_a_random_unfiltered_artisan_game -from .list_games import get_a_random_unfiltered_indie_game \ No newline at end of file +from .list_games import get_a_random_unfiltered_indie_game +from .videogames_search import get_search_results \ No newline at end of file diff --git a/thefiltershop/filtershop_main/views/videogames_search.py b/thefiltershop/filtershop_main/views/videogames_search.py index 31312aa..acf4eb4 100644 --- a/thefiltershop/filtershop_main/views/videogames_search.py +++ b/thefiltershop/filtershop_main/views/videogames_search.py @@ -1,4 +1,6 @@ import random +import re + from django.shortcuts import render from django.db.models import Q from django.db.models import Count @@ -6,7 +8,7 @@ from .view_a_game import game -from ..models import Videogame_common, Game_Category, Studio, Publisher +from ..models import Videogame_common, Studio, Publisher, Filter """Search view for the video game category @@ -19,13 +21,21 @@ def get_search_results(request): # Get the keywords from the string # TODO: support AND/OR and others - keywords = request.GET.get('keywords') + keywords_txt = request.GET.get('keywords') + + # Split using space or comma + keywords_not_curated = re.split(r';|,|\s', keywords_txt) # Remove if too short - for k, v in list(keywords.items()): - if len(v[0] < 3): + keywords = [] + + for one_keyword in keywords_not_curated: + one_keyword = one_keyword.strip() + if len(one_keyword) >= 3 : + keywords.append(one_keyword) + print(f"One keyword {one_keyword}") + else : was_one_short_keyword = True - del keywords[k] if len(keywords) == 0 : if was_one_short_keyword : @@ -42,7 +52,7 @@ def get_search_results(request): found_indie_games = get_all_games_for_keywords(keywords, Studio.SizeInPersons.INDIE) - found_other_games = get_all_games_for_keywords(keywords, Studio.SizeInPersons.MEDIUM) + get_all_games_for_keywords(keywords, Studio.SizeInPersons.BIG) + get_all_games_for_keywords(keywords, Studio.SizeInPersons.HUGE) + found_other_games = get_all_games_for_keywords(keywords, Studio.SizeInPersons.MEDIUM) | get_all_games_for_keywords(keywords, Studio.SizeInPersons.BIG) | get_all_games_for_keywords(keywords, Studio.SizeInPersons.HUGE) found_studios = get_all_studios_for_keywords(keywords) @@ -54,7 +64,7 @@ def get_search_results(request): # The paginator cannot be used as we have several querysets. It might need to be addressed if the returned sets are too long. context = {"artisan_games": found_artisan_games, "indie_games": found_indie_games, "other_games": found_other_games, "studios": found_studios, "publishers": found_publishers, "filters": found_filters} - return render(request, "thefiltershop/search_results.html", context) + return render(request, "thefiltershop/vg_search_results.html", context) @@ -63,27 +73,27 @@ def get_all_games_for_keywords(keywords, max_size_of_studio) : raise TypeError('max_size_of_studio_or_publisher must be a Studio_and_Publisher_Size') # Build-up the query string for keywords - q_keyword = Q(Videogame_common.name__startswith(keywords[0])) - + q_keyword = Q(name__icontains = keywords[0]) + if len(keywords) > 1 : for one_keyword in keywords[1:] : - q_keyword = q_keyword | Q(Videogame_common.name__startswith(one_keyword)) + q_keyword = q_keyword | Q(name__icontains = one_keyword) if max_size_of_studio != Studio.SizeInPersons.ARTISAN : # No filter on publisher size for non-artisan all_for_size = Videogame_common.objects.filter(studios__size_of_studio = max_size_of_studio).filter(q_keyword).order_by("known_popularity") else : all_for_size = Videogame_common.objects.filter(studios__size_of_studio = Studio.SizeInPersons.ARTISAN, publishers__size_of_publisher = Publisher.SizeInPersons.ARTISAN).filter(q_keyword).order_by("known_popularity") - + print(all_for_size.query) return all_for_size def get_all_studios_for_keywords(keywords) : # Build-up the query string for keywords - q_keyword = Q(Studio.name__startswith(keywords[0])) + q_keyword = Q(name__icontains = keywords[0]) if len(keywords) > 1 : for one_keyword in keywords[1:] : - q_keyword = q_keyword | Q(Studio.name__startswith(one_keyword)) + q_keyword = q_keyword | Q(name__icontains = one_keyword) all_for_keyword = Studio.objects.filter(q_keyword) @@ -91,11 +101,11 @@ def get_all_studios_for_keywords(keywords) : def get_all_publishers_for_keywords(keywords) : # Build-up the query string for keywords - q_keyword = Q(Publisher.name__startswith(keywords[0])) + q_keyword = Q(name__icontains = keywords[0]) if len(keywords) > 1 : for one_keyword in keywords[1:] : - q_keyword = q_keyword | Q(Publisher.name__startswith(one_keyword)) + q_keyword = q_keyword | Q(name__icontains = one_keyword) all_for_keyword = Publisher.objects.filter(q_keyword) @@ -103,11 +113,11 @@ def get_all_publishers_for_keywords(keywords) : def get_all_filters_for_keywords(keywords) : # Build-up the query string for keywords - q_keyword = Q(Filter.name__startswith(keywords[0])) + q_keyword = Q(name__icontains = keywords[0]) if len(keywords) > 1 : for one_keyword in keywords[1:] : - q_keyword = q_keyword | Q(Filter.name__startswith(one_keyword)) + q_keyword = q_keyword | Q(name__icontains = one_keyword) all_for_keyword = Filter.objects.filter(q_keyword) diff --git a/thefiltershop/filtershop_main/views/view_a_studio.py b/thefiltershop/filtershop_main/views/view_a_studio.py new file mode 100644 index 0000000..f6979e1 --- /dev/null +++ b/thefiltershop/filtershop_main/views/view_a_studio.py @@ -0,0 +1,14 @@ +from django.shortcuts import render +from django.shortcuts import get_object_or_404 + +from ..models import Videogame_common +from ..models import Filter +from ..models import Links_to_shops +from ..models import Videogame_rating +from ..models import Studio + +def studio(request, studio_id): + a_studio = get_object_or_404(Studio, pk=studio_id) + + return render(request, "thefiltershop/studio.html", {"a_studio": a_studio) + \ No newline at end of file