Skip to content

Commit

Permalink
WIP Search support for games
Browse files Browse the repository at this point in the history
  • Loading branch information
abecam committed Jun 15, 2024
1 parent c417555 commit 4881ad4
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 0 deletions.
Binary file added gfx/Loop.pspimage
Binary file not shown.
Binary file added gfx/Préservation automatique Corel/Loop.pspimage
Binary file not shown.
Binary file added gfx/Search.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
{% extends "thefiltershop/base.html" %}
{% load static %}

{% block main_content %}
<div class="Sub-Menu">
<a href="/">Under the spotlight!</a> <a href="{% url 'filtershop_games:artisans_games' %}">Artisan's games</a> <a href="{% url 'filtershop_games:indies_games' %}">Indie's games</a>
<a href="{% url 'filtershop_games:game_filters' %}">Filters for games</a> <a href="{% url 'filtershop_games:they_made_it' %}">They made it!</a>
<a href="{% url 'filtershop_games:best_of_the_rest' %}">Best of the rest...</a>
</div>
<div class="search_input">
<img width="40" src="{% static 'images/Search.png' %}" alt="Search">
<form action="/search_game/" method="get">
<input id="search" name="search" type="text" value="" minlength="3" required />
<input type="submit" hidden />
</form>
</div>
<div class="leftcolumn">
{% include "thefiltershop/left_column_games.html"%}
</div>
Expand Down
114 changes: 114 additions & 0 deletions thefiltershop/filtershop_main/views/videogames_search.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
import random
from django.shortcuts import render
from django.db.models import Q
from django.db.models import Count
from django.core.paginator import Paginator

from .view_a_game import game

from ..models import Videogame_common, Game_Category, Studio, Publisher

"""Search view for the video game category
Description:
Search through all categories related to video games and show them also in categories.
Order should be: artisan games, indie games, other games, studios, publishers, filters
"""

def get_search_results(request):

# Get the keywords from the string
# TODO: support AND/OR and others
keywords = request.GET.get('keywords')
# Remove if too short

for k, v in list(keywords.items()):
if len(v[0] < 3):
was_one_short_keyword = True
del keywords[k]

if len(keywords) == 0 :
if was_one_short_keyword :
error = "Keyword must have at least 3 characters"
else :
error = "No keyword given"
context = {"keywords" : request.GET.get('keywords'), "error": error}

return render(request, "thefiltershop/search_page.html", context)


# do start_with filter for our different elements, fill a dictionnary with the results
found_artisan_games = get_all_games_for_keywords(keywords, Studio.SizeInPersons.ARTISAN)

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_studios = get_all_studios_for_keywords(keywords)

found_publishers = get_all_publishers_for_keywords(keywords)

found_filters = get_all_filters_for_keywords(keywords)

# render with all results
# 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)



def get_all_games_for_keywords(keywords, max_size_of_studio) :
if not isinstance(max_size_of_studio, Studio.SizeInPersons):
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]))

if len(keywords) > 1 :
for one_keyword in keywords[1:] :
q_keyword = q_keyword | Q(Videogame_common.name__startswith(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")

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]))

if len(keywords) > 1 :
for one_keyword in keywords[1:] :
q_keyword = q_keyword | Q(Studio.name__startswith(one_keyword))

all_for_keyword = Studio.objects.filter(q_keyword)

return all_for_keyword

def get_all_publishers_for_keywords(keywords) :
# Build-up the query string for keywords
q_keyword = Q(Publisher.name__startswith(keywords[0]))

if len(keywords) > 1 :
for one_keyword in keywords[1:] :
q_keyword = q_keyword | Q(Publisher.name__startswith(one_keyword))

all_for_keyword = Publisher.objects.filter(q_keyword)

return all_for_keyword

def get_all_filters_for_keywords(keywords) :
# Build-up the query string for keywords
q_keyword = Q(Filter.name__startswith(keywords[0]))

if len(keywords) > 1 :
for one_keyword in keywords[1:] :
q_keyword = q_keyword | Q(Filter.name__startswith(one_keyword))

all_for_keyword = Filter.objects.filter(q_keyword)

return all_for_keyword
Binary file added thefiltershop/static/images/Search.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 4881ad4

Please sign in to comment.