You can simply factor this unpublish_date
into your queries, for example:
articles_to_show = Articles.objects.filter(is_published=True,
unpublish_date__gt=datetime.date.today())
This is likely to make your code more complicated, though. You'd have to rewrite all your queries to be aware of the unpublish_date
.
Cron works for this sort of thing but is only good for changing things in large batches once or a few times a day. It doesn't scale well when you have a large batch of tasks to do or want more fine-grained scheduling.
If running a task via cron once per day is sufficient, do that. It might be worth investigating more advanced options such as message queues, though.
To make a scheduled task which will run at a precise time (provided a worker is available), I would use Celery.
from celery.decorators import task
@task
def unpublish(article_pk):
article = Article.objects.get(pk=article_pk)
article.is_published = False
article.save()
# Unpublish article in exactly 7 days from now
from datetime import datetime, timedelta
unpublish.apply_async([article.pk], eta=datetime.now() + datetime.timedelta(7))