Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

12.0 add shift unsubscribe #395

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions beesdoo_shift/data/system_parameter.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,8 @@
<field name="key">regular_counter_to_unsubscribe</field>
<field name="value">-4</field>
</record>
<record id="min_hours_to_unsubscribe" model="ir.config_parameter">
<field name="key">min_hours_to_unsubscribe</field>
<field name="value">2</field>
</record>
</odoo>
20 changes: 20 additions & 0 deletions beesdoo_shift/models/task.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand All @@ -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:
Expand Down
28 changes: 28 additions & 0 deletions beesdoo_website_shift/controllers/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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/<int:shift_id>/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
Expand All @@ -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
Expand Down
6 changes: 6 additions & 0 deletions beesdoo_website_shift/models/res_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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
)
7 changes: 7 additions & 0 deletions beesdoo_website_shift/models/website.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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",
)
64 changes: 62 additions & 2 deletions beesdoo_website_shift/views/my_shift_website_templates.xml
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,16 @@
</div>
<div class="card-body clearfix">
<t t-esc="shift.task_type_id.name" />
<button
type="button"
class="btn btn-danger btn-sm pull-right"
data-toggle="modal"
t-att-data-target="'#unsubscribe-shift-%s' % shift.id"
t-if="shift.can_unsubscribe and ((shift.is_compensation and request.website.enable_unsubscribe_compensation) or (not shift.is_compensation and request.website.irregular_enable_unsubscribe))"
>
<span class="fa fa-user-plus" aria-hidden="true" />
Unsubscribe
</button>
<button
type="button"
class="btn btn-default btn-sm pull-right"
Expand Down Expand Up @@ -220,6 +230,52 @@
</div>
</div>
</div>
<t t-foreach="subscribed_shifts" t-as="shift">
<div
class="modal fade"
t-att-id="'unsubscribe-shift-%s' % shift.id"
tabindex="-1"
role="dialog"
t-att-aria-labelledby="'unsubscribe-shift-%s-label' % shift.id"
>
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h4
class="modal-title"
t-att-id="'subscribe-shift-%s-label' % shift.id"
>
Please confirm unsubscription
</h4>
</div>
<div class="modal-body">
<span t-field="shift.start_time" />
-
<span
t-field="shift.end_time"
t-options='{"format": "HH:mm"}'
/>
<br />
<t t-esc="shift.task_type_id.name" />
</div>
<div class="modal-footer">
<button
type="button"
class="btn btn-default"
data-dismiss="modal"
>Close
</button>
<a
class="btn btn-primary"
t-att-href="'/shift/%s/unsubscribe?nexturl=%s' % (shift.id, nexturl or '/my/shift')"
>
Unsubscribe
</a>
</div>
</div>
</div>
</div>
</t>
</t>

<div class="alert alert-warning" t-if="not subscribed_shifts">
Expand Down Expand Up @@ -837,7 +893,7 @@
<div
t-if="back_from_subscription"
role="alert"
t-att-class="'alert alert-%s alert-dismissible' % ('success' if success else 'danger',)"
t-att-class="'alert alert-%s alert-dismissible' % ('success' if (success or unsubscribe_success) else 'danger',)"
>
<button
type="button"
Expand All @@ -851,7 +907,11 @@
<strong>Success!</strong>
Your subscription has succeded.
</t>
<t t-if="not success">
<t t-elif="unsubscribe_success">
<strong>Success!</strong>
You have been successfully unsubscribed.
</t>
<t t-else="">
<strong>Failed!</strong>
Your subscription has failed. Someone
subscribed before you or the shift was deleted.
Expand Down
55 changes: 45 additions & 10 deletions beesdoo_website_shift/views/res_config_views.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,19 +45,38 @@
</div>
</div>
<div
id="irregular_enable_sign_up_settings"
id="irregular_enable_sign_up_unsubscribe_settings"
class="col-12 col-md-6 o_setting_box"
>
<div class="o_setting_left_pane">
<field name="irregular_enable_sign_up" />
<div class="row">
<div class="col-12">
<div class="o_setting_left_pane">
<field name="irregular_enable_sign_up" />
</div>
<div class="o_setting_right_pane">
<label for="irregular_enable_sign_up" />
<span
class="fa fa-lg fa-globe"
title="Values set here are website-specific."
groups="website.group_multi_website"
/>
</div>
</div>
</div>
<div class="o_setting_right_pane">
<label for="irregular_enable_sign_up" />
<span
class="fa fa-lg fa-globe"
title="Values set here are website-specific."
groups="website.group_multi_website"
/>
<div class="row">
<div class="col-12">
<div class="o_setting_left_pane">
<field name="irregular_enable_unsubscribe" />
</div>
<div class="o_setting_right_pane">
<label for="irregular_enable_unsubscribe" />
<span
class="fa fa-lg fa-globe"
title="Values set here are website-specific."
groups="website.group_multi_website"
/>
</div>
</div>
</div>
</div>
<div
Expand Down Expand Up @@ -127,6 +146,22 @@
</div>
</div>
</div>
<div
id="regular_enable_unsubscribe_compensation_settings"
class="col-12 col-md-6 o_setting_box"
>
<div class="o_setting_left_pane">
<field name="enable_unsubscribe_compensation" />
</div>
<div class="o_setting_right_pane">
<label for="enable_unsubscribe_compensation" />
<span
class="fa fa-lg fa-globe"
title="Values set here are website-specific."
groups="website.group_multi_website"
/>
</div>
</div>
</div>
</div>
</field>
Expand Down