Commit 68154423 authored by Thijs de Jong's avatar Thijs de Jong
Browse files

Merge branch 'newsletter-ordering' into 'master'

Allow mixed ordering of items in newsletter

Closes #371

See merge request !1223
parents f8199aa1 312715aa
......@@ -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
@admin.register(Newsletter)
......@@ -45,18 +36,6 @@ class NewsletterAdmin(TranslatedModelAdmin):
}),
)
def save_formset(self, request, form, formset, change):
"""Save formsets with their order"""
formset.save()
form.instance.set_newslettercontent_order([
f.instance.pk
for f in sorted(formset.forms,
key=lambda x: (x.cleaned_data['order'],
x.instance.pk))
])
form.instance.save()
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)
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 = 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 <se_bastiaan@outlook.com>\n"
"Language-Team: Technicie <www@thalia.nu>\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"
#: apps.py
msgid "News letters"
msgid "Newsletters"
msgstr "Nieuwsbrieven"
#: forms.py
msgid "order"
msgstr "volgorde"
msgid "Event"
msgstr "Evenement"
#: models.py
msgid "Title"
......@@ -66,6 +66,10 @@ msgstr "Controleer of alle links absoluut zijn en http(s):// bevatten."
msgid "Description"
msgstr "Beschrijving"
#: models.py
msgid "order"
msgstr "volgorde"
#: models.py 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 = [
migrations.AlterModelOptions(
name='newslettercontent',
options={'ordering': ('order',)},
),
migrations.AddField(
model_name='newslettercontent',
name='order',
field=models.PositiveIntegerField(default=0, null=True, verbose_name='order'),
),
migrations.AlterOrderWithRespectTo(
name='newslettercontent',
order_with_respect_to=None,
),
]
......@@ -108,6 +108,13 @@ class NewsletterContent(models.Model, metaclass=ModelTranslateMeta):
newsletter = models.ForeignKey(Newsletter, on_delete=models.CASCADE)
order = models.PositiveIntegerField(
verbose_name=_('order'),
blank=False,
null=True,
default=0
)
def clean(self):
super().clean()
......@@ -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"""
......
Markdown is supported
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