Browse Source

[FIX] b_shift : fix datetime issues coming from migration to 12.0

pull/143/head
Elouan Le Bars 5 years ago
parent
commit
47ff591c1f
  1. 4
      beesdoo_shift/data/cron.xml
  2. 47
      beesdoo_shift/models/attendance_sheet.py
  3. 12
      beesdoo_shift/models/cooperative_status.py
  4. 20
      beesdoo_shift/models/planning.py
  5. 8
      beesdoo_shift/models/task.py
  6. 2
      beesdoo_shift/tests/test_beesdoo_shift.py
  7. 4
      beesdoo_shift/views/attendance_sheet.xml
  8. 2
      beesdoo_shift/views/task.xml
  9. 4
      beesdoo_shift/wizard/extension.py

4
beesdoo_shift/data/cron.xml

@ -49,7 +49,7 @@
<field name="interval_type">days</field> <field name="interval_type">days</field>
<field name="numbercall">-1</field> <field name="numbercall">-1</field>
<field name="doall" eval="False" /> <field name="doall" eval="False" />
<field name="nextcall" eval="(datetime.now()).strftime('%Y-%m-%d 00:00:10')"/>
<field name="nextcall" eval="datetime.combine(datetime.now(), time(hour=00, minute=00, second=10))">
<field name="model_id" ref="model_beesdoo_shift_sheet" /> <field name="model_id" ref="model_beesdoo_shift_sheet" />
<field name="code">model._cron_non_validated_sheets()</field> <field name="code">model._cron_non_validated_sheets()</field>
<field name="active" eval="False" /> <field name="active" eval="False" />
@ -62,7 +62,7 @@
<field name="numbercall">-1</field> <field name="numbercall">-1</field>
<field name="doall" eval="True" /> <field name="doall" eval="True" />
<field name="nextcall" <field name="nextcall"
eval="str(datetime.utcnow() + timedelta((6-datetime.utcnow().weekday()) % 7 ))"
eval="datetime.utcnow() + timedelta((6-datetime.utcnow().weekday()) % 7"
/> />
<field name="model_id" ref="model_beesdoo_shift_shift" /> <field name="model_id" ref="model_beesdoo_shift_shift" />
<field name="code">_cron_send_weekly_emails()</field> <field name="code">_cron_send_weekly_emails()</field>

47
beesdoo_shift/models/attendance_sheet.py

