Skip to content

Commit

Permalink
Use the local database to determine related anime.
Browse files Browse the repository at this point in the history
This means that unmatched shows won't show up as related to matched ones
any more, but it also means we won't get false-positive relation matches
on, for instance, the page for Speed Racer. Instead, it'll point you at
Yatterman, which will then point you at Time Bokan.
  • Loading branch information
colons committed Feb 14, 2024
1 parent e3406ca commit ee126fe
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 38 deletions.
18 changes: 17 additions & 1 deletion nkdsu/apps/vote/anime.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from __future__ import annotations

import hashlib
import os
from itertools import chain
from typing import Literal, Optional
from typing import Iterable, Literal, Optional
from urllib.parse import urlparse

from django.conf import settings
Expand Down Expand Up @@ -36,6 +38,7 @@ class Anime(BaseModel):
thumbnail: str
synonyms: list[str]
sources: list[str]
relations: list[str]
anime_season: Season
type: Literal['MOVIE', 'ONA', 'OVA', 'SPECIAL', 'TV', 'UNKNOWN']

Expand Down Expand Up @@ -78,6 +81,19 @@ def urls(self) -> list[tuple[str, str]]:
key=lambda u: u[0],
)

def related_anime(self) -> Iterable[str]:
from .models import Track

return (
title
for title, anime in (
(anime_title, get_anime(anime_title))
for anime_title in Track.all_anime_titles()
)
if anime is not None
and any((source in self.relations for source in anime.sources))
)


by_title: dict[str, Anime] = {}
by_synonym: dict[str, Anime] = {}
Expand Down
17 changes: 11 additions & 6 deletions nkdsu/apps/vote/views/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
)

from nkdsu.mixins import MarkdownView
from ..anime import suggest_anime
from ..anime import get_anime, suggest_anime
from ..forms import BadMetadataForm, DarkModeForm, RequestForm, VoteForm
from ..models import (
Profile,
Expand Down Expand Up @@ -530,14 +530,19 @@ def get_context_data(self, **kwargs) -> dict[str, Any]:
),
key=lambda rt: rt[0],
)
anime_data = get_anime(self.kwargs['anime'])
related_anime = (
list(anime_data.related_anime())
if anime_data is not None
else context['tracks'][0]
.role_details_for_anime(self.kwargs['anime'])[0]
.related_anime
)
context.update({
'anime': self.kwargs['anime'],
'role_tracks': role_tracks,
'related_anime': (
context['tracks'][0]
.role_details_for_anime(self.kwargs['anime'])[0]
.related_anime
),
'anime_data': anime_data,
'related_anime': related_anime,
})
return context

Expand Down
44 changes: 13 additions & 31 deletions nkdsu/templates/anime_detail.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,38 +11,20 @@

{% block content %}

{% with anime|anime as anime_detail %}
{% if anime_detail %}
<div class="anime-detail-header">
<img class="anime-picture" src="{% url "vote:anime-picture" anime=anime %}" alt=""/>

<div class="detail">
<h2>{{ anime }}</h2>
{% include "include/anime.html" with anime=anime_detail %}
{% if related_anime %}
{# replace this to use the relations from the offline db #}
<p class="related-anime">
related anime:
{% for anime in related_anime %}
<a href="{% url "vote:anime" anime=anime %}">{{ anime }}</a>{% if not forloop.last %},{% endif %}
{% endfor %}
</p>
{% endif %}
</div>
{% if anime_data %}
<div class="anime-detail-header">
<img class="anime-picture" src="{% url "vote:anime-picture" anime=anime %}" alt=""/>

<div class="detail">
<h2>{{ anime }}</h2>
{% include "include/anime.html" with anime=anime_data %}
{% include "include/related_anime.html" %}
</div>
{% else %}
<h2>{{ anime }}</h2>

{% if related_anime %}
<p class="related-anime">
related anime:
{% for anime in related_anime %}
<a href="{% url "vote:anime" anime=anime %}">{{ anime }}</a>{% if not forloop.last %},{% endif %}
{% endfor %}
</p>
{% endif %}
{% endif %}
{% endwith %}
</div>
{% else %}
<h2>{{ anime }}</h2>
{% include "include/related_anime.html" %}
{% endif %}

{% regroup role_tracks by 0.plural as roles %}

Expand Down
8 changes: 8 additions & 0 deletions nkdsu/templates/include/related_anime.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{% if related_anime %}
<p class="related-anime">
related anime:
{% for anime in related_anime %}
<a href="{% url "vote:anime" anime=anime %}">{{ anime }}</a>{% if not forloop.last %},{% endif %}
{% endfor %}
</p>
{% endif %}

0 comments on commit ee126fe

Please sign in to comment.