author Borja Lopez Sun, 09 Jul 2017 10:35:58 +0200 changeset 66 548534cb10d4 parent 65 25b55b621ea4 child 67 4303a45e8a2f
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.
 stamper/filters.py file | annotate | diff | comparison | revisions
```--- a/stamper/filters.py	Sun Jul 09 09:47:35 2017 +0200
+++ b/stamper/filters.py	Sun Jul 09 10:35:58 2017 +0200
@@ -67,43 +67,49 @@
"""
return self.months(current_date, months, '+')

-    def parse_number_filter(self, number_filter, future=False):
+    def parse_number_filter(self, number_filter, start_date=None,
+                            future=False):
"""
-        If a filter contains a number of days/months/years, try to find
-        out which filter it is exactly to apply it, then calculate the
-        number of days and find
+        Given a numeric filter with the N[d|w|m|y] pattern, return both
+        the number of days/months/years that apply as a filter, + the
+        date N[d|w|m|y] ago from start_date (which defaults to today if
+        None is passed).
+
+        If future is True, it will return a date in the future, not from
+        the past.
"""
number = None
filtered_date = None
+        start_date = start_date or datetime.today()
+
+        # set which methods use to process the date, going backwards into the
+        # past as a default, unless we are told otherwise
+        filter_days = self.days_ago
+        filter_months = self.months_ago
+        if future:
+            filter_days = self.days_future
+            filter_months = self.months_future

if re.search(r'(\d+[dD]{1})', number_filter):
number = int(number_filter.lower().replace('d', ''))
-            if future:
-                filtered_date = self.days_future(datetime.today(), number)
-            else:
-                filtered_date = self.days_ago(datetime.today(), number)
+            filtered_date = filter_days(start_date, number)

elif re.search(r'(\d+[wW]{1})', number_filter):
number = int(number_filter.lower().replace('w', '')) * 7
-            if future:
-                filtered_date = self.days_future(datetime.today(), number)
-            else:
-                filtered_date = self.days_ago(datetime.today(), number)
+            filtered_date = filter_days(start_date, number)

elif re.search(r'(\d+[mM]{1})', number_filter):
number = int(number_filter.lower().replace('m', ''))
-            if future:
-                filtered_date = self.months_future(datetime.today(), number)
-            else:
-                filtered_date = self.months_ago(datetime.today(), number)
+            filtered_date = filter_months(start_date, number)

elif re.search(r'(\d+[yY]{1})', number_filter):
number = int(number_filter.lower().replace('y', ''))
today = date.today()
+            # by default assume going backwards into the past...
+            year = today.year - number
if future:
+                #...unless told otherwise
year = today.year + number
-            else:
-                year = today.year - number
filtered_date = datetime(year, today.month, today.day)

return number, filtered_date
@@ -150,12 +156,22 @@

elif '+' in stamp_filter:
# "+" filtering works with a date following by "+", a number and
-            # a letter (d|w|m|y) which sets the number of days, months, years
-            # we would like to go into the future
-            filter_to, number = stamp_filter.split('+')
+            # a letter (d|w|m|y) which sets the number of days, weeks, months,
+            # years we would like to go into the future
+            filter_from, number = stamp_filter.split('+')
+            filter_from = datetime.strptime(filter_from, self.date_format)
+            number, filter_to = self.parse_number_filter(
+                number, filter_from, future=True)
+
+        elif stamp_filter.count('-') == 3:
+            # "-" filtering works with a date followed by "-", a number and
+            # a letter (d|w|m|y) which sets the number of days, weeks, months,
+            # years we would like to go backwards into the past
+            year, month, day, number = stamp_filter.split('-')
+            filter_to = '-'.join([year, month, day])
filter_to = datetime.strptime(filter_to, self.date_format)
-            number, filter_from = self.parse_number_filter(number)
-            filter_from = self.days_ago(filter_to, int(number))
+            number, filter_from = self.parse_number_filter(number, filter_to)
+
else:
# Check if the user is asking for N days/weeks/months/years
number, filter_from = self.parse_number_filter(stamp_filter)```