@ -223,26 +223,19 @@ class AttendanceSheet(models.Model):
@api.depends("start_time", "end_time") @api.depends("start_time", "end_time")
def _compute_time_slot(self): def _compute_time_slot(self):
for rec in self: for rec in self:
start_time_dt = fields.Datetime.from_string(rec.start_time)
start_time_dt = fields.Datetime.context_timestamp(
rec, start_time_dt
)
end_time_dt = fields.Datetime.from_string(rec.end_time)
end_time_dt = fields.Datetime.context_timestamp(rec, end_time_dt)
start_time = fields.Datetime.context_timestamp(rec, rec.start_time)
end_time = fields.Datetime.context_timestamp(rec, rec.end_time)
rec.time_slot = ( rec.time_slot = (
start_time_dt.strftime("%H:%M")
start_time.strftime("%H:%M")
+ "-" + "-"
+ end_time_dt.strftime("%H:%M")
+ end_time.strftime("%H:%M")
) )
@api.depends("start_time", "end_time", "week", "day_abbrevation") @api.depends("start_time", "end_time", "week", "day_abbrevation")
def _compute_name(self): def _compute_name(self):
for rec in self: for rec in self:
start_time_dt = fields.Datetime.from_string(rec.start_time)
start_time_dt = fields.Datetime.context_timestamp(
rec, start_time_dt
)
name = "[%s] " % fields.Date.to_string(start_time_dt)
start_time = fields.Datetime.context_timestamp(rec, rec.start_time)
name = "[%s] " % fields.Date.to_string(start_time)
if rec.week: if rec.week:
name += rec.week + " " name += rec.week + " "
if rec.day_abbrevation: if rec.day_abbrevation:
@ -254,7 +247,7 @@ class AttendanceSheet(models.Model):
@api.depends("start_time") @api.depends("start_time")
def _compute_day(self): def _compute_day(self):
for rec in self: for rec in self:
rec.day = fields.Date.from_string(rec.start_time)
rec.day = rec.start_time.date()
@api.depends("expected_shift_ids") @api.depends("expected_shift_ids")
def _compute_day_abbrevation(self): def _compute_day_abbrevation(self):
@ -397,19 +390,16 @@ class AttendanceSheet(models.Model):
# to the time range # to the time range
tasks = self.env["beesdoo.shift.shift"] tasks = self.env["beesdoo.shift.shift"]
expected_shift = self.env["beesdoo.shift.sheet.expected"] expected_shift = self.env["beesdoo.shift.sheet.expected"]
s_time = fields.Datetime.from_string(new_sheet.start_time)
e_time = fields.Datetime.from_string(new_sheet.end_time)
# Fix issues with equality check on datetime # Fix issues with equality check on datetime
# by searching on a small intervall instead # by searching on a small intervall instead
delta = timedelta(minutes=1) delta = timedelta(minutes=1)
to_string = fields.Datetime.to_string
tasks = tasks.search( tasks = tasks.search(
[ [
("start_time", ">", to_string(s_time - delta)),
("start_time", "<", to_string(s_time + delta)),
("end_time", ">", to_string(e_time - delta)),
("end_time", "<", to_string(e_time + delta)),
("start_time", ">", new_sheet.start_time - delta),
("start_time", "<", new_sheet.start_time + delta),
("end_time", ">", new_sheet.end_time - delta),
("end_time", "<", new_sheet.end_time + delta),
] ]
) )
for task in tasks: for task in tasks:
@ -484,17 +474,14 @@ class AttendanceSheet(models.Model):
# Fix issues with equality check on datetime # Fix issues with equality check on datetime
# by searching on a small intervall instead # by searching on a small intervall instead
delta = timedelta(minutes=1) delta = timedelta(minutes=1)
s_time = fields.Datetime.from_string(self.start_time)
e_time = fields.Datetime.from_string(self.end_time)
to_string = fields.Datetime.to_string
non_assigned_shifts = self.env["beesdoo.shift.shift"].search( non_assigned_shifts = self.env["beesdoo.shift.shift"].search(
[ [
("worker_id", "=", False), ("worker_id", "=", False),
("start_time", ">", to_string(s_time - delta)),
("start_time", "<", to_string(s_time + delta)),
("end_time", ">", to_string(e_time - delta)),
("end_time", "<", to_string(e_time + delta)),
("start_time", ">", self.start_time - delta),
("start_time", "<", self.start_time + delta),
("end_time", ">", self.end_time - delta),
("end_time", "<", self.end_time + delta),
("task_type_id", "=", added_shift.task_type_id.id), ("task_type_id", "=", added_shift.task_type_id.id),
], ],
limit=1, limit=1,
@ -530,11 +517,11 @@ class AttendanceSheet(models.Model):
@api.multi @api.multi
def validate_with_checks(self): def validate_with_checks(self):
self.ensure_one() self.ensure_one()
start_time_dt = fields.Datetime.from_string(self.start_time)
if self.state == "validated": if self.state == "validated":
raise UserError(_("The sheet has already been validated.")) raise UserError(_("The sheet has already been validated."))
if start_time_dt > datetime.now():
if self.start_time > datetime.now():
raise UserError( raise UserError(
_("Attendance sheet can only be validated once the shifts have started.") _("Attendance sheet can only be validated once the shifts have started.")
) )

12
beesdoo_shift/models/cooperative_status.py

@ -10,8 +10,8 @@ PERIOD = 28 # TODO: use system parameter
def add_days_delta(date_from, days_delta): def add_days_delta(date_from, days_delta):
if not date_from: if not date_from:
return date_from return date_from
next_date = fields.Date.from_string(date_from) + timedelta(days=days_delta)
return fields.Date.to_string(next_date)
next_date = date_from + timedelta(days=days_delta)
return next_date
class ExemptReason(models.Model): class ExemptReason(models.Model):
_name = 'cooperative.exempt.reason' _name = 'cooperative.exempt.reason'
@ -220,9 +220,8 @@ class CooperativeStatus(models.Model):
This does not take holiday and other status into account. This does not take holiday and other status into account.
""" """
today = today or fields.Date.today() today = today or fields.Date.today()
today_dt = fields.Date.from_string(today)
irregular_start_dt = fields.Date.from_string(irregular_start_date)
delta = (today_dt - irregular_start_dt).days
delta = (today - irregular_start_date).days
if not delta % PERIOD: if not delta % PERIOD:
return today return today
return add_days_delta(today, PERIOD - (delta % PERIOD)) return add_days_delta(today, PERIOD - (delta % PERIOD))
@ -398,11 +397,10 @@ class CooperativeStatus(models.Model):
'|', ('holiday_start_time', '>', today), ('holiday_end_time', '<', today), '|', ('holiday_start_time', '>', today), ('holiday_end_time', '<', today),
] ]
irregular = self.search(domain) irregular = self.search(domain)
today_date = fields.Date.from_string(today)
for status in irregular: for status in irregular:
if status.status == 'exempted': if status.status == 'exempted':
continue continue
delta = (today_date - fields.Date.from_string(status.irregular_start_date)).days
delta = (today - status.irregular_start_date).days
if delta and delta % PERIOD == 0 and status not in journal.line_ids: if delta and delta % PERIOD == 0 and status not in journal.line_ids:
if status.sr > 0: if status.sr > 0:
status.sr -= 1 status.sr -= 1

