removing date.today and replacing with timezone.now
This commit is contained in:
@@ -26,6 +26,7 @@ queuehigh = django_rq.get_queue('low')
|
||||
import json
|
||||
|
||||
import pandas as pd
|
||||
import arrow
|
||||
|
||||
from rowingdata import rowingdata as rrdata
|
||||
from rowingdata import rower as rrower
|
||||
@@ -350,7 +351,7 @@ def get_indoorraces(workout):
|
||||
|
||||
return races
|
||||
|
||||
def get_todays_micro(plan,thedate=date.today()):
|
||||
def get_todays_micro(plan,thedate=timezone.now()):
|
||||
thismicro = None
|
||||
|
||||
thismacro = TrainingMacroCycle.objects.filter(
|
||||
@@ -381,7 +382,7 @@ def get_todays_micro(plan,thedate=date.today()):
|
||||
thismicro = thismicro[0]
|
||||
else:
|
||||
mms = TrainingMicroCycle.objects.all()
|
||||
|
||||
|
||||
return None
|
||||
|
||||
return thismicro
|
||||
@@ -619,7 +620,7 @@ cratiocolors = {
|
||||
def is_session_complete_ws(ws,ps):
|
||||
ws = ws.order_by("date")
|
||||
if ws.count()==0:
|
||||
today = date.today()
|
||||
today = timezone.now()
|
||||
if today > ps.enddate:
|
||||
verdict = 'missed'
|
||||
ratio = 0
|
||||
@@ -851,7 +852,7 @@ def get_team(request):
|
||||
return teamid
|
||||
|
||||
def get_dates_timeperiod(request,startdatestring='',enddatestring='',
|
||||
defaulttimeperiod='thisweek'):
|
||||
defaulttimeperiod='thisweek',rower=None):
|
||||
# set start end date according timeperiod
|
||||
# should always return datetime.date
|
||||
|
||||
@@ -883,40 +884,47 @@ def get_dates_timeperiod(request,startdatestring='',enddatestring='',
|
||||
startdate = enddate
|
||||
enddate = e
|
||||
|
||||
if rower is not None:
|
||||
tz = pytz.timezone(rower.defaulttimezone)
|
||||
startdate = arrow.get(startdate)
|
||||
enddate = arrow.get(enddate)
|
||||
startdate = startdate.astimezone(tz)
|
||||
enddate = enddate.astimezone(tz)
|
||||
|
||||
return startdate,enddate
|
||||
|
||||
daterangetester = re.compile('^(\d+-\d+-\d+)\/(\d+-\d+-\d+)')
|
||||
|
||||
|
||||
if timeperiod=='today': # pragma: no cover
|
||||
startdate=date.today()
|
||||
enddate=date.today()
|
||||
startdate=timezone.now()
|
||||
enddate=timezone.now()
|
||||
elif timeperiod=='last30':
|
||||
startdate = date.today()-timezone.timedelta(days=30)
|
||||
enddate = date.today()+timezone.timedelta(days=1)
|
||||
startdate = timezone.now()-timezone.timedelta(days=30)
|
||||
enddate = timezone.now()+timezone.timedelta(days=1)
|
||||
elif timeperiod=='tomorrow': # pragma: no cover
|
||||
startdate=date.today()+timezone.timedelta(days=1)
|
||||
enddate=date.today()+timezone.timedelta(days=1)
|
||||
startdate=timezone.now()+timezone.timedelta(days=1)
|
||||
enddate=timezone.now()+timezone.timedelta(days=1)
|
||||
elif timeperiod=='thisweek': # pragma: no cover
|
||||
today = date.today()
|
||||
startdate = date.today()-timezone.timedelta(days=today.weekday())
|
||||
today = timezone.now()
|
||||
startdate = timezone.now()-timezone.timedelta(days=today.weekday())
|
||||
enddate = startdate+timezone.timedelta(days=6)
|
||||
elif timeperiod=='thismonth': # pragma: no cover
|
||||
today = date.today()
|
||||
today = timezone.now()
|
||||
startdate = today.replace(day=1)
|
||||
enddate = startdate+timezone.timedelta(days=32)
|
||||
enddate = enddate.replace(day=1)
|
||||
enddate = enddate-timezone.timedelta(days=1)
|
||||
elif timeperiod=='lastweek': # pragma: no cover
|
||||
today = date.today()
|
||||
today = timezone.now()
|
||||
enddate = today-timezone.timedelta(days=today.weekday())-timezone.timedelta(days=1)
|
||||
startdate = enddate-timezone.timedelta(days=6)
|
||||
elif timeperiod=='nextweek': # pragma: no cover
|
||||
today = date.today()
|
||||
today = timezone.now()
|
||||
startdate = today-timezone.timedelta(days=today.weekday())+timezone.timedelta(days=7)
|
||||
enddate = startdate+timezone.timedelta(days=6)
|
||||
elif timeperiod=='lastmonth': # pragma: no cover
|
||||
today = date.today()
|
||||
today = timezone.now()
|
||||
startdate = today.replace(day=1)
|
||||
startdate = startdate-timezone.timedelta(days=3)
|
||||
startdate = startdate.replace(day=1)
|
||||
@@ -924,7 +932,7 @@ def get_dates_timeperiod(request,startdatestring='',enddatestring='',
|
||||
enddate = enddate.replace(day=1)
|
||||
enddate = enddate-timezone.timedelta(days=1)
|
||||
elif timeperiod=='nextmonth': # pragma: no cover
|
||||
today = date.today()
|
||||
today = timezone.now()
|
||||
startdate = today.replace(day=1)
|
||||
startdate = startdate+timezone.timedelta(days=32)
|
||||
startdate = startdate.replace(day=1)
|
||||
@@ -932,7 +940,7 @@ def get_dates_timeperiod(request,startdatestring='',enddatestring='',
|
||||
enddate = enddate.replace(day=1)
|
||||
enddate = enddate-timezone.timedelta(days=1)
|
||||
elif timeperiod=='lastyear': # pragma: no cover
|
||||
today = date.today()
|
||||
today = timezone.now()
|
||||
startdate = today-timezone.timedelta(days=365)
|
||||
enddate = today+timezone.timedelta(days=1)
|
||||
elif daterangetester.match(timeperiod):
|
||||
@@ -946,11 +954,11 @@ def get_dates_timeperiod(request,startdatestring='',enddatestring='',
|
||||
enddate = startdate
|
||||
startdate = startdate2
|
||||
except ValueError: # pragma: no cover
|
||||
startdate = date.today()
|
||||
enddate = date.today()
|
||||
startdate = timezone.now()
|
||||
enddate = timezone.now()
|
||||
else:
|
||||
startdate = date.today()
|
||||
enddate = date.today()
|
||||
startdate = timezone.now()
|
||||
enddate = timezone.now()
|
||||
|
||||
|
||||
if startdatestring != '':
|
||||
@@ -965,10 +973,18 @@ def get_dates_timeperiod(request,startdatestring='',enddatestring='',
|
||||
except ParseError:
|
||||
pass
|
||||
|
||||
if rower is not None:
|
||||
startdate = arrow.get(startdate)
|
||||
enddate = arrow.get(enddate)
|
||||
tz = pytz.timezone(rower.defaulttimezone)
|
||||
startdate = startdate.astimezone(tz)
|
||||
|
||||
enddate = enddate.astimezone(tz)
|
||||
|
||||
return startdate,enddate
|
||||
|
||||
def get_sessions_manager(m,teamid=0,startdate=date.today(),
|
||||
enddate=date.today()+timezone.timedelta(+1000)):
|
||||
def get_sessions_manager(m,teamid=0,startdate=timezone.now(),
|
||||
enddate=timezone.now()+timezone.timedelta(+1000)):
|
||||
if teamid: # pragma: no cover
|
||||
t = Team.objects.get(id=teamid)
|
||||
rs = Rower.objects.filter(team__in=[t]).distinct()
|
||||
@@ -991,8 +1007,8 @@ def get_sessions_manager(m,teamid=0,startdate=date.today(),
|
||||
|
||||
return sps
|
||||
|
||||
def get_sessions(r,startdate=date.today(),
|
||||
enddate=date.today()+timezone.timedelta(+1000)):
|
||||
def get_sessions(r,startdate=timezone.now(),
|
||||
enddate=timezone.now()+timezone.timedelta(+1000)):
|
||||
|
||||
sps = PlannedSession.objects.filter(
|
||||
rower__in=[r],
|
||||
|
||||
@@ -514,6 +514,12 @@ class DataPrepTests(TestCase):
|
||||
self.assertEqual(starttime,'19:55:13')
|
||||
self.assertEqual(startdate,'2021-06-15')
|
||||
|
||||
def test_calculate_age(self):
|
||||
today = timezone.now()
|
||||
born = today-datetime.timedelta(days=45+49*365.2425)
|
||||
age = dataprep.calculate_age(born,today=today)
|
||||
self.assertEqual(age,49)
|
||||
|
||||
@patch('rowers.dataprep.getsmallrowdata_db',side_effect=mocked_getsmallrowdata_uh)
|
||||
def test_goldmedalstandard(self,mocked_getsmallrowdata_uh):
|
||||
maxvalue, delta = dataprep.calculate_goldmedalstandard(self.r,self.wuh_otw)
|
||||
|
||||
BIN
rowers/tests/testdata/testdata.tcx.gz
vendored
Normal file
BIN
rowers/tests/testdata/testdata.tcx.gz
vendored
Normal file
Binary file not shown.
@@ -347,7 +347,7 @@ from datetime import date
|
||||
|
||||
def calculate_age(born,today=None):
|
||||
if not today:
|
||||
today = date.today()
|
||||
today = timezone.now()
|
||||
if born:
|
||||
try:
|
||||
return today.year - born.year - ((today.month, today.day) < (born.month, born.day))
|
||||
|
||||
@@ -1387,7 +1387,7 @@ from rowers.plannedsessions import cratiocolors,checkscores
|
||||
def plannedsessions_view(request,
|
||||
userid=0,startdatestring='',enddatestring=''):
|
||||
|
||||
|
||||
|
||||
try:
|
||||
r = getrequestplanrower(request,userid=userid)
|
||||
except PermissionDenied: # pragma: no cover
|
||||
@@ -1410,7 +1410,9 @@ def plannedsessions_view(request,
|
||||
startdate,enddate = get_dates_timeperiod(
|
||||
request,
|
||||
startdatestring=startdatestring,
|
||||
enddatestring=enddatestring)
|
||||
enddatestring=enddatestring,
|
||||
rower=r)
|
||||
|
||||
|
||||
try:
|
||||
trainingplan = TrainingPlan.objects.filter(
|
||||
@@ -2634,7 +2636,14 @@ def rower_view_instantplan(request,id='',userid=0):
|
||||
|
||||
|
||||
elif not request.user.is_anonymous:
|
||||
form = InstantPlanSelectForm(targets=targets,instantplan=plan,initial={'datechoice':'startdate'})
|
||||
tz = pytz.timezone(r.defaulttimezone)
|
||||
nowdate = timezone.now().astimezone(tz)
|
||||
initial = {
|
||||
'datechoice':'startdate',
|
||||
'startdate': nowdate,
|
||||
'enddate': nowdate+datetime.timedelta(days=21)
|
||||
}
|
||||
form = InstantPlanSelectForm(targets=targets,instantplan=plan,initial=initial)
|
||||
else: # pragma: no cover
|
||||
form = None
|
||||
|
||||
|
||||
Reference in New Issue
Block a user