From f9dbded4b37fe2ae0f90cd8a752afc5a9ba563c6 Mon Sep 17 00:00:00 2001 From: MatteoLdx Date: Thu, 28 Apr 2022 14:39:29 +0200 Subject: [PATCH 1/4] [ADD] b_website_shift: ability for irregualar workers to unsubscribe to a shift --- beesdoo_shift/models/task.py | 15 +++++ beesdoo_website_shift/controllers/main.py | 10 ++++ .../data/res_config_data.xml | 4 ++ .../views/my_shift_website_templates.xml | 59 ++++++++++++++++++- 4 files changed, 87 insertions(+), 1 deletion(-) diff --git a/beesdoo_shift/models/task.py b/beesdoo_shift/models/task.py index 419da3227..37bf133cc 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,20 @@ 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() + max_hours = int( + ICP.get_param("beesdoo_website_shift.max_hours_to_unsubscribe", 2) + ) + for rec in self: + if now > rec.start_time or rec.state != "open": + rec.can_unsubscribe = False + else: + delta = rec.start_time - now + delta = delta.seconds / 3600.0 + delta.days * 24 + rec.can_unsubscribe = delta >= max_hours + @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..d6eeec775 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,15 @@ 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) + # Get current user + if request.env.user.partner_id != shift.worker_id or not shift.can_unsubscribe: + raise Forbidden() + shift.worker_id = False + return request.redirect(kw["nexturl"]) + def my_shift_irregular_worker(self, nexturl=""): """ Return template variables for diff --git a/beesdoo_website_shift/data/res_config_data.xml b/beesdoo_website_shift/data/res_config_data.xml index c4cff7c43..ae5ba2584 100644 --- a/beesdoo_website_shift/data/res_config_data.xml +++ b/beesdoo_website_shift/data/res_config_data.xml @@ -9,5 +9,9 @@ beesdoo_website_shift.shift_period 28 + + beesdoo_website_shift.max_hours_to_unsubscribe + 2 + diff --git a/beesdoo_website_shift/views/my_shift_website_templates.xml b/beesdoo_website_shift/views/my_shift_website_templates.xml index b7590c88b..5a160cd0a 100644 --- a/beesdoo_website_shift/views/my_shift_website_templates.xml +++ b/beesdoo_website_shift/views/my_shift_website_templates.xml @@ -157,7 +157,17 @@
- + +
+ + +
From 899f8408e964228c552fc7b31026ad76d4649f6d Mon Sep 17 00:00:00 2001 From: MatteoLdx Date: Fri, 29 Apr 2022 09:31:55 +0200 Subject: [PATCH 2/4] [IMP] b_website_shift: add success alert when unsubscribing --- beesdoo_website_shift/controllers/main.py | 7 +++++++ .../views/my_shift_website_templates.xml | 8 ++++++-- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/beesdoo_website_shift/controllers/main.py b/beesdoo_website_shift/controllers/main.py index d6eeec775..46b3f57c9 100644 --- a/beesdoo_website_shift/controllers/main.py +++ b/beesdoo_website_shift/controllers/main.py @@ -212,6 +212,7 @@ def unsubscribe_to_shift(self, shift_id=-1, **kw): if request.env.user.partner_id != shift.worker_id or not shift.can_unsubscribe: raise Forbidden() shift.worker_id = False + request.session["unsubscribe_success"] = True return request.redirect(kw["nexturl"]) def my_shift_irregular_worker(self, nexturl=""): @@ -240,6 +241,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/views/my_shift_website_templates.xml b/beesdoo_website_shift/views/my_shift_website_templates.xml index 5a160cd0a..f4ac4b0b3 100644 --- a/beesdoo_website_shift/views/my_shift_website_templates.xml +++ b/beesdoo_website_shift/views/my_shift_website_templates.xml @@ -894,7 +894,7 @@
- +
-
- +
+
+
+ +
+
+
+
-
-
+
+
+ +
+
+
+