use timezone aware datetime

This commit is contained in:
2025-09-06 10:33:12 +02:00
parent d4b40b1967
commit 4e2ecd8e7e
3 changed files with 9 additions and 3 deletions
+3 -1
View File
@@ -3,6 +3,7 @@ from apps.transactions.models import Transaction
from apps.rates.models import ExchangeRate from apps.rates.models import ExchangeRate
from decimal import Decimal from decimal import Decimal
import datetime import datetime
from django.utils import timezone
class Calc: class Calc:
def ShowDiffRate(self, DayDiff, CurrentRate, Crypto, Fiat): def ShowDiffRate(self, DayDiff, CurrentRate, Crypto, Fiat):
@@ -135,7 +136,8 @@ class Calc:
amount_within_past_year = 0 amount_within_past_year = 0
with connection.cursor() as cursor: with connection.cursor() as cursor:
sql = """SELECT SUM(crypto_amount) as amount FROM `transaction` WHERE transaction_type='B' and crypto_currency=%s and owner_id=%s and date_valid>%s""" sql = """SELECT SUM(crypto_amount) as amount FROM `transaction` WHERE transaction_type='B' and crypto_currency=%s and owner_id=%s and date_valid>%s"""
cursor.execute(sql, [crypto, userid, datetime.datetime.now() - datetime.timedelta(days=365)]) date_valid = timezone.make_aware(datetime.datetime.now() - datetime.timedelta(days=365), timezone=timezone.get_current_timezone())
cursor.execute(sql, [crypto, userid, date_valid])
bought = cursor.fetchone() bought = cursor.fetchone()
out["bought_recently"] = None out["bought_recently"] = None
if bought[0]: if bought[0]:
+3 -1
View File
@@ -1,6 +1,7 @@
from django import forms from django import forms
import sys import sys
import datetime import datetime
from django.utils import timezone
from apps.transactions.models import Transaction from apps.transactions.models import Transaction
from apps.rates.models import ExchangeRate from apps.rates.models import ExchangeRate
@@ -19,7 +20,8 @@ class TransactionForm(forms.ModelForm):
# do not run this code in initial migration, because the database has not been built yet # do not run this code in initial migration, because the database has not been built yet
if not "migrate" in sys.argv: if not "migrate" in sys.argv:
exchangerates = ExchangeRate.objects.filter(datetime_valid__gt = datetime.datetime.today() - datetime.timedelta(days=7)) datetime_valid = timezone.make_aware(datetime.datetime.today() - datetime.timedelta(days=7), timezone=timezone.get_current_timezone())
exchangerates = ExchangeRate.objects.filter(datetime_valid__gt = datetime_valid)
crypto_currencies = [(x, x) for x in sorted({ex.crypto_currency for ex in exchangerates})] crypto_currencies = [(x, x) for x in sorted({ex.crypto_currency for ex in exchangerates})]
fiat_currencies = [(x, x) for x in sorted({ex.fiat_currency for ex in exchangerates})] fiat_currencies = [(x, x) for x in sorted({ex.fiat_currency for ex in exchangerates})]
+3 -1
View File
@@ -6,6 +6,7 @@ from apps.transactions.forms import ImportForm
from apps.transactions.models import Transaction from apps.transactions.models import Transaction
from apps.transactions.importbtcde import ImportBtcDe from apps.transactions.importbtcde import ImportBtcDe
import datetime import datetime
from django.utils import timezone
import socket import socket
import xlwt import xlwt
from django.http import HttpResponse from django.http import HttpResponse
@@ -67,8 +68,9 @@ def show(request):
tr.fiat_total += float(tr.fiat_amount) tr.fiat_total += float(tr.fiat_amount)
if tr.fiat_fee: if tr.fiat_fee:
tr.fiat_total += float(tr.fiat_fee) tr.fiat_total += float(tr.fiat_fee)
tax_limit_day = timezone.make_aware(datetime.datetime.now() - datetime.timedelta(days=1*365), timezone=timezone.get_current_timezone())
return render(request,"show.html",{'transactions':transactions, return render(request,"show.html",{'transactions':transactions,
'tax_limit_day': datetime.datetime.now() - datetime.timedelta(days=1*365), 'tax_limit_day': tax_limit_day,
'crypto': crypto}) 'crypto': crypto})