Last active
December 11, 2015 23:39
-
-
Save ollieglass/4678141 to your computer and use it in GitHub Desktop.
CSV export function from my personal finance mini-hackathon http://ollieglass.com/2013/01/30/personal-finance-mini-hackathon
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def weeks(self): | |
final_date = Transaction.objects.all().order_by('-transaction_date')[0].transaction_date | |
start_date = Transaction.objects.all().order_by('transaction_date')[0].transaction_date | |
end_date = start_date + datetime.timedelta(weeks=1) | |
while end_date <= final_date: | |
yield start_date, end_date | |
start_date += datetime.timedelta(weeks=1) | |
end_date += datetime.timedelta(weeks=1) | |
def handle(self, *args, **options): | |
headers = ['Week'] + [category.name for category in Category.objects.all()] + ['Not set'] | |
print ",".join([str(item) for item in headers]) | |
for start_date, end_date in self.weeks(): | |
transactions = Transaction.objects.filter(transaction_date__gte=start_date, transaction_date__lt=end_date) | |
row = [str(start_date)] | |
for category in Category.objects.all(): | |
total = 0 | |
for x in transactions.filter(merchant__category=category): | |
total += x.billing_amount | |
row.append(total) | |
total = 0 | |
for x in transactions.filter(merchant__category=None): | |
total += x.billing_amount | |
row.append(total) | |
print ",".join([str(item) for item in row]) | |
exit() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment