0

What I am trying to do is to get a filter from Monday to Friday and then count the objects based on the filter. This is what I have so far:

def closed_by_count(request, template = 'count_closed_user.html'):

    date = datetime.now()

    week_1 = relativedelta(weeks=1)

    closed_by_count = Created_Ticket.objects.filter(closed_by__username='lastname.firstname').filter(date_closed__gte = date - week_1).filter(date_closed__lt = date).count()

    print closed_by_count

    payload = {'closed_by_count': closed_by_count,}    

    return render_to_response(template, payload, context_instance=RequestContext(request))

It works, but not in the right manner, right now the start I can get to work is the current day. How would I start it from any Monday to Sunday (Weekly)? Instead of the current date, so when someone looks at the count of closed by username tickets, it would only fall on that week. I hope this makes sense.

Infinixd
  • 141
  • 15

2 Answers2

1

Use the datetime module in python:

http://docs.python.org/2/library/datetime.html#datetime.date.weekday

UPDATE:

To use a Boolean flag for dates that are weekdays, you can overridde the save method in your model, and then call then call this field as a filter in your view.

First override the save method in your model:

import datetime

class MyModel(models.Model):
    date = models.DateField(auto_add_now=True)
    is_weekday = models.BooleanField(blank=True)

    # other model fields

    def save(self, *args, **kwargs):
        self.is_weekday = 4 >= datetime.date.weekday(self.date) >= 0
        super(MyModel, self).save(*args, **kwargs)

Then use the is_weekday Boolean flag to filter from within your view for objects that are weekday only:

weekday_objects = MyModel.objects.filter(is_weekday=True)

Yes, an Boolean flag is correct per your comment. Thanks for catching that.

Aaron Lelevier
  • 19,850
  • 11
  • 76
  • 111
  • On closed_by_count = Created_Ticket.objects.filter(is_weekday(date)=True).count() I get this error: "keyword can't be an expression" – Infinixd Jan 09 '14 at 04:17
  • Just discovered something, do I need to create a boolean field in my model for this to work? – Infinixd Jan 09 '14 at 06:44
  • Please see my update. Yes, a using a Boolean is correct. Let me know if you need anything else, but that should work as planned. – Aaron Lelevier Jan 09 '14 at 14:00
  • Thank you for your solution, however I went with the answer below. I think this is useful like you said for any other function I need for adding a Weekly filter. – Infinixd Jan 13 '14 at 00:19
1

Just find the last monday for the current week using the datetime module:

import datetime
today = datetime.date.today()
last_monday = today - datetime.timedelta(days=today.weekday())
one_week = datetime.timedelta(days=7)
end_of_week = last_monday + one_week

Then you can run your query as follows:

closed_by_count = Created_Ticket.objects.filter(closed_by__username='lastname.firstname'
        ).filter(date_closed__gte = last_monday).filter(date_closed__lt = end_of_week).count()

These use naive datetime objects. You can use timezone aware objects by using:

from django.utils.timezone import now
today = now().replace(hour=0,minute=0,second=0,microsecond=0)
arocks
  • 2,862
  • 1
  • 12
  • 20
  • Thank you for your solution, currently I think this works best for me since I don't have alter my models. However I had to correct some things. – Infinixd Jan 13 '14 at 00:18