Changeset 66:548534cb10d4 in stamper


Ignore:
Timestamp:
Jul 9, 2017, 10:35:58 AM (7 years ago)
Author:
Borja Lopez <borja@…>
Branch:
default
Phase:
public
Message:

Fixed the filtering based on N[d|w|m|y] from/to a given date.

Now you can go backwards/forward as many days/weeks/months/years, from
a given date or to a given date, as you like.

Examples:

  • 2017-07-01+3d: return data from the 2017-07-01 to 2017-07-04
  • 2017-07-01-1w: return data from 2017-06-26 to 2017-07-01

The -N[d|w|m|y] filter works pretty much like what we already had for
gathering data from N days/weeks/months/years ago from the current
date.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • stamper/filters.py

    r65 r66  
    6868        return self.months(current_date, months, '+')
    6969
    70     def parse_number_filter(self, number_filter, future=False):
     70    def parse_number_filter(self, number_filter, start_date=None,
     71                            future=False):
    7172        """
    72         If a filter contains a number of days/months/years, try to find
    73         out which filter it is exactly to apply it, then calculate the
    74         number of days and find
     73        Given a numeric filter with the N[d|w|m|y] pattern, return both
     74        the number of days/months/years that apply as a filter, + the
     75        date N[d|w|m|y] ago from start_date (which defaults to today if
     76        None is passed).
     77
     78        If future is True, it will return a date in the future, not from
     79        the past.
    7580        """
    7681        number = None
    7782        filtered_date = None
     83        start_date = start_date or datetime.today()
     84
     85        # set which methods use to process the date, going backwards into the
     86        # past as a default, unless we are told otherwise
     87        filter_days = self.days_ago
     88        filter_months = self.months_ago
     89        if future:
     90            filter_days = self.days_future
     91            filter_months = self.months_future
    7892
    7993        if re.search(r'(\d+[dD]{1})', number_filter):
    8094            number = int(number_filter.lower().replace('d', ''))
    81             if future:
    82                 filtered_date = self.days_future(datetime.today(), number)
    83             else:
    84                 filtered_date = self.days_ago(datetime.today(), number)
     95            filtered_date = filter_days(start_date, number)
    8596
    8697        elif re.search(r'(\d+[wW]{1})', number_filter):
    8798            number = int(number_filter.lower().replace('w', '')) * 7
    88             if future:
    89                 filtered_date = self.days_future(datetime.today(), number)
    90             else:
    91                 filtered_date = self.days_ago(datetime.today(), number)
     99            filtered_date = filter_days(start_date, number)
    92100
    93101        elif re.search(r'(\d+[mM]{1})', number_filter):
    94102            number = int(number_filter.lower().replace('m', ''))
    95             if future:
    96                 filtered_date = self.months_future(datetime.today(), number)
    97             else:
    98                 filtered_date = self.months_ago(datetime.today(), number)
     103            filtered_date = filter_months(start_date, number)
    99104
    100105        elif re.search(r'(\d+[yY]{1})', number_filter):
    101106            number = int(number_filter.lower().replace('y', ''))
    102107            today = date.today()
     108            # by default assume going backwards into the past...
     109            year = today.year - number
    103110            if future:
     111                #...unless told otherwise
    104112                year = today.year + number
    105             else:
    106                 year = today.year - number
    107113            filtered_date = datetime(year, today.month, today.day)
    108114
     
    151157        elif '+' in stamp_filter:
    152158            # "+" filtering works with a date following by "+", a number and
    153             # a letter (d|w|m|y) which sets the number of days, months, years
    154             # we would like to go into the future
    155             filter_to, number = stamp_filter.split('+')
     159            # a letter (d|w|m|y) which sets the number of days, weeks, months,
     160            # years we would like to go into the future
     161            filter_from, number = stamp_filter.split('+')
     162            filter_from = datetime.strptime(filter_from, self.date_format)
     163            number, filter_to = self.parse_number_filter(
     164                number, filter_from, future=True)
     165
     166        elif stamp_filter.count('-') == 3:
     167            # "-" filtering works with a date followed by "-", a number and
     168            # a letter (d|w|m|y) which sets the number of days, weeks, months,
     169            # years we would like to go backwards into the past
     170            year, month, day, number = stamp_filter.split('-')
     171            filter_to = '-'.join([year, month, day])
    156172            filter_to = datetime.strptime(filter_to, self.date_format)
    157             number, filter_from = self.parse_number_filter(number)
    158             filter_from = self.days_ago(filter_to, int(number))
     173            number, filter_from = self.parse_number_filter(number, filter_to)
     174
    159175        else:
    160176            # Check if the user is asking for N days/weeks/months/years
Note: See TracChangeset for help on using the changeset viewer.