Source code for shoop.campaigns.admin_module.forms._basket

# This file is part of Shoop.
#
# Copyright (c) 2012-2016, Shoop Ltd. All rights reserved.
#
# This source code is licensed under the AGPLv3 license found in the
# LICENSE file in the root directory of this source tree.
from django import forms
from django.db.models import Q
from django.utils.translation import ugettext_lazy as _

from shoop.campaigns.models import BasketCampaign, Coupon

from ._base import BaseCampaignForm


[docs]class BasketCampaignForm(BaseCampaignForm):
[docs] class Meta(BaseCampaignForm.Meta): model = BasketCampaign
def __init__(self, *args, **kwargs): super(BasketCampaignForm, self).__init__(*args, **kwargs) coupon_code_choices = [('', '')] + list( Coupon.objects.filter( Q(active=True), Q(campaign=None) | Q(campaign=self.instance) ).values_list("pk", "code") ) field_kwargs = dict(choices=coupon_code_choices, required=False) field_kwargs["help_text"] = _("Define the required coupon for this campaign.") if self.instance.pk and self.instance.coupon: field_kwargs["initial"] = self.instance.coupon.pk self.fields["coupon"] = forms.ChoiceField(**field_kwargs)
[docs] def clean_coupon(self): coupon = self.cleaned_data.get("coupon") if coupon: coupon = Coupon.objects.get(pk=coupon) return coupon or None