Repository: carpentries/amy

Issue:
Warning if training date is outside membership dates
When I associate a person at instructor training with a membership I would like to see a warning if the training date falls outside of the membership dates.  This should raise a flag but not prevent the person from being assigned like this.


Discussion:

Resolving patch:
diff --git a/amy/extrequests/views.py b/amy/extrequests/views.py
--- a/amy/extrequests/views.py
+++ b/amy/extrequests/views.py
@@ -196,6 +196,7 @@ def all_trainingrequests(request):
         match_form = BulkMatchTrainingRequestForm(request.POST)
 
         if match_form.is_valid():
+            event = match_form.cleaned_data['event']
             member_site = match_form.cleaned_data['seat_membership']
             open_seat = match_form.cleaned_data['seat_open_training']
 
@@ -209,7 +210,7 @@ def all_trainingrequests(request):
                 Task.objects.get_or_create(
                     person=r.person,
                     role=Role.objects.get(name='learner'),
-                    event=match_form.cleaned_data['event'],
+                    event=event,
                     seat_membership=member_site,
                     seat_open_training=open_seat,
                 )
@@ -235,6 +236,18 @@ def all_trainingrequests(request):
                             str(member_site))
                     )
 
+                # show warning if training falls out of agreement dates
+                if event.start and event.start < member_site.agreement_start \
+                        or event.end and event.end > member_site.agreement_end:
+                    messages.warning(
+                        request,
+                        'Training "{}" has start or end date outside '
+                        'membership "{}" agreement dates.'.format(
+                            str(event),
+                            str(member_site),
+                        ),
+                    )
+
             messages.success(request, 'Successfully accepted and matched '
                                       'selected people to training.')
 
diff --git a/amy/workshops/views.py b/amy/workshops/views.py
--- a/amy/workshops/views.py
+++ b/amy/workshops/views.py
@@ -1222,6 +1222,7 @@ def post(self, request, *args, **kwargs):
     def form_valid(self, form):
         """Check associated membership remaining seats and validity."""
         seat_membership = form.cleaned_data['seat_membership']
+        event = form.cleaned_data['event']
         if hasattr(self, 'request') and seat_membership is not None:
             # check number of available seats
             if seat_membership.seats_instructor_training_remaining == 1:
@@ -1234,15 +1235,29 @@ def form_valid(self, form):
                 messages.warning(
                     self.request,
                     'Membership "{}" is using more training seats'
-                    ' than it\'s been allowed.'.format(str(seat_membership))
+                    " than it's been allowed.".format(str(seat_membership))
                 )
 
             today = datetime.date.today()
             # check if membership is active
-            if not (seat_membership.agreement_start <= today <= seat_membership.agreement_end):
+            if not (seat_membership.agreement_start <= today <=
+                    seat_membership.agreement_end):
                 messages.warning(
                     self.request,
-                    'Membership "{}" is not active.'.format(str(seat_membership))
+                    'Membership "{}" is not active.'.format(
+                        str(seat_membership))
+                )
+
+            # show warning if training falls out of agreement dates
+            if event.start and event.start < seat_membership.agreement_start \
+                    or event.end and event.end > seat_membership.agreement_end:
+                messages.warning(
+                    self.request,
+                    'Training "{}" has start or end date outside '
+                    'membership "{}" agreement dates.'.format(
+                        str(event),
+                        str(seat_membership),
+                    ),
                 )
 
         return super().form_valid(form)


Test patch: