diff --git a/beesdoo_shift/data/system_parameter.xml b/beesdoo_shift/data/system_parameter.xml index 576beac53..a01e6307b 100644 --- a/beesdoo_shift/data/system_parameter.xml +++ b/beesdoo_shift/data/system_parameter.xml @@ -31,4 +31,8 @@ regular_counter_to_unsubscribe -4 + + min_hours_to_unsubscribe + 2 + diff --git a/beesdoo_shift/models/task.py b/beesdoo_shift/models/task.py index 419da3227..57f14aa41 100644 --- a/beesdoo_shift/models/task.py +++ b/beesdoo_shift/models/task.py @@ -82,6 +82,7 @@ def _get_final_state(self): ) revert_info = fields.Text(copy=False) working_mode = fields.Selection(related="worker_id.working_mode") + can_unsubscribe = fields.Boolean(compute="_compute_can_unsubscribe") def _expand_states(self, states, domain, order): return [key for key, val in self._fields["state"].selection(self)] @@ -91,6 +92,25 @@ def _compute_color(self): for rec in self: rec.color = self._get_color_mapping(rec.state) + def _compute_can_unsubscribe(self): + now = datetime.now() + ICP = self.env["ir.config_parameter"].sudo() + min_hours = int(ICP.get_param("min_hours_to_unsubscribe", 2)) + for rec in self: + if ( + rec.start_time > now + and rec.state == "open" + and ( + (rec.worker_id and rec.worker_id.working_mode == "irregular") + or rec.is_compensation + ) + ): + delta = rec.start_time - now + delta = delta.seconds / 3600.0 + delta.days * 24 + rec.can_unsubscribe = delta >= min_hours + else: + rec.can_unsubscribe = False + @api.constrains("state") def _lock_future_task(self): if datetime.now() < self.start_time: diff --git a/beesdoo_website_shift/controllers/main.py b/beesdoo_website_shift/controllers/main.py index c9f622110..2ea0e3f5c 100644 --- a/beesdoo_website_shift/controllers/main.py +++ b/beesdoo_website_shift/controllers/main.py @@ -8,6 +8,7 @@ from itertools import groupby from pytz import timezone, utc +from werkzeug.exceptions import Forbidden from odoo import http from odoo.fields import Datetime @@ -204,6 +205,27 @@ def public_shift_template_regular_worker(self, **kw): {"task_tpls_data": task_tpls_data, "float_to_time": float_to_time}, ) + @http.route("/shift//unsubscribe", auth="user", website=True) + def unsubscribe_to_shift(self, shift_id=-1, **kw): + shift = request.env["beesdoo.shift.shift"].sudo().browse(shift_id) + cur_user = request.env["res.users"].browse(request.uid) + if ( + cur_user.partner_id != shift.worker_id + or not shift.can_unsubscribe + or ( + shift.is_compensation + and not request.website.enable_unsubscribe_compensation + ) + or ( + cur_user.partner_id.working_mode == "irregular" + and not request.website.irregular_enable_unsubscribe + ) + ): + raise Forbidden() + shift.write({"is_regular": False, "is_compensation": False, "worker_id": False}) + request.session["unsubscribe_success"] = True + return request.redirect(kw["nexturl"]) + def my_shift_irregular_worker(self, nexturl=""): """ Return template variables for @@ -230,6 +252,12 @@ def my_shift_irregular_worker(self, nexturl=""): template_context["back_from_subscription"] = True template_context["success"] = request.session.get("success") del request.session["success"] + if "unsubscribe_success" in request.session: + template_context["back_from_subscription"] = True + template_context["unsubscribe_success"] = request.session.get( + "unsubscribe_success" + ) + del request.session["unsubscribe_success"] # Add setting for subscription allowed time # TODO: move this to the attendance_sheet module diff --git a/beesdoo_website_shift/models/res_config.py b/beesdoo_website_shift/models/res_config.py index 06b2503c5..27bf53c43 100644 --- a/beesdoo_website_shift/models/res_config.py +++ b/beesdoo_website_shift/models/res_config.py @@ -18,6 +18,9 @@ class WebsiteShiftConfigSettings(models.TransientModel): irregular_past_shift_limit = fields.Integer( related="website_id.irregular_past_shift_limit", readonly=False ) + irregular_enable_unsubscribe = fields.Boolean( + related="website_id.irregular_enable_unsubscribe", readonly=False + ) # Regular worker settings regular_past_shift_limit = fields.Integer( @@ -29,3 +32,6 @@ class WebsiteShiftConfigSettings(models.TransientModel): regular_highlight_rule = fields.Integer( related="website_id.regular_highlight_rule", readonly=False ) + enable_unsubscribe_compensation = fields.Boolean( + related="website_id.enable_unsubscribe_compensation", readonly=False + ) diff --git a/beesdoo_website_shift/models/website.py b/beesdoo_website_shift/models/website.py index cee4a8886..a3e177068 100644 --- a/beesdoo_website_shift/models/website.py +++ b/beesdoo_website_shift/models/website.py @@ -27,6 +27,9 @@ class Website(models.Model): default=10, help="Maximum past shift that will be shown for irregular worker", ) + irregular_enable_unsubscribe = fields.Boolean( + default=True, help="Enable irregular workers to unsubscribe from their shifts" + ) # Regular worker settings regular_past_shift_limit = fields.Integer( @@ -41,3 +44,7 @@ class Website(models.Model): help="Treshold (in %) of available space in a shift that trigger the " "the highlight of a shift template.", ) + enable_unsubscribe_compensation = fields.Boolean( + default=True, + help="Enable regular workers to unsubscribe from compensation shifts", + ) diff --git a/beesdoo_website_shift/views/my_shift_website_templates.xml b/beesdoo_website_shift/views/my_shift_website_templates.xml index b7590c88b..eded8ed75 100644 --- a/beesdoo_website_shift/views/my_shift_website_templates.xml +++ b/beesdoo_website_shift/views/my_shift_website_templates.xml @@ -158,6 +158,16 @@ + + + Unsubscribe + + + + + + + + Please confirm unsubscription + + + + + - + + + + + + + + + @@ -837,7 +893,7 @@ Success! Your subscription has succeded. - + + Success! + You have been successfully unsubscribed. + + Failed! Your subscription has failed. Someone subscribed before you or the shift was deleted. diff --git a/beesdoo_website_shift/views/res_config_views.xml b/beesdoo_website_shift/views/res_config_views.xml index 00b13866e..4d4f19854 100644 --- a/beesdoo_website_shift/views/res_config_views.xml +++ b/beesdoo_website_shift/views/res_config_views.xml @@ -45,19 +45,38 @@ - - + + + + + + + + + + - - - + + + + + + + + + + + + + + + + + + +