목표
- 인터페이스와 구현부를 명확히 분리하고자 했다.
- 단일 책임 원칙을 준수하자
- 생성자를 활용하자
코드
class PenaltyAction:
_RELEASE = "RELEASE"
_CANCEL = "CANCEL"
_PAY_TYPE_LIST = ["SUPER_SUB", "CASH", "COUPON"]
def __init__(self, order) -> None:
self.order = order
self.user = order.user
self.match = order.match_apply.match if order.match_apply else None
def apply(self) -> None:
if self._is_valid():
self._subtract_manner_point()
def _is_valid(self) -> bool:
return (
self._is_user_identified()
and self._is_order_validated()
and self._is_order_related_to_match()
and self._is_match_released()
and self._is_first_time_violated()
and self._has_violated_time_policy()
)
def _is_user_identified(self) -> bool:
if not self.user:
return False
elif self.user.id == 38:
return False
elif self.user.is_superuser:
return False
else:
return True
def _is_order_validated(self) -> bool:
return (
self.order.pay_type in self._PAY_TYPE_LIST
and self.order.order_status == self._CANCEL
)
def _is_order_related_to_match(self) -> bool:
return self.match is not None
def _is_match_released(self) -> bool:
return self.match.status == self._RELEASE
def _is_first_time_violated(self) -> bool:
return not FairPlay.objects.filter(
user=self.user,
match=self.match,
is_active=1,
end_dt=self.match.get_last_date_of_quarter(),
).exists()
def _has_violated_time_policy(self) -> bool:
now = timezone.now()
diff = (self.match.schedule - now).total_seconds()
return -1 * 60 <= diff <= 88 * 60
def _subtract_manner_point(self):
FairPlay.objects.create(
user=self.user,
match=self.match,
type_id=11,
is_active=1,
start_dt=self.match.get_first_date_of_quarter(),
end_dt=self.match.get_last_date_of_quarter(),
)