Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#471] Script for removing total budgetitem label 13 #515

Merged
merged 4 commits into from
May 6, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions akvo/rsr/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -354,9 +354,28 @@ class BudgetItemLabelAdmin(admin.ModelAdmin):
admin.site.register(get_model('rsr', 'budgetitemlabel'), BudgetItemLabelAdmin)


class BudgetItemAdminInLineFormSet(forms.models.BaseInlineFormSet):
def clean(self):
super(BudgetItemAdminInLineFormSet, self).clean()

budget_item_count = 0
including_total = False
for form in self.forms:
if not form.is_valid():
return
if form.cleaned_data and not form.cleaned_data.get('DELETE'):
budget_item_count += 1
if form.cleaned_data.get('label').label == 'total':
including_total = True

if budget_item_count > 1 and including_total:
raise forms.ValidationError(_("The 'total' budget item cannot be used in combination with other budget items."))


class BudgetItemAdminInLine(admin.TabularInline):
model = get_model('rsr', 'budgetitem')
extra = 1
formset = BudgetItemAdminInLineFormSet

class Media:
css = {'all': (os.path.join(settings.MEDIA_URL, 'akvo/css/src/rsr_admin.css').replace('\\', '/'),)}
Expand Down
5 changes: 4 additions & 1 deletion akvo/rsr/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -788,7 +788,10 @@ def anonymous_donations_amount_received(self):
# New API, de-normalized fields support

def get_budget(self):
return BudgetItem.objects.filter(project__exact=self).aggregate(Sum('amount'))['amount__sum'] or 0
if 'total' in BudgetItemLabel.objects.filter(budgetitem__project__exact=self):
return BudgetItem.objects.filter(project__exact=self).filter(label__label='total')[0].amount
else:
return BudgetItem.objects.filter(project__exact=self).aggregate(Sum('amount'))['amount__sum'] or 0

def update_budget(self):
"Update de-normalized field"
Expand Down
68 changes: 68 additions & 0 deletions akvo/scripts/budget_totals.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# -*- coding: utf-8 -*-
#!/usr/bin/env python

"""
Script for setting all budgets with budget item 13 to 14 and remove budget item 13 (total) from RSR.
"""

from django.core.management import setup_environ
from akvo import settings
setup_environ(settings)

from akvo.rsr.models import BudgetItem, BudgetItemLabel, Project


def set_budget_totals():
'''Change all budget items with id 13 to 14'''

print "\nGetting all budget items...\n"

budget_items = BudgetItem.objects.filter(label_id="13")
budgets_count = budget_items.count()

for count, item in enumerate(budget_items, start=1):
item.label_id = 14
item.save()

print "Budget item", str(item.pk), "adjusted (" + str(count), "out of", str(budgets_count) + ")..."


def remove_budgetitem_label():
'''Remove budget item label 13 (total)'''

BudgetItemLabel.objects.filter(id="13").delete()
print "\nRemoved the total budget item, label 13..."


def update_budgetitem_label():
'''Update label of budget item 14'''

total_budget_label = BudgetItemLabel.objects.get(id="14")
total_budget_label.label = 'total'
total_budget_label.save()

print "\nUpdated label of item 14 to 'total'...\n"


def update_projects():
'''Updates all projects using the budget sum calculator'''

print "\nGetting all projects...\n"

projects = Project.objects.all()
projects_count = projects.count()

for count, project in enumerate(projects):
project.update_budget()
project.update_funds_needed()

print "Updating project:", project.id, "(" + str(count), "out of", str(projects_count) + ")..."


if __name__ == '__main__':
set_budget_totals()
remove_budgetitem_label()
update_budgetitem_label()
update_projects()

print "\nDone!\n"