Api Team list and Teamdetail:
- team/<id> with more Informations (Statistics) - team/ with auftritte_count Team Detail: - Added Wikipedialink Badge. - Added Profile Picture captched from Wikipedia.
This commit is contained in:
1 parent
126ac88b92
commit
115cf72a81
4 files changed
+228
-39
No files matched your search
+55
@@ -8,6 +8,56 @@ from django.utils import timezone
|
||||
from collections import OrderedDict
|
||||
import random
|
||||
|
||||
# Kleiner Helfer: Versucht, ein Thumbnail und Seiten-URL von Wikipedia zu holen.
|
||||
# Nutzung von stdlib (urllib) um Abhängigkeiten zu vermeiden.
|
||||
import urllib.request
|
||||
import urllib.parse
|
||||
import json
|
||||
|
||||
def _get_wikipedia_image(name, request=None):
|
||||
"""Versucht, ein Thumbnail aus der DE- oder EN-Wikipedia zu holen.
|
||||
Gibt ein Tupel (thumbnail_url, page_url) zurück; fehlende Werte sind None.
|
||||
Der User-Agent verwendet entweder request.get_host() (falls request übergeben wurde)
|
||||
oder settings.SITE_DOMAIN als Fallback.
|
||||
"""
|
||||
if not name:
|
||||
return (None, None)
|
||||
title = name.strip().replace(' ', '_')
|
||||
quoted = urllib.parse.quote(title)
|
||||
|
||||
# Bestimme Host für User-Agent
|
||||
host = None
|
||||
if request is not None and hasattr(request, 'get_host'):
|
||||
try:
|
||||
host = request.get_host().split(':')[0]
|
||||
except Exception:
|
||||
host = None
|
||||
if not host:
|
||||
host = getattr(settings, 'SITE_DOMAIN', 'gd.pi.lan')
|
||||
|
||||
user_agent = f"{host}/1.0 (+https://{host})"
|
||||
headers = {'User-Agent': user_agent}
|
||||
|
||||
for lang in ('de', 'en'):
|
||||
url = (
|
||||
f"https://{lang}.wikipedia.org/w/api.php?"
|
||||
f"action=query&titles={quoted}&prop=pageimages|info&inprop=url&format=json&pithumbsize=400"
|
||||
)
|
||||
try:
|
||||
req = urllib.request.Request(url, headers=headers)
|
||||
with urllib.request.urlopen(req, timeout=6) as resp:
|
||||
data = json.load(resp)
|
||||
except Exception:
|
||||
continue
|
||||
pages = data.get('query', {}).get('pages', {})
|
||||
for p in pages.values():
|
||||
page_url = p.get('fullurl') or p.get('canonicalurl')
|
||||
thumb = p.get('thumbnail')
|
||||
thumb_url = thumb.get('source') if thumb else None
|
||||
if page_url or thumb_url:
|
||||
return (thumb_url, page_url)
|
||||
return (None, None)
|
||||
|
||||
def dashboard(request):
|
||||
# --- BASIS STATISTIKEN ---
|
||||
anzahl_folgen = Folge.objects.count()
|
||||
@@ -271,6 +321,9 @@ def team_member_detail(request, pk):
|
||||
quote_gesamt = (geloeste_antworten.count() / fragen_moeglich * 100) if fragen_moeglich > 0 else 0
|
||||
quote_solo = (solo_loesungen_count / fragen_moeglich * 100) if fragen_moeglich > 0 else 0
|
||||
|
||||
# Wikipedia-Thumbnail und Seiten-URL holen (falls vorhanden) — dynamischer User-Agent über request
|
||||
wiki_image_url, wiki_page_url = _get_wikipedia_image(member.name, request)
|
||||
|
||||
return render(request, "gd/team_member_detail.html", {
|
||||
"member": member,
|
||||
"historie_nach_jahr": historie_nach_jahr,
|
||||
@@ -280,6 +333,8 @@ def team_member_detail(request, pk):
|
||||
"auftritte_count": alle_auftritte.count(),
|
||||
"quote_gesamt": round(quote_gesamt, 1),
|
||||
"quote_solo": round(quote_solo, 1),
|
||||
"wiki_image_url": wiki_image_url,
|
||||
"wiki_page_url": wiki_page_url,
|
||||
})
|
||||
|
||||
def search(request):
|
||||
|
||||
Reference in new issue
Block a user