continued work on sync engine
still left to do: create benchcoachobjects when needed.
This commit is contained in:
@@ -7,27 +7,107 @@ import teamsnap.models
|
|||||||
from benchcoach.utils.sync_engine import AbstractSyncEngine
|
from benchcoach.utils.sync_engine import AbstractSyncEngine
|
||||||
|
|
||||||
class TeamsnapSyncEngine(AbstractSyncEngine):
|
class TeamsnapSyncEngine(AbstractSyncEngine):
|
||||||
models = [Availability, Player, Team, Positioning, Event, Venue]
|
models = [
|
||||||
|
Availability,
|
||||||
|
Player,
|
||||||
|
Team,
|
||||||
|
# Positioning, # Not Implemented
|
||||||
|
Event,
|
||||||
|
Venue
|
||||||
|
]
|
||||||
|
|
||||||
def __init__(self, managed_team_teamsnap_id, teamsnap_token):
|
def __init__(self, managed_team_teamsnap_id, teamsnap_token):
|
||||||
self.managed_teamsnap_team_id = managed_team_teamsnap_id
|
self.managed_teamsnap_team_id = managed_team_teamsnap_id
|
||||||
self.client = TeamSnap(token=teamsnap_token)
|
self.client = TeamSnap(token=teamsnap_token)
|
||||||
|
|
||||||
model_map = {
|
|
||||||
Availability: teamsnap.models.Availability,
|
|
||||||
Player: teamsnap.models.Member,
|
|
||||||
Team: teamsnap.models.Team,
|
|
||||||
Positioning: teamsnap.models.LineupEntry,
|
|
||||||
Event: teamsnap.models.Event,
|
|
||||||
Venue: teamsnap.models.Location
|
|
||||||
}
|
|
||||||
|
|
||||||
def _update_teamsnapdb_from_teamsnapapi(self, teamsnap_instance):
|
def _update_teamsnapdb_from_teamsnapapi(self, teamsnap_instance):
|
||||||
|
ApiObject = {
|
||||||
|
teamsnap.models.Availability:teamsnap.teamsnap.api.Availability,
|
||||||
|
teamsnap.models.Member:teamsnap.teamsnap.api.Member,
|
||||||
|
teamsnap.models.Team:teamsnap.teamsnap.api.Team,
|
||||||
|
teamsnap.models.Opponent:teamsnap.teamsnap.api.Opponent,
|
||||||
|
# teamsnap.models.LineupEntry # Not Implemented
|
||||||
|
teamsnap.models.Event:teamsnap.teamsnap.api.Event,
|
||||||
|
teamsnap.models.Location:teamsnap.teamsnap.api.Location
|
||||||
|
}.get(teamsnap_instance._meta.model)
|
||||||
teamsnap_model = teamsnap_instance._meta.model
|
teamsnap_model = teamsnap_instance._meta.model
|
||||||
new_data = teamsnap_instance.ApiObject.get(client=self.client, id=teamsnap_instance.id).data
|
new_data = ApiObject.get(client=self.client, id=teamsnap_instance.id).data
|
||||||
obj, created = teamsnap_model.update_or_create_from_teamsnap_api(new_data)
|
obj, created = self._update_or_create_from_teamsnapapi(teamsnap_model, new_data)
|
||||||
return [(obj, created)]
|
return [(obj, created)]
|
||||||
|
|
||||||
|
def _update_or_create_from_teamsnapapi(self, teamsnap_model, teamsnap_data, create_benchcoach_object = False):
|
||||||
|
related_objects = {}
|
||||||
|
fields = ['id', 'created_at', 'updated_at']
|
||||||
|
if teamsnap_model == teamsnap.models.Event:
|
||||||
|
fields += [
|
||||||
|
'label',
|
||||||
|
'start_date',
|
||||||
|
'formatted_title',
|
||||||
|
'points_for_opponent',
|
||||||
|
'points_for_team',
|
||||||
|
'is_game',
|
||||||
|
'game_type'
|
||||||
|
]
|
||||||
|
if teamsnap_data.get('location_id'):
|
||||||
|
related_objects['location'] = self._update_or_create_from_teamsnapapi(
|
||||||
|
teamsnap.models.Location,
|
||||||
|
{'id':teamsnap_data['location_id']}
|
||||||
|
)
|
||||||
|
if teamsnap_data.get('opponent_id'):
|
||||||
|
related_objects['opponent'] = self._update_or_create_from_teamsnapapi(
|
||||||
|
teamsnap.models.Opponent,
|
||||||
|
{'id':teamsnap_data['opponent_id']}
|
||||||
|
)
|
||||||
|
|
||||||
|
elif teamsnap_model == teamsnap.models.Opponent:
|
||||||
|
fields += ['name']
|
||||||
|
|
||||||
|
elif teamsnap_model == teamsnap.models.Team:
|
||||||
|
fields += ['name']
|
||||||
|
|
||||||
|
elif teamsnap_model == teamsnap.models.Location:
|
||||||
|
fields += ['name']
|
||||||
|
|
||||||
|
elif teamsnap_model == teamsnap.models.Member:
|
||||||
|
fields += [
|
||||||
|
'first_name',
|
||||||
|
'last_name',
|
||||||
|
'jersey_number',
|
||||||
|
'is_non_player'
|
||||||
|
]
|
||||||
|
elif teamsnap_model == teamsnap.models.Availability:
|
||||||
|
fields += ['status_code']
|
||||||
|
|
||||||
|
related_objects['member'] = self._update_or_create_from_teamsnapapi(
|
||||||
|
teamsnap.models.Member,
|
||||||
|
{'id': teamsnap_data['member_id']}
|
||||||
|
)
|
||||||
|
|
||||||
|
related_objects['event'] = self._update_or_create_from_teamsnapapi(
|
||||||
|
teamsnap.models.Event,
|
||||||
|
{'id': teamsnap_data['event_id']}
|
||||||
|
)
|
||||||
|
|
||||||
|
else:
|
||||||
|
raise ValueError
|
||||||
|
|
||||||
|
if teamsnap_data.get('team_id'):
|
||||||
|
related_objects['team'] = self._update_or_create_from_teamsnapapi(teamsnap.models.Team,
|
||||||
|
{"id":teamsnap_data['team_id']})
|
||||||
|
|
||||||
|
data = {field: teamsnap_data[field] for field in fields if teamsnap_data.get(field) != None}
|
||||||
|
id = data.pop('id')
|
||||||
|
instance, created = teamsnap_model.objects.update_or_create(id=id, defaults=data)
|
||||||
|
r_related_objects = []
|
||||||
|
for related_object_name, related_objectcreated_list in related_objects.items():
|
||||||
|
related_object, created = related_objectcreated_list[0] #FIXME This can't be right, do we need a list for related?
|
||||||
|
setattr(instance, related_object_name, related_object)
|
||||||
|
r_related_objects.append((related_object, created))
|
||||||
|
instance.save()
|
||||||
|
# if create_benchcoach_object:
|
||||||
|
# ben
|
||||||
|
return [(instance, created)] + r_related_objects
|
||||||
|
|
||||||
def _update_teamsnapdb_to_benchcoachdb(self, benchcoach_instance, teamsnap_instance,
|
def _update_teamsnapdb_to_benchcoachdb(self, benchcoach_instance, teamsnap_instance,
|
||||||
create_if_doesnt_exist: bool = False) -> List[Tuple[django.db.models.Model, bool]]:
|
create_if_doesnt_exist: bool = False) -> List[Tuple[django.db.models.Model, bool]]:
|
||||||
''' Function to update from a teamsnap object to Benchcoach object.
|
''' Function to update from a teamsnap object to Benchcoach object.
|
||||||
@@ -217,26 +297,58 @@ class TeamsnapSyncEngine(AbstractSyncEngine):
|
|||||||
return r
|
return r
|
||||||
|
|
||||||
def import_items(self, object_name):
|
def import_items(self, object_name):
|
||||||
Object = {
|
object_names = []
|
||||||
obj.__name__.lower(): obj
|
if object_name == 'team':
|
||||||
for obj in
|
object_names += ['opponent', 'team']
|
||||||
[
|
elif object_name == 'player':
|
||||||
teamsnap.models.Availability,
|
object_names = ['member']
|
||||||
teamsnap.models.Event,
|
elif object_name == 'venue':
|
||||||
teamsnap.models.LineupEntry,
|
object_name == ['location']
|
||||||
teamsnap.models.Location,
|
elif object_name in [model.__name__.lower() for model in self.models]:
|
||||||
teamsnap.models.Member,
|
object_names += [object_name]
|
||||||
teamsnap.models.Opponent,
|
|
||||||
teamsnap.models.Team,
|
if len(object_names) == 0:
|
||||||
teamsnap.models.User
|
raise ValueError('no valid keyword provided')
|
||||||
]
|
|
||||||
}.get(object_name)
|
for object_name in object_names:
|
||||||
if not Object: raise KeyError(f"key {object_name} not found.")
|
Object = {
|
||||||
r = []
|
obj.__name__.lower(): obj
|
||||||
for Obj in [Object]:
|
for obj in
|
||||||
a = Obj.ApiObject.search(self.client, team_id=self.managed_teamsnap_team_id)
|
[
|
||||||
|
teamsnap.models.Availability,
|
||||||
|
teamsnap.models.Event,
|
||||||
|
# teamsnap.models.LineupEntry,
|
||||||
|
teamsnap.models.Location,
|
||||||
|
teamsnap.models.Member,
|
||||||
|
teamsnap.models.Opponent,
|
||||||
|
teamsnap.models.Team,
|
||||||
|
# teamsnap.models.User
|
||||||
|
]
|
||||||
|
}.get(object_name)
|
||||||
|
|
||||||
|
ApiObject = {
|
||||||
|
apiobj.__name__.lower(): apiobj
|
||||||
|
for apiobj in
|
||||||
|
[
|
||||||
|
teamsnap.teamsnap.api.Availability,
|
||||||
|
teamsnap.teamsnap.api.Event,
|
||||||
|
# teamsnap.teamsnap.api.LineupEntry,
|
||||||
|
teamsnap.teamsnap.api.Location,
|
||||||
|
teamsnap.teamsnap.api.Member,
|
||||||
|
teamsnap.teamsnap.api.Opponent,
|
||||||
|
teamsnap.teamsnap.api.Team,
|
||||||
|
# teamsnap.teamsnap.api.User
|
||||||
|
]
|
||||||
|
}.get(object_name)
|
||||||
|
|
||||||
|
pass
|
||||||
|
|
||||||
|
if not Object: raise KeyError(f"key {object_name} not found.")
|
||||||
|
r = []
|
||||||
|
|
||||||
|
a = ApiObject.search(self.client, team_id=self.managed_teamsnap_team_id)
|
||||||
for _a in a:
|
for _a in a:
|
||||||
obj, created = Obj.update_or_create_from_teamsnap_api(_a.data)
|
response = self._update_or_create_from_teamsnapapi(Object, _a.data)
|
||||||
r += [(obj, created)]
|
r += response
|
||||||
|
|
||||||
return r
|
return r
|
||||||
@@ -8,6 +8,10 @@ import benchcoach.models
|
|||||||
TEAMSNAP_TOKEN = os.environ['TEAMSNAP_TOKEN']
|
TEAMSNAP_TOKEN = os.environ['TEAMSNAP_TOKEN']
|
||||||
TEAM_TEAMSNAP_ID = os.environ['TEAM_TEAMSNAP_ID']
|
TEAM_TEAMSNAP_ID = os.environ['TEAM_TEAMSNAP_ID']
|
||||||
|
|
||||||
|
syncengine = TeamsnapSyncEngine(managed_team_teamsnap_id=TEAM_TEAMSNAP_ID, teamsnap_token=TEAMSNAP_TOKEN)
|
||||||
|
r=syncengine.import_items('event')
|
||||||
|
pass
|
||||||
|
|
||||||
class TestEventModel(TestCase):
|
class TestEventModel(TestCase):
|
||||||
fixtures = ['minimal']
|
fixtures = ['minimal']
|
||||||
|
|
||||||
@@ -17,5 +21,5 @@ class TestEventModel(TestCase):
|
|||||||
def test_all_models(self):
|
def test_all_models(self):
|
||||||
for Model in self.syncengine.models:
|
for Model in self.syncengine.models:
|
||||||
with self.subTest():
|
with self.subTest():
|
||||||
|
self.syncengine.import_items(Model.__name__.lower())
|
||||||
pass
|
pass
|
||||||
Reference in New Issue
Block a user