[boardgames] Fix manual import and add aggregations
This commit is contained in:
@ -1,13 +1,13 @@
|
|||||||
from functools import cached_property
|
|
||||||
import logging
|
import logging
|
||||||
from dataclasses import dataclass
|
from dataclasses import dataclass
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from typing import Optional, Any
|
from functools import cached_property
|
||||||
|
from typing import Any, Optional
|
||||||
from uuid import uuid4
|
from uuid import uuid4
|
||||||
|
|
||||||
from django import forms
|
|
||||||
import requests
|
import requests
|
||||||
from boardgames.sources.bgg import lookup_boardgame_from_bgg
|
from boardgames.sources.bgg import lookup_boardgame_from_bgg
|
||||||
|
from django import forms
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from django.core.files.base import ContentFile
|
from django.core.files.base import ContentFile
|
||||||
from django.db import models
|
from django.db import models
|
||||||
@ -329,7 +329,12 @@ class BoardGame(ScrobblableMixin):
|
|||||||
)
|
)
|
||||||
return game
|
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", [])
|
mechanics = bgg_data.pop("mechanics", [])
|
||||||
designers = bgg_data.pop("designers", [])
|
designers = bgg_data.pop("designers", [])
|
||||||
|
|||||||
@ -1,17 +1,23 @@
|
|||||||
from typing import Any
|
from typing import Any, Union
|
||||||
from boardgamegeek import BGGClient
|
from boardgamegeek import BGGClient
|
||||||
|
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
|
|
||||||
|
|
||||||
def lookup_boardgame_from_bgg(title: str) -> dict[str, Any]:
|
def lookup_boardgame_from_bgg(
|
||||||
game_dict = {"title": title}
|
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)
|
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:
|
if game:
|
||||||
|
game_dict["title"] = game.name
|
||||||
game_dict["description"] = game.description
|
game_dict["description"] = game.description
|
||||||
game_dict["published_year"] = game.yearpublished
|
game_dict["published_year"] = game.yearpublished
|
||||||
game_dict["cover_url"] = game.image
|
game_dict["cover_url"] = game.image
|
||||||
|
|||||||
@ -1,11 +1,68 @@
|
|||||||
|
import datetime
|
||||||
|
from django.utils import timezone
|
||||||
from django.views import generic
|
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
|
from scrobbles.views import ScrobbleableListView, ScrobbleableDetailView
|
||||||
|
|
||||||
|
|
||||||
class BoardGameListView(ScrobbleableListView):
|
class BoardGameListView(ScrobbleableListView):
|
||||||
model = BoardGame
|
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):
|
class BoardGameDetailView(ScrobbleableDetailView):
|
||||||
model = BoardGame
|
model = BoardGame
|
||||||
|
|||||||
@ -12,6 +12,52 @@
|
|||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
|
||||||
{% block lists %}
|
{% 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="row">
|
||||||
|
|
||||||
<div class="col-md">
|
<div class="col-md">
|
||||||
|
|||||||
Reference in New Issue
Block a user