def all_confirmed_alike(request, game_code): country = request.query_params.get('country', 'US') game = get_object_or_404(SwitchGame, game_code_unique=game_code) alike_query = game.alike_game1 \ .filter(game1=game) \ .values('game2_id') games_ids = map(lambda x: x['game2_id'], alike_query) games_query = games_all_base_query(request.user, country) \ .filter(id__in=games_ids) \ .annotate(votes=Count('suggested_alike_game2', filter=Q(suggested_alike_game2__game1=game))) \ .order_by('-votes', 'game_title') games_list = [] for game in games_query: # If game already in games_list list, skip it if len(games_list) and \ games_list[-1]['game_code'] == game.game_code_unique: continue games_list.append(game_to_json(game, request.user)) return Response(games_list, status=status.HTTP_200_OK)
def games_get_recomended_following(request): quantity = request.query_params.get('qtd', None) offset = request.query_params.get('offset', 0) country = request.query_params.get('country', 'US') if quantity: quantity = int(quantity) if offset: offset = int(offset) following = map(lambda x: x['followed_id'], request.user.follower.values('followed_id')) today = now() date_lowerbound = today + timedelta(days=-6 * 30) game_ids = map( lambda x: x['game_id'], Recomendation.objects.filter(date__gte=date_lowerbound).filter( user__in=following).filter(recomends=True).values('game_id')) games = games_all_base_query(request.user, country) \ .filter(id__in=game_ids) \ .distinct() \ .order_by('-vote_sum')[ offset: offset + quantity if quantity else None ] response = [] for game in games: response.append(game_to_json(game, request.user)) return Response(response, status=status.HTTP_200_OK)
def games_get(request): quantity = request.query_params.get('qtd', None) offset = request.query_params.get('offset', 0) search_text = request.query_params.get('text', '') tags = request.query_params.get('tags', None) order_by = request.query_params.get('order_by', None) released_status = request.query_params.get('released', None) date_from = request.query_params.get('date_from', None) date_to = request.query_params.get('date_to', None) price_from = request.query_params.get('price_from', None) price_to = request.query_params.get('price_to', None) sales_only = request.query_params.get('sales_only', None) min_discount = request.query_params.get('min_discount', None) highlights_only = request.query_params.get('highlights_only', None) unrated_only = request.query_params.get('unrated_only', None) country = request.query_params.get('country', 'US') if quantity: quantity = int(quantity) if offset: offset = int(offset) if min_discount: min_discount = int(min_discount) if tags: tags = tags.split(',') msg = [] games = search_query( user=request.user, search_text=search_text, tags=tags, order_by=order_by, released_status=released_status, date_from=date_from, date_to=date_to, price_from=price_from, price_to=price_to, sales_only=sales_only, min_discount=min_discount, highlights_only=highlights_only, unrated_only=unrated_only, quantity=quantity, offset=offset, country=country, ) for game in games: msg.append(game_to_json(game, request.user)) return Response(msg, status=status.HTTP_200_OK)
def games_get_random_tag(request): quantity = request.query_params.get('qtd', None) offset = request.query_params.get('offset', 0) country = request.query_params.get('country', 'US') if quantity: quantity = int(quantity) if offset: offset = int(offset) # Randomly choose a tag with a minimum of 4 games tags = Tag.objects \ .annotate( games=Count( 'confirmedtag__game', filter=Q(confirmedtag__game__hide=False), distinct=True ) ) \ .filter(games__gte=16) \ .values('id', 'games') all_tags_ids = [x['id'] for x in tags] if len(all_tags_ids) == 0: return Response(status=status.HTTP_404_NOT_FOUND) random_index = randint(0, len(all_tags_ids) - 1) random_tag_id = all_tags_ids[random_index] # Return the best ranked games from the chosen tag games_ids = map( lambda x: x['game_id'], ConfirmedTag.objects.filter(tag_id=random_tag_id).values('game_id')) today = now() date_lowerbound = today + timedelta(days=-8 * 30) games_query = games_all_base_query(request.user, country) \ .filter(id__in=games_ids) \ .annotate(release_date=Greatest('release_us', 'release_eu')) \ .filter(release_date__gte=date_lowerbound) \ .order_by('-vote_sum')[ offset: offset + quantity if quantity else None] response = {'tag': Tag.objects.get(id=random_tag_id).name, 'games': []} for game in games_query: response['games'].append(game_to_json(game, request.user)) return Response(response, status=status.HTTP_200_OK)
def games_get_liked_tag(request): quantity = request.query_params.get('qtd', None) offset = request.query_params.get('offset', 0) country = request.query_params.get('country', 'US') if quantity: quantity = int(quantity) if offset: offset = int(offset) # Randomly choose a tag from a liked game liked_games_ids = map( lambda x: x['game_id'], Recomendation.objects.filter(recomends=True, user=request.user).values('game_id')) liked_tags = list( ConfirmedTag.objects.filter(game_id__in=liked_games_ids).values( 'tag_id', 'tag__name').order_by('tag_id')) if len(liked_tags) == 0: return Response(status=status.HTTP_404_NOT_FOUND) random_index = randint(0, len(liked_tags) - 1) random_tag_id = liked_tags[random_index]['tag_id'] # Return the best ranked games from the chosen tag games_ids = map( lambda x: x['game_id'], ConfirmedTag.objects.filter(tag_id=random_tag_id).values('game_id')) today = now() date_lowerbound = today + timedelta(days=-8 * 30) games_query = games_all_base_query(request.user, country) \ .filter(id__in=games_ids) \ .annotate(release_date=Greatest('release_us', 'release_eu')) \ .filter(release_date__gte=date_lowerbound) \ .order_by('-vote_sum')[offset: offset + quantity if quantity else None] response = {'tag': Tag.objects.get(id=random_tag_id).name, 'games': []} for game in games_query: response['games'].append(game_to_json(game, request.user)) return Response(response, status=status.HTTP_200_OK)
def wishlist(request): country = request.query_params.get('country', 'US') games = games_all_base_query(request.user, country) \ .filter(wishlist__user=request.user) \ .annotate(wish_date=F('wishlist__date')) \ .filter(has_wish__gte=1) \ .order_by('-wish_date') # 'has_wish' filter is necessary because 'wish_date' annotate brings # duplicates response = [] for game in games: response.append(game_to_json(game, request.user)) return Response(response, status=status.HTTP_200_OK)
def games_get_alike_to_recomended(request): country = request.query_params.get('country', 'US') liked = request.user.recomendation_set \ .filter(recomends=True) \ .filter(game__alike_game1__isnull=False) \ .values('game_id') \ .distinct() if liked.count() == 0: return Response(status=status.HTTP_404_NOT_FOUND) liked = list(liked) random_index = randint(0, len(liked) - 1) random_game_id = liked[random_index]['game_id'] chosen_game_title = games_all_base_query() \ .get(id=random_game_id).game_title alike_query = ConfirmedAlike.objects \ .filter(game1_id=random_game_id) \ .annotate(votes=Count('game2__suggested_alike_game2', filter=Q(game1_id=random_game_id))) \ .order_by('-votes') \ .values('game2_id') games_ids = map(lambda x: x['game2_id'], alike_query) games_query = games_all_base_query(request.user, country) \ .filter(id__in=games_ids) games_list = [] for game in games_query: # If game already in games_list list, skip it if len(games_list) and \ games_list[-1]['game_code'] == game.game_code_unique: continue games_list.append(game_to_json(game, request.user)) response = {'game_title': chosen_game_title, 'games': games_list} return Response(response, status=status.HTTP_200_OK)
def game_lists_results(request): country = request.query_params.get('country', 'US') user = request.user slot_dict = {} list_slots = SwitchGameListSlot.objects.all() # If user not logged, ignore logged lists if type(user) == AnonymousUser: logged_list_subquery = SwitchGameList.objects \ .filter(slot_id=OuterRef('pk'), logged_list=False) list_slots = list_slots \ .annotate(has_not_logged=Exists(logged_list_subquery)) \ .filter(has_not_logged=True) # Initialize slot_dicts for slot in list_slots \ .annotate(lists=Count('switchgamelist')) \ .filter(lists__gte=1) \ .order_by('order'): slot_dict[slot.id] = {'lists': [], 'games': [], 'title': ''} # If user not logged, ignore logged lists lists = SwitchGameList.objects.all() if type(user) == AnonymousUser: lists = lists.filter(logged_list=False) for game_list in lists: slot_dict[game_list.slot.id]['lists'].append({ 'title': game_list.title, 'query_json': game_list.query_json, 'frequency': game_list.frequency }) slots = list(map(lambda slot_id: slot_dict[slot_id], slot_dict)) # For each slot, randomly pick a list based on each list's frequency for slot in slots: max_freq = 0 for game_list in slot['lists']: max_freq = max_freq + game_list['frequency'] random_int = randint(1, max_freq) for game_list in slot['lists']: random_int = random_int - game_list['frequency'] if random_int <= 0: slot['title'] = game_list['title'] slot['json'] = game_list['query_json'] slot.pop('lists') break for slot in slots: list_json = json.loads(slot['json']) slot.pop('json') tags = list_json['tags'].split(',') if 'tags' in list_json else None order_by = list_json[ 'order_by'] if 'order_by' in list_json else '-game_title' released_status = list_json[ 'released_status'] if 'released_status' in list_json else None date_from = list_json['date_from'] if 'date_from' in list_json else None date_to = list_json['date_to'] if 'date_to' in list_json else None price_from = list_json[ 'price_from'] if 'price_from' in list_json else None price_to = list_json['price_to'] if 'price_to' in list_json else None sales_only = list_json[ 'sales_only'] if 'sales_only' in list_json else None min_discount = list_json[ 'min_discount'] if 'min_discount' in list_json else None highlights_only = list_json[ 'highlights_only'] if 'highlights_only' in list_json else None unrated_only = list_json[ 'unrated_only'] if 'unrated_only' in list_json else None quantity = list_json['qtd'] if 'qtd' in list_json else 20 # Changes price from/ to in countries with "bigger" currencies if price_from: price_from = currencyMultiplier(price_from, country) slot['title'] = slot['title'].replace( '{{price_from}}', currencyFormat(price_from, country)) if price_to: price_to = currencyMultiplier(price_to, country) slot['title'] = slot['title'].replace( '{{price_to}}', currencyFormat(price_to, country)) games = search_query( user=request.user, search_text=None, tags=tags, order_by=order_by, released_status=released_status, date_from=date_from, date_to=date_to, price_from=price_from, price_to=price_to, sales_only=sales_only, min_discount=min_discount, highlights_only=highlights_only, unrated_only=unrated_only, quantity=quantity, offset=0, country=country, ) for game in games: slot['games'].append(game_to_json(game, request.user)) return Response(slots, status=status.HTTP_200_OK)