def by_date model None def decorator func def wrapper request args kwa

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
def by_date(model=None):
def decorator(func):
def wrapper(request, *args, **kwargs):
if kwargs['range'] == 'day':
date = datetime.date(int(kwargs['year']), int(kwargs['month']), int(kwargs['day']))
object_list = model.objects.filter(date__range=(datetime.datetime.combine(date, datetime.time.min), datetime.datetime.combine(date, datetime.time.max)))
elif kwargs['range'] == 'month':
start_date = datetime.date(int(kwargs['year']), int(kwargs['month']), 1)
end_date = datetime.date(int(kwargs['year']), int(kwargs['month']), calendar.monthrange(int(kwargs['year']), int(kwargs['month']))[1])
object_list = model.objects.filter(date__range=(datetime.datetime.combine(start_date, datetime.time.min), datetime.datetime.combine(end_date, datetime.time.max)))
elif kwargs['range'] == 'year':
start_date = datetime.date(int(kwargs['year']), 1, 1)
end_date = datetime.date(int(kwargs['year']), start_date.max.month, calendar.monthrange(int(kwargs['year']), int(kwargs['month']))[1])
object_list = model.objects.filter(date__range=(datetime.datetime.combine(start_date, datetime.time.min), datetime.datetime.combine(end_date, datetime.time.max)))
output = func(request, *args, **kwargs)
if isinstance(output, dict):
output.update({'paginate_qs': object_list})
return output
else:
return output
return wrapper
return decorator