Add mineral search and permalink pages
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
- Search bar toggle in header (magnifying glass icon) - /daily-stone/search/?q= endpoint with results list - /daily-stone/mineral/<id>/ permalink for each mineral - Mineral count shown in footer Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -1,11 +1,14 @@
|
||||
from datetime import date
|
||||
|
||||
from django.shortcuts import render, redirect
|
||||
from django.urls import reverse
|
||||
from django.shortcuts import render, redirect, get_object_or_404
|
||||
|
||||
from .models import Mineral
|
||||
|
||||
|
||||
def _base_context():
|
||||
return {'total_minerals': Mineral.objects.count()}
|
||||
|
||||
|
||||
def daily_stone(request):
|
||||
today = date.today()
|
||||
day = today.timetuple().tm_yday
|
||||
@@ -14,11 +17,9 @@ def daily_stone(request):
|
||||
if total == 0:
|
||||
return render(request, 'dailystone/stone.html', {'mineral': None})
|
||||
|
||||
# Wrap around if we have fewer minerals than days in the year
|
||||
index = (day - 1) % total + 1
|
||||
mineral = Mineral.objects.filter(day_of_year=index).first()
|
||||
|
||||
# Fallback: pick by modulo of pk list
|
||||
if not mineral:
|
||||
minerals = list(Mineral.objects.all())
|
||||
mineral = minerals[(day - 1) % len(minerals)]
|
||||
@@ -26,6 +27,7 @@ def daily_stone(request):
|
||||
return render(request, 'dailystone/stone.html', {
|
||||
'mineral': mineral,
|
||||
'today': today,
|
||||
**_base_context(),
|
||||
})
|
||||
|
||||
|
||||
@@ -37,4 +39,31 @@ def random_stone(request):
|
||||
'mineral': mineral,
|
||||
'today': date.today(),
|
||||
'is_random': True,
|
||||
**_base_context(),
|
||||
})
|
||||
|
||||
|
||||
def mineral_detail(request, pk):
|
||||
mineral = get_object_or_404(Mineral, pk=pk)
|
||||
return render(request, 'dailystone/stone.html', {
|
||||
'mineral': mineral,
|
||||
'today': date.today(),
|
||||
**_base_context(),
|
||||
})
|
||||
|
||||
|
||||
def search_minerals(request):
|
||||
query = request.GET.get('q', '').strip()
|
||||
if not query:
|
||||
return redirect('dailystone:daily_stone')
|
||||
|
||||
results = Mineral.objects.filter(name__icontains=query)
|
||||
|
||||
if results.count() == 1:
|
||||
return redirect('dailystone:mineral_detail', pk=results.first().pk)
|
||||
|
||||
return render(request, 'dailystone/search.html', {
|
||||
'query': query,
|
||||
'results': results,
|
||||
**_base_context(),
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user