Skip to content

Commit

Permalink
Set organization of indicators appropriately when saving
Browse files Browse the repository at this point in the history
  • Loading branch information
bbliem committed Jan 20, 2021
1 parent 8ac87c3 commit 5c39f83
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions indicators/wagtail_admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,26 +208,32 @@ class IndicatorForm(AplansAdminModelForm):
def __init__(self, *args, **kwargs):
self.plan = kwargs.pop('plan')
super().__init__(*args, **kwargs)
# If we are editing an existing indicator, set the `level` field to the proper indicator level in the database
if self.instance.pk is not None:
# We are editing an existing indicator. If the indicator is in the
# active plan, set this form's `level` field to the proper value.
try:
indicator_level = IndicatorLevel.objects.get(indicator=self.instance, plan=self.plan)
self.fields['level'].initial = indicator_level.level
except IndicatorLevel.DoesNotExist:
# Indicator is not in active plan
pass

def save(self, commit=True):
assert self.instance.organization_id is None or self.instance.organization == self.plan.organization
self.instance.organization = self.plan.organization
return super().save(commit)

def _save_m2m(self):
assert self.plan
chosen_level = self.data['level']
# Update related IndicatorLevel object, deleting it if chosen_level is empty or None
try:
indicator_level = IndicatorLevel.objects.get(indicator=self.instance, plan=self.plan)
if not chosen_level:
indicator_level.delete()
else:
if chosen_level:
indicator_level.level = chosen_level
indicator_level.save()
else:
indicator_level.delete()
except IndicatorLevel.DoesNotExist:
# Indicator was not in active plan
if chosen_level:
Expand Down

0 comments on commit 5c39f83

Please sign in to comment.