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>
70 lines
1.7 KiB
Python
70 lines
1.7 KiB
Python
from datetime import date
|
|
|
|
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
|
|
total = Mineral.objects.count()
|
|
|
|
if total == 0:
|
|
return render(request, 'dailystone/stone.html', {'mineral': None})
|
|
|
|
index = (day - 1) % total + 1
|
|
mineral = Mineral.objects.filter(day_of_year=index).first()
|
|
|
|
if not mineral:
|
|
minerals = list(Mineral.objects.all())
|
|
mineral = minerals[(day - 1) % len(minerals)]
|
|
|
|
return render(request, 'dailystone/stone.html', {
|
|
'mineral': mineral,
|
|
'today': today,
|
|
**_base_context(),
|
|
})
|
|
|
|
|
|
def random_stone(request):
|
|
mineral = Mineral.objects.order_by('?').first()
|
|
if not mineral:
|
|
return redirect('dailystone:daily_stone')
|
|
return render(request, 'dailystone/stone.html', {
|
|
'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(),
|
|
})
|