Beispiel #1
0
    def __init__(self, *args, **kwargs):
        self.request = kwargs.pop("request")
        self.shop = get_shop(self.request)
        super(HappyHourForm, self).__init__(*args, **kwargs)

        if self.instance.pk:
            self.fields["discounts"] = ObjectSelect2MultipleField(
                label=_("Product Discounts"),
                help_text=_("Select discounts for this happy hour."),
                model=Discount,
                required=False,
            )
            initial_discounts = self.instance.discounts.all(
            ) if self.instance.pk else []
            self.fields["discounts"].initial = initial_discounts
            self.fields["discounts"].widget.choices = [
                (discount.pk, force_text(discount))
                for discount in initial_discounts
            ]

        if self.instance.pk:
            weekdays, from_hour, to_hour = _get_initial_data_for_time_ranges(
                self.instance)
            if weekdays and from_hour and to_hour:
                self.fields["weekdays"].initial = weekdays
                self.fields["from_hour"].initial = from_hour
                self.fields["to_hour"].initial = to_hour

        # Since we touch these views in init we need to reset some
        # widgets and help texts after setting the initial values.
        self.fields["from_hour"].widget = TimeInput(attrs={"class": "time"})
        self.fields["to_hour"].widget = TimeInput(attrs={"class": "time"})
        help_texts = [
            ("from_hour",
             _("12pm is considered noon and 12am as midnight. Start hour is included to the discount."
               )),
            ("to_hour",
             _("12pm is considered noon and 12am as midnight. End hours is included to the discount."
               )),
            ("weekdays", _("Weekdays the happy hour is active.")),
        ]
        for field, help_text in help_texts:
            self.fields[field].help_text = help_text
 class Meta(BaseRuleModelForm.Meta):
     model = HourCondition
     widgets = {
         "hour_start": TimeInput(),
         "hour_end": TimeInput(),
     }