Browse Source

[CLEAN] separate timezone logic in another methoda

pull/52/head
Thibault Francois 6 years ago
committed by Rémy Taymans
parent
commit
c00483309c
  1. 54
      beesdoo_shift/models/planning.py

54
beesdoo_shift/models/planning.py

@ -4,12 +4,9 @@ from openerp import models, fields, api, _
from openerp.exceptions import UserError from openerp.exceptions import UserError
from pytz import timezone, UTC from pytz import timezone, UTC
import logging
import math import math
from datetime import datetime, timedelta from datetime import datetime, timedelta
_logger = logging.getLogger(__name__)
def float_to_time(f): def float_to_time(f):
decimal, integer = math.modf(f) decimal, integer = math.modf(f)
@ -21,7 +18,7 @@ def floatime_to_hour_minute(f):
def get_first_day_of_week(): def get_first_day_of_week():
today = datetime.now() today = datetime.now()
return datetime.now() - timedelta(days=today.weekday())
return (datetime.now() - timedelta(days=today.weekday())).strftime("%Y-%m-%d")
class TaskType(models.Model): class TaskType(models.Model):
_name = 'beesdoo.shift.type' _name = 'beesdoo.shift.type'
@ -100,30 +97,20 @@ class TaskTemplate(models.Model):
start_date = fields.Datetime(compute="_get_fake_date", search="_dummy_search") start_date = fields.Datetime(compute="_get_fake_date", search="_dummy_search")
end_date = fields.Datetime(compute="_get_fake_date", search="_dummy_search") end_date = fields.Datetime(compute="_get_fake_date", search="_dummy_search")
def _get_utc_date(self, day, hour, minute):
#Don't catch error since the error should be raise on the log as an error
#because generate time with UTC timezone is worse than not generate them
context_tz = timezone(self._context.get('tz') or self.env.user.tz)
day_time = day.replace(hour=hour, minute=minute)
day_local_time = context_tz.localize(day_time)
day_utc_time = day_local_time.astimezone(UTC)
return day_utc_time
@api.depends('start_time', 'end_time') @api.depends('start_time', 'end_time')
def _get_fake_date(self): def _get_fake_date(self):
# Get context/client specific timezone. If not found log it and
# assume UTC.
try:
context_tz = timezone(
self._context.get('tz') or self.env.user.tz
)
except Exception:
_logger.debug(
"failed to compute context/client-specific timestamp, "
"using the UTC value",
exc_info=True
)
context_tz = UTC
# Found today date which is the beginning day of the planning
if self._context.get('visualize_date'):
today = datetime.strptime(
self._context.get('visualize_date'),
'%Y-%m-%d'
)
else:
today = get_first_day_of_week()
today = self._context.get('visualize_date', get_first_day_of_week())
today = datetime.strptime(today, '%Y-%m-%d')
for rec in self: for rec in self:
# Find the day of this task template 'rec'. # Find the day of this task template 'rec'.
day = today + timedelta(days=rec.day_nb_id.number - 1) day = today + timedelta(days=rec.day_nb_id.number - 1)
@ -131,17 +118,8 @@ class TaskTemplate(models.Model):
# context timezone. # context timezone.
h_begin, m_begin = floatime_to_hour_minute(rec.start_time) h_begin, m_begin = floatime_to_hour_minute(rec.start_time)
h_end, m_end = floatime_to_hour_minute(rec.end_time) h_end, m_end = floatime_to_hour_minute(rec.end_time)
# Create the start_date and end_date of this task template
# 'rec' according to the context timezone.
start_date = context_tz.localize(
day.replace(hour=h_begin, minute=m_begin, second=0)
)
end_date = context_tz.localize(
day.replace(hour=h_end, minute=m_end, second=0)
)
# Finally, set the dates in UTC.
rec.start_date = start_date.astimezone(UTC)
rec.end_date = end_date.astimezone(UTC)
rec.start_date = self._get_utc_date(day, h_begin, m_begin)
rec.end_date = self._get_utc_date(day, h_end, m_end)
def _dummy_search(self, operator, value): def _dummy_search(self, operator, value):
return [] return []
@ -198,5 +176,5 @@ class TaskTemplate(models.Model):
'end_time' : rec.end_date, 'end_time' : rec.end_date,
'stage_id': self.env.ref('beesdoo_shift.open').id, 'stage_id': self.env.ref('beesdoo_shift.open').id,
}) })
return tasks return tasks
Loading…
Cancel
Save