diff --git a/dav_base/static/dav_base/css/local.css b/dav_base/static/dav_base/css/local.css index c0ea572..51487ee 100644 --- a/dav_base/static/dav_base/css/local.css +++ b/dav_base/static/dav_base/css/local.css @@ -140,4 +140,11 @@ thead input { content:"*"; color: red; } -*/ \ No newline at end of file +*/ + +/* + * Used to remove the space around link like buttons ( +   +   + {% if registration.answered %} + + {% endif %} {{ registration.get_full_name }} ({{ registration.email_address }}, {{ registration.phone_number }}) @@ -264,93 +268,101 @@ {% endif %} {% empty %} - Keine unbestätigten Anmeldungen vorhanden + {% trans 'Keine unbestätigten Anmeldungen vorhanden' %} {% endfor %} -
+ {% endif %} +
-
{% bootstrap_form_errors create_participant_form %}
{% csrf_token %} -
-
- {% bootstrap_field create_participant_form.personal_names %} -
-
- {% bootstrap_field create_participant_form.family_names %} -
-
-
-
- {% bootstrap_field create_participant_form.address %} -
-
-
-
- {% bootstrap_field create_participant_form.postal_code %} -
-
- {% bootstrap_field create_participant_form.city %} -
-
-
-
- {% bootstrap_field create_participant_form.email_address %} -
-
- {% bootstrap_field create_participant_form.phone_number %} -
-
-
-
- {% bootstrap_field create_participant_form.dav_number %} -
-
- {% bootstrap_field create_participant_form.emergency_contact %} -
-
-
-
- {% bootstrap_field create_participant_form.experience %} -
-
- {% bootstrap_field create_participant_form.note %} -
-
- + {% include './includes/participant_form.html' with form=create_participant_form %} +
- {% for participant in participants %} -
- diff --git a/dav_events/templates/dav_events/includes/participant_form.html b/dav_events/templates/dav_events/includes/participant_form.html new file mode 100644 index 0000000..4eb0e04 --- /dev/null +++ b/dav_events/templates/dav_events/includes/participant_form.html @@ -0,0 +1,46 @@ +{% load bootstrap3 %} +
+
+ {% bootstrap_field form.personal_names %} +
+
+ {% bootstrap_field form.family_names %} +
+
+
+
+ {% bootstrap_field form.address %} +
+
+
+
+ {% bootstrap_field form.postal_code %} +
+
+ {% bootstrap_field form.city %} +
+
+
+
+ {% bootstrap_field form.email_address %} +
+
+ {% bootstrap_field form.phone_number %} +
+
+
+
+ {% bootstrap_field form.dav_number %} +
+
+ {% bootstrap_field form.emergency_contact %} +
+
+
+
+ {% bootstrap_field form.experience %} +
+
+ {% bootstrap_field form.note %} +
+
\ No newline at end of file diff --git a/dav_events/views/events.py b/dav_events/views/events.py index fda013c..c19309b 100644 --- a/dav_events/views/events.py +++ b/dav_events/views/events.py @@ -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) diff --git a/dav_registration/models.py b/dav_registration/models.py index c0842ca..39b0772 100644 --- a/dav_registration/models.py +++ b/dav_registration/models.py @@ -9,10 +9,10 @@ from django.utils import timezone from django.utils.encoding import python_2_unicode_compatible from django.utils.translation import ugettext_lazy as _ +from dav_base.validators import DAVNumberValidator from dav_events.models.event import Event from . import signals -from .validators import DAVNumberValidator logger = logging.getLogger(__name__)