Skip to content

Commit

Permalink
[#2540] Run strategy in some views
Browse files Browse the repository at this point in the history
  • Loading branch information
pbanaszkiewicz committed Oct 28, 2023
1 parent ffabc32 commit fe098a5
Showing 1 changed file with 51 additions and 0 deletions.
51 changes: 51 additions & 0 deletions amy/trainings/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
from django.shortcuts import redirect, render
from django.urls import reverse_lazy

from emails.actions.instructor_training_completed_not_badged import (
instructor_training_completed_not_badged_strategy,
run_instructor_training_completed_not_badged_strategy,
)
from trainings.filters import TraineeFilter
from trainings.forms import BulkAddTrainingProgressForm, TrainingProgressForm
from trainings.utils import raise_validation_error_if_no_learner_task
Expand Down Expand Up @@ -74,16 +78,55 @@ def get_context_data(self, **kwargs):
context["form"].helper = context["form"].create_helper
return context

def form_valid(self, form):
person = form.cleaned_data["trainee"]
event = form.cleaned_data["event"]
result = super().form_valid(form)
run_instructor_training_completed_not_badged_strategy(
instructor_training_completed_not_badged_strategy(person),
request=self.request,
person=person,
training_completed_date=event.end if event else None,
)
return result


class TrainingProgressUpdate(RedirectSupportMixin, OnlyForAdminsMixin, AMYUpdateView):
model = TrainingProgress
form_class = TrainingProgressForm
template_name = "trainings/trainingprogress_form.html"

def form_valid(self, form):
person = form.cleaned_data["trainee"]
event = form.cleaned_data["event"]
run_instructor_training_completed_not_badged_strategy(
instructor_training_completed_not_badged_strategy(person),
request=self.request,
person=person,
training_completed_date=event.end if event else None,
)
return super().form_valid(form)


class TrainingProgressDelete(RedirectSupportMixin, OnlyForAdminsMixin, AMYDeleteView):
model = TrainingProgress
success_url = reverse_lazy("all_trainees")
object: TrainingProgress

def before_delete(self, *args, **kwargs):
"""Save for use in `after_delete` method."""
self._person = self.object.trainee
self._event = self.object.event

def after_delete(self, *args, **kwargs):
person = self._person
event = self._event
run_instructor_training_completed_not_badged_strategy(
instructor_training_completed_not_badged_strategy(person),
request=self.request,
person=person,
training_completed_date=event.end if event else None,
)


def all_trainees_queryset():
Expand Down Expand Up @@ -144,6 +187,14 @@ def all_trainees(request):
)
progress.full_clean()
progress.save()

run_instructor_training_completed_not_badged_strategy(
instructor_training_completed_not_badged_strategy(trainee),
request=request,
person=trainee,
training_completed_date=event.end if event else None,
)

except ValidationError as e:
unique_constraint_message = (
"Training progress with this Trainee "
Expand Down

0 comments on commit fe098a5

Please sign in to comment.