From f64e4ecaf4c7c266c8e675bfe65efa2de442ab89 Mon Sep 17 00:00:00 2001 From: Dag7 Date: Tue, 14 May 2024 20:11:27 +0200 Subject: [PATCH] feat: random entry --- hhub/views.py | 99 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 99 insertions(+) diff --git a/hhub/views.py b/hhub/views.py index 053cfbd..0349763 100644 --- a/hhub/views.py +++ b/hhub/views.py @@ -1,4 +1,5 @@ import json +import random from django.core.exceptions import FieldError from django.core.paginator import EmptyPage, PageNotAnInteger, Paginator @@ -78,6 +79,104 @@ def entries_all(request): return JsonResponse(serializer.data, safe=False) +def random_entries(request): + """ + Returns a random entry matching some condition. + + Similar to search + """ + # Parse query params, providing defaults + # Filters + developer = request.GET.get("developer", "") + typetag = request.GET.get("typetag", "") + tags = request.GET.get("tags", "") + platform = request.GET.get("platform", "") + + # Pagination + # Request a specific page + page = request.GET.get("page", "") + # Request a specific number of results (>1,<30) + num_elements = request.GET.get("results", 10) + + # Order and sort + order_by_param = request.GET.get("order_by", "") + sort_by_param = request.GET.get("sort", "") + + # Start by selecting everything + entries = Entry.objects.all() + + # Boundaries for elements per page + # if num_elements <= 1: + # num_elements = 1 + # elif num_elements >= 30: + # num_elements = 30 + + # change 3 to how many random items you want + entries = random.sample(list(entries), int(num_elements)) + + # sort and order + if sort_by_param: + entries = sort_and_order(entries, order_by_param, sort_by_param) + + if developer: + entries = entries.filter(developer=developer) + + if platform: + entries = entries.filter(platform=platform) + + if typetag: + entries = entries.filter(typetag=typetag) + + if tags: + # Read the value of tags as an array of tags separated by commas + tags = tags.split(",") + entries = entries.filter(tags__contains=tags) + + results = len(entries) + + # Prepare paginators and number of results + paginator = Paginator(entries, num_elements) + + # Request the desired page of results + try: + entries = paginator.page(page) + except PageNotAnInteger: + entries = paginator.page(1) + page = 1 + except EmptyPage: + entries = paginator.page(paginator.num_pages) + page = paginator.num_pages + + serializer = EntrySerializer(entries, many=True) + + # Read from disks the manifests of the result entries + json_entries = [] + for entry in entries: + data = open(f"db-sources/{entry.basepath}/{entry.slug}/game.json").read() + json_data = json.loads(data) + # Enrich the manifest with some values available only in the (postgres) database + json_data["basepath"] = entry.basepath + json_entries.append(json_data) + + # Prepare final JSON response + return JsonResponse( + { + # total number of results from the query + "results": results, + # total number of pages + "page_total": paginator.num_pages, + # current request page + "page_current": page, + # number of elements in this page + "page_elements": len(serializer.data), + # array of entries manifests + "entries": json_entries, + }, + # Allow non-dict instances to be passed and serialized + safe=False, + ) + + def search_entries(request): """ Returns every entry matching the conditions given in the query