20
beesdoo_shift/models/planning.py

@ -4,7 +4,7 @@ from odoo.exceptions import UserError
from pytz import timezone, UTC from pytz import timezone, UTC
import math import math
from datetime import datetime, timedelta
from datetime import datetime, time, timedelta
def float_to_time(f): def float_to_time(f):
@ -17,7 +17,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())).strftime("%Y-%m-%d")
return (datetime.now() - timedelta(days=today.weekday())).date()
class TaskType(models.Model): class TaskType(models.Model):
_name = 'beesdoo.shift.type' _name = 'beesdoo.shift.type'
@ -55,7 +55,7 @@ class Planning(models.Model):
def _get_next_planning_date(self, date): def _get_next_planning_date(self, date):
self.ensure_one() self.ensure_one()
nb_of_day = max(self.task_template_ids.mapped('day_nb_id.number')) nb_of_day = max(self.task_template_ids.mapped('day_nb_id.number'))
return fields.Date.to_string(fields.Date.from_string(date) + timedelta(days=nb_of_day))
return date + timedelta(days=nb_of_day)
@api.model @api.model
def _generate_next_planning(self): def _generate_next_planning(self):
@ -98,19 +98,15 @@ class TaskTemplate(models.Model):
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): 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) 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_local_time=datetime.combine(day, time(hour=hour, minute=minute), tzinfo=context_tz)
day_utc_time=day_local_time.astimezone(UTC) day_utc_time=day_local_time.astimezone(UTC)
return day_utc_time
# Return naïve datetime so as to be saved in database
return day_utc_time.replace(tzinfo=None)
@api.depends('start_time', 'end_time') @api.depends('start_time', 'end_time')
def _get_fake_date(self): def _get_fake_date(self):
today = self._context.get('visualize_date', 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)
@ -160,10 +156,10 @@ class TaskTemplate(models.Model):
if worker_id and worker_id.cooperative_status_ids: if worker_id and worker_id.cooperative_status_ids:
status = worker_id.cooperative_status_ids[0] status = worker_id.cooperative_status_ids[0]
if status.holiday_start_time and status.holiday_end_time and \ if status.holiday_start_time and status.holiday_end_time and \
status.holiday_start_time <= rec.start_date[:10] and status.holiday_end_time >= rec.end_date[:10]:
status.holiday_start_time <= rec.start_date.date() and status.holiday_end_time >= rec.end_date.date():
worker_id = False worker_id = False
if status.temporary_exempt_start_date and status.temporary_exempt_end_date and \ if status.temporary_exempt_start_date and status.temporary_exempt_end_date and \
status.temporary_exempt_start_date <= rec.start_date[:10] and status.temporary_exempt_end_date >= rec.end_date[:10]:
status.temporary_exempt_start_date <= rec.start_date.date() and status.temporary_exempt_end_date >= rec.end_date.date():
worker_id = False worker_id = False
tasks |= tasks.create({ tasks |= tasks.create({
'name' : "%s %s (%s - %s) [%s]" % (rec.name, rec.day_nb_id.name, float_to_time(rec.start_time), float_to_time(rec.end_time), i), 'name' : "%s %s (%s - %s) [%s]" % (rec.name, rec.day_nb_id.name, float_to_time(rec.start_time), float_to_time(rec.end_time), i),

8
beesdoo_shift/models/task.py

@ -148,7 +148,7 @@ class Task(models.Model):
today += ' 00:00:00' today += ' 00:00:00'
date_domain = [('start_time', '>', today)] date_domain = [('start_time', '>', today)]
if end_date: if end_date:
end_date += ' 23:59:59'
end_date = datetime.combine(end_date,time(hour=23, minute=59, second=59))
date_domain.append(('end_time', '<=', end_date)) date_domain.append(('end_time', '<=', end_date))
to_unsubscribe = self.search([('worker_id', 'in', worker_ids)] + date_domain) to_unsubscribe = self.search([('worker_id', 'in', worker_ids)] + date_domain)
@ -258,7 +258,7 @@ class Task(models.Model):
if new_state == "absent_2" or new_state == "absent_1": if new_state == "absent_2" or new_state == "absent_1":
if new_state == "absent_2": if new_state == "absent_2":
data['sr'] = -1 data['sr'] = -1
data['irregular_absence_date'] = self.start_time[:10]
data['irregular_absence_date'] = self.start_time.date()
data['irregular_absence_counter'] = -1 data['irregular_absence_counter'] = -1
else: else:
@ -282,8 +282,8 @@ class Task(models.Model):
confirmed_tasks = tasks.search( confirmed_tasks = tasks.search(
[ [
("start_time", ">", start_time.strftime("%Y-%m-%d 00:00:01")),
("start_time", "<", end_time.strftime("%Y-%m-%d 23:59:59")),
("start_time", ">", start_time),
("start_time", "<", end_time),
("worker_id", "!=", False), ("worker_id", "!=", False),
("state", "=", "open"), ("state", "=", "open"),
] ]

2
beesdoo_shift/tests/test_beesdoo_shift.py

@ -155,8 +155,6 @@ class TestBeesdooShift(TransactionCase):
def search_sheets(self, start_time, end_time): def search_sheets(self, start_time, end_time):
if (type(start_time) and type(end_time)) == datetime: if (type(start_time) and type(end_time)) == datetime:
start_time = fields.Datetime.to_string(start_time)
end_time = fields.Datetime.to_string(end_time)
return self.attendance_sheet_model.search( return self.attendance_sheet_model.search(
[("start_time", "=", start_time), ("end_time", "=", end_time)] [("start_time", "=", start_time), ("end_time", "=", end_time)]
) )

4
beesdoo_shift/views/attendance_sheet.xml

@ -231,8 +231,8 @@
name="Daily attendance sheets" name="Daily attendance sheets"
res_model="beesdoo.shift.sheet" res_model="beesdoo.shift.sheet"
view_mode="tree,form" view_mode="tree,form"
domain="[('end_time','&gt;', datetime.date.today().strftime('%Y-%m-%d 00:00:00')),
('start_time','&lt;', datetime.date.today().strftime('%Y-%m-%d 23:59:59'))]"
domain="[('end_time','&gt;', datetime.combine(datetime.now(), time(hour=00, minute=00, second=10))),
('start_time','&lt;', datetime.combine(datetime.now(), time(hour=23, minute=59, second=59)))]"
/> />
<!-- Top menu item --> <!-- Top menu item -->

2
beesdoo_shift/views/task.xml

@ -104,14 +104,12 @@
options="{'no_create': True, 'no_open': True}" options="{'no_create': True, 'no_open': True}"
domain="[('working_mode', '=', 'regular')]" domain="[('working_mode', '=', 'regular')]"
attrs="{'invisible': [('working_mode', '!=', 'regular')]}" /> attrs="{'invisible': [('working_mode', '!=', 'regular')]}" />
<group>
<field name="is_regular" <field name="is_regular"
attrs="{'invisible': [('working_mode', '!=', 'regular')]}"/> attrs="{'invisible': [('working_mode', '!=', 'regular')]}"/>
<field name="is_compensation" <field name="is_compensation"
attrs="{'invisible': [('working_mode', '!=', 'regular')]}"/> attrs="{'invisible': [('working_mode', '!=', 'regular')]}"/>
<field name="working_mode" invisible="1"/> <field name="working_mode" invisible="1"/>
</group> </group>
</group>
<group> <group>
<field name="start_time" /> <field name="start_time" />
<field name="end_time" /> <field name="end_time" />

4
beesdoo_shift/wizard/extension.py

@ -26,9 +26,7 @@ class Subscribe(models.TransientModel):
status_id = self.env['cooperative.status'].search([('cooperator_id', '=', self.cooperator_id.id)]) status_id = self.env['cooperative.status'].search([('cooperator_id', '=', self.cooperator_id.id)])
if not status_id.extension_start_time: if not status_id.extension_start_time:
raise UserError(_('You should not make a manual extension when the grace delay has not been triggered yet')) raise UserError(_('You should not make a manual extension when the grace delay has not been triggered yet'))
extension_date = fields.Date.from_string(status_id.extension_start_time)
today = fields.Date.from_string(status_id.today)
today_delay = (today - extension_date).days - grace_delay
today_delay = (status_id.today - status_id.extension_start_time).days - grace_delay
if today_delay < 0: if today_delay < 0:
raise UserError(_('You should not start a manual extension during the grace delay')) raise UserError(_('You should not start a manual extension during the grace delay'))
status_id.sudo().write({'time_extension': self.extension_days + today_delay}) status_id.sudo().write({'time_extension': self.extension_days + today_delay})
Loading…
Cancel
Save