def save(self, commit=True): instance = super().save(commit) if commit: usage = self.cleaned_data['usage'] offers = self.cleaned_data['offers'] if instance is not None: # Update vouchers in this set for i, voucher in enumerate( instance.vouchers.order_by('date_created')): voucher.name = "%s - %d" % (instance.name, i + 1) voucher.usage = usage voucher.start_datetime = instance.start_datetime voucher.end_datetime = instance.end_datetime voucher.save() voucher.offers.set(offers) # Add vouchers to this set vouchers_added = False for i in range(instance.vouchers.count(), instance.count): voucher = Voucher.objects.create( name="%s - %d" % (instance.name, i + 1), code=get_unused_code(length=instance.code_length), voucher_set=instance, usage=usage, start_datetime=instance.start_datetime, end_datetime=instance.end_datetime) voucher.offers.add(*offers) vouchers_added = True if vouchers_added: instance.update_count() return instance
def vouchers(obj, create, extracted, **kwargs): if not create: return offer = ConditionalOfferFactory(offer_type=ConditionalOffer.VOUCHER) for i in range(0, obj.count): voucher = Voucher.objects.create(name="%s - %d" % (obj.name, i + 1), code=get_unused_code(length=obj.code_length), voucher_set=obj, usage=Voucher.MULTI_USE, start_datetime=obj.start_datetime, end_datetime=obj.end_datetime) voucher.offers.add(offer)
def add_new(self): """Add a new voucher to this set""" Voucher = get_model('voucher', 'Voucher') code = get_unused_code(length=self.code_length) voucher = Voucher.objects.create(name=self.name, code=code, voucher_set=self, usage=Voucher.SINGLE_USE, start_datetime=self.start_datetime, end_datetime=self.end_datetime) if self.offer: voucher.offers.add(self.offer) return voucher
def add_new(self): """Add a new voucher to this set""" Voucher = get_model('voucher', 'Voucher') code = get_unused_code(length=self.code_length) voucher = Voucher.objects.create( name=self.name, code=code, voucher_set=self, usage=Voucher.SINGLE_USE, start_datetime=self.start_datetime, end_datetime=self.end_datetime) if self.offer: voucher.offers.add(self.offer) return voucher