Changeset 52:8d45fe507fa4 in stamper


Ignore:
Timestamp:
Nov 8, 2014, 1:58:59 AM (10 years ago)
Author:
Borja Lopez <borja@…>
Branch:
default
Phase:
public
Message:

Push stamps to a remote server

File:
1 edited

Legend:

Unmodified
Added
Removed
  • stamper/stamper.py

    r43 r52  
    99import pygal
    1010
     11from http import HTTPClient
     12
    1113
    1214STAMPS_FILE = os.path.expanduser('~/.workstamps.json')
     
    1719HOURS_DAY = 8
    1820SECS_DAY = HOURS_DAY * 60 * 60
     21REMOTE_BASE_URL = 'https://localhost'
     22REMOTE_PUSH_URL = '/stamper/listen/'  # IMPORTANT: keep the trailing /
     23REMOTE_USER = None
     24REMOTE_PASSWORD = None
    1925
    2026
     
    427433        print('[warning] ' + str(len(stamps)) + ' stamps merged')
    428434        print('[warning] remember to review the resulting stamps file')
     435
     436    def push_stamps(self, customer=None, filter_from=None, filter_to=None):
     437        stamps = []
     438        for stamp in self.stamps:
     439            if stamp['customer']:
     440                if customer and customer != stamp['customer']:
     441                    continue
     442                start = datetime.strptime(stamp['start'], DATETIME_FORMAT)
     443                start_day = start.strftime('%Y-%m-%d')
     444                end = datetime.strptime(stamp['end'], DATETIME_FORMAT)
     445                if filter_from and start < filter_from:
     446                    # if there is a filter setting a starting date for the
     447                    # report and the current stamp is from an earlier date, do
     448                    # not add it to the totals
     449                    continue
     450                if filter_to and start > filter_to:
     451                    # similar for the end date
     452                    continue
     453                stamps.append(stamp)
     454        stamps = json.dumps(stamps, indent=4)
     455        http_client = HTTPClient(REMOTE_BASE_URL, REMOTE_USER, REMOTE_PASSWORD)
     456        http_client.post(REMOTE_PUSH_URL, {'stamps': stamps})
Note: See TracChangeset for help on using the changeset viewer.