# This file is part of Shuup.
#
# Copyright (c) 2012-2021, Shuup Commerce Inc. All rights reserved.
#
# This source code is licensed under the OSL-3.0 license found in the
# LICENSE file in the root directory of this source tree.
from django.db import models
from django.utils import timezone
from django.utils.translation import ugettext_lazy as _
from polymorphic.models import PolymorphicModel
from shuup.campaigns.utils.time_range import is_in_time_range
from shuup.core.models import AnonymousContact, Contact, ContactGroup
[docs]class ContextCondition(PolymorphicModel):
model = None
identifier = "context_condition"
name = _("Context Condition")
description = _("Context Condition")
active = models.BooleanField(default=True)
[docs] def matches(self, context):
return False
[docs]class HourCondition(ContextCondition):
identifier = "hour_condition"
name = _("Day and hour")
hour_start = models.TimeField(
verbose_name=_("start time"), help_text=_("12pm is considered noon and 12am as midnight.")
)
hour_end = models.TimeField(
verbose_name=_("end time"),
help_text=_("12pm is considered noon and 12am as midnight. End time is not considered match."),
)
days = models.CharField(max_length=255, verbose_name=_("days"))
[docs] def matches(self, context):
return is_in_time_range(timezone.now(), self.hour_start, self.hour_end, self.values)
@property
def description(self):
return _("Limit the campaign to selected days.")
@property
def values(self):
return [v for v in map(int, self.days.split(","))] if self.days else []
@values.setter
def values(self, values):
self.days = ",".join(values)