I am trying to set a parameter for a boolean from True to false after a time frame.
For my limited knowledge in Python and Django, I am trying to learn the concept and the logic so that I can apply it in different other places in the project.
here is the Models.py
class Coupon(models.Model):
code = models.CharField(max_length=15, unique=True)
valid_from = models.DateTimeField(blank=True, null=True)
valid_to = models.DateTimeField(blank=True, null=True)
active = models.BooleanField(default=True)
How do set that when the time frame is after valid_to
the Active=status becomes False
here is the views.py
class AddCouponView(View):
def post(self, *args, **kwargs):
now = timezone.now()
form = CouponForm(self.request.POST or None)
if form.is_valid():
try:
code = form.cleaned_data.get('code')
order = Order.objects.get(
user=self.request.user, ordered=False)
coupon = Coupon.objects.filter(code__iexact=code, valid_from__lte=now, valid_to__gte=now).exclude(
order__user=self.request.user, max_value__lte=F('used')).first()
if not coupon:
messages.error(self.request, 'You can\'t use same coupon again, or coupon does not exist')
return redirect('core:checkout')
else:
try:
coupon.used += 1
coupon.save()
order.coupon = coupon
order.save()
messages.success(self.request, "Successfully added coupon")
except:
messages.info(self.request, "Max level exceeded for coupon")
return redirect('core:checkout')
except ObjectDoesNotExist:
messages.info(self.request, "You do not have an active order")
return redirect('core:checkout')