Created
September 27, 2010 11:44
-
-
Save ojii/598900 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from datetime import date, timedelta | |
from django.db.models import Q | |
def next_birthdays(qs, days, fieldname): | |
def f(*args): | |
args = list(args) | |
last = args.pop(-1) | |
names = [fieldname] + args | |
return {'__'.join(names): last} | |
now = date.today() | |
limit = now + timedelta(days=days) | |
lo_day = now.day | |
lo_month = now.month | |
hi_day = limit.day | |
hi_month = limit.month | |
q = Q(f('month', 'gte', lo_month)) | |
q &= Q(f('day', 'gte', lo_day)) | |
q &= Q(f('month', 'lt', hi_month)) | |
q &= Q(f('day', 'lt', hi_day)) | |
return qs.filter(q) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment