Skip to content

Commit

Permalink
[#2540] Correct strategy, extend context
Browse files Browse the repository at this point in the history
  • Loading branch information
pbanaszkiewicz committed Oct 28, 2023
1 parent 7be7fac commit 6f8d5d9
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 23 deletions.
52 changes: 30 additions & 22 deletions amy/emails/actions/instructor_training_completed_not_badged.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
from datetime import datetime
from datetime import date, datetime
import logging

from django.contrib.contenttypes.models import ContentType
from django.http import HttpRequest
from typing_extensions import Unpack

from emails.actions.base_action import BaseAction, BaseActionCancel, BaseActionUpdate
from emails.models import ScheduledEmail
from emails.signals import (
INSTRUCTOR_TRAINING_COMPLETED_NOT_BADGED_SIGNAL_NAME,
Signal,
instructor_training_completed_not_badged_remove_signal,
instructor_training_completed_not_badged_signal,
Expand All @@ -17,16 +20,14 @@
StrategyEnum,
)
from emails.utils import two_months_after
from workshops.models import Award, Person, TrainingProgress
from workshops.models import Person, TrainingProgress

from .instructor_training_approaching import EmailStrategyException

logger = logging.getLogger("amy")


def instructor_training_completed_not_badged_strategy(
person: Person,
) -> StrategyEnum:
def instructor_training_completed_not_badged_strategy(person: Person) -> StrategyEnum:
logger.info(f"Running InstructorTrainingCompletedNotBadged strategy for {person}")

person_annotated = (
Expand All @@ -35,27 +36,30 @@ def instructor_training_completed_not_badged_strategy(
)
)

has_instructor_role = Award.objects.filter(
role__name="instructor", person=person
all_requirements_passed = (
bool(person_annotated.passed_training)
and bool(person_annotated.passed_get_involved)
and bool(person_annotated.passed_welcome)
and bool(person_annotated.passed_demo)
)

ct = ContentType.objects.get_for_model(person) # type: ignore
has_email_scheduled = ScheduledEmail.objects.filter(
generic_relation_content_type=ct,
generic_relation_pk=person.pk,
template__signal=INSTRUCTOR_TRAINING_COMPLETED_NOT_BADGED_SIGNAL_NAME,
state="scheduled",
).exists()

all_requirements_passed = (
person_annotated.passed_training
and person_annotated.passed_get_involved
and person_annotated.passed_welcome
and person_annotated.passed_demo
email_should_exist = (
bool(person_annotated.passed_training) and not all_requirements_passed
)

if has_instructor_role or all_requirements_passed:
result = StrategyEnum.REMOVE
elif person_annotated.passed_training and not all_requirements_passed:
if not has_email_scheduled and email_should_exist:
result = StrategyEnum.CREATE
elif (
person_annotated.passed_training
or person_annotated.passed_get_involved
or person_annotated.passed_welcome
or person_annotated.passed_demo
):
elif has_email_scheduled and not email_should_exist:
result = StrategyEnum.REMOVE
elif has_email_scheduled and email_should_exist:
result = StrategyEnum.UPDATE
else:
result = StrategyEnum.NOOP
Expand All @@ -66,7 +70,10 @@ def instructor_training_completed_not_badged_strategy(

# TODO: turn into a generic function/class
def run_instructor_training_completed_not_badged_strategy(
strategy: StrategyEnum, request: HttpRequest, person: Person
strategy: StrategyEnum,
request: HttpRequest,
person: Person,
training_completed_date: date | None,
) -> None:
mapping: dict[StrategyEnum, Signal | None] = {
StrategyEnum.CREATE: instructor_training_completed_not_badged_signal,
Expand All @@ -88,6 +95,7 @@ def run_instructor_training_completed_not_badged_strategy(
sender=person,
request=request,
person=person,
training_completed_date=training_completed_date,
)


Expand Down
5 changes: 4 additions & 1 deletion amy/emails/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from django.http import HttpRequest

from recruitment.models import InstructorRecruitmentSignup
from workshops.models import Award, Event, Person
from workshops.models import Award, Event, Person, TrainingProgress


class InstructorBadgeAwardedKwargs(TypedDict):
Expand Down Expand Up @@ -105,6 +105,9 @@ class InstructorTrainingCompletedNotBadgedKwargs(TypedDict):

class InstructorTrainingCompletedNotBadgedContext(TypedDict):
person: Person
passed_requirements: list[TrainingProgress]
missing_requirements: list[TrainingProgress]
training_completed_date: date | None


class StrategyEnum(StrEnum):
Expand Down

0 comments on commit 6f8d5d9

Please sign in to comment.