diff --git a/polln_shift/controllers/__init__.py b/polln_shift/controllers/__init__.py new file mode 100644 index 000000000..deec4a8b8 --- /dev/null +++ b/polln_shift/controllers/__init__.py @@ -0,0 +1 @@ +from . import main \ No newline at end of file diff --git a/polln_shift/controllers/main.py b/polln_shift/controllers/main.py new file mode 100644 index 000000000..2d3109440 --- /dev/null +++ b/polln_shift/controllers/main.py @@ -0,0 +1,16 @@ + +from odoo import http +from odoo.http import request + +from werkzeug.exceptions import Forbidden + +class WebsiteMacavracShiftController(http.Controller): + + @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"]) \ No newline at end of file diff --git a/polln_shift/models/planning.py b/polln_shift/models/planning.py index cec1e0a04..34e667db0 100644 --- a/polln_shift/models/planning.py +++ b/polln_shift/models/planning.py @@ -46,6 +46,20 @@ class Task(models.Model): _period = 28 + can_unsubscribe = fields.Boolean(compute="_compute_can_unsubscribe") + + def _compute_can_unsubscribe(self): + now = datetime.now() + ICP = self.env["ir.config_parameter"].sudo() + max_hours = int(ICP.get_param("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 + def _get_selection_status(self): return [ ("open", _("Confirmed")), diff --git a/polln_shift/views/shift.xml b/polln_shift/views/shift.xml index d709647dc..40ef89b23 100644 --- a/polln_shift/views/shift.xml +++ b/polln_shift/views/shift.xml @@ -124,6 +124,58 @@ + + Subscribe Cooperator beesdoo.shift.subscribe