0

Info: I am trying to make monthly billing app. Where a customer can buy a property on a monthly basis payment. i want to change the pending attribute True to False if the customer last payment 30 days old.

It is a schedule based app but i am not using django-crontab or celrey. i can use view funtion for it if user visit the pending_customers page the view can check the all and which customers the last payments date created_at if the created_at datetime is 30 days old. Then the view funtion can change the pending to False.

pending_customers view is working fine but it is changed the pending to False when i visit the page pending_customers. It could not wait for 30 days. if the customer last payment is not 30 days old then how can i hold the view funtion until for 30 days?

models.py

class Customer(models.Model):
    """Customer Model"""

    name = models.CharField(max_length=255)
    prop_select = models.ForeignKey(Property, on_delete=models.SET_NULL, null=True)
    created_on = models.DateTimeField(auto_now_add=True)
    pending = models.BooleanField(default=False)

class Payment(models.Model):
    """Payment Model"""

    customer = models.ForeignKey(Customer, null=True, on_delete=models.SET_NULL, related_name='payment')
    created_at = models.DateTimeField(auto_now_add=True)
    amount = models.DecimalField(max_digits=17, decimal_places=2, default=0.0)

    def save(self, *args, **kwargs):
        super(Payment, self).save(*args, **kwargs)
        self.customer.pending = True
        self.customer.save()

views.py

def pending_customers(request):

    queryset = Customer.objects.all()

    for i in queryset:
        for d in i.payment.filter(created_at__gte=datetime.now() - timedelta(minutes=30)).order_by('-pk')[:1]:
            d.customer.pending = False
            d.customer.save()

    context = {
        'customers': queryset,
    }
    return render(request, 'customer/pending-customers.html', context)
afi
  • 573
  • 4
  • 18

1 Answers1

-1

I think you're using the wrong condition to mark the pending_customer to False. Right now you're doing the opposite (marking those less than or equal to 30)

From what I gather you want to do created_at__gte=datetime.now() - timedelta(days=30))

That is "get me all the payments that are 30 days or older"

  • my question is updated now i also try with `created_at__gte=datetime.now() - timedelta(days=30))` but it's not working... – afi Aug 01 '21 at 07:27
  • Can you elaborate on "not working" a bit more? What's going on, what are you expecting and what's the actual response? – Tarun Kumar Aug 01 '21 at 07:30
  • Dear i want to check the last payment of customer if the customer last payment is 30 day old then the `pending` False else True... but my code and your code working same. It's not checking the last payment object date it's just update the `pending` false – afi Aug 01 '21 at 07:35
  • See the `save` function of Payment class, there's no `pending_customer` field. The `pending` field is probably being set `False` by default. – Tarun Kumar Aug 01 '21 at 07:39
  • yes the `pending` is automatically True when new payment object is created. if this payment is 30 days old then customer goes in `pending` False this is the logic i want to do... – afi Aug 01 '21 at 07:45
  • This is the third time you're editing the code. Please take a better look at what you've written before posting on Stack Overflow. You're logic is correct, just the code is riddled with wrong variable names and invalid expressions. – Tarun Kumar Aug 01 '21 at 07:56
  • i am updating my code to show you your code is not working... – afi Aug 01 '21 at 08:29