Commit f60fbd0a authored by rexxnor's avatar rexxnor

fixed a bug mentioned in #5

parent d0499b26
......@@ -3,7 +3,7 @@ Settings for the Admin interface
"""
from django.contrib import admin
from .models import Card, Transaction, Charge
from .models import Card, Transaction, Charge, Report
# Register your models here.
class CardAdmin(admin.ModelAdmin):
......@@ -26,6 +26,13 @@ class ChargeAdmin(admin.ModelAdmin):
"""
list_display = ('owner_id', 'amount', 'date_time')
class ReportAdmin(admin.ModelAdmin):
"""
Customize the Charge Listing in Admin interface
"""
list_display = ('report_id', 'date_time', 'previous_date_time', 'totalspent', 'totalcharged', 'currentcharge')
admin.site.register(Card, CardAdmin)
admin.site.register(Transaction, TransactionAdmin)
admin.site.register(Charge, ChargeAdmin)
admin.site.register(Report, ReportAdmin)
......@@ -280,79 +280,112 @@ class CurrentReport(TemplateView):
"""
Class that represents the CurrentReport
"""
# get last month relative to current date
today = timezone.now()
# Gets all Transactions without report id
transactions = Transaction.objects.filter(report_id=None)
totalvalue = Money(currency='CHF')
for transaction in transactions:
totalvalue += transaction.amount
# Gets all Charges without report id
charges = Charge.objects.filter(report_id=None)
totalcharged = Money(currency='CHF')
for charge in charges:
totalcharged += charge.amount
# get current balance over all cards
cards = Card.objects.filter().distinct()
totalbalance = Money(currency='CHF')
for card in cards:
totalbalance += card.current_balance
# create a new report object
report = Report(date_time=today,
totalspent=totalvalue,
totalcharged=totalcharged,
currentcharge=totalbalance)
# get latest report from database
try:
oldreport = Report.objects.order_by('report_id')[0]
except ObjectDoesNotExist:
# can safely ignore and assume report does not exist
oldreport = Report(date_time=timezone.now().replace(year=2000, month=1, day=1))
except IndexError:
# can safely ignore and assume report does not exist
oldreport = Report(date_time=timezone.now().replace(year=2000, month=1, day=1))
# set new reports olddate to date from old report
if oldreport:
report.previous_date_time = oldreport.date_time
else:
report.previous_date_time = None
def get(self, request):
"""
This is a readonly view for the report
"""
totalvalue = Money(currency='CHF')
totalcharged = Money(currency='CHF')
totalbalance = Money(currency='CHF')
oldreport = None
# get last month relative to current date
today = timezone.now()
# Gets all Transactions without report id
transactions = Transaction.objects.filter(report_id=None)
for transaction in transactions:
totalvalue += transaction.amount
# Gets all Charges without report id
charges = Charge.objects.filter(report_id=None)
for charge in charges:
totalcharged += charge.amount
# get current balance over all cards
cards = Card.objects.filter().distinct()
for card in cards:
totalbalance += card.current_balance
# get latest report from database
try:
oldreport = Report.objects.order_by('-report_id')[0]
except ObjectDoesNotExist:
# can safely ignore and assume report does not exist
oldreport = Report(date_time=timezone.now().replace(year=2000, month=1, day=1))
except IndexError:
# can safely ignore and assume report does not exist
oldreport = Report(date_time=timezone.now().replace(year=2000, month=1, day=1))
return render(request, 'currentreport.html.j2',
{'reportid': self.report.report_id,
'olddate': self.report.previous_date_time.strftime("%d.%m.%Y %H:%M"),
'datetoday': self.today.strftime("%d.%m.%Y %H:%M"),
'totalamount': self.totalvalue,
'totalcharged': self.totalcharged,
'totalbalance': self.totalbalance})
{'reportid': "Temporary Report",
'olddate': oldreport.date_time.strftime("%d.%m.%Y %H:%M"),
'datetoday': today.strftime("%d.%m.%Y %H:%M"),
'totalamount': totalvalue,
'totalcharged': totalcharged,
'totalbalance': totalbalance})
def post(self, request):
"""
This generates the report object and commits it to database
"""
totalvalue = Money(currency='CHF')
totalcharged = Money(currency='CHF')
totalbalance = Money(currency='CHF')
report = None
oldreport = None
today = timezone.now()
# Gets all Transactions without report id
transactions = Transaction.objects.filter(report_id=None)
for transaction in transactions:
totalvalue += transaction.amount
# Gets all Charges without report id
charges = Charge.objects.filter(report_id=None)
for charge in charges:
totalcharged += charge.amount
# get current balance over all cards
cards = Card.objects.filter().distinct()
for card in cards:
totalbalance += card.current_balance
# create a new report object
report = Report(date_time=today,
totalspent=totalvalue,
totalcharged=totalcharged,
currentcharge=totalbalance)
# get latest report from database
try:
oldreport = Report.objects.order_by('-report_id')[0]
except ObjectDoesNotExist:
# can safely ignore and assume report does not exist
oldreport = Report(date_time=timezone.now().replace(year=2000, month=1, day=1))
except IndexError:
# can safely ignore and assume report does not exist
oldreport = Report(date_time=timezone.now().replace(year=2000, month=1, day=1))
# set new reports olddate to date from old report
if oldreport:
report.previous_date_time = oldreport.date_time
else:
report.previous_date_time = None
# Commit the report to database
self.report.save()
report.save()
# Update Transaction objects with reportid
for transaction in self.transactions:
transaction.report_id = self.report
for transaction in transactions:
transaction.report_id = report
transaction.save()
# Update Charge objects with reportid
for charge in self.charges:
charge.report_id = self.report
for charge in charges:
charge.report_id = report
charge.save()
# Redirect to the periodreport view
return redirect('../periodreport/%d' % self.report.report_id)
return redirect('../periodreport/%d' % report.report_id)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment