refactor, stage 1
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
from abc import ABCMeta, ABC, abstractmethod
|
from abc import ABCMeta, ABC, abstractmethod
|
||||||
from importlib import import_module
|
from importlib import import_module
|
||||||
from rowers.models import Rower, User
|
from rowers.models import Rower, User, SyncRecord
|
||||||
from rowers.utils import NoTokenError,dologging
|
from rowers.utils import NoTokenError,dologging
|
||||||
|
|
||||||
import requests
|
import requests
|
||||||
@@ -267,3 +267,4 @@ class SyncIntegration(metaclass=ABCMeta):
|
|||||||
return access_token
|
return access_token
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -3631,6 +3631,35 @@ class SyncRecord(models.Model):
|
|||||||
self.rower = self.workout.user
|
self.rower = self.workout.user
|
||||||
return super(SyncRecord, self).save(*args, **kwargs)
|
return super(SyncRecord, self).save(*args, **kwargs)
|
||||||
|
|
||||||
|
def create_or_update_syncrecord(rower, workout, **kwargs):
|
||||||
|
records = SyncRecord.objects.filter(workout=workout,rower=rower)
|
||||||
|
if records.count():
|
||||||
|
record = records[0]
|
||||||
|
else:
|
||||||
|
record = SyncRecord(workout=workout, rower=rower)
|
||||||
|
|
||||||
|
try:
|
||||||
|
kwargs.pop('rower')
|
||||||
|
except KeyError:
|
||||||
|
pass
|
||||||
|
|
||||||
|
try:
|
||||||
|
kwargs.pop('workout')
|
||||||
|
except KeyError:
|
||||||
|
pass
|
||||||
|
|
||||||
|
for field in record._meta.fields:
|
||||||
|
value = kwargs.get(field.name, None)
|
||||||
|
if value:
|
||||||
|
setattr(record, field.name, value)
|
||||||
|
|
||||||
|
try:
|
||||||
|
record.save()
|
||||||
|
except IntegrityError:
|
||||||
|
pass
|
||||||
|
|
||||||
|
return record
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ application = get_wsgi_application()
|
|||||||
from rowers.models import (
|
from rowers.models import (
|
||||||
Workout, GeoPolygon, GeoPoint, GeoCourse,
|
Workout, GeoPolygon, GeoPoint, GeoCourse,
|
||||||
VirtualRaceResult, CourseTestResult, Rower,
|
VirtualRaceResult, CourseTestResult, Rower,
|
||||||
GraphImage, SyncRecord
|
GraphImage
|
||||||
)
|
)
|
||||||
|
|
||||||
import math
|
import math
|
||||||
@@ -35,6 +35,7 @@ import arrow
|
|||||||
import rowers.longtask as longtask
|
import rowers.longtask as longtask
|
||||||
import requests
|
import requests
|
||||||
import rowers.datautils as datautils
|
import rowers.datautils as datautils
|
||||||
|
from rowers.models import create_or_update_syncrecord
|
||||||
|
|
||||||
""" Background tasks done by QR (production) """
|
""" Background tasks done by QR (production) """
|
||||||
import time
|
import time
|
||||||
@@ -523,8 +524,8 @@ def handle_c2_sync(workoutid, url, headers, data, debug=False, **kwargs):
|
|||||||
workout.uploadedtoc2 = c2id
|
workout.uploadedtoc2 = c2id
|
||||||
workout.save()
|
workout.save()
|
||||||
|
|
||||||
record = SyncRecord(workout=workout,c2id=c2id)
|
record = create_or_update_syncrecord(workout.user, workout, c2id=c2id)
|
||||||
record.save()
|
|
||||||
|
|
||||||
return 1
|
return 1
|
||||||
|
|
||||||
@@ -3409,19 +3410,7 @@ def handle_nk_async_workout(alldata, userid, nktoken, nkid, delaysec, defaulttim
|
|||||||
|
|
||||||
workout = Workout.objects.get(id=workoutid)
|
workout = Workout.objects.get(id=workoutid)
|
||||||
newnkid = workout.uploadedtonk
|
newnkid = workout.uploadedtonk
|
||||||
sr = SyncRecord.objects.filter(nkid=newnkid)
|
sr = create_or_update_syncrecord(workout.user, workout, nkid=newnkid)
|
||||||
if len(sr):
|
|
||||||
sr[0].workout = workout
|
|
||||||
try:
|
|
||||||
sr[0].save()
|
|
||||||
except IntegrityError:
|
|
||||||
pass
|
|
||||||
else:
|
|
||||||
sr = SyncRecord(workout=workout,nkid=newnkid)
|
|
||||||
try:
|
|
||||||
sr.save()
|
|
||||||
except IntegrityError:
|
|
||||||
pass
|
|
||||||
|
|
||||||
return workoutid
|
return workoutid
|
||||||
|
|
||||||
@@ -3696,7 +3685,7 @@ def handle_c2_async_workout(alldata, userid, c2token, c2id, delaysec,
|
|||||||
workout = Workout.objects.get(id=workoutid)
|
workout = Workout.objects.get(id=workoutid)
|
||||||
newc2id = workout.uploadedtoc2
|
newc2id = workout.uploadedtoc2
|
||||||
|
|
||||||
record = SyncRecord(workout=workout,c2id=newc2id)
|
record = create_or_update_syncrecord(workout.user, workout, c2id=newc2id)
|
||||||
|
|
||||||
|
|
||||||
# set distance, time
|
# set distance, time
|
||||||
|
|||||||
BIN
rowers/tests/testdata/testdata.tcx.gz
vendored
BIN
rowers/tests/testdata/testdata.tcx.gz
vendored
Binary file not shown.
@@ -10,6 +10,7 @@ from rowers.utils import (
|
|||||||
# for actions related to uploads
|
# for actions related to uploads
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from django.utils import timezone, translation
|
from django.utils import timezone, translation
|
||||||
|
|
||||||
from rowers.tasks import (
|
from rowers.tasks import (
|
||||||
handle_sendemail_unrecognized, handle_sendemailnewcomment,
|
handle_sendemail_unrecognized, handle_sendemailnewcomment,
|
||||||
handle_sendemailnewresponse, handle_updatedps,
|
handle_sendemailnewresponse, handle_updatedps,
|
||||||
@@ -17,7 +18,7 @@ from rowers.tasks import (
|
|||||||
handle_sendemailcsv
|
handle_sendemailcsv
|
||||||
)
|
)
|
||||||
|
|
||||||
from rowers.models import GraphImage, SyncRecord
|
from rowers.models import GraphImage, create_or_update_syncrecord
|
||||||
|
|
||||||
from rowers.rower_rules import ispromember
|
from rowers.rower_rules import ispromember
|
||||||
from rowers.utils import dologging
|
from rowers.utils import dologging
|
||||||
@@ -148,11 +149,7 @@ def do_sync(w, options, quick=False):
|
|||||||
if options['nkid'] != 0 and options['nkid'] != '': # pragma: no cover
|
if options['nkid'] != 0 and options['nkid'] != '': # pragma: no cover
|
||||||
w.uploadedtonk = options['nkid']
|
w.uploadedtonk = options['nkid']
|
||||||
w.save()
|
w.save()
|
||||||
record = SyncRecord(workout=w,nkid=options['nkid'])
|
record = create_or_update_syncrecord(w.user, w, {nkid:options['nkid']})
|
||||||
try:
|
|
||||||
record.save()
|
|
||||||
except:
|
|
||||||
pass
|
|
||||||
except KeyError:
|
except KeyError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
@@ -187,11 +184,7 @@ def do_sync(w, options, quick=False):
|
|||||||
# upload_to_c2 = False
|
# upload_to_c2 = False
|
||||||
do_c2_export = False
|
do_c2_export = False
|
||||||
w.save()
|
w.save()
|
||||||
record = SyncRecord(workout=w,c2id=options['c2id'])
|
record = create_or_update_syncrecord(w.user, w, {c2id:options['c2id']})
|
||||||
try:
|
|
||||||
record.save()
|
|
||||||
except:
|
|
||||||
pass
|
|
||||||
except KeyError:
|
except KeyError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|||||||
@@ -1388,3 +1388,5 @@ def get_startdatetime_from_c2data(data):
|
|||||||
).strftime('%H:%M:%S')
|
).strftime('%H:%M:%S')
|
||||||
|
|
||||||
return startdatetime, starttime, workoutdate, duration, starttimeunix, timezone
|
return startdatetime, starttime, workoutdate, duration, starttimeunix, timezone
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user