summaryrefslogtreecommitdiff
path: root/addons/timer/models
diff options
context:
space:
mode:
authorOkan SUMER (osu) <osu@odoo.com>2020-02-05 16:15:29 +0100
committerYannick Tivisse <yti@odoo.com>2020-02-05 16:44:37 +0100
commite2b749fb7a441b4337c40cc856459e2e5f95b597 (patch)
treeca9de35d2674582ed440e2f28484bd5d9bcbef0e /addons/timer/models
parenta6d7c7b04a48669037e6568849a26571602386f6 (diff)
[ADD] timer: New mixin to record time on models
Purpose ======= The timer mechanism is already existing on timesheet. We would like to introduce it on helpdesk.ticket too, and probably on other models afterward. On the other hand, only one timer can be launched on a single timesheet currently, shared amongst the different users. This commit introduced a new mixin on which - Each user can launch a timer on each record of the models inheriting the mixin. - Launching a new timer on a record pauses the timer of the user that could be already running. - The mixin brings methods to start/stop/pause/resume the timer, that could be overridden to implement a specific behavior for the related model, for example create a timesheet entry. TaskID: 2149450
Diffstat (limited to 'addons/timer/models')
-rw-r--r--addons/timer/models/__init__.py5
-rw-r--r--addons/timer/models/timer.py70
-rw-r--r--addons/timer/models/timer_mixin.py135
3 files changed, 210 insertions, 0 deletions
diff --git a/addons/timer/models/__init__.py b/addons/timer/models/__init__.py
new file mode 100644
index 00000000000..66c7f179a40
--- /dev/null
+++ b/addons/timer/models/__init__.py
@@ -0,0 +1,5 @@
+# -*- coding: utf-8 -*-
+# Part of Odoo. See LICENSE file for full copyright and licensing details.
+
+from . import timer
+from . import timer_mixin
diff --git a/addons/timer/models/timer.py b/addons/timer/models/timer.py
new file mode 100644
index 00000000000..d8e87533703
--- /dev/null
+++ b/addons/timer/models/timer.py
@@ -0,0 +1,70 @@
+# -*- coding: utf-8 -*-
+# Part of Odoo. See LICENSE file for full copyright and licensing details.
+
+from odoo import models, fields, api
+from odoo.exceptions import ValidationError
+
+class TimerTimer(models.Model):
+ _name = 'timer.timer'
+ _description = 'Timer Module'
+
+ timer_start = fields.Datetime("Timer Start")
+ timer_pause = fields.Datetime("Timer Last Pause")
+ is_timer_running = fields.Boolean(compute="_compute_is_timer_running")
+ res_model = fields.Char(required=True)
+ res_id = fields.Char(required=True)
+ user_id = fields.Many2one('res.users')
+
+ _sql_constraints = [(
+ 'unique_timer', 'UNIQUE(res_model, res_id, user_id)',
+ 'Only one timer occurrence by model, record and user')]
+
+ @api.depends('timer_start', 'timer_pause')
+ def _compute_is_timer_running(self):
+ for record in self:
+ record.is_timer_running = record.timer_start and not record.timer_pause
+
+ @api.model
+ def create(self, vals):
+ # Reset the user_timer_id to force the recomputation
+ self.env[vals['res_model']].invalidate_cache(fnames=['user_timer_id'], ids=[vals['res_id']])
+ return super().create(vals)
+
+ def action_timer_start(self):
+ if not self.timer_start:
+ self.write({'timer_start': fields.Datetime.now()})
+
+ def action_timer_stop(self):
+ """ Stop the timer and return the spent minutes since it started
+ :return minutes_spent if the timer is started,
+ otherwise return False
+ """
+ if not self.timer_start:
+ return False
+ minutes_spent = self._get_minutes_spent()
+ self.write({'timer_start': False, 'timer_pause': False})
+ return minutes_spent
+
+ def _get_minutes_spent(self):
+ start_time = self.timer_start
+ stop_time = fields.Datetime.now()
+ # timer was either running or paused
+ if self.timer_pause:
+ start_time += (stop_time - self.timer_pause)
+ return (stop_time - start_time).total_seconds() / 60
+
+ def action_timer_pause(self):
+ self.write({'timer_pause': fields.Datetime.now()})
+
+ def action_timer_resume(self):
+ new_start = self.timer_start + (fields.Datetime.now() - self.timer_pause)
+ self.write({'timer_start': new_start, 'timer_pause': False})
+
+ @api.model
+ def get_server_time(self):
+ """ Returns the server time.
+ The timer widget needs the server time instead of the client time
+ to avoid time desynchronization issues like the timer beginning at 0:00
+ and not 23:59 and so on.
+ """
+ return fields.Datetime.now()
diff --git a/addons/timer/models/timer_mixin.py b/addons/timer/models/timer_mixin.py
new file mode 100644
index 00000000000..201628a16af
--- /dev/null
+++ b/addons/timer/models/timer_mixin.py
@@ -0,0 +1,135 @@
+# -*- coding: utf-8 -*-
+# Part of Odoo. See LICENSE file for full copyright and licensing details.
+
+from odoo import models, fields, api
+from math import ceil
+
+class TimerMixin(models.AbstractModel):
+ _name = 'timer.mixin'
+ _description = 'Timer Mixin'
+
+ timer_start = fields.Datetime(related='user_timer_id.timer_start')
+ timer_pause = fields.Datetime(related='user_timer_id.timer_pause')
+ is_timer_running = fields.Boolean(related='user_timer_id.is_timer_running')
+ user_timer_id = fields.One2many('timer.timer', compute='_compute_user_timer_id', search='_search_user_timer_id')
+
+ display_timer_start_primary = fields.Boolean(compute='_compute_display_timer_buttons')
+ display_timer_stop = fields.Boolean(compute='_compute_display_timer_buttons')
+ display_timer_pause = fields.Boolean(compute='_compute_display_timer_buttons')
+ display_timer_resume = fields.Boolean(compute='_compute_display_timer_buttons')
+
+ def _search_user_timer_id(self, operator, value):
+ timers = self.env['timer.timer'].search([
+ ('id', operator, value),
+ ('user_id', '=', self.env.user.id),
+ ])
+ return [('id', 'in', timers.mapped('res_id'))]
+
+ @api.depends_context('uid')
+ def _compute_user_timer_id(self):
+ """ Get the timers according these conditions
+ :user_id is is the current user
+ :res_id is the current record
+ :res_model is the current model
+ limit=1 by security but the search should never have more than one record
+ """
+ for record in self:
+ record.user_timer_id = self.env['timer.timer'].search([
+ ('user_id', '=', record.env.user.id),
+ ('res_id', '=', record.id),
+ ('res_model', '=', record._name)
+ ], limit=1)
+
+ @api.model
+ def _get_user_timers(self):
+ # Return user's timers. Can have multiple timers if some are in pause
+ return self.env['timer.timer'].search([('user_id', '=', self.env.user.id)])
+
+ def action_timer_start(self):
+ """ Start the timer of the current record
+ First, if a timer is running, stop or pause it
+ If there isn't a timer for the current record, create one then start it
+ Otherwise, resume or start it
+ """
+ self.ensure_one()
+ self._stop_timer_in_progress()
+ timer = self.user_timer_id
+ if not timer:
+ timer = self.env['timer.timer'].create({
+ 'timer_start' : False,
+ 'timer_pause' : False,
+ 'is_timer_running' : False,
+ 'res_model' : self._name,
+ 'res_id' : self.id,
+ 'user_id' : self.env.user.id,
+ })
+ timer.action_timer_start()
+ else:
+ # Check if it is in pause then resume it or start it
+ if timer.timer_pause:
+ timer.action_timer_resume()
+ else:
+ timer.action_timer_start()
+
+ def action_timer_stop(self):
+ """ Stop the timer of the current record
+ Unlink the timer, it's useless to keep the stopped timer.
+ A new timer can be create if needed
+ Return the amount of minutes spent
+ """
+ self.ensure_one()
+ timer = self.user_timer_id
+ minutes_spent = timer.action_timer_stop()
+ timer.unlink()
+ return minutes_spent
+
+ def action_timer_pause(self):
+ self.ensure_one()
+ timer = self.user_timer_id
+ timer.action_timer_pause()
+
+ def action_timer_resume(self):
+ self.ensure_one()
+ self._stop_timer_in_progress()
+ timer = self.user_timer_id
+ timer.action_timer_resume()
+
+ def _action_interrupt_user_timers(self):
+ # Interruption is the action called when the timer is stoped by the start of another one
+ self.action_timer_pause()
+
+ def _stop_timer_in_progress(self):
+ """
+ Cancel the timer in progress if there is one
+ Each model can interrupt the running timer in a specific way
+ By setting it in pause or stop by example
+ """
+ timer = self._get_user_timers().filtered(lambda t: t.is_timer_running)
+ if timer:
+ model = self.env[timer.res_model].browse(timer.res_id)
+ model._action_interrupt_user_timers()
+
+ @api.depends('timer_start', 'timer_pause')
+ def _compute_display_timer_buttons(self):
+ for record in self:
+ start_p, stop, pause, resume = True, True, True, True
+ if record.timer_start:
+ start_p = False
+ if record.timer_pause:
+ pause = False
+ else:
+ resume = False
+ record.write({
+ 'display_timer_start_primary': start_p,
+ 'display_timer_stop': stop,
+ 'display_timer_pause': pause,
+ 'display_timer_resume': resume,
+ })
+ return {'start_p': start_p, 'stop': stop, 'pause': pause, 'resume': resume}
+
+ @api.model
+ def _timer_rounding(self, minutes_spent, minimum, rounding):
+ minutes_spent = max(minimum, minutes_spent)
+ if rounding and ceil(minutes_spent % rounding) != 0:
+ minutes_spent = ceil(minutes_spent / rounding) * rounding
+ return minutes_spent