Skip to content

Commit

Permalink
fix #6
Browse files Browse the repository at this point in the history
  • Loading branch information
bbliem committed Jan 18, 2021
1 parent 162febc commit 7d32be5
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
6 changes: 3 additions & 3 deletions actions/models.py
Expand Up @@ -279,7 +279,7 @@ class Action(ModelWithImage, OrderedModel, ClusterableModel):
"""One action/measure tracked in an action plan."""

plan = ParentalKey(
Plan, on_delete=models.CASCADE, default=latest_plan, related_name='actions',
Plan, on_delete=models.CASCADE, related_name='actions',
verbose_name=_('plan')
)
name = models.CharField(max_length=1000, verbose_name=_('name'))
Expand Down Expand Up @@ -401,8 +401,8 @@ def clean(self):

def save(self, *args, **kwargs):
if self.pk is None:
qs = self.plan.objects.actions.all()
max_order = qs.aggregate(Max(self.order))['%s__max' % self.order] or 0
qs = self.plan.actions.values('order').order_by()
max_order = qs.aggregate(Max('order'))['order__max'] or 0
self.order = max_order
return super().save(*args, **kwargs)

Expand Down
10 changes: 10 additions & 0 deletions actions/wagtail_admin.py
Expand Up @@ -183,8 +183,18 @@ def get_form_class(self, request=None):
return form_class


class ActionCreateView(AplansCreateView):
def get_instance(self):
# Override default implementation, which would try to create an
# instance of self.model (i.e., Action) without a plan, causing an
# error
plan = self.request.user.get_active_admin_plan()
return getattr(self, 'instance', None) or Action(plan=plan)


class ActionAdmin(OrderableMixin, AplansModelAdmin):
model = Action
create_view_class = ActionCreateView
menu_icon = 'fa-cubes' # change as required
menu_label = _('Actions')
menu_order = 1
Expand Down

0 comments on commit 7d32be5

Please sign in to comment.