[boardgames] Fix manual import and add aggregations
Some checks failed
build & deploy / deploy (push) Has been cancelled
build & deploy / test (push) Has been cancelled

This commit is contained in:
2026-03-18 10:47:47 -04:00
parent 2e7d6364a2
commit 6d25e5f663
4 changed files with 123 additions and 9 deletions

View File

@ -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", [])

View File

@ -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

View File

@ -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

View File

@ -12,6 +12,52 @@
{% endblock %}
{% block lists %}
{% if designers_this_week or designers_this_month %}
<div class="row">
{% if designers_this_week or designers_this_month %}
<div class="col-md">
<h2>Designers</h2>
{% if designers_this_week %}
<h3>This Week</h3>
<div class="table-responsive">
<table class="table table-striped table-sm">
<tbody>
{% for item in designers_this_week %}
<tr>
<td>{{item.designer}}</td>
<td>{{item.count}} games</td>
</tr>
{% empty %}
<tr><td>No board games played this week</td></tr>
{% endfor %}
</tbody>
</table>
</div>
{% endif %}
{% if designers_this_month %}
<h3>This Month</h3>
<div class="table-responsive">
<table class="table table-striped table-sm">
<tbody>
{% for item in designers_this_month %}
<tr>
<td>{{item.designer}}</td>
<td>{{item.count}} games</td>
</tr>
{% empty %}
<tr><td>No board games played this month</td></tr>
{% endfor %}
</tbody>
</table>
</div>
{% endif %}
</div>
{% endif %}
</div>
{% endif %}
<div class="row">
<div class="col-md">