-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
122 additions
and
0 deletions.
There are no files selected for viewing
Binary file not shown.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 8 additions & 0 deletions
8
thefiltershop/filtershop_main/templates/thefiltershop/base_games.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
114 changes: 114 additions & 0 deletions
114
thefiltershop/filtershop_main/views/videogames_search.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.