Changeset 63:76140cb24ebd in stamper
- Timestamp:
- Jun 29, 2017, 8:28:12 AM (7 years ago)
- Branch:
- default
- Phase:
- public
- Location:
- stamper
- Files:
-
- 1 added
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
stamper/stamper.py
r61 r63 1 1 2 2 import json 3 import re4 3 import os.path 5 from datetime import datetime, date,timedelta4 from datetime import datetime, timedelta 6 5 from os import symlink, remove, makedirs 7 6 from collections import OrderedDict … … 11 10 from .http import HTTPClient 12 11 from .config import Config 12 from .filters import DateFilter 13 13 14 14 … … 29 29 self.ensure_charts_dir() 30 30 self.stamps = [] 31 self.date_filter = DateFilter(self.date_format) 31 32 32 33 def __json_load(self, filename): … … 93 94 datetime.strptime(start, self.datetime_format)) 94 95 return worktime.seconds 95 96 def validate_filter(self, stamp_filter):97 """98 Validate a given filter. Filters can have the following notation:99 100 - %Y-%m-%d: Times recorded at a given date101 102 - %Y-%m-%d--%Y-%m-%d: Times recorded between two dates103 104 - *%Y-%m-%d: Times recorded up to a given date105 106 - %Y-%m-%d*: Times recorded from a given date107 108 - N...N[d|w|m|y]: Times recorded N...N days/weeks/months/years ago109 110 Important: all date comparisons are made on datetime objects, using111 00:00 as the time (first second of the given day). This means that112 for range filters, the first day is included, but the second day is not113 """114 filter_from = None115 filter_to = None116 117 if stamp_filter is None:118 return filter_from, filter_to119 120 if '--' in stamp_filter:121 filter_from, filter_to = stamp_filter.split('--')122 filter_from = datetime.strptime(filter_from, self.date_format)123 filter_to = datetime.strptime(filter_to, self.date_format)124 125 elif stamp_filter.startswith('*'):126 filter_to = datetime.strptime(stamp_filter, '*'+self.date_format)127 filter_to = filter_to.replace(hour=0, minute=0, second=0)128 129 elif stamp_filter.endswith('*'):130 filter_from = datetime.strptime(stamp_filter, self.date_format+'*')131 filter_from = filter_from.replace(hour=0, minute=0, second=0)132 133 elif re.search(r'(\d+[dD]{1})', stamp_filter):134 number = int(stamp_filter.lower().replace('d', ''))135 delta = timedelta(days=number)136 filter_from = datetime.today() - delta137 filter_from = filter_from.replace(hour=0, minute=0, second=0)138 139 elif re.search(r'(\d+[wW]{1})', stamp_filter):140 number = int(stamp_filter.lower().replace('w', '')) * 7141 delta = timedelta(days=number)142 filter_from = datetime.today() - delta143 filter_from = filter_from.replace(hour=0, minute=0, second=0)144 145 elif re.search(r'(\d+[mM]{1})', stamp_filter):146 number = int(stamp_filter.lower().replace('m', ''))147 past = date.today()148 # start travelling in time, back to N months ago149 for n in range(number):150 past = past.replace(day=1) - timedelta(days=1)151 # Now use the year/month from the past + the current day to set152 # the proper date153 filter_from = datetime(past.year, past.month, date.today().day)154 155 elif re.search(r'(\d+[yY]{1})', stamp_filter):156 number = int(stamp_filter.lower().replace('y', ''))157 today = date.today()158 filter_from = datetime(today.year - number, today.month, today.day)159 160 else:161 # maybe they are giving us a fixed date162 try:163 filter_from = datetime.strptime(stamp_filter, self.date_format)164 except:165 # nothing to be used as a filter, go on, printing a warning166 print('[warning] invalid date filter: ' + stamp_filter)167 else:168 filter_from = filter_from.replace(hour=0, minute=0, second=0)169 filter_to = filter_from + timedelta(days=1)170 171 return filter_from, filter_to172 96 173 97 @property … … 252 176 253 177 def timeline(self, customer=None, stamp_filter=None, filter_descr=None): 254 filter_from, filter_to = self. validate_filter(stamp_filter)178 filter_from, filter_to = self.date_filter.validate(stamp_filter) 255 179 for stamp in self.stamps: 256 180 start = datetime.strptime(stamp['start'], self.datetime_format) … … 283 207 Generate charts with information from the stamps 284 208 """ 285 filter_from, filter_to = self. validate_filter(stamp_filter)209 filter_from, filter_to = self.date_filter.validate(stamp_filter) 286 210 chart = pygal.Bar(title='Work hours per day', 287 211 range=(0, self.hours_day), … … 333 257 def show_stamps(self, customer=None, stamp_filter=None, verbose=False, 334 258 sum=False, filter_descr=None): 335 filter_from, filter_to = self. validate_filter(stamp_filter)259 filter_from, filter_to = self.date_filter.validate(stamp_filter) 336 260 337 261 # If the user asks for verbose information, show it before the … … 441 365 442 366 def push_stamps(self, customer=None, stamp_filter=None, filter_descr=None): 443 filter_from, filter_to = self. validate_filter(stamp_filter)367 filter_from, filter_to = self.date_filter.validate(stamp_filter) 444 368 445 369 stamps = []
Note:
See TracChangeset
for help on using the changeset viewer.