Commit 67a68d96 authored by Luuk Scholten's avatar Luuk Scholten
Browse files

Merge branch 'fix/newsletter-ordering' into 'master'

Preserve newsletter content ordering by setting the initial order value in the form

Closes #179

See merge request !387
parents 8915c45b 9a68854b
......@@ -13,12 +13,17 @@ class NewsletterItemInline(admin.StackedInline):
extra = 0
ordering = ('_order',)
def get_formset(self, request, obj=None, **kwargs):
formset = super().get_formset(request, obj, **kwargs)
if obj is not None:
count = obj.newslettercontent_set.count()
formset.form.declared_fields['order'].initial = count
return formset
class NewsletterEventInline(admin.StackedInline):
class NewsletterEventInline(NewsletterItemInline):
form = NewsletterEventForm
model = NewsletterEvent
extra = 0
ordering = ('_order',)
@admin.register(Newsletter)
......
from django import forms
from django.utils.translation import ugettext_lazy as _
from .models import NewsletterItem, NewsletterEvent
from .models import NewsletterItem, NewsletterEvent, Newsletter
class NewsletterEventForm(forms.ModelForm):
class NewsletterItemForm(forms.ModelForm):
order = forms.IntegerField(label=_('order'), initial=0)
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
try:
newsletter = self.instance.newsletter
order = newsletter.get_newslettercontent_order()
order_value = list(order).index(self.instance.pk)
self.fields['order'].initial = order_value
except Newsletter.DoesNotExist:
pass
class Meta:
fields = '__all__'
model = NewsletterEvent
model = NewsletterItem
class NewsletterItemForm(forms.ModelForm):
order = forms.IntegerField(label=_('order'), initial=0)
class NewsletterEventForm(NewsletterItemForm):
class Meta:
fields = '__all__'
model = NewsletterItem
model = NewsletterEvent
Supports Markdown
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