Commit 11896c45 authored by Luko van der Maas's avatar Luko van der Maas
Browse files

Add sending of notification to participants of event by organisers

parent 6d47ebcf
......@@ -230,6 +230,10 @@ class EventAdmin(DoNextTranslatedModelAdmin):
self.admin_site.admin_view(
admin_views.EventRegistrationsMarkPresent.as_view()),
name='events_event_all_present'),
path('<int:pk>/message/',
self.admin_site.admin_view(
admin_views.EventMessage.as_view(admin=self)),
name='events_event_message'),
]
return custom_urls + urls
......
......@@ -18,7 +18,8 @@ from django.views.generic import DetailView, TemplateView, FormView
from events import services
from events.decorators import organiser_only
from events.exceptions import RegistrationError
from events.forms import FieldsForm
from events.forms import FieldsForm, EventMessageForm
from pushnotifications.models import Message, Category
from .models import Event, Registration
......@@ -110,6 +111,74 @@ class RegistrationAdminFields(FormView):
))
@method_decorator(staff_member_required, name='dispatch')
@method_decorator(organiser_only, name='dispatch')
class EventMessage(FormView):
"""
Renders a form that allows the user to create a push notification for all
users registers to the event.
"""
form_class = EventMessageForm
template_name = 'events/admin/message_form.html'
admin = None
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context.update({
**self.admin.admin_site.each_context(self.request),
'add': False,
'change': True,
'has_view_permission': True,
'has_add_permission': False,
'has_change_permission':
self.request.user.has_perms('events.change_event'),
'has_delete_permission': False,
'has_editable_inline_admin_formsets': False,
'app_label': 'events',
'opts': self.event._meta,
'is_popup': False,
'save_as': False,
'save_on_top': False,
'original': self.event,
'obj_id': self.event.pk,
'title': _('Send push notification'),
'adminform': helpers.AdminForm(context['form'], (
(None, {
'fields': [f for f in context['form'].fields.keys()]
}),
), {})
})
return context
def form_valid(self, form):
values = form.cleaned_data
message = Message(
title_nl=values['title_nl'],
title_en=values['title_en'],
body_nl=values['title_nl'],
body_en=values['title_en'],
url=values['url'],
category=Category.objects.get(key='event')
)
message.save()
message.users.set([r.member for r in self.event.participants
if r.member])
message.send()
print(message)
messages.success(self.request,
_("Message sent successfully."))
if '_save' in self.request.POST:
return HttpResponseRedirect(reverse(
'admin:events_event_details',
args=[str(self.event.pk)]
))
def dispatch(self, request, *args, **kwargs):
self.event = get_object_or_404(Event, pk=self.kwargs['pk'])
return super().dispatch(request, *args, **kwargs)
@method_decorator(staff_member_required, name='dispatch')
@method_decorator(organiser_only, name='dispatch')
class EventRegistrationsExport(View, PermissionRequiredMixin):
......
......@@ -86,3 +86,12 @@ class FieldsForm(forms.Form):
def field_values(self):
for key, field in self.information_fields.items():
yield (key, self.cleaned_data[key])
class EventMessageForm(forms.Form):
"""Form that outputs a widget to get info to send a push notification"""
title_en = forms.CharField(required=True, max_length=150)
title_nl = forms.CharField(required=True, max_length=150)
body_en = forms.CharField(required=True)
body_nl = forms.CharField(required=True)
url = forms.CharField(max_length=256)
......@@ -55,6 +55,7 @@
<a href="{% url 'admin:events_event_export' pk=event.pk %}" class="button">{% trans "Export registrations" %}</a>
<a href="{% url 'admin:events_event_export_email' pk=event.pk %}" class="button">{% trans "Export email addresses of attendees" %}</a>
<a href="{% url 'admin:events_event_all_present' pk=event.pk %}" class="button">{% trans "Set all registered to paid and present" %}</a>
<a href="{% url 'admin:events_event_message' pk=event.pk %}" class="button">{% trans "Send pushnotification to registerd users" %}</a>
</div>
</div>
{% endblock %}
......
{% extends "admin/change_form.html" %}
{% load i18n admin_urls static compress %}
{% block extrastyle %}
{{ block.super }}
{% compress css %}<link rel="stylesheet" type="text/x-scss" href="{% static 'admin/pushnotifications/css/forms.scss' %}" />{% endcompress %}
{% endblock %}
{% block submit_buttons_bottom %}
<div class="submit-row pushnotifications-row">
<input type="submit" value="Send message" class="default button" name="_save" data-_extension-text-contrast="">
</div>
{% endblock %}
......@@ -11,7 +11,7 @@
}
}
.pushnotifications-row a.button {
.pushnotifications-row .button {
background-color: #e65c95;
font-size: 13px;
......
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