Commit 312715aa authored by Sébastiaan Versteeg's avatar Sébastiaan Versteeg Committed by Thijs de Jong
Browse files

Allow mixed ordering of items in newsletter

parent f8199aa1
......@@ -4,29 +4,20 @@ from django.shortcuts import redirect
from newsletters.models import Newsletter, NewsletterEvent, NewsletterItem
from utils.translation import TranslatedModelAdmin
from .forms import NewsletterEventForm, NewsletterItemForm
from .forms import NewsletterEventForm
class NewsletterItemInline(admin.StackedInline):
"""The inline for the text items in the newsletter"""
form = NewsletterItemForm
model = NewsletterItem
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(NewsletterItemInline):
"""The inline for the event items in the newsletter"""
form = NewsletterEventForm
model = NewsletterEvent
extra = 0
......@@ -45,18 +36,6 @@ class NewsletterAdmin(TranslatedModelAdmin):
def save_formset(self, request, form, formset, change):
"""Save formsets with their order"""
for f in sorted(formset.forms,
key=lambda x: (x.cleaned_data['order'],
def change_view(self, request, object_id, form_url=''):
Renders the change view
......@@ -6,4 +6,4 @@ from django.utils.translation import gettext_lazy as _
class NewslettersConfig(AppConfig):
"""AppConfig for the newsletters package"""
name = 'newsletters'
verbose_name = _('News letters')
verbose_name = _('Newsletters')
......@@ -4,29 +4,10 @@ from django.utils import timezone
from django.utils.translation import ugettext_lazy as _
from events.models import Event
from .models import NewsletterItem, NewsletterEvent, Newsletter
from .models import NewsletterEvent
class NewsletterItemForm(forms.ModelForm):
"""Custom ModelForm for the NewsletterItem model to add the order field"""
order = forms.IntegerField(label=_('order'), initial=0)
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
newsletter = self.instance.newsletter
order = newsletter.get_newslettercontent_order()
order_value = list(order).index(
self.fields['order'].initial = order_value
except Newsletter.DoesNotExist:
class Meta:
fields = '__all__'
model = NewsletterItem
class NewsletterEventForm(NewsletterItemForm):
class NewsletterEventForm(forms.ModelForm):
Custom ModelForm for the NewsletterEvent model to
add the order field and javascript for automatic field filling
......@@ -45,7 +26,7 @@ class NewsletterEventForm(NewsletterItemForm):
self.fields['event'].required = False
class Meta:
fields = ('event', 'title_en', 'title_nl',
fields = ('order', 'event', 'title_en', 'title_nl',
'description_en', 'description_nl', 'what_en', 'what_nl',
'where_en', 'where_nl', 'start_datetime', 'end_datetime',
'show_costs_warning', 'price', 'penalty_costs')
......@@ -5,8 +5,8 @@ msgid ""
msgstr ""
"Project-Id-Version: Thaliawebsite: nieuwsbrieven\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2018-10-24 23:55+0200\n"
"PO-Revision-Date: 2018-10-24 23:56+0200\n"
"POT-Creation-Date: 2019-04-13 16:03+0200\n"
"PO-Revision-Date: 2019-04-13 16:00+0200\n"
"Last-Translator: Sébastiaan Versteeg <>\n"
"Language-Team: Technicie <>\n"
"Language: nl\n"
......@@ -14,15 +14,15 @@ msgstr ""
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=2; plural=(n != 1);\n"
"X-Generator: Poedit 2.2\n"
"X-Generator: Poedit 2.2.1\n"
msgid "News letters"
msgid "Newsletters"
msgstr "Nieuwsbrieven"
msgid "order"
msgstr "volgorde"
msgid "Event"
msgstr "Evenement"
msgid "Title"
......@@ -66,6 +66,10 @@ msgstr "Controleer of alle links absoluut zijn en http(s):// bevatten."
msgid "Description"
msgstr "Beschrijving"
msgid "order"
msgstr "volgorde"
#: templates/newsletters/email.html templates/newsletters/email.txt
msgid "What"
msgstr "Wat"
# Generated by Django 2.1.8 on 2019-04-13 14:03
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('newsletters', '0007_newsletter_send_date'),
operations = [
options={'ordering': ('order',)},
field=models.PositiveIntegerField(default=0, null=True, verbose_name='order'),
......@@ -108,6 +108,13 @@ class NewsletterContent(models.Model, metaclass=ModelTranslateMeta):
newsletter = models.ForeignKey(Newsletter, on_delete=models.CASCADE)
order = models.PositiveIntegerField(
def clean(self):
......@@ -127,12 +134,12 @@ class NewsletterContent(models.Model, metaclass=ModelTranslateMeta):
if errors:
raise ValidationError(errors)
class Meta:
order_with_respect_to = 'newsletter'
def __str__(self):
return self.title
class Meta:
ordering = ('order',)
class NewsletterItem(NewsletterContent):
"""Describes one piece of text content of a newsletter"""
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