Commit 0e099022 authored by Sébastiaan Versteeg's avatar Sébastiaan Versteeg
Browse files

Order newsletter items using order field

parent 3e0c58b5
......@@ -4,13 +4,21 @@ from django.shortcuts import redirect
from newsletters.models import Newsletter, NewsletterEvent, NewsletterItem
from utils.translation import TranslatedModelAdmin
from .forms import NewsletterEventForm, NewsletterItemForm
class NewsletterItemInline(admin.StackedInline):
form = NewsletterItemForm
model = NewsletterItem
extra = 0
ordering = ('_order',)
class NewsletterEventInline(admin.StackedInline):
form = NewsletterEventForm
model = NewsletterEvent
extra = 0
ordering = ('_order',)
@admin.register(Newsletter)
......@@ -26,6 +34,18 @@ 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=''):
obj = Newsletter.objects.filter(id=object_id)[0]
if obj is not None and obj.sent is True:
......
from django import forms
from django.utils.translation import ugettext_lazy as _
from .models import NewsletterItem, NewsletterEvent
class NewsletterEventForm(forms.ModelForm):
order = forms.IntegerField(label=_('order'), initial=0)
class Meta:
fields = '__all__'
model = NewsletterEvent
class NewsletterItemForm(forms.ModelForm):
order = forms.IntegerField(label=_('order'), initial=0)
class Meta:
fields = '__all__'
model = NewsletterItem
from django.core.exceptions import ValidationError
from django.db import models
from django.db.models import ForeignKey
from django.urls import reverse
from django.utils.translation import ugettext_lazy as _
from tinymce.models import HTMLField
......@@ -133,5 +132,5 @@ class NewsletterEvent(NewsletterContent):
super().clean()
if self.end_datetime < self.start_datetime:
raise ValidationError({
'end': _("Can't have an event travel back in time")
'end_datetime': _("Can't have an event travel back in time")
})
......@@ -74,7 +74,7 @@
{% for row in agenda_events|rows_distributed:2 %}
<td style="vertical-align: top;">
{% for item in row %}
<li style="font-family: Arial, sans-serif;"><i>{{ item.title }} - {{ item.start_datetime|date:"d F" }}</i></li>
<li style="font-family: Arial, sans-serif;"><i>{{ item.title }} - {{ item.newsletterevent.start_datetime|date:"d F" }}</i></li>
{% endfor %}
</td>
{% endfor %}
......@@ -84,98 +84,93 @@
</td>
</tr>
{% for item in newsletter.newsletteritem_set.all %}
{% for item in newsletter.newslettercontent_set.all %}
<tr>
<td colspan="2" style="{{ item_tr_style }}">
<h2 style="{{ item_h2_style }}">{{ item.title.upper }}</h2>
</td>
</tr>
<tr>
<td colspan="2" style="{{ item_tr_style }}">
{{ item.description|safe }}
</td>
</tr>
{% endfor %}
{% for event in newsletter.newsletterevent_set.all %}
<tr>
<td colspan="2" style="{{ item_tr_style }}">
<h2 style="{{ item_h2_style }}">{{ event.title.upper }}</h2>
</td>
</tr>
<tr>
<td style="{{ item_tr_style }}">
{{ event.description|safe }}
{% if event.price != 0 and event.show_costs_warning %}
<p style="font-family: Arial, sans-serif">
<i><b>{% trans "Attention" %}:</b> {% trans "Registration deadline = unregistration deadline" %}!
{% trans "Thalia will recover the costs on you if you do not unregister on time" %}.
{% if event.penalty_costs and event.penalty_costs > 0 %}
{% trans "These costs are" %}: <b>€{{ event.penalty_costs }}.</b>
{% endif %}
</i>
</p>
{% endif %}
</td>
<td style="vertical-align: top;">
<div style="padding: 20px; background-color: black; border-left: 5px solid {{ primary_color }};">
<div style="margin-bottom: 5px;">
<b style="font-family: Calibri, 'Trebuchet MS', sans-serif;font-size: 15px; color: {{ primary_color }};">
{% trans "What"|upper %}
</b>
<br>
<span style="font-family: Arial, sans-serif; font-size: 15px; color: white; ">
{{ event.what }}
</span>
</div>
<div style="margin-bottom: 5px;">
<b style="font-family: Calibri, 'Trebuchet MS', sans-serif;font-size: 15px; color: {{ primary_color }};">
{% trans "Where"|upper %}
</b>
<br>
<span style="font-family: Arial, sans-serif; font-size: 15px; color: white; ">
{{ event.where }}
</span>
</div>
<div style="margin-bottom: 5px;">
<b style="font-family: Calibri, 'Trebuchet MS', sans-serif;font-size: 15px; color: {{ primary_color }};">
{% trans "When"|upper %}
</b>
<br>
<span style="font-family: Arial, sans-serif; font-size: 15px; color: white; ">
{% if event.start_datetime|date:'d-M-Y H:i' == event.end_datetime|date:'d-M-Y H:i' %}
{{ event.start_datetime }}
{% elif event.start_datetime|date:'d-M-Y' == event.end_datetime|date:'d-M-Y' %}
{{ event.start_datetime }} - {{ event.end_datetime|date:'H:i' }}
{% else %}
{{ event.start_datetime }} - {{ event.end_datetime }}
{% endif %}
</span>
</div>
{% if event.price is not None %}
<div style="margin-bottom: 5px;">
<b style="font-family: Calibri, 'Trebuchet MS', sans-serif;font-size: 15px; color: {{ primary_color }};">
{% trans "Price"|upper %}
</b>
<br>
<span style="font-family: Arial, sans-serif; font-size: 15px; color: white; ">
{% if event.price == 0 %}
{% trans "Free" %}
{% else %}
€{{ event.price }}
{% if item.newsletterevent %}
<tr>
<td style="{{ item_tr_style }}">
{{ item.description|safe }}
{% if item.newsletterevent.price != 0 and item.newsletterevent.show_costs_warning %}
<p style="font-family: Arial, sans-serif">
<i><b>{% trans "Attention" %}:</b> {% trans "Registration deadline = unregistration deadline" %}!
{% trans "Thalia will recover the costs on you if you do not unregister on time" %}.
{% if event.penalty_costs and item.newsletterevent.penalty_costs > 0 %}
{% trans "These costs are" %}: <b>€{{ event.penalty_costs }}.</b>
{% endif %}
</span>
</div>
</i>
</p>
{% endif %}
</div>
</td>
</tr>
</td>
<td style="vertical-align: top;">
<div style="padding: 20px; background-color: black; border-left: 5px solid {{ primary_color }};">
<div style="margin-bottom: 5px;">
<b style="font-family: Calibri, 'Trebuchet MS', sans-serif;font-size: 15px; color: {{ primary_color }};">
{% trans "What"|upper %}
</b>
<br>
<span style="font-family: Arial, sans-serif; font-size: 15px; color: white; ">
{{ item.newsletterevent.what }}
</span>
</div>
<div style="margin-bottom: 5px;">
<b style="font-family: Calibri, 'Trebuchet MS', sans-serif;font-size: 15px; color: {{ primary_color }};">
{% trans "Where"|upper %}
</b>
<br>
<span style="font-family: Arial, sans-serif; font-size: 15px; color: white; ">
{{ item.newsletterevent.where }}
</span>
</div>
<div style="margin-bottom: 5px;">
<b style="font-family: Calibri, 'Trebuchet MS', sans-serif;font-size: 15px; color: {{ primary_color }};">
{% trans "When"|upper %}
</b>
<br>
<span style="font-family: Arial, sans-serif; font-size: 15px; color: white; ">
{% if item.newsletterevent.start_datetime|date:'d-M-Y H:i' == item.newsletterevent.end_datetime|date:'d-M-Y H:i' %}
{{ item.newsletterevent.start_datetime }}
{% elif item.newsletterevent.start_datetime|date:'d-M-Y' == item.newsletterevent.end_datetime|date:'d-M-Y' %}
{{ item.newsletterevent.start_datetime }} - {{ item.newsletterevent.end_datetime|date:'H:i' }}
{% else %}
{{ item.newsletterevent.start_datetime }} - {{ item.newsletterevent.end_datetime }}
{% endif %}
</span>
</div>
{% if item.newsletterevent.price is not None %}
<div style="margin-bottom: 5px;">
<b style="font-family: Calibri, 'Trebuchet MS', sans-serif;font-size: 15px; color: {{ primary_color }};">
{% trans "Price"|upper %}
</b>
<br>
<span style="font-family: Arial, sans-serif; font-size: 15px; color: white; ">
{% if item.newsletterevent.price == 0 %}
{% trans "Free" %}
{% else %}
€{{ item.newsletterevent.price }}
{% endif %}
</span>
</div>
{% endif %}
</div>
</td>
</tr>
{% else %}
<tr>
<td colspan="2" style="{{ item_tr_style }}">
{{ item.description|safe }}
</td>
</tr>
{% endif %}
{% endfor %}
<tr>
......
......@@ -9,20 +9,18 @@
{% for item in agenda_events %} {{ item.title }} - {{ item.start_datetime|date:"d F" }}
{% endfor %}
{% for item in newsletter.newsletteritem_set.all %}{{ item.title|upper }}:
{% for item in newsletter.newslettercontent_set.all %}{{ item.title|upper }}:
{{ item.description|striptags }}
{% endfor %}{% for event in newsletter.newsletterevent_set.all %}{{ event.title|upper }}:
{{ event.description|striptags }}
{% if event.price != 0 and event.show_costs_warning %}
{% if item.newsletterevent %}{% if event.price != 0 and event.show_costs_warning %}
{% trans "Attention" %}: {% trans "Registration deadline = unregistration deadline" %}! {% trans "Thalia will recover the costs on you if you do not unregister on time" %}. {% if event.penalty_costs and event.penalty_costs > 0 %}{% trans "These costs are" %}: €{{ event.penalty_costs }}.{% endif %}
{% endif %}
{% trans "What"|upper %}: {{ event.what }}
{% trans "Where"|upper %}: {{ event.where }}
{% trans "When"|upper %}: {% if event.start_datetime == event.end_datetime %}{{ event.start_datetime }}{% elif event.start_datetime|date:'d-M-Y' == event.end_datetime|date:'d-M-Y' %}{{ event.start_datetime }} - {{ event.end_datetime|date:'H:i' }}{% else %}{{ event.start_datetime }} - {{ event.end_datetime }}{% endif %}
{% if event.price is not None %}{% trans "Price"|upper %}: {% if event.price == 0 %}{% trans "Free" %}{% else %}€{{ event.price }}{% endif %}{% endif %}
{% endif %}
{% endfor %}
{% trans "room"|capfirst %}: HG00.150
{% trans "website"|capfirst %}: www.thalia.nu
{% trans "email"|capfirst %}: info@thalia.nu
......
from datetime import datetime, timedelta, date
from django.conf import settings
from django.contrib.admin.views.decorators import staff_member_required
from django.contrib.auth.decorators import permission_required
......@@ -6,9 +8,6 @@ from django.shortcuts import get_object_or_404, redirect, render
from django.template import Context
from django.template.loader import get_template
from django.utils import translation
from datetime import datetime, timedelta, date
from django.utils.translation import activate, get_language_info
from members.models import Member
......@@ -34,7 +33,7 @@ def preview(request, pk, lang=None):
return render(request, 'newsletters/email.html', {
'newsletter': newsletter,
'agenda_events': newsletter.newslettercontent_set.filter(
newsletteritem=None).order_by('start_datetime'),
newsletteritem=None).order_by('newsletterevent__start_datetime'),
'main_partner': main_partner,
'lang_code': lang_code
})
......@@ -79,8 +78,11 @@ def admin_send(request, pk):
context = Context({
'newsletter': newsletter,
'agenda_events': newsletter.newslettercontent_set.filter(
newsletteritem=None).order_by('start_datetime'),
'agenda_events': (
newsletter.newslettercontent_set
.filter(newsletteritem=None)
.order_by('newsletterevent__start_datetime')
),
'main_partner': main_partner,
'lang_code': language[0],
'request': request
......
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