Browse Source

[IMP] website_shift: Next shifts regular worker

Add configuration to specify how many next shifts for regular worker
must be shown to them on their personal web page. This config is called
`regular_next_shift_limit`.

Improve the view that shows the next shifts on the personal page for a
regular worker regarding to the `regular_next_shift_limit`.

Because all the next shifts are not created in the database, we need to
create 'fictive' one that are not stored.
pull/33/head
Rémy Taymans 6 years ago
parent
commit
0e4085e2fe
  1. 133
      beesdoo_website_shift/controllers/main.py
  2. 4
      beesdoo_website_shift/data/res_config_data.xml
  3. 11
      beesdoo_website_shift/models/res_config.py
  4. 32
      beesdoo_website_shift/views/my_shift_website_templates.xml
  5. 4
      beesdoo_website_shift/views/res_config_views.xml

133
beesdoo_website_shift/controllers/main.py

@ -5,14 +5,18 @@
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
from ast import literal_eval from ast import literal_eval
from copy import copy
from datetime import datetime, timedelta from datetime import datetime, timedelta
from itertools import groupby from itertools import groupby
from openerp import http, fields from openerp import http, fields
from openerp.http import request from openerp.http import request
from openerp.tools import DEFAULT_SERVER_DATETIME_FORMAT as DATETIME_FORMAT
from openerp.addons.beesdoo_shift.models.planning import float_to_time from openerp.addons.beesdoo_shift.models.planning import float_to_time
PERIOD = 28 # TODO: use the same constant as in 'beesdoo_shift'
class WebsiteShiftController(http.Controller): class WebsiteShiftController(http.Controller):
@ -34,7 +38,7 @@ class WebsiteShiftController(http.Controller):
@http.route('/my/shift', auth='user', website=True) @http.route('/my/shift', auth='user', website=True)
def my_shift(self, **kw): def my_shift(self, **kw):
""" """
Personnal page for managing your shifts
Personal page for managing your shifts
""" """
if self.is_user_irregular(): if self.is_user_irregular():
return request.render( return request.render(
@ -141,7 +145,7 @@ class WebsiteShiftController(http.Controller):
# Compute date before which the worker is up to date # Compute date before which the worker is up to date
today_date = fields.Date.from_string(cur_cooperative_status.today) today_date = fields.Date.from_string(cur_cooperative_status.today)
delta = (today_date - fields.Date.from_string(cur_cooperative_status.irregular_start_date)).days delta = (today_date - fields.Date.from_string(cur_cooperative_status.irregular_start_date)).days
date_before_last_shift = today_date + timedelta(days=(cur_cooperative_status.sr + 1) * 28 - delta % 28)
date_before_last_shift = today_date + timedelta(days=(cur_cooperative_status.sr + 1) * PERIOD - delta % PERIOD)
date_before_last_shift = date_before_last_shift.strftime('%Y-%m-%d') date_before_last_shift = date_before_last_shift.strftime('%Y-%m-%d')
template_context.update( template_context.update(
@ -252,11 +256,49 @@ class WebsiteShiftController(http.Controller):
cur_user = request.env['res.users'].browse(request.uid) cur_user = request.env['res.users'].browse(request.uid)
# Get shifts where user is subscribed # Get shifts where user is subscribed
now = datetime.now() now = datetime.now()
subscribed_shifts = request.env['beesdoo.shift.shift'].sudo().search(
subscribed_shifts_rec = request.env['beesdoo.shift.shift'].sudo().search(
[('start_time', '>', now.strftime("%Y-%m-%d %H:%M:%S")), [('start_time', '>', now.strftime("%Y-%m-%d %H:%M:%S")),
('worker_id', '=', cur_user.partner_id.id)], ('worker_id', '=', cur_user.partner_id.id)],
order="start_time, task_template_id, task_type_id", order="start_time, task_template_id, task_type_id",
) )
# We don't use record to show the next shift as we need to add
# fictive one for regular worker. I say 'fictive' one because
# the next shifts for the regular worker are generated on a
# PERIOD basis and database doesn't contain more than a PERIOD
# of shift. So a regular worker will always see only one
# next shift, the one that is generated and stored in the
# database. Meaning that if we want to show the next shifts
# for an entire year, we need to compute the dates for the next
# shifts and create it. But we want to keep it 'fictive',
# meaning that we don't want to write them in the database.
# So here we convert recordset into Shift object.
subscribed_shifts = []
for shift_rec in subscribed_shifts_rec:
shift = Shift(shift_rec)
subscribed_shifts.append(shift)
# We want to keep a copy of the shift that will serve as a
# master to create the fictive shifts.
if shift_rec.worker_id in shift_rec.task_template_id.worker_ids:
main_shift = shift
main_shift_rec = shift_rec
# In case of regular worker, we compute his fictive next shifts
# according to the regular_next_shift_limit
if self.is_user_regular() and subscribed_shifts and main_shift:
# Get config
regular_next_shift_limit = int(request.env['ir.config_parameter'].get_param(
'beesdoo_website_shift.regular_next_shift_limit'))
for i in range(1, regular_next_shift_limit):
# Compute the new date for the created shift
start_time = fields.Datetime.from_string(main_shift_rec.start_time)
start_time = (start_time + timedelta(days=i*PERIOD)).strftime(DATETIME_FORMAT)
# Create the fictive shift
shift = copy(main_shift)
shift.id = -i # We give negative id 'caus this shift doesn't exist in database
shift.start_day = start_time
shift.start_date = start_time
subscribed_shifts.append(shift)
return { return {
'subscribed_shifts': subscribed_shifts, 'subscribed_shifts': subscribed_shifts,
} }
@ -303,3 +345,88 @@ class WebsiteShiftController(http.Controller):
return { return {
'status': cur_user.partner_id.cooperative_status_ids, 'status': cur_user.partner_id.cooperative_status_ids,
} }
class Shift(object):
"""
Represent a shift with all useful information in a format that is directly printable in a template
"""
def __init__(self, shift_rec=None):
self.id = 0
self._start_day = ''
self._start_date = ''
self._start_time = ''
self._end_time = ''
self.task_type_name = ''
self.super_coop_name = ''
self.super_coop_phone = ''
self.super_coop_email = ''
if shift_rec:
self.update(shift_rec)
def update(self, shift_rec=None):
""" Fill in self with data in the given record"""
if shift_rec:
self.id = shift_rec.id
self.start_day = shift_rec.start_time
self.start_date = shift_rec.start_time
self.start_time = shift_rec.start_time
self.end_time = shift_rec.end_time
if shift_rec.task_type_id:
self.task_type_name = shift_rec.task_type_id.name
if shift_rec.super_coop_id:
self.super_coop_name = shift_rec.super_coop_id.name
self.super_coop_phone = shift_rec.super_coop_id.phone
self.super_coop_email = shift_rec.super_coop_id.email
# Properties
@property
def start_day(self):
return self._start_day
@property
def start_date(self):
return self._start_date
@property
def start_time(self):
return self._start_time
@property
def end_time(self):
return self._end_time
# Setters
@start_day.setter
def start_day(self, datetime_str):
self._start_day = datetime.strptime(datetime_str, DATETIME_FORMAT).strftime('%A')
@start_date.setter
def start_date(self, datetime_str):
self._start_date = datetime.strptime(datetime_str, DATETIME_FORMAT).strftime('%d %B %Y')
@start_time.setter
def start_time(self, datetime_str):
self._start_time = datetime.strptime(datetime_str, DATETIME_FORMAT).strftime('%H:%M')
@end_time.setter
def end_time(self, datetime_str):
self._end_time = datetime.strptime(datetime_str, DATETIME_FORMAT).strftime('%H:%M')
# Deleters
@start_day.deleter
def start_day(self):
del self._start_day
@start_date.deleter
def start_date(self):
del self._start_date
@start_time.deleter
def start_time(self):
del self._start_time
@end_time.deleter
def end_time(self):
del self._end_time

4
beesdoo_website_shift/data/res_config_data.xml

@ -29,5 +29,9 @@
<field name="key">beesdoo_website_shift.regular_past_shift_limit</field> <field name="key">beesdoo_website_shift.regular_past_shift_limit</field>
<field name="value">10</field> <field name="value">10</field>
</record> </record>
<record id="beesdoo_website_shift.regular_next_shift_limit" model="ir.config_parameter">
<field name="key">beesdoo_website_shift.regular_next_shift_limit</field>
<field name="value">13</field>
</record>
</data> </data>
</openerp> </openerp>

11
beesdoo_website_shift/models/res_config.py

@ -13,6 +13,7 @@ PARAMS = [
('irregular_enable_sign_up', 'beesdoo_website_shift.irregular_enable_sign_up'), ('irregular_enable_sign_up', 'beesdoo_website_shift.irregular_enable_sign_up'),
('irregular_past_shift_limit', 'beesdoo_website_shift.irregular_past_shift_limit'), ('irregular_past_shift_limit', 'beesdoo_website_shift.irregular_past_shift_limit'),
('regular_past_shift_limit', 'beesdoo_website_shift.regular_past_shift_limit'), ('regular_past_shift_limit', 'beesdoo_website_shift.regular_past_shift_limit'),
('regular_next_shift_limit', 'beesdoo_website_shift.regular_next_shift_limit'),
] ]
@ -42,6 +43,9 @@ class WebsiteShiftConfigSettings(models.TransientModel):
regular_past_shift_limit = fields.Integer( regular_past_shift_limit = fields.Integer(
help="Maximum past shift that will be shown for regular worker" help="Maximum past shift that will be shown for regular worker"
) )
regular_next_shift_limit = fields.Integer(
help="Maximun number of next shift that will be shown"
)
@api.multi @api.multi
def set_params(self): def set_params(self):
@ -90,3 +94,10 @@ class WebsiteShiftConfigSettings(models.TransientModel):
'regular_past_shift_limit': int(self.env['ir.config_parameter'].get_param( 'regular_past_shift_limit': int(self.env['ir.config_parameter'].get_param(
'beesdoo_website_shift.regular_past_shift_limit')) 'beesdoo_website_shift.regular_past_shift_limit'))
} }
@api.multi
def get_default_regular_next_shift_limit(self):
return {
'regular_next_shift_limit': int(self.env['ir.config_parameter'].get_param(
'beesdoo_website_shift.regular_next_shift_limit'))
}

32
beesdoo_website_shift/views/my_shift_website_templates.xml

@ -120,15 +120,15 @@
<div class="panel panel-default"> <div class="panel panel-default">
<div class="panel-heading clearfix"> <div class="panel-heading clearfix">
<div class="panel-title"> <div class="panel-title">
<t t-esc="time.strftime('%A %d %B %Y', time.strptime(shift.start_time, '%Y-%m-%d %H:%M:%S'))"/>
<span t-field="shift.start_time" t-field-options='{"format": "HH:mm"}'/> -
<span t-field="shift.end_time" t-field-options='{"format": "HH:mm"}'/>
<t t-esc="'%s %s' % (shift.start_day, shift.start_date)"/>
<span t-esc="shift.start_time"/> -
<span t-esc="shift.end_time"/>
</div> </div>
</div> </div>
<div class="panel-body"> <div class="panel-body">
<t t-esc="shift.task_type_id.name"/>
<t t-esc="shift.task_type_name"/>
<button type="button" class="btn btn-default btn-sm pull-right" <button type="button" class="btn btn-default btn-sm pull-right"
t-if="shift.super_coop_id"
t-if="shift.super_coop_name"
data-toggle="modal" data-toggle="modal"
t-att-data-target="'#super_coop-shift-%s' % shift.id"> t-att-data-target="'#super_coop-shift-%s' % shift.id">
<span class="fa fa-info" aria-hidden="true"></span> <span class="fa fa-info" aria-hidden="true"></span>
@ -152,21 +152,21 @@
<t t-foreach="subscribed_shifts" t-as="shift"> <t t-foreach="subscribed_shifts" t-as="shift">
<tr> <tr>
<td> <td>
<t t-esc="time.strftime('%A', time.strptime(shift.start_time, '%Y-%m-%d %H:%M:%S'))"/>
<t t-esc="shift.start_day"/>
</td> </td>
<td> <td>
<t t-esc="time.strftime('%d %B %Y', time.strptime(shift.start_time, '%Y-%m-%d %H:%M:%S'))"/>
<t t-esc="shift.start_date"/>
</td> </td>
<td> <td>
<span t-field="shift.start_time" t-field-options='{"format": "HH:mm"}'/> -
<span t-field="shift.end_time" t-field-options='{"format": "HH:mm"}'/>
<span t-esc="shift.start_time"/> -
<span t-esc="shift.end_time"/>
</td> </td>
<td> <td>
<t t-esc="shift.task_type_id.name"/>
<t t-esc="shift.task_type_name"/>
</td> </td>
<td class="text-center"> <td class="text-center">
<button type="button" class="btn btn-default btn-sm" <button type="button" class="btn btn-default btn-sm"
t-if="shift.super_coop_id"
t-if="shift.super_coop_name"
data-toggle="modal" data-toggle="modal"
t-att-data-target="'#super_coop-shift-%s' % shift.id"> t-att-data-target="'#super_coop-shift-%s' % shift.id">
<span class="fa fa-info" aria-hidden="true"></span> <span class="fa fa-info" aria-hidden="true"></span>
@ -179,7 +179,7 @@
<!-- Super Co-operator info modal --> <!-- Super Co-operator info modal -->
<t t-foreach="subscribed_shifts" t-as="shift"> <t t-foreach="subscribed_shifts" t-as="shift">
<div class="modal fade" t-if="shift.super_coop_id" t-att-id="'super_coop-shift-%s' % shift.id" tabindex="-1" role="dialog"
<div class="modal fade" t-if="shift.super_coop_name" t-att-id="'super_coop-shift-%s' % shift.id" tabindex="-1" role="dialog"
t-att-aria-labelledby="'super_coop-shift-%s-label' % shift.id"> t-att-aria-labelledby="'super_coop-shift-%s-label' % shift.id">
<div class="modal-dialog" role="document"> <div class="modal-dialog" role="document">
<div class="modal-content"> <div class="modal-content">
@ -188,14 +188,14 @@
<span aria-hidden="true">×</span> <span aria-hidden="true">×</span>
</button> </button>
<h4 class="modal-title" t-att-id="'super_coop-shift-%s-label' % shift.id"> <h4 class="modal-title" t-att-id="'super_coop-shift-%s-label' % shift.id">
<t t-esc="shift.super_coop_id.name"/>
<t t-esc="shift.super_coop_name"/>
</h4> </h4>
</div> </div>
<div class="modal-body"> <div class="modal-body">
<i class="fa fa-phone" aria-hidden="true"></i> <t t-esc="shift.super_coop_id.phone"/><br/>
<i class="fa fa-phone" aria-hidden="true"></i> <t t-esc="shift.super_coop_phone"/><br/>
<i class="fa fa-envelope" aria-hidden="true"></i> <i class="fa fa-envelope" aria-hidden="true"></i>
<a t-att-href="'mailto:%s' % shift.super_coop_id.email">
<t t-esc="shift.super_coop_id.email"/>
<a t-att-href="'mailto:%s' % shift.super_coop_email">
<t t-esc="shift.super_coop_email"/>
</a> </a>
</div> </div>
<div class="modal-footer"> <div class="modal-footer">

4
beesdoo_website_shift/views/res_config_views.xml

@ -52,6 +52,10 @@
<label for="regular_past_shift_limit"/> <label for="regular_past_shift_limit"/>
<field name="regular_past_shift_limit"/> <field name="regular_past_shift_limit"/>
</div> </div>
<div>
<label for="regular_next_shift_limit"/>
<field name="regular_next_shift_limit"/>
</div>
</form> </form>
</field> </field>
</record> </record>

Loading…
Cancel
Save