def form_valid(self, form): condition = Condition.objects.create( range=form.cleaned_data['benefit_range'], type=Condition.COUNT, value=1) benefit = Benefit.objects.create( range=form.cleaned_data['benefit_range'], type=form.cleaned_data['benefit_type'], value=form.cleaned_data['benefit_value']) name = form.cleaned_data['name'] offer = ConditionalOffer.objects.create( name=get_offer_name(name), offer_type=ConditionalOffer.VOUCHER, benefit=benefit, condition=condition, ) VoucherSet.objects.create( name=name, count=form.cleaned_data['count'], code_length=form.cleaned_data['code_length'], description=form.cleaned_data['description'], start_datetime=form.cleaned_data['start_datetime'], end_datetime=form.cleaned_data['end_datetime'], offer=offer, ) return HttpResponseRedirect(self.get_success_url())
def form_valid(self, form): # Create offer and benefit condition = Condition.objects.create( range=form.cleaned_data['benefit_range'], type=Condition.COUNT, value=1) benefit = Benefit.objects.create( range=form.cleaned_data['benefit_range'], type=form.cleaned_data['benefit_type'], value=form.cleaned_data['benefit_value']) name = form.cleaned_data['name'] offer = ConditionalOffer.objects.create( name=get_offer_name(name), offer_type=ConditionalOffer.VOUCHER, benefit=benefit, condition=condition, exclusive=form.cleaned_data['exclusive'], ) voucher = Voucher.objects.create( name=name, code=form.cleaned_data['code'], usage=form.cleaned_data['usage'], start_datetime=form.cleaned_data['start_datetime'], end_datetime=form.cleaned_data['end_datetime'], ) voucher.offers.add(offer) return HttpResponseRedirect(self.get_success_url())
def delete_unused_related_conditional_offer(instance, **kwargs): voucher = instance # the object is no longer in the database try: conditional_offer = ConditionalOffer.objects.get( name=get_offer_name(voucher.name), offer_type=ConditionalOffer.VOUCHER ) except (ConditionalOffer.DoesNotExist, ConditionalOffer.MultipleObjectsReturned): pass else: # Only delete if not used by other vouchers if not conditional_offer.vouchers.exists(): conditional_offer.delete()
def form_valid(self, form): voucherset = form.save() if not voucherset.offer: condition = Condition.objects.create( range=form.cleaned_data['benefit_range'], type=Condition.COUNT, value=1) benefit = Benefit.objects.create( range=form.cleaned_data['benefit_range'], type=form.cleaned_data['benefit_type'], value=form.cleaned_data['benefit_value']) name = form.cleaned_data['name'] offer, __ = ConditionalOffer.objects.update_or_create( name=get_offer_name(name), defaults=dict( offer_type=ConditionalOffer.VOUCHER, benefit=benefit, condition=condition, )) voucherset.offer = offer for voucher in voucherset.vouchers.all(): if offer not in voucher.offers.all(): voucher.offers.add(offer) else: benefit = voucherset.offer.benefit benefit.range = form.cleaned_data['benefit_range'] benefit.type = form.cleaned_data['benefit_type'] benefit.value = form.cleaned_data['benefit_value'] benefit.save() condition = voucherset.offer.condition condition.range = form.cleaned_data['benefit_range'] condition.save() voucherset.save() return HttpResponseRedirect(self.get_success_url())
def form_valid(self, form): voucher = self.get_voucher() voucher.name = form.cleaned_data['name'] voucher.code = form.cleaned_data['code'] voucher.usage = form.cleaned_data['usage'] voucher.start_datetime = form.cleaned_data['start_datetime'] voucher.end_datetime = form.cleaned_data['end_datetime'] voucher.save() offer = voucher.offers.first() offer.condition.range = form.cleaned_data['benefit_range'] offer.condition.save() offer.exclusive = form.cleaned_data['exclusive'] offer.name = get_offer_name(voucher.name) offer.save() benefit = voucher.benefit benefit.range = form.cleaned_data['benefit_range'] benefit.type = form.cleaned_data['benefit_type'] benefit.value = form.cleaned_data['benefit_value'] benefit.save() return HttpResponseRedirect(self.get_success_url())