diff --git a/vrobbler/apps/boardgames/models.py b/vrobbler/apps/boardgames/models.py index c1aa8ff..a1403cd 100644 --- a/vrobbler/apps/boardgames/models.py +++ b/vrobbler/apps/boardgames/models.py @@ -1,13 +1,13 @@ -from functools import cached_property import logging from dataclasses import dataclass from datetime import datetime -from typing import Optional, Any +from functools import cached_property +from typing import Any, Optional from uuid import uuid4 -from django import forms import requests from boardgames.sources.bgg import lookup_boardgame_from_bgg +from django import forms from django.conf import settings from django.core.files.base import ContentFile from django.db import models @@ -329,7 +329,12 @@ class BoardGame(ScrobblableMixin): ) return game - bgg_data = lookup_boardgame_from_bgg(data.get("name")) + if data.get("name"): + bgg_data = lookup_boardgame_from_bgg(title=data.get("name")) + if data.get("bgg_id"): + bgg_data = lookup_boardgame_from_bgg(lookup_id=data.get("bgg_id")) + else: + bgg_data = lookup_boardgame_from_bgg(lookup_id=lookup_id) mechanics = bgg_data.pop("mechanics", []) designers = bgg_data.pop("designers", []) diff --git a/vrobbler/apps/boardgames/sources/bgg.py b/vrobbler/apps/boardgames/sources/bgg.py index fff0ae2..1813daf 100644 --- a/vrobbler/apps/boardgames/sources/bgg.py +++ b/vrobbler/apps/boardgames/sources/bgg.py @@ -1,17 +1,23 @@ -from typing import Any +from typing import Any, Union from boardgamegeek import BGGClient from django.conf import settings -def lookup_boardgame_from_bgg(title: str) -> dict[str, Any]: - game_dict = {"title": title} +def lookup_boardgame_from_bgg( + lookup_id: str | None = None, title: str | None = None +) -> dict[str, Any]: + game_dict: dict[str, Any] = {} bgg = BGGClient(access_token=settings.BGG_ACCESS_TOKEN) - game = bgg.game(title) + if lookup_id: + game = bgg.game(game_id=lookup_id) + else: + game = bgg.game(lookup_id) if game: + game_dict["title"] = game.name game_dict["description"] = game.description game_dict["published_year"] = game.yearpublished game_dict["cover_url"] = game.image diff --git a/vrobbler/apps/boardgames/views.py b/vrobbler/apps/boardgames/views.py index c2ffce2..adff3ed 100644 --- a/vrobbler/apps/boardgames/views.py +++ b/vrobbler/apps/boardgames/views.py @@ -1,11 +1,68 @@ +import datetime +from django.utils import timezone from django.views import generic -from boardgames.models import BoardGame, BoardGamePublisher +from boardgames.models import BoardGame, BoardGameDesigner, BoardGamePublisher +from scrobbles.models import Scrobble from scrobbles.views import ScrobbleableListView, ScrobbleableDetailView class BoardGameListView(ScrobbleableListView): model = BoardGame + def get_context_data(self, **kwargs): + context_data = super().get_context_data(**kwargs) + user = self.request.user + now = timezone.now() + start_day_of_week = now - datetime.timedelta(days=now.weekday()) + start_day_of_month = now.replace(day=1) + + scrobbles_this_week = Scrobble.objects.filter( + user=user, + board_game__isnull=False, + timestamp__gte=start_day_of_week, + ).select_related("board_game") + + scrobbles_this_month = Scrobble.objects.filter( + user=user, + board_game__isnull=False, + timestamp__gte=start_day_of_month, + ).select_related("board_game") + + designers_this_week = {} + for scrobble in scrobbles_this_week: + for designer in scrobble.board_game.designers.all(): + designers_this_week[designer.id] = { + "designer": designer, + "count": designers_this_week.get(designer.id, {}).get( + "count", 0 + ) + + 1, + } + + designers_this_month = {} + for scrobble in scrobbles_this_month: + for designer in scrobble.board_game.designers.all(): + designers_this_month[designer.id] = { + "designer": designer, + "count": designers_this_month.get(designer.id, {}).get( + "count", 0 + ) + + 1, + } + + context_data["designers_this_week"] = sorted( + designers_this_week.values(), + key=lambda x: x["count"], + reverse=True, + ) + context_data["designers_this_month"] = sorted( + designers_this_month.values(), + key=lambda x: x["count"], + reverse=True, + ) + + return context_data + class BoardGameDetailView(ScrobbleableDetailView): model = BoardGame diff --git a/vrobbler/templates/boardgames/boardgame_list.html b/vrobbler/templates/boardgames/boardgame_list.html index 9172803..8856c36 100644 --- a/vrobbler/templates/boardgames/boardgame_list.html +++ b/vrobbler/templates/boardgames/boardgame_list.html @@ -12,6 +12,52 @@ {% endblock %} {% block lists %} +{% if designers_this_week or designers_this_month %} +
+ {% if designers_this_week or designers_this_month %} +
+

Designers

+ + {% if designers_this_week %} +

This Week

+
+ + + {% for item in designers_this_week %} + + + + + {% empty %} + + {% endfor %} + +
{{item.designer}}{{item.count}} games
No board games played this week
+
+ {% endif %} + + {% if designers_this_month %} +

This Month

+
+ + + {% for item in designers_this_month %} + + + + + {% empty %} + + {% endfor %} + +
{{item.designer}}{{item.count}} games
No board games played this month
+
+ {% endif %} +
+ {% endif %} +
+{% endif %} +