Changeset 66:548534cb10d4 in stamper
- Timestamp:
- Jul 9, 2017, 10:35:58 AM (7 years ago)
- Branch:
- default
- Phase:
- public
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
stamper/filters.py
r65 r66 68 68 return self.months(current_date, months, '+') 69 69 70 def parse_number_filter(self, number_filter, future=False): 70 def parse_number_filter(self, number_filter, start_date=None, 71 future=False): 71 72 """ 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. 75 80 """ 76 81 number = None 77 82 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 78 92 79 93 if re.search(r'(\d+[dD]{1})', number_filter): 80 94 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) 85 96 86 97 elif re.search(r'(\d+[wW]{1})', number_filter): 87 98 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) 92 100 93 101 elif re.search(r'(\d+[mM]{1})', number_filter): 94 102 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) 99 104 100 105 elif re.search(r'(\d+[yY]{1})', number_filter): 101 106 number = int(number_filter.lower().replace('y', '')) 102 107 today = date.today() 108 # by default assume going backwards into the past... 109 year = today.year - number 103 110 if future: 111 #...unless told otherwise 104 112 year = today.year + number 105 else:106 year = today.year - number107 113 filtered_date = datetime(year, today.month, today.day) 108 114 … … 151 157 elif '+' in stamp_filter: 152 158 # "+" 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]) 156 172 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 159 175 else: 160 176 # Check if the user is asking for N days/weeks/months/years
Note:
See TracChangeset
for help on using the changeset viewer.