so i'm still on my todo list and i want to know when an activity has passed so as to flag it as expired.
my views.py
def progress(request):
activities = ToDo.objects.all()
today = timezone.localtime(timezone.now())
context = {
'activities' : activities,
'today' : today,
}
return render(request, 'percent.html', context)
in my templates i have it as:
{% for activity in activities %}
{% if activity.end.date < today.date %}
{{activity}} <br>
{% endif %}
{% endfor %}
i'm going to add my models.py for referencing
class ToDo(models.Model):
owner = models.ForeignKey(User, on_delete=models.CASCADE)
todo = models.CharField(max_length=50)
description = models.TextField(max_length=200, blank=True)
created = models.DateField(auto_now=True)
end = models.DateField()
start = models.DateField()
completed = models.BooleanField(default=False)
def __str__(self):
return f'{self.owner} - {self.todo}'
or would it be easier to add an expired boolean field to my models? i'm so confused
CodePudding user response:
Note: You need to use Celery Beat as a scheduler.
import datetime
def expired_activity():
today = datetime.date.today()
expired_activity = ToDo.objects.filter(end=today)
if expired_activity:
expired_activity.update(completed=True)
CodePudding user response:
If you don't want to use any scheduler, you can get expired items in another request, using ORM Manager:
from django.db import models
class ToDoManager(models.Manager):
def active(self):
return self.excude(end__lt=today().date)
def expired(self):
return self.filter(end__lt=today().date)
class ToDo(models.Model):
owner = models.ForeignKey(User, on_delete=models.CASCADE)
todo = models.CharField(max_length=50)
description = models.TextField(max_length=200, blank=True)
created = models.DateField(auto_now=True)
end = models.DateField()
start = models.DateField()
completed = models.BooleanField(default=False)
objects = ToDoManager()
def __str__(self):
return f'{self.owner} - {self.todo}'
And in your view you'll get them in a different requests:
ToDo.objects.active()
ToDo.objects.expired()
Or you can change manager with setting expired flag and saving results when requesting data from db