https://dpaste.de/f9kb

Are you sure to delete this snippet? No, don't delete
  1. class Company (models.Model):
  2. ....
  3. def project_insurance_valid(self, project_pk):
  4. insurance_set = self.insurance_set.filter(project=project_pk)
  5. expire_within_30_days = False
  6. for insurance in insurance_set:
  7. if insurance.expired:
  8. return False
  9. if insurance.expire_within_30_days:
  10. expire_within_30_days = True
  11. if expire_within_30_days:
  12. return "expires within 30 days"
  13. else:
  14. return True
  15. class Insurance(models.Model):
  16. project = models.ForeignKey(Project,
  17. on_delete=models.CASCADE,
  18. null= False,
  19. blank= False)
  20. company = models.ForeignKey(Company,
  21. on_delete=models.CASCADE,
  22. null=False,
  23. blank=False)
  24. @property
  25. def expired(self):
  26. return self.expiry_date < date.today()
  27. @property
  28. def expire_within_30_days(self):
  29. today = date.today()
  30. date_30_days = today + timedelta(days=30)
  31. return today <= self.expiry_date <= date_30_days

Edit this Snippet