Files
links/invest/template_views.py
T
2026-04-18 22:24:17 +10:00

67 lines
2.3 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""Template views for the invest app."""
import logging
from django.shortcuts import render, get_object_or_404
from django.utils import timezone
from .models import Portfolio
from .services import get_portfolio_value, get_weekly_overview, get_all_holdings, get_performance_chart_data
logger = logging.getLogger(__name__)
def dashboard(request):
"""Landing page: weekly snapshot overview + per-portfolio table."""
overview = get_weekly_overview()
# Determine current Australian financial year (JulJun)
now = timezone.now()
fy_start = now.year if now.month >= 7 else now.year - 1
fy_label = f"FY {str(fy_start)[2:]}-{str(fy_start + 1)[2:]}"
all_holdings = get_all_holdings()
# Merge snapshot data (value, change, change_pct) into each holdings group
rows_by_id = {row['portfolio'].id: row for row in overview.get('portfolio_rows', [])}
for group in all_holdings:
row = rows_by_id.get(group['portfolio'].id, {})
group['this_week_value'] = row.get('this_week_value')
group['change'] = row.get('change')
group['change_pct'] = row.get('change_pct')
group['position_count'] = row.get('position_count', len(group['holdings']))
return render(request, 'invest/dashboard.html', {
'overview': overview,
'fy_label': fy_label,
'all_holdings': all_holdings,
'chart_data_json': get_performance_chart_data() or 'null',
})
def portfolio_detail(request, pk):
"""Portfolio detail: live holdings, no cost/P&L."""
portfolio = get_object_or_404(Portfolio, pk=pk)
try:
summary = get_portfolio_value(portfolio)
except Exception as exc:
logger.error("get_portfolio_value failed for %s: %s", pk, exc)
summary = {
'portfolio_id': portfolio.id,
'portfolio_name': portfolio.name,
'holdings': [],
'total_value': 0,
}
return render(request, 'invest/portfolio_detail.html', {
'portfolio': portfolio,
'summary': summary,
})
def portfolio_transactions(request, pk):
"""Transaction history for a portfolio."""
portfolio = get_object_or_404(Portfolio, pk=pk)
transactions = portfolio.transactions.all().order_by('-date', '-created_at')
return render(request, 'invest/transactions.html', {
'portfolio': portfolio,
'transactions': transactions,
})