replace all usages of datetime with django.timezone and timedelta with relativedelta
This commit is contained in:
@@ -1,9 +1,8 @@
|
||||
from django.contrib import admin
|
||||
from django.urls import reverse_lazy
|
||||
from django.utils.safestring import mark_safe
|
||||
|
||||
from financeplanner.models import Transaction, Balance
|
||||
from financeplanner.utils import format_price, get_transaction_progress
|
||||
from financeplanner.utils import format_price
|
||||
|
||||
|
||||
class AdminSite(admin.AdminSite):
|
||||
@@ -23,35 +22,9 @@ def amount(obj):
|
||||
amount.short_description = "amount"
|
||||
|
||||
|
||||
def progress(transaction):
|
||||
total, paid = get_transaction_progress(transaction)
|
||||
if total:
|
||||
percentage = int((paid / total) * 100)
|
||||
else:
|
||||
percentage = 0
|
||||
outer_style = (
|
||||
"width: 90%;",
|
||||
"height: 10px;",
|
||||
"background-color: #dd4646;",
|
||||
)
|
||||
inner_style = (
|
||||
f"width: {percentage}%;",
|
||||
"height: 100%;",
|
||||
"background-color: #70bf2b;",
|
||||
)
|
||||
return mark_safe(
|
||||
f'<div style="{" ".join(outer_style)}">'
|
||||
f'<div style="{" ".join(inner_style)}"></div>'
|
||||
f'</div>'
|
||||
)
|
||||
|
||||
|
||||
progress.short_description = "progress"
|
||||
|
||||
|
||||
@admin.register(Transaction, site=admin_site)
|
||||
class TransactionAdmin(admin.ModelAdmin):
|
||||
list_display = ("subject", amount, "booking_date", "recurring_months", "not_recurring_after", progress)
|
||||
list_display = ("subject", amount, "booking_date", "recurring_months", "not_recurring_after")
|
||||
|
||||
|
||||
@admin.register(Balance, site=admin_site)
|
||||
|
||||
@@ -1,11 +1,12 @@
|
||||
from datetime import datetime, timedelta
|
||||
from decimal import Decimal
|
||||
|
||||
from dateutil.relativedelta import relativedelta
|
||||
from django.contrib.auth import get_user_model
|
||||
from django.core.management import BaseCommand
|
||||
|
||||
from financeplanner.management.commands.createadmin import USERNAME
|
||||
from financeplanner.models import Transaction, Balance
|
||||
from financeplanner.utils import current_date
|
||||
|
||||
User = get_user_model()
|
||||
|
||||
@@ -15,29 +16,29 @@ class Command(BaseCommand):
|
||||
def handle(self, *args, **options):
|
||||
user = User.objects.get(**{User.USERNAME_FIELD: USERNAME})
|
||||
|
||||
today = datetime.now().date()
|
||||
today = current_date()
|
||||
|
||||
Transaction.objects.create(
|
||||
user=user,
|
||||
subject="simple transaction some days ago",
|
||||
amount=Decimal("34.95"),
|
||||
booking_date=today - timedelta(days=5),
|
||||
booking_date=today - relativedelta(days=5),
|
||||
)
|
||||
|
||||
Transaction.objects.create(
|
||||
user=user,
|
||||
subject="simple transaction a week ahead",
|
||||
amount=Decimal("66.66"),
|
||||
booking_date=today + timedelta(weeks=1),
|
||||
booking_date=today + relativedelta(weeks=1),
|
||||
)
|
||||
|
||||
Transaction.objects.create(
|
||||
user=user,
|
||||
subject="recurring stuff ending soon",
|
||||
amount=Decimal("49"),
|
||||
booking_date=today - timedelta(days=180),
|
||||
booking_date=today - relativedelta(days=180),
|
||||
recurring_months=1,
|
||||
not_recurring_after=today + timedelta(weeks=6)
|
||||
not_recurring_after=today + relativedelta(weeks=6)
|
||||
)
|
||||
|
||||
Transaction.objects.create(
|
||||
@@ -45,23 +46,23 @@ class Command(BaseCommand):
|
||||
subject="recurring stuff beginning soon",
|
||||
amount=Decimal("30"),
|
||||
recurring_months=2,
|
||||
booking_date=today + timedelta(weeks=2),
|
||||
booking_date=today + relativedelta(weeks=2),
|
||||
)
|
||||
|
||||
Balance.objects.create(
|
||||
user=user,
|
||||
date=today - timedelta(weeks=7),
|
||||
date=today - relativedelta(weeks=7),
|
||||
amount=Decimal("600"),
|
||||
)
|
||||
|
||||
Balance.objects.create(
|
||||
user=user,
|
||||
date=today - timedelta(weeks=3),
|
||||
date=today - relativedelta(weeks=3),
|
||||
amount=Decimal("380"),
|
||||
)
|
||||
|
||||
Balance.objects.create(
|
||||
user=user,
|
||||
date=today - timedelta(days=2),
|
||||
date=today - relativedelta(days=2),
|
||||
amount=Decimal("450"),
|
||||
)
|
||||
|
||||
@@ -1,11 +1,10 @@
|
||||
from datetime import datetime
|
||||
from decimal import Decimal
|
||||
|
||||
from dateutil.relativedelta import relativedelta
|
||||
from django.utils import timezone
|
||||
|
||||
|
||||
def current_date():
|
||||
return datetime.now().date()
|
||||
return timezone.now().date()
|
||||
|
||||
|
||||
def round_with_dec_places(number, places):
|
||||
@@ -19,35 +18,3 @@ def format_price(number):
|
||||
if number is None:
|
||||
return None
|
||||
return f"{round_with_dec_places(number, 2)} €"
|
||||
|
||||
|
||||
def _count_rates(start, end, months_delta):
|
||||
delta = relativedelta(months=months_delta)
|
||||
count = 0
|
||||
current = start
|
||||
while current <= end:
|
||||
count += 1
|
||||
current += delta
|
||||
return count
|
||||
|
||||
|
||||
def get_transaction_progress(transaction):
|
||||
if rec_months := transaction.recurring_months:
|
||||
start = transaction.booking_date
|
||||
current = current_date()
|
||||
if end := transaction.not_recurring_after:
|
||||
total = _count_rates(start, end, rec_months)
|
||||
if current >= end:
|
||||
paid = total
|
||||
else:
|
||||
paid = _count_rates(start, current, rec_months)
|
||||
else:
|
||||
total = None
|
||||
paid = _count_rates(start, current, rec_months)
|
||||
else:
|
||||
total = 1
|
||||
if transaction.booking_date < current_date():
|
||||
paid = 1
|
||||
else:
|
||||
paid = 0
|
||||
return total, paid
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
from datetime import datetime
|
||||
from decimal import Decimal
|
||||
from math import floor
|
||||
|
||||
@@ -7,6 +6,8 @@ from django.contrib.auth.decorators import login_required
|
||||
from django.db.models import Q
|
||||
from django.shortcuts import render
|
||||
|
||||
from financeplanner.utils import current_date
|
||||
|
||||
|
||||
def _get_relevant_balances(user, range_start, range_end):
|
||||
balance_list = []
|
||||
@@ -144,7 +145,7 @@ def _build_graph_data(range_start, range_end, calculation_start, balance_list, a
|
||||
...
|
||||
}
|
||||
"""
|
||||
today = datetime.now().date()
|
||||
today = current_date()
|
||||
stats = _calculate_daily_stats(calculation_start, range_end, balance_list, actual_transactions)
|
||||
stats = _trim_stats(stats, range_start)
|
||||
amount_limit = _calculate_scale(stats)
|
||||
@@ -179,7 +180,7 @@ def _build_graph_data(range_start, range_end, calculation_start, balance_list, a
|
||||
def _calculate_analysis(user):
|
||||
result = {}
|
||||
|
||||
today = datetime.now().date()
|
||||
today = current_date()
|
||||
booked = Q(booking_date__lte=today)
|
||||
outgoing = Q(amount__gt=0)
|
||||
incoming = Q(amount__lt=0)
|
||||
@@ -200,9 +201,9 @@ def _calculate_analysis(user):
|
||||
@login_required
|
||||
def index(request):
|
||||
user = request.user
|
||||
today = datetime.now().date()
|
||||
range_start = today - relativedelta(days=30)
|
||||
range_end = today + relativedelta(days=50)
|
||||
today = current_date()
|
||||
range_start = today - relativedelta(months=6)
|
||||
range_end = today + relativedelta(months=6)
|
||||
balance_list = _get_relevant_balances(user, range_start, range_end)
|
||||
calculation_start = balance_list[0].date if balance_list else range_start
|
||||
transaction_list = _get_relevant_transactions(user, calculation_start, range_end)
|
||||
@@ -213,9 +214,9 @@ def index(request):
|
||||
graph_data = None
|
||||
analysis = _calculate_analysis(user)
|
||||
context = {
|
||||
"today": today,
|
||||
"range_start": range_start,
|
||||
"range_end": range_end,
|
||||
"today": today,
|
||||
"balance_list": balance_list,
|
||||
"transaction_list": transaction_list,
|
||||
"actual_transactions": sorted(actual_transactions, key=lambda t: t[0]),
|
||||
|
||||
Reference in New Issue
Block a user