UPD: more cool participant admin stuff.
This commit is contained in:
@@ -7,6 +7,7 @@ from django.contrib.auth import login
|
||||
from django.contrib.auth.decorators import login_required
|
||||
from django.core.exceptions import PermissionDenied, SuspiciousOperation, FieldDoesNotExist
|
||||
from django.db.models import Q
|
||||
from django.forms import modelformset_factory
|
||||
from django.http import HttpResponse, HttpResponseRedirect
|
||||
from django.shortcuts import get_object_or_404
|
||||
from django.urls import reverse, reverse_lazy
|
||||
@@ -169,22 +170,39 @@ class EventRegistrationsView(EventPermissionMixin, generic.DetailView):
|
||||
self.enforce_permission(obj)
|
||||
return obj
|
||||
|
||||
def get_form_kwargs(self):
|
||||
def get_form_kwargs(self, process_request=True):
|
||||
kwargs = {}
|
||||
if self.request.method in ('POST', 'PUT'):
|
||||
if process_request and self.request.method in ('POST', 'PUT'):
|
||||
kwargs.update({
|
||||
'data': self.request.POST,
|
||||
'files': self.request.FILES,
|
||||
})
|
||||
if 'form_prefix' in self.request.POST:
|
||||
form_prefix = self.request.POST.get('form_prefix')
|
||||
kwargs['prefix'] = form_prefix
|
||||
pk_field = form_prefix + '-id'
|
||||
else:
|
||||
pk_field = 'id'
|
||||
if pk_field in self.request.POST:
|
||||
pk = self.request.POST.get(pk_field)
|
||||
event = self.object
|
||||
kwargs['instance'] = event.participants.get(pk=pk)
|
||||
return kwargs
|
||||
|
||||
def get_create_participant_form(self):
|
||||
event = self.get_object()
|
||||
form = forms.participant.ParticipantForm(**self.get_form_kwargs())
|
||||
def get_participant_form(self, process_request=True):
|
||||
event = self.object
|
||||
form = forms.participant.ParticipantForm(**self.get_form_kwargs(process_request=process_request))
|
||||
form.instance.event = event
|
||||
form.instance.position = event.participants.count() + 1
|
||||
if not form.instance.position:
|
||||
form.instance.position = event.participants.count() + 1
|
||||
return form
|
||||
|
||||
def get_participant_formset(self):
|
||||
event = self.object
|
||||
ParticipantFormSet = modelformset_factory(models.Participant, form=forms.participant.ParticipantForm, extra=0)
|
||||
formset = ParticipantFormSet(queryset=event.participants.all())
|
||||
return formset
|
||||
|
||||
def get_context_data(self, **kwargs):
|
||||
context = super(EventRegistrationsView, self).get_context_data(**kwargs)
|
||||
event = context.get('event')
|
||||
@@ -194,12 +212,20 @@ class EventRegistrationsView(EventPermissionMixin, generic.DetailView):
|
||||
|
||||
participants = event.participants.all()
|
||||
context['participants'] = participants
|
||||
if hasattr(event, 'registrations'):
|
||||
|
||||
if 'participant_formset' not in context:
|
||||
context['participant_formset'] = self.get_participant_formset()
|
||||
|
||||
if 'create_participant_form' not in context:
|
||||
context['create_participant_form'] = self.get_participant_form(process_request=False)
|
||||
|
||||
registrations_support = hasattr(event, 'registrations')
|
||||
context['registrations_support'] = registrations_support
|
||||
if registrations_support:
|
||||
# registrations = event.registrations.filter(answered=False)
|
||||
registrations = event.registrations.all()
|
||||
context['registrations'] = registrations
|
||||
if 'create_participant_form' not in context:
|
||||
context['create_participant_form'] = self.get_create_participant_form()
|
||||
|
||||
return context
|
||||
|
||||
def _notify_publisher(self, event, editor):
|
||||
@@ -259,6 +285,18 @@ class EventRegistrationsView(EventPermissionMixin, generic.DetailView):
|
||||
registration.answered = True
|
||||
registration.save()
|
||||
|
||||
def _swap_participants_position(self, participant1, participant2):
|
||||
event = participant1.event
|
||||
pos_tmp = event.participants.count() + 1
|
||||
pos1 = participant1.position
|
||||
pos2 = participant2.position
|
||||
participant1.position = pos_tmp
|
||||
participant1.save()
|
||||
participant2.position = pos1
|
||||
participant2.save()
|
||||
participant1.position = pos2
|
||||
participant1.save()
|
||||
|
||||
def post(self, request, *args, **kwargs):
|
||||
event = self.get_object()
|
||||
self.object = event
|
||||
@@ -284,15 +322,62 @@ class EventRegistrationsView(EventPermissionMixin, generic.DetailView):
|
||||
self._reject_registration(registration)
|
||||
else:
|
||||
raise FieldDoesNotExist('Event has no registrations')
|
||||
else:
|
||||
form = self.get_create_participant_form()
|
||||
elif action == 'remove_participant':
|
||||
participant_id = request.POST.get('id')
|
||||
participant = event.participants.get(id=participant_id)
|
||||
full_name = participant.get_full_name()
|
||||
position = participant.position
|
||||
participant.delete()
|
||||
qs = event.participants.filter(position__gt=position)
|
||||
for participant in qs:
|
||||
participant.position -= 1
|
||||
participant.save()
|
||||
messages.success(request, _(u'Teilnehmer gelöscht: {}'.format(full_name)))
|
||||
elif action == 'moveup_participant':
|
||||
participant_id = request.POST.get('id')
|
||||
participant = event.participants.get(id=participant_id)
|
||||
current_position = participant.position
|
||||
if current_position > 1:
|
||||
upper_position = current_position - 1
|
||||
upper_participant = event.participants.get(position=upper_position)
|
||||
self._swap_participants_position(participant, upper_participant)
|
||||
else:
|
||||
messages.error(request, 'Participant is already on first position')
|
||||
elif action == 'movedown_participant':
|
||||
participant_id = request.POST.get('id')
|
||||
participant = event.participants.get(id=participant_id)
|
||||
current_position = participant.position
|
||||
if current_position < event.participants.count():
|
||||
lower_position = current_position + 1
|
||||
lower_participant = event.participants.get(position=lower_position)
|
||||
self._swap_participants_position(participant, lower_participant)
|
||||
else:
|
||||
messages.error(request, 'Participant is already on last position')
|
||||
elif action == 'update_participant':
|
||||
form = self.get_participant_form()
|
||||
if form.is_valid():
|
||||
form.save()
|
||||
participant = form.instance
|
||||
messages.success(request, _(u'Teilnehmer aktualisiert: {}'.format(participant.get_full_name())))
|
||||
else:
|
||||
formset = self.get_participant_formset()
|
||||
for i in range(0, len(formset)):
|
||||
orig_form = formset.forms[i]
|
||||
if orig_form.prefix == form.prefix:
|
||||
formset.forms[i] = form
|
||||
messages.error(request, _(u'Ungültige Eingabe - Siehe unten'))
|
||||
return self.render_to_response(self.get_context_data(participant_formset=formset))
|
||||
elif action == 'create_participant':
|
||||
form = self.get_participant_form()
|
||||
if form.is_valid():
|
||||
form.save()
|
||||
participant = form.instance
|
||||
messages.success(request, _(u'Teilnehmer hinzugefügt: {}'.format(participant.get_full_name())))
|
||||
else:
|
||||
messages.error(request, _(u'irgendwas ging schief.'))
|
||||
messages.error(request, _(u'Ungültige Eingabe - Siehe unten'))
|
||||
return self.render_to_response(self.get_context_data(create_participant_form=form))
|
||||
else:
|
||||
messages.error(request, 'unsupported action: {}'.format(action))
|
||||
return HttpResponseRedirect(reverse('dav_events:registrations', kwargs={'pk': event.pk}))
|
||||
|
||||
@method_decorator(login_required)
|
||||
|
||||
Reference in New Issue
Block a user