Logic to jump weekends Django/python
<p>I got this function. What I need is to make some changes in my model depending on the day. As my if statements shown below, if the "ddate" is today or tomorrow make some changes in my "pull_ins" column and set it up as "Ready to ship" but if is the day after tomorrow and the next day, set "Not yet". This is working but my problem is that I need to jump weekends and keep the 4 days logic, any ideas? </p>

<p>As an example if today is Thrusday to get the ddate from today, tomorrow(friday) -----jump weekend --- monday, tuesday.</p>

<p>This is what I got:</p>

<pre><code>def Ship_status():   

    week = {0, 1, 2, 3, 4}
    deltaday = timedelta(days=1)
    today = datetime.now().date()
    day = today
    day1 = day + deltaday
    day2 = day1 + deltaday
    day3 = day2 + deltaday


    for i in Report.objects.all():
        if i.ddate.weekday() in week:
            if i.ddate == day:
                i.pull_ins = "Ready to ship"
                i.save()
            if i.date == day1:
                i.pull_ins = "Ready to ship"
                i.save()
            if i.date == day2:
                i.pull_ins = "Not yet"
                i.save()
            if i.date == day3:
                i.pull_ins = "not yet"
                i.save()
</code></pre>

<p>Thanks for your time.</p>

<p><a href="http://labix.org/python-dateutil#head-470fa22b2db72000d7abe698a5783a46b0731b57" rel="nofollow"><code>dateutil.rrule</code></a> is library you could leverage. To get the next weekday:</p>

<pre><code>from dateutil import rrule
next_weekday = rrule.rrule(rrule.DAILY, count=3, byweekday=(0, 1, 2, 3, 4), dtstart=dt))
</code></pre>

<p>So, in your query, you could do something like this:</p>

<pre><code>def compute_shipping(dt=datetime.datetime.date(), count=2):
    next_weekdays = rrule.rrule(rrule.DAILY, count=count, byweekday=(0, 1, 2, 3, 4), dtstart=dt))
   return list(next_weekdays)

#Ready to ship
Report.objects.filter(ddate__in=compute_shipping()).update(pull_ins="Ready to ship")

#For Not yet
#Query would be similar - just set the appropriate start date
</code></pre>

python django python-2.7 datetime weekday