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