def get_health_by_date(cls, user, range_type, date_str): """ Get queryset of health datas by range :param user: user object :param range_type: dates range type - week, month or year :param date_str: date string :return: queryset of health datas :rtype: queryset """ date_format = get_date_format(range_type) # get health queryset health = cls.objects.filter(user=user).order_by("related_date") if range_type in (ChartTimeRange.YEAR, ChartTimeRange.MONTH): date = datetime.strptime(date_str, date_format) health = health.filter(related_date__year=date.strftime("%Y")) if range_type == ChartTimeRange.MONTH: health = health.filter(related_date__month=date.strftime("%m")) elif range_type == ChartTimeRange.WEEK: date = datetime.strptime(date_str, date_format) start_date = date.strftime("%Y-%m-%d") end_date = date + timedelta(days=6) end_date = end_date.strftime("%Y-%m-%d") health = health.filter(related_date__range=[start_date, end_date]) return health
def __init__(self, user, range_type, date=None): self.date_format = get_date_format(range_type) self.range_type = range_type self.user = user if date: self.date = datetime.strptime(date, self.date_format) self.queryset = self._get_queryset()