2022-01-30 17:40:19 -06:00
|
|
|
# -*- coding: utf-8; -*-
|
|
|
|
################################################################################
|
|
|
|
#
|
|
|
|
# Rattail -- Retail Software Framework
|
|
|
|
# Copyright © 2010-2022 Lance Edgar
|
|
|
|
#
|
|
|
|
# This file is part of Rattail.
|
|
|
|
#
|
|
|
|
# Rattail is free software: you can redistribute it and/or modify it under the
|
|
|
|
# terms of the GNU General Public License as published by the Free Software
|
|
|
|
# Foundation, either version 3 of the License, or (at your option) any later
|
|
|
|
# version.
|
|
|
|
#
|
|
|
|
# Rattail is distributed in the hope that it will be useful, but WITHOUT ANY
|
|
|
|
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
|
|
|
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
|
|
|
# details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License along with
|
|
|
|
# Rattail. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
#
|
|
|
|
################################################################################
|
|
|
|
"""
|
|
|
|
Harvest model importers
|
|
|
|
"""
|
|
|
|
|
|
|
|
from rattail import importing
|
|
|
|
from rattail_harvest.harvest.webapi import make_harvest_webapi
|
|
|
|
|
|
|
|
|
|
|
|
class ToHarvest(importing.Importer):
|
|
|
|
|
|
|
|
def setup(self):
|
|
|
|
super(ToHarvest, self).setup()
|
|
|
|
self.webapi = make_harvest_webapi(self.config)
|
|
|
|
|
|
|
|
|
|
|
|
class TimeEntryImporter(ToHarvest):
|
|
|
|
"""
|
|
|
|
Harvest time entry data importer.
|
|
|
|
"""
|
|
|
|
model_name = 'TimeEntry'
|
2022-01-30 19:34:15 -06:00
|
|
|
key = 'id'
|
2022-01-30 17:40:19 -06:00
|
|
|
supported_fields = [
|
2022-01-30 19:34:15 -06:00
|
|
|
'id',
|
2022-01-30 17:40:19 -06:00
|
|
|
'user_id',
|
|
|
|
'project_id',
|
|
|
|
'task_id',
|
|
|
|
'spent_date',
|
|
|
|
# 'started_time',
|
|
|
|
# 'ended_time',
|
|
|
|
'hours',
|
|
|
|
'notes',
|
|
|
|
]
|
|
|
|
caches_local_data = True
|
|
|
|
|
|
|
|
def cache_local_data(self, host_data=None):
|
|
|
|
"""
|
|
|
|
Fetch existing time entries from Harvest.
|
|
|
|
"""
|
|
|
|
cache = {}
|
|
|
|
entries = self.webapi.get_time_entries(**{'from': self.start_date,
|
|
|
|
'to': self.end_date})
|
|
|
|
for entry in entries:
|
|
|
|
data = self.normalize_local_object(entry)
|
|
|
|
if data:
|
|
|
|
normal = self.normalize_cache_object(entry, data)
|
|
|
|
key = self.get_cache_key(entry, normal)
|
|
|
|
cache[key] = normal
|
|
|
|
return cache
|
|
|
|
|
|
|
|
def normalize_local_object(self, entry):
|
|
|
|
data = {
|
2022-01-30 19:34:15 -06:00
|
|
|
'id': entry['id'],
|
2022-01-30 17:40:19 -06:00
|
|
|
'project_id': entry['project']['id'],
|
|
|
|
'task_id': entry['task']['id'],
|
|
|
|
'spent_date': entry['spent_date'],
|
|
|
|
# 'started_time': entry['started_time'],
|
|
|
|
# 'ended_time': entry['ended_time'],
|
|
|
|
'hours': entry['hours'],
|
|
|
|
'notes': entry['notes'],
|
|
|
|
}
|
|
|
|
|
|
|
|
if 'user_id' in self.fields:
|
|
|
|
data['user_id'] = entry['user']['id']
|
|
|
|
|
|
|
|
return data
|
|
|
|
|
2022-01-30 19:34:15 -06:00
|
|
|
def get_next_harvest_id(self):
|
|
|
|
if hasattr(self, 'next_harvest_id'):
|
|
|
|
next_id = self.next_harvest_id
|
|
|
|
else:
|
|
|
|
next_id = 1
|
|
|
|
self.next_harvest_id = next_id + 1
|
|
|
|
return -next_id
|
|
|
|
|
2022-01-30 17:40:19 -06:00
|
|
|
def create_object(self, key, host_data):
|
|
|
|
if self.dry_run:
|
|
|
|
# mock out return value
|
|
|
|
result = dict(host_data)
|
|
|
|
if 'user_id' in self.fields:
|
|
|
|
result['user'] = {'id': result['user_id']}
|
|
|
|
result['project'] = {'id': result['project_id']}
|
|
|
|
result['task'] = {'id': result['task_id']}
|
|
|
|
return result
|
|
|
|
|
|
|
|
kwargs = {
|
|
|
|
'project_id': host_data['project_id'],
|
|
|
|
'task_id': host_data['task_id'],
|
|
|
|
'spent_date': host_data['spent_date'],
|
|
|
|
# 'started_time': host_data['started_time'],
|
|
|
|
# 'ended_time': host_data['ended_time'],
|
|
|
|
'hours': host_data['hours'],
|
|
|
|
'notes': host_data['notes'],
|
|
|
|
}
|
|
|
|
if 'user_id' in self.fields:
|
|
|
|
kwargs['user_id'] = host_data['user_id']
|
|
|
|
entry = self.webapi.put_time_entry(**kwargs)
|
|
|
|
return entry
|
|
|
|
|
|
|
|
def update_object(self, obj, host_data, local_data=None, all_fields=False):
|
|
|
|
if self.dry_run:
|
|
|
|
return obj
|
|
|
|
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
def delete_object(self, obj):
|
|
|
|
if self.dry_run:
|
|
|
|
return True
|
|
|
|
|
|
|
|
raise NotImplementedError
|