Skip to content

Instantly share code, notes, and snippets.

@ikks
Created September 25, 2014 21:27
Show Gist options
  • Save ikks/da4d67144018ab780a16 to your computer and use it in GitHub Desktop.
Save ikks/da4d67144018ab780a16 to your computer and use it in GitHub Desktop.
An approach to have a weekly report
def weekly(query, weeks, initial, final):
u"""Returns a partitioned result grouped by 7 days.
query: Initial query
weeks: Max Number of weeks
initial: initial part of the week
final: end part of the week
You can use like the following:
previous = weekly(
my_query, 8, 'validated_at__gt', 'validated_at__lte',
)
"""
previous = []
nextw = -7 * weeks
week = 0
while week < weeks:
ini = now() + timedelta(nextw)
end = now() + timedelta(nextw + 7)
previous.insert(0, query.filter(
**{initial: ini, final: end}
).count())
nextw += 7
week += 1
return previous
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment