From 876d0f02ecd094c264ac614f30ddc335858a9c59 Mon Sep 17 00:00:00 2001 From: Alain Date: Sat, 30 Sep 2023 14:17:22 +0200 Subject: [PATCH] Refactoring for better clarity, and removable of obsolete code. --- .../filtershop_main/views/__init__.py | 6 ++--- .../filtershop_main/views/indexOnlineShop.py | 14 +++++++++++ thefiltershop/filtershop_main/views/main.py | 23 +++---------------- .../filtershop_main/views/viewAGame.py | 23 ++++++++----------- 4 files changed, 29 insertions(+), 37 deletions(-) create mode 100644 thefiltershop/filtershop_main/views/indexOnlineShop.py diff --git a/thefiltershop/filtershop_main/views/__init__.py b/thefiltershop/filtershop_main/views/__init__.py index 51667df..bc24a0d 100644 --- a/thefiltershop/filtershop_main/views/__init__.py +++ b/thefiltershop/filtershop_main/views/__init__.py @@ -1,4 +1,4 @@ from .main import index -from .main import game -from .main import index_online_shops -from .main import online_shop \ No newline at end of file +from .viewAGame import game +from .indexOnlineShop import index_online_shops +from .indexOnlineShop import online_shop \ No newline at end of file diff --git a/thefiltershop/filtershop_main/views/indexOnlineShop.py b/thefiltershop/filtershop_main/views/indexOnlineShop.py new file mode 100644 index 0000000..3428a5a --- /dev/null +++ b/thefiltershop/filtershop_main/views/indexOnlineShop.py @@ -0,0 +1,14 @@ +from django.shortcuts import render +from django.shortcuts import get_object_or_404 + +from ..models import Online_Shop + +# TODO: pushed in a different file +def index_online_shops(request): + latest_shops = Online_Shop.objects.order_by("-date_creation")[:5] + context = {"latest_shops": latest_shops} + return render(request, "thefiltershop/index_online_shops.html", context) + +def online_shop(request, shop_id): + a_game = get_object_or_404(Online_Shop, pk=shop_id) + return render(request, "thefiltershop/online_shop.html", {"a_shop": a_game}) \ No newline at end of file diff --git a/thefiltershop/filtershop_main/views/main.py b/thefiltershop/filtershop_main/views/main.py index aee40b9..f6de91a 100644 --- a/thefiltershop/filtershop_main/views/main.py +++ b/thefiltershop/filtershop_main/views/main.py @@ -6,6 +6,8 @@ from django.db.models import Q from django.db.models import Max from django.db.models import Count +from django.views.generic import ListView + from datetime import datetime, timedelta, timezone from logging import Logger @@ -13,7 +15,6 @@ from ..models import Filter from ..models import Publisher from ..models import Studio -from ..models import Online_Shop from filtershop_main.constants import SPOTLIGHT_LIMIT from filtershop_main.constants import Studio_and_Publisher_Size @@ -90,22 +91,4 @@ def get_game_for_spotlight(max_size_of_studio_or_publisher) : else : # No game available... game_in_spotlight = None - return game_in_spotlight -# To do: -# -def game(request, videogame_id): - a_game = get_object_or_404(Videogame_common, pk=videogame_id) - negative_filters = Filter.objects.filter(valueforfilter__for_entity__pk = a_game.pk, valueforfilter__filter__is_positive=False) - positive_filters = Filter.objects.filter(valueforfilter__for_entity__pk = a_game.pk, valueforfilter__filter__is_positive=True) - - return render(request, "thefiltershop/game.html", {"a_game": a_game, "title_image": a_game.image_set.first(), "screenshots": a_game.image_set.all()[2:], - "negative_filters": negative_filters, "positive_filters": positive_filters}) - -def index_online_shops(request): - latest_shops = Online_Shop.objects.order_by("-date_creation")[:5] - context = {"latest_shops": latest_shops} - return render(request, "thefiltershop/index_online_shops.html", context) - -def online_shop(request, shop_id): - a_game = get_object_or_404(Online_Shop, pk=shop_id) - return render(request, "thefiltershop/online_shop.html", {"a_shop": a_game}) \ No newline at end of file + return game_in_spotlight \ No newline at end of file diff --git a/thefiltershop/filtershop_main/views/viewAGame.py b/thefiltershop/filtershop_main/views/viewAGame.py index 879da1a..77182fe 100644 --- a/thefiltershop/filtershop_main/views/viewAGame.py +++ b/thefiltershop/filtershop_main/views/viewAGame.py @@ -1,21 +1,16 @@ from django.shortcuts import render from django.http import HttpResponse from django.template import loader +from django.shortcuts import get_object_or_404 from ..models import Videogame_common -from ..models import Studio -from ..models import Publisher +from ..models import Filter -def index(request): - # For small studio first - #smallest_studio = Studio.objects.order_by("-known_popularity").order_by("-spotlight_count")[:5] +def game(request, videogame_id): + a_game = get_object_or_404(Videogame_common, pk=videogame_id) + negative_filters = Filter.objects.filter(valueforfilter__for_entity__pk = a_game.pk, valueforfilter__filter__is_positive=False) + positive_filters = Filter.objects.filter(valueforfilter__for_entity__pk = a_game.pk, valueforfilter__filter__is_positive=True) - # also with no publisher or a small one - # Find the game with lowest time in the spotlight, and the fewer (bad) filters. Give a bonus for good filters - latest_games = Videogame_common.objects.filter(Videogame_common.studios_set__size <= 5).filter(Videogame_common.publishers_set__size <= 5).order_by("-known_popularity").order_by("-spotlight_count")[:5] - for aGame in latest_games: - aGame.spotlight_count+=1 - aGame.save() - - context = {"latest_games": latest_games} - return render(request, "thefiltershop/index.html", context) \ No newline at end of file + return render(request, "thefiltershop/game.html", {"a_game": a_game, "title_image": a_game.image_set.first(), "screenshots": a_game.image_set.all()[2:], + "negative_filters": negative_filters, "positive_filters": positive_filters}) + \ No newline at end of file