Beispiel #1
0
def _update_query_with_dates(q: AdvancedQuery, date_data: MultiDict) \
        -> AdvancedQuery:
    filter_by = date_data['filter_by']
    if filter_by == 'all_dates':  # Nothing to do; all dates by default.
        return q
    elif filter_by == 'past_12':
        one_year_ago = date.today() - relativedelta(months=12)
        # Fix for these typing issues is coming soon!
        #  See: https://github.com/python/mypy/pull/4397
        q.date_range = DateRange(  # type: ignore
            start_date=datetime(year=one_year_ago.year,
                                month=one_year_ago.month,
                                day=1,
                                hour=0,
                                minute=0,
                                second=0,
                                tzinfo=EASTERN))
    elif filter_by == 'specific_year':
        q.date_range = DateRange(  # type: ignore
            start_date=datetime(year=date_data['year'].year,
                                month=1,
                                day=1,
                                hour=0,
                                minute=0,
                                second=0,
                                tzinfo=EASTERN),
            end_date=datetime(year=date_data['year'].year + 1,
                              month=1,
                              day=1,
                              hour=0,
                              minute=0,
                              second=0,
                              tzinfo=EASTERN),
        )
    elif filter_by == 'date_range':
        if date_data['from_date']:
            date_data['from_date'] = datetime.combine(  # type: ignore
                date_data['from_date'],
                datetime.min.time(),
                tzinfo=EASTERN)
        if date_data['to_date']:
            date_data['to_date'] = datetime.combine(  # type: ignore
                date_data['to_date'],
                datetime.min.time(),
                tzinfo=EASTERN)

        q.date_range = DateRange(  # type: ignore
            start_date=date_data['from_date'],
            end_date=date_data['to_date'],
        )

    if q.date_range:
        q.date_range.date_type = date_data['date_type']
    return q
Beispiel #2
0
def _update_query_with_dates(q: AdvancedQuery,
                             date_data: MultiDict) -> AdvancedQuery:
    filter_by = date_data["filter_by"]
    if filter_by == "all_dates":  # Nothing to do; all dates by default.
        return q
    elif filter_by == "past_12":
        one_year_ago = date.today() - relativedelta(months=12)
        # Fix for these typing issues is coming soon!
        #  See: https://github.com/python/mypy/pull/4397
        q.date_range = DateRange(  # type: ignore
            start_date=datetime(
                year=one_year_ago.year,
                month=one_year_ago.month,
                day=1,
                hour=0,
                minute=0,
                second=0,
                tzinfo=consts.EASTERN,
            ))
    elif filter_by == "specific_year":
        q.date_range = DateRange(  # type: ignore
            start_date=datetime(
                year=date_data["year"].year,
                month=1,
                day=1,
                hour=0,
                minute=0,
                second=0,
                tzinfo=consts.EASTERN,
            ),
            end_date=datetime(
                year=date_data["year"].year + 1,
                month=1,
                day=1,
                hour=0,
                minute=0,
                second=0,
                tzinfo=consts.EASTERN,
            ),
        )
    elif filter_by == "date_range":
        if date_data["from_date"]:
            date_data["from_date"] = datetime.combine(  # type: ignore
                date_data["from_date"],
                datetime.min.time(),
                tzinfo=consts.EASTERN,
            )
        if date_data["to_date"]:
            date_data["to_date"] = datetime.combine(  # type: ignore
                date_data["to_date"],
                datetime.min.time(),
                tzinfo=consts.EASTERN,
            )

        q.date_range = DateRange(  # type: ignore
            start_date=date_data["from_date"],
            end_date=date_data["to_date"])

    if q.date_range:
        q.date_range.date_type = date_data["date_type"]
    